How to modify dijkstra algorithm to find all possible paths?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- How to modify Levenshteins Edit Distance to count adjacent letter exchanges as 1 edit
- How to move duplicates to the end of an array while preserving order in C?
- How to multiply matrices in the result of using chain rule in the backpropagation algorithm
- How to obtain all subsequence combinations of a String in Java, or C etc
- How to modify list entries during for loop?
- How to monitor queue health in celery
- How to obtain features' weights
- How to obtain the index permutation after the sorting

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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.