Dijkstra's algorithm solves the single-source shortest-path problem with nonnegative edge weights: it finds minimum path lengths from the source to all reachable vertices and can store predecessor edges. With adjacency lists and a binary-heap priority queue, it takes time; a direct array implementation takes .
The Bellman-Ford algorithm allows negative edge weights. If no negative-weight cycle is reachable from the source, it computes the finite shortest-path distances to reachable vertices and identifies unreachable ones. Otherwise it can report a reachable negative cycle, whose repeated traversal makes distances to some reachable vertices unbounded below. It performs at most full relaxation rounds and one further detection round, taking time, conventionally written for nontrivial edge sets. A negative cycle unreachable from the source does not affect its distance problem.
No: equal-length shortest paths permit different trees. Consider edges , all of weight one. Both routes to have length two. A shortest-path tree may therefore choose either or as the predecessor of .
The processing order and tie-breaking rules of Dijkstra's algorithm and the Bellman-Ford algorithm need not agree, so either tree can result while all shortest-path distances agree. If every reachable vertex has a unique shortest path from , then both correct algorithms must select those same paths.
No. For a path with edges, adding to every edge changes its cost toDifferent paths can have different numbers of edges, so this transformation need not preserve their order.
For a concrete Directed acyclic graph, use edges of weight , of weight , and of weight . Originally the route through has cost and beats the direct edge of cost . Adding makes every edge positive, but the direct route now costs , while the route through costs . Dijkstra's algorithm on the modified graph therefore returns a path that is not shortest in the original graph.
The two-edge path receives the added constant twice; the direct path receives it once. This is the failure of uniform edge shifts to preserve shortest paths.
A topological sort of a Directed acyclic graph is a linear ordering of its vertices in which the tail of every directed edge precedes its head. Along a path , this property successively givesin that ordering. Thus the vertices of every directed path occur in topological order. The acyclicity condition is necessary, since a directed cycle would force its first vertex to precede itself.
Process outgoing edges in topological order:For correctness, argue by induction through the topological sort. Every path ending at a vertex reaches it through some incoming edge . All such predecessors occur earlier, so their finalized shortest-path distances have already been considered when is processed. Taking the minimum of over these edges therefore gives exactly the shortest-path distance to . Unreachable vertices retain infinity. Negative edge weights cause no difficulty because a later vertex cannot lead back to an earlier one.
order = topological_sort(G)
for v in V:
distance[v] = infinity
predecessor[v] = none
distance[s] = 0
for u in order:
if distance[u] != infinity:
for edge (u,v) of weight w:
if distance[u] + w < distance[v]:
distance[v] = distance[u] + w
predecessor[v] = uThis is the DAG shortest-path algorithm. Topological sorting and the single pass of edge relaxation each take time with adjacency lists; distances, predecessors and the order use auxiliary space. Following predecessors reconstructs a shortest path to any reachable target.
Articles by others on the same topic
There are currently no matching articles.
