Mark As Completed Discussion

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 unvisited nodes in the graph.

  • A list of distances with their nodes in the graph. At first, the distance of all the nodes is initialized as infinity, except the source node (which is initialized as 0).

  • A list of all nodes that are visited, and which make the shortest 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.

First-step