Dijkstra
Dijkstra
Dijkstra
Best-first search Bidirectional search Breadth-first search D* Depth-first search Depth-limited search Dijkstra's algorithm Floyd-Warshall algorithm Hill climbing Iterative deepening depth-first search Johnson's algorithm Uniform-cost search
Contents
[hide]
1 Algorithm 2 Lay descriptions of the algorithm 3 Pseudocode 4 Running time 5 Related problems and algorithms 6 See also 7 Notes 8 References 9 External links
[edit] Algorithm
It should be noted that distance between nodes can also be referred to as weight. 1. 2. 3. 4. Create a distance list, a previous vertex list, a visited list, and a current vertex. All the values in the distance list are set to infinity except the starting vertex which is set to zero. All values in visited list are set to false. All values in the previous list are set to a special value signifying that they are undefined, such as
5. 6. 7. 8. 9.
null. Current vertex is set as the starting vertex. Mark the current vertex as visited. Update distance and previous lists based on those vertices which can be immediately reached from the current vertex. Update the current vertex to the unvisited vertex that can be reached by the shortest path from the starting vertex. Repeat (from step 6) until all nodes are visited.
[edit] Pseudocode
In the following algorithm, u := node in Q with smallest dist[] searches for the vertex u in the vertex set Q that has the least dist[u] 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. alt on line 11 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: 3 dist[v] := infinity source to v 4 previous[v] := undefined from source 5 dist[source] := 0 6 Q := the set of all nodes in Graph unoptimized - thus are in Q 7 while Q is not empty: // Initializations // Unknown distance function from // Previous node in optimal path // Distance from source to source // All nodes in the graph are // The main loop
8 u := node in Q with smallest dist[] 9 remove u from Q 10 for each neighbor v of u: // where v has not yet been removed from Q. 11 alt := dist[u] + dist_between(u, v) 12 if alt < dist[v] // Relax (u,v) 13 dist[v] := alt 14 previous[v] := u 15 return previous[]
If we are only interested in a shortest path between vertices source and target, we can terminate the search at line 10 if u = target. Now we can read the shortest path from source to target by iteration:
1 2 3 4 5 S := empty sequence u := target while defined previous[u] insert u at the beginning of S u := previous[u]
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[target]. 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 short paths between two given nodes we would use a path finding algorithm on the new graph, such as depth-first search.
example, sometimes it is desirable to present solutions which are less than mathematically optimal. To obtain a ranked list of less-than-optimal solutions, the optimal solution is first calculated. A single edge appearing in the optimal solution is removed from the graph, and the optimum solution to this new graph is calculated. Each edge of the original solution is suppressed in turn and a new shortest-path calculated. The secondary solutions are then ranked and presented after the first optimal solution. Dijkstra's algorithm is usually the working principle behind link-state routing protocols, OSPF and IS-IS being the most common ones. Unlike Dijkstra's algorithm, the Bellman-Ford algorithm can be used on graphs with negative edge weights, as long as the graph contains no negative cycle reachable from the source vertex s. (The presence of such cycles means there is no shortest path, since the total weight becomes lower each time the cycle is traversed.) The A* algorithm is a generalization of Dijkstra's algorithm that cuts down on the size of the subgraph that must be explored, if additional information is available that provides a lower-bound on the "distance" to the target. The process that underlies Dijkstra's algorithm is similar to the greedy process used in Prim's algorithm. Prim's purpose is to find a minimum spanning tree for a graph. For the solution of nonconvex cost trees (typical for real-world costs exhibiting economies of scale) one solution allowing application of this algorithm is to successively divide the problem into convex subtrees (using bounding linear costs) and to pursue subsequent divisions using branch and bound methods. Such methods have been superseded by more efficient direct methods.