learngraphtheory.org

Guest User

Using app without sign in

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 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.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Yen's K Shortest Paths Calculator

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

Time: O(K·V·(E + V log V))
Space: O(K·V)
Use Case: Alternative routes, backup paths, traffic assignment
Showing the example graph for this algorithm.
Auto10
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Yen's K Shortest Paths

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.

How it works

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.

Applications

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.

Pseudocode

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 A

Every 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.

Worked example, step by step

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.

  1. Path 1. Dijkstra gives A-C-E-D-F with cost 2 + 3 + 1 + 2 = 8.
  2. Spurs of path 1. Spur at A, with A-C blocked: A-B-D-F, cost 9. Spur at C, with C-E blocked and A removed: A-C-B-D-F, cost 11. Spur at E, with E-D blocked and A, C removed: A-C-E-F, cost 10. Spur at D, with D-F blocked and A, C, E removed: no route to F is left.
  3. Path 2. The candidates cost 9, 11 and 10. The cheapest, A-B-D-F with cost 9, becomes path 2. The other two stay in B.
  4. Spurs of path 2. Spur at A: both A-C and A-B are blocked, because paths 1 and 2 share the root A, so nothing is found. Spur at B, with B-D blocked and A removed: A-B-C-E-D-F, cost 12. Spur at D, with D-F blocked and A, B removed: A-B-D-E-F, cost 13.
  5. Path 3. B now holds candidates of cost 11, 10, 12 and 13. The cheapest is A-C-E-F with cost 10, generated in the previous round as a spur of path 1.

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.

Complexity, and where it comes from

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.

When to use Yen's K Shortest Paths, and when not to

K shortest paths come in two flavours: with or without repeated vertices. The difference decides the algorithm.

AlternativePrefer it whenCost
Dijkstra's algorithmYou only need the single shortest path, K = 1.O(E + V log V)
Eppstein's algorithmPaths may repeat vertices. It then lists K paths far faster than Yen.O(E + V log V + K)
Suurballe's algorithmYou 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 pathYou 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

Common pitfalls

  • Discarding the candidates each round. The next best path may have been generated rounds earlier. In the example, path 3 came from a spur of path 1. Clearing B between rounds returns wrong paths.
  • Blocking only the edge of the last path. At a spur vertex, the edge out of the root must be blocked for every accepted path with the same root, not only the most recent one. Otherwise a known path is found again.
  • Not removing the root vertices. If the vertices of the root stay in the graph, the spur path can loop back through them, and the result is no longer a simple path.
  • Adding duplicates to the candidate set. The same candidate can be generated from different spur vertices or rounds. Check for duplicates before inserting, or the same path can be accepted twice.

Frequently asked questions

What is Yen's algorithm?
It is an algorithm, published by Jin Y. Yen in 1971, that finds the K shortest loopless paths between two vertices of a graph with nonnegative weights, in increasing order of cost. Each new path is the cheapest deviation from an already accepted path.
What is a spur node in Yen's algorithm?
The vertex at which a candidate path leaves an accepted path. The part before it is the root path. The algorithm finds the cheapest spur path from that vertex to the target, avoiding the edges that would rebuild an accepted path and the vertices of the root.
What is the time complexity of Yen's algorithm?
O(K V (E + V log V)) with Dijkstra and a Fibonacci heap. Each of the K rounds runs up to V shortest path searches, one per spur vertex.
What is the difference between Yen and Eppstein algorithms?
Yen returns loopless paths, with no repeated vertices. Eppstein allows repeated vertices and in exchange runs in O(E + V log V + K), which is much faster. For route planning, where loops are useless, Yen or its refinements are used.
Where are k shortest paths used?
In route planning to offer alternative routes, in network routing for backup paths, in transport modelling to spread traffic over several reasonable routes, and as a building block for constrained shortest path problems, where paths are listed until one meets the constraint.

Related algorithms: Dijkstra's Algorithm, A* Search Algorithm, Bellman-Ford Algorithm, Widest Path (Max Bottleneck)

Interactive Controls

Zoom Controls

40%
Nodes: 6
Edges: 8