Let's dive deep into the algorithm by initializing some variables that are required for Dijkstra's algorithm. We'll need the following:
A list of all
unvisitednodes in the graph.A list of
distanceswith their nodes in the graph. At first, the distance of all the nodes is initialized asinfinity, except the source node (which is initialized as0).A list of all nodes that are
visited, and which make theshortest path. At first, this list will contain the source node only.
The algorithm starts from the source node, which in this case is A. We change the distances of nodes B and C in the distance list to be 5 and 2. As the sum of distance value from source node to nodes B and C is greater than the original distance, they are changed. The graph and the corresponding lists look as illustrated below.


