Proofs: Yen's algorithm
These are the human proofs of Yen's algorithm, at full length.
Yen's algorithm lists the k shortest simple paths in order full
This proof shows: correctness, termination.
Let be a directed graph with nonnegative weights (parallel arcs merged to the lightest, self-loops dropped), and vertices. A path means a simple path from to , written as its list of vertices indexed from 0. For a path , is its prefix with vertices. Line numbers refer to YEN on the main page. Let .
Fact 1 (candidates are new simple paths). Every path added to at line L9 is a simple - path, and when it is added it is not in .
Proof. The candidate is the root followed by a spur path from the spur vertex to . The root is simple, as a prefix of a simple path. is a shortest path found by Dijkstra, so it is simple, and by line L7 it avoids the root vertices other than the spur. So the candidate is simple and ends at . Line L9 skips paths that are already in or .
Fact 2 (weights). The key of a candidate in is its weight, and Dijkstra at line L8 returns a lightest spur path in the graph with the blocked arcs and vertices removed.
Proof. The weights are nonnegative, and Dijkstra is correct on such graphs. The key is the weight of the root plus the weight of the spur path.
Lemma 3 (every other path has a cheaper candidate in ). At line L11 in round , for every path , the queue contains a path with .
Proof. For , let be the number of leading vertices that and have in common. Both start with , so . Let , and let be the largest index with . Set , so and or one of them ends at index . Neither path can end at index unless both do: a path that ends there ends at , and the other path, being simple, contains only as its last vertex, so it would end there too and they would be equal, but . So and .
Spur of was computed in round (rounds compute the spurs of ). In that round, line L6 blocked the arcs for the paths with . The arc is not among them: if for such a , then , which contradicts the choice of . Line L7 blocked , and the part avoids these vertices, because is simple. So is a spur path in the graph of line L8, and by Fact 2 Dijkstra found a spur path of weight at most . The resulting path followed by that spur path has .
was added to then, or it was already in or . It is not in : since and is not blocked, differs from every with that prefix, and paths in without that prefix differ from anyway; and if with , then , so with , against the choice of . Paths leave only at line L12, into , so is still in .
Theorem. YEN returns distinct simple - paths , with where is the number of simple - paths, such that for every , is a lightest path outside . In particular .
Proof. If is not reachable, there is no path, , and line L1 returns the empty list. Otherwise is a shortest - path from Dijkstra, a simple path of least weight among all walks and so among all paths.
In round , by Fact 1 every element of is a path outside . If some path lies outside , Lemma 3 says is not empty, so line L11 does not stop, and line L12 takes with . So is a lightest path outside , and it is new. If no path lies outside , then is empty by Fact 1 and line L11 returns, with . Since is outside and therefore outside , the minimality of gives .
Termination. Each round runs at most spur computations, each a run of Dijkstra, which terminates. There are at most rounds.