Explaining the Dijkstra's Algorithm Steps on the Example Graph



sample directed graph


When Dijkstra's algorithm is used to find all the shortest paths from 1 to the other nodes, it proceeds as indicated in the table below.

Step     v        C        BackPoint          Dist
Init    --    {2,3,4,5}   [X,1,1,1,1]   [0,50,30,100,10] 
 1       5     {2,3,4}    [X,1,1,5,1]   [0,50,30,20,10]
 2       4      {2,3}     [X,4,1,5,1]   [0,40,30,20,10]
 3       3       {2}      [X,3,1,5,1]   [0,35,30,20,10]
In step 1, we choose node 5 because Dist[5]=10 is minimal. Dist[4] then changes from 100 to 20 because path 1-->5-->4 is shorter than path 1-->4. BackPoint[4] changes from 1 to 5 to indicate that the shortest known path from 1 to 4 now has 5-->4 as its last edge.

In step 2, we choose node 4 because now Dist[4]=20 is the minimal distance value. Dist[2] changes from 50 to 40 because path 1-->5-->4-->2 is shorter than path 1-->2. BackPoint[2] changes from 1 to 4 to indicate that the shortest known path from 1 to 2 now has 4-->2 as its last edge.

In step 3, we choose node 3 because now Dist[3]=30 is the minimal distance value. Dist[2] changes from 40 to 35 because path 1-->3-->2 is shorter than path 1-->5-->4-->2. BackPoint[2] changes from 4 to 3 to indicate that the shortest known path from 1 to 2 now has 3-->2 as its last edge.