Eppstein's algorithm and Yen's algorithm for k shortest paths
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
In the field of computer science and graph theory, finding the shortest paths between nodes is a fundamental problem with numerous applications, such as network routing, transportation, and logistics. While the single-source shortest path problem has efficient solutions like Dijkstra's and Bellman-Ford algorithms, the problem of finding the k shortest paths is more complex. Two prominent algorithms to address this challenge are Eppstein's algorithm and Yen's algorithm. This article provides a detailed examination of these algorithms and their applications.
Eppstein's Algorithm
Overview
Eppstein's algorithm was introduced by David Eppstein in 1998. It finds the shortest paths between two nodes in a given graph efficiently. Contrary to Yen’s algorithm, Eppstein’s approach does not recompute paths from scratch and utilizes advanced data structures for efficiency.
Technical Explanation
Eppstein’s algorithm employs the following main steps:
- Preprocessing: Construct the shortest-path tree from the source node to all other nodes using a standard shortest path algorithm like Dijkstra's.
- Auxiliary Graph Construction: Construct an auxiliary graph that captures possible deviations from paths in the shortest-path tree using priority queues and heaps.
- Path Enumeration: Generate the shortest paths using the auxiliary graph, validating each path and maintaining an ordered list.
Key data structures used include Fibonacci heaps and binary heaps to efficiently manage and retrieve information needed for the path enumeration.
Example
Consider a directed graph with vertices and edges . Suppose we want to find the 3 shortest paths from node to node . Eppstein's algorithm will preprocess the graph to create the shortest path tree and auxiliary data structures. It will then iteratively extract paths efficiently using these structures until three distinct paths are found.
Complexity
Eppstein’s algorithm runs in time, where is the number of edges, is the number of vertices, and is the number of paths to find. This makes it particularly efficient for large graphs with significant values.
Yen's Algorithm
Overview
Yen’s algorithm, introduced by Jin Y. Yen in 1971, is another well-known approach for finding the shortest paths. It utilizes path deviations and Dijkstra’s algorithm to iteratively produce paths after the initial shortest path is determined.
Technical Explanation
The main steps of Yen's algorithm are:
- Initial Shortest Path Calculation: Use Dijkstra’s algorithm to find the shortest path from the source to the destination.
- Path Deviation: For each path found, generate new candidate paths by deviating from the current shortest path at various nodes.
- Candidate Evaluation: Store and evaluate candidate paths using a priority queue to ensure the next shortest path is selected efficiently.
- Repeat: Iterate the process until the k-shortest paths are found.
Example
In a graph, assume you need 2 shortest paths between nodes and . After finding the shortest path with Dijkstra's, say , Yen’s will look for deviations from it, such as or , evaluating each to find the next shortest path.
Complexity
The complexity of Yen’s algorithm is , primarily due to the repeated application of Dijkstra's algorithm for each path. This makes it less efficient than Eppstein’s for large but practical for smaller values.
Comparison and Use Cases
Below is a table summarizing the key similarities and differences between Eppstein's and Yen's algorithms:
| Characteristic | Eppstein's Algorithm | Yen's Algorithm |
| Initial Step | Shortest-path tree construction | Single shortest path with Dijkstra's |
| Path Expansion | Using an auxiliary graph | Path deviation strategy |
| Data Structures | Fibonacci heaps, priority queues | Priority queues |
| Complexity | ||
| Efficiency for Large k | Very efficient | Less efficient |
| Ease of Implementation | More complex | Relatively simple |
Conclusion
Eppstein's and Yen's algorithms provide effective methods for solving the k shortest paths problem, each with distinct approaches and efficiencies. Eppstein’s algorithm is well-suited for scenarios where is large or when computational efficiency is critical. In contrast, Yen’s algorithm is often preferred for its relatively straightforward implementation, especially when is small. Understanding the nuances of each algorithm allows computational scientists and engineers to choose the right tool for their specific requirements in graph-related problem-solving.
Related reading
- Epsilon and learning rate decay in epsilon greedy q learning
- Equal sum subsets hybrid
- Equivalence classes and union/find in a functional language
- Error calculating pi using the Chudnovsky algorithm - Java
- equals vs Arrays.equals in Java
- Errata in the original paper on suffix arrays?
- Estimate the minimum Distance between two Clusters
- Eugene Myers' Diff Algorithm Finding the Longest Common Subsequence of A and B

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.