Dijkstra's algorithm

From PEGWiki
Revision as of 01:18, 19 December 2009 by Brian (Talk | contribs)

Jump to: navigation, search

Dijkstra's algorithm finds single-source shortest paths in a directed graph with non-negative edge weights. (When negative-weight edges are allowed, the Bellman–Ford algorithm must be used instead.) It is the algorithm of choice for solving this problem, because it is easy to understand, relatively easy to code, and, so far, the fastest algorithm known for solving this problem in the general case. In sparse graphs, running it once on every vertex to generate all-pairs shortest paths is faster than solving the same problem with the Floyd–Warshall algorithm. (The precise time complexity of Dijkstra's depends on the nature of the data structures used; read on.)

Theory of the algorithm

Dijkstra's may be characterized as a greedy algorithm, which builds the shortest-paths tree one edge at a time, adding vertices in non-decreasing order of their distance from the source. That is, in each step of the algorithm, we will find the next-closest vertex to the source. (If there is a tie, it does not matter which one is chosen.) We assume below that all nodes are reachable from the source.

Lemma

Suppose that we are given a set T of vertices, containing the source s. We shall call a path admissible if it starts at s, proceeds through a sequence of vertices contained within T, and ends with a single non-T vertex. We claim that there exists an admissible path such that no other path from s to a non-T vertex is shorter.

Proof: This consists of nothing but a series of observations. First, any path from s to a vertex v outside T contains at least one edge from a vertex in T to one outside, since sT. Second, if the first such edge encountered along the path from s is not the last edge in the path, we can "cut off" the path at that point to obtain a path from s out of T that is not longer (since all edges have non-negative weights). Third, if the sub-path from s to the last vertex in T, denoted u, is not itself a shortest path from s to u, the length of the whole path may be decreased by substituting a shortest path from s to u for the current one. Now, suppose the opposite of what we want to prove: the shortest path from s out of T, or all the shortest paths from s out of T, is/are inadmissible. Then we may construct an admissible path using the three observations above which is not longer, a contradiction.

The algorithm

The preceding Lemma should give us an idea of how to proceed. We start with only the source vertex in the shortest-paths tree; its distance to itself is obviously zero. Then, we repeatedly apply the Lemma: we consider all outgoing edges u-v of vertices in T; each one induces a possible shortest path from the source s to v when the u-v edge is appended to the shortest s-u path (already known).