Dijkstra's Algorithm - Pseudocode

Pseudocode

In the following algorithm, the code u := vertex in Q with smallest dist, searches for the vertex u in the vertex set Q that has the least dist value. That vertex is removed from the set Q and returned to the user. dist_between(u, v) calculates the length between the two neighbor-nodes u and v. The variable alt on lines 20 & 22 is the length of the path from the root node to the neighbor node v if it were to go through u. If this path is shorter than the current shortest path recorded for v, that current path is replaced with this alt path. The previous array is populated with a pointer to the "next-hop" node on the source graph to get the shortest route to the source.

1 function Dijkstra(Graph, source): 2 for each vertex v in Graph: // Initializations 3 dist := infinity ; // Unknown distance function from 4 // source to v 5 previous := undefined ; // Previous node in optimal path 6 end for // from source 7 8 dist := 0 ; // Distance from source to source 9 Q := the set of all nodes in Graph ; // All nodes in the graph are 10 // unoptimized - thus are in Q 11 while Q is not empty: // The main loop 12 u := vertex in Q with smallest distance in dist ; // Start node in first case 13 remove u from Q ; 14 if dist = infinity: 15 break ; // all remaining vertices are 16 end if // inaccessible from source 17 18 for each neighbor v of u: // where v has not yet been 19 // removed from Q. 20 alt := dist + dist_between(u, v) ; 21 if alt < dist: // Relax (u,v,a) 22 dist := alt ; 23 previous := u ; 24 decrease-key v in Q; // Reorder v in the Queue 25 end if 26 end for 27 end while 28 return dist;

If we are only interested in a shortest path between vertices source and target, we can terminate the search at line 13 if u = target. Now we can read the shortest path from source to target by reverse iteration:

1 S := empty sequence 2 u := target 3 while previous is defined: // Construct the shortest path with a stack S 4 insert u at the beginning of S // Push the vertex into the stack 5 u := previous // Traverse from target to source 6 end while ;

Now sequence S is the list of vertices constituting one of the shortest paths from source to target, or the empty sequence if no path exists.

A more general problem would be to find all the shortest paths between source and target (there might be several different ones of the same length). Then instead of storing only a single node in each entry of previous we would store all nodes satisfying the relaxation condition. For example, if both r and source connect to target and both of them lie on different shortest paths through target (because the edge cost is the same in both cases), then we would add both r and source to previous. When the algorithm completes, previous data structure will actually describe a graph that is a subset of the original graph with some edges removed. Its key property will be that if the algorithm was run with some starting node, then every path from that node to any other node in the new graph will be the shortest path between those nodes in the original graph, and all paths of that length from the original graph will be present in the new graph. Then to actually find all these shortest paths between two given nodes we would use a path finding algorithm on the new graph, such as depth-first search.

Read more about this topic:  Dijkstra's Algorithm