Guest User
Using app without sign in

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
The best route, then the next best ones
The K shortest loopless paths between two vertices, in order of cost, as detours from the paths already found
Select an algorithm and generate steps to begin visualization
Yen's algorithm finds the K shortest loopless paths between two vertices of a weighted graph, in order of cost: the shortest path, then the second shortest, and so on. A loopless (simple) path never visits a vertex twice. Jin Y. Yen published the method in 1971. It answers questions a single shortest path cannot: what is the best alternative if the first route is closed, how much longer is the next option, and which routes are almost as good.
The first path is an ordinary shortest path, found with Dijkstra's algorithm. Every later path leaves one of the paths already found at some vertex, the spur node, and takes a detour from there. So for each vertex of the last path found, Yen keeps the part before it, the root, blocks the next edge of every found path that shares that root so the detour is new, removes the root's other vertices so the path stays loopless, and runs Dijkstra from the spur node to the target. Root plus detour is a candidate. All candidates wait in a list B, and the cheapest becomes the next path. With V vertices this needs at most K · V shortest-path runs.
Route planners and navigation apps offer alternative routes this way, and transport models use K shortest paths to spread traffic over several routes. Network operators keep backup paths ready for when a link fails, and logistics tools compare the best few delivery routes against other criteria such as tolls or time windows. In biology and chemistry the method ranks the most likely reaction pathways, and in natural language processing the best few analyses of a sentence.
Yen's algorithm lists the K shortest loopless paths between two vertices in order of cost. Each new path is found by deviating from an earlier one at some spur vertex, with the edges that would recreate known paths temporarily removed.
Yen(graph, s, t, K):
A = [Dijkstra(s, t)] // accepted paths
B = {} // candidates, kept across rounds
for k = 2 .. K:
P = A[k - 1]
for each vertex v of P except t:
root = the part of P from s to v
block the next edge after root of every path in A
that starts with root
remove the vertices of root except v
spur = Dijkstra(v, t) on what is left
if spur exists: add root + spur to B
restore the graph
if B is empty: stop
move the cheapest path of B into A
return AEvery loopless path other than the shortest agrees with some accepted path up to a spur vertex and then leaves it by a different edge. Blocking the edges already taken from that root, and removing the root vertices to forbid loops, makes each Dijkstra run find the cheapest path of exactly that shape. Keeping the candidate set B between rounds matters, because the next best path may have been generated long before it is chosen.
Find the three shortest routes from A to F on the example this page loads. The third route turns out to be a deviation from the first one, discovered a full round before it is selected.
Example graph: Undirected weighted edges A-B 3, A-C 2, B-C 3, B-D 4, C-E 3, D-E 1, D-F 2 and E-F 5.
The three shortest loopless routes from A to F cost 8, 9 and 10: A-C-E-D-F, A-B-D-F and A-C-E-F. If the candidates had been discarded after each round, the third answer would have been A-B-C-E-D-F with cost 12, which is wrong. This is the single most important implementation detail of Yen's algorithm.
Time: O(K V (E + V log V)) · Space: O(K V + E)
Each of the K - 1 rounds examines the spur vertices of one path, at most V of them, and runs one Dijkstra per spur vertex at O(E + V log V). That gives O(K V (E + V log V)). The candidate set can be kept in a priority queue, and it holds at most K V paths of up to V vertices each. In practice many spur searches end quickly, and Lawler noted that spur vertices before the deviation point of the previous path need not be re-examined, which saves a large share of the work.
K shortest paths come in two flavours: with or without repeated vertices. The difference decides the algorithm.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Dijkstra's algorithm | You only need the single shortest path, K = 1. | O(E + V log V) |
| Eppstein's algorithm | Paths may repeat vertices. It then lists K paths far faster than Yen. | O(E + V log V + K) |
| Suurballe's algorithm | You need two or more paths that share no edges or vertices, for redundancy rather than ranking. | O(E + V log V) per path |
| Constrained shortest path | You want the cheapest path that satisfies a side limit, such as time or tolls. Listing paths until one fits is a common simple method. | NP-hard in general |
Read the full article: Shortest Path Algorithms Explained
Read the full article: Dijkstra's Algorithm Explained Step by Step
Related algorithms: Dijkstra's Algorithm, A* Search Algorithm, Bellman-Ford Algorithm, Widest Path (Max Bottleneck)