How to modify dijkstra algorithm to find all possible paths?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Dijkstra's algorithm is a classic algorithm used to find the shortest path between nodes in a weighted graph. However, in some applications, it is necessary to find not just the single shortest path, but all possible paths from a source node to a destination node. This article explores how to modify Dijkstra's algorithm to achieve this. The approach differs fundamentally because while Dijkstra is optimized for finding the shortest path efficiently, finding all paths involves understanding the complete topology of the graph.
Understanding Dijkstra's Algorithm
Before discussing the modifications, let's briefly understand how Dijkstra's algorithm works:
- Initialize: Set the initial distance to the source node to 0 and all other nodes to infinity.
- Priority Queue: Use a priority queue to keep track of nodes to explore, ordered by the shortest discovered distance.
- Visit Nodes: Pop the node with the smallest distance from the queue. Update the distances to its neighbors if a shorter path is found.
- Repeat: Continue this process until all nodes have been visited.
Dijkstra's algorithm relies on the idea that once a node has been visited with the shortest path, it does not need to be revisited.
Modifying Dijkstra for All Possible Paths
To modify the algorithm to find all possible paths, we must fundamentally transform how we search through the graph:
Key Modifications
- Remove Shortest Path Optimization: Instead of finding just the shortest path to each node, all paths need to be explored, which means nodes may need to be revisited, unlike in Dijkstra’s original design.
- Path Storage: Use a list to store multiple paths as they are discovered, rather than tracking just the shortest path to each node.
- Recursive DFS with Backtracking: Implement a Depth-First Search (DFS) from the source node, maintaining a path history that can be reversed (backtracked) to explore all potential paths.
- Avoid Cycles: Track visited nodes in the current path to avoid cycles (mandatory for DAGs [Directed Acyclic Graphs], optional depending on problem constraints for general graphs).
Pseudocode Example
- Recursive Function: The function
findAllPathsexplores each path recursively. It uses a backtracking approach by marking a node as visited when it is added to the current path and marking it unvisited when that path is fully explored. - Handling Cycles: To avoid cycles, once a node is added to the current path, it is marked as visited, preventing it from being revisited within the same path.
- Path Storage: Paths that reach the destination are stored in
all_paths. - Network Analysis: Understanding all potential routes a packet could travel.
- Game Theory: Exploring all possibilities in decision trees.
- Robotics and GPS: Identifying all feasible paths in navigation solutions.

