Eppstein's algorithm
Yen's algorithm
k shortest paths
graph algorithms
computational theory

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.

Practice algorithms

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 kk 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:

  1. Preprocessing: Construct the shortest-path tree from the source node to all other nodes using a standard shortest path algorithm like Dijkstra's.
  2. Auxiliary Graph Construction: Construct an auxiliary graph that captures possible deviations from paths in the shortest-path tree using priority queues and heaps.
  3. Path Enumeration: Generate the kk 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 GG with vertices VV and edges EE. Suppose we want to find the 3 shortest paths from node AA to node DD. 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 O(m+nlog⁡n+klog⁡k)O(m + n \log n + k \log k) time, where mm is the number of edges, nn is the number of vertices, and kk is the number of paths to find. This makes it particularly efficient for large graphs with significant kk values.

Yen's Algorithm

Overview

Yen’s algorithm, introduced by Jin Y. Yen in 1971, is another well-known approach for finding the kk 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:

  1. Initial Shortest Path Calculation: Use Dijkstra’s algorithm to find the shortest path from the source to the destination.
  2. Path Deviation: For each path found, generate new candidate paths by deviating from the current shortest path at various nodes.
  3. Candidate Evaluation: Store and evaluate candidate paths using a priority queue to ensure the next shortest path is selected efficiently.
  4. Repeat: Iterate the process until the k-shortest paths are found.

Example

In a graph, assume you need 2 shortest paths between nodes SS and TT. After finding the shortest path with Dijkstra's, say S→A→B→TS \rightarrow A \rightarrow B \rightarrow T, Yen’s will look for deviations from it, such as S→C→B→TS \rightarrow C \rightarrow B \rightarrow T or S→A→D→TS \rightarrow A \rightarrow D \rightarrow T, evaluating each to find the next shortest path.

Complexity

The complexity of Yen’s algorithm is O(kn(m+nlog⁡n))O(kn (m + n \log n)), primarily due to the repeated application of Dijkstra's algorithm for each path. This makes it less efficient than Eppstein’s for large kk 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:

CharacteristicEppstein's AlgorithmYen's Algorithm
Initial StepShortest-path tree constructionSingle shortest path with Dijkstra's
Path ExpansionUsing an auxiliary graphPath deviation strategy
Data StructuresFibonacci heaps, priority queuesPriority queues
ComplexityO(m+nlog⁡n+klog⁡k)O(m + n \log n + k \log k)O(kn(m+nlog⁡n))O(kn (m + n \log n))
Efficiency for Large kVery efficientLess efficient
Ease of ImplementationMore complexRelatively 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 kk is large or when computational efficiency is critical. In contrast, Yen’s algorithm is often preferred for its relatively straightforward implementation, especially when kk 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.


Course
Intermediate
27 lessons
15 hours
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 course
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.

Practice algorithms