Find the paths between two given nodes?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding a path between two nodes sounds simple, but the correct algorithm depends on what path means in your problem. Do you want any path, the shortest path by number of edges, the lowest-cost path in a weighted graph, or every simple path?
Those are different tasks. The fastest way to solve the problem is to choose the algorithm that matches the question instead of reaching for one generic graph traversal every time.
Start by Defining the Goal
Here are the common interpretations:
- Any path in an unweighted graph.
- The shortest path in an unweighted graph.
- The lowest-cost path in a weighted graph.
- All simple paths between two nodes.
If your graph is unweighted and you want the shortest route by number of edges, Breadth-First Search is usually the right answer. If the graph has non-negative edge weights, Dijkstra's algorithm is the standard choice. If you need all simple paths, use backtracking with Depth-First Search.
Breadth-First Search for the Shortest Unweighted Path
BFS explores the graph level by level. The first time it reaches the target, it has found the shortest path in terms of edge count.
For an unweighted graph, this is usually the best first solution.
Depth-First Search for Any Path or All Paths
DFS is useful when you only need one valid path or when you want to enumerate every simple path.
This version returns all simple paths:
This is a good fit for small graphs, but the number of simple paths can explode quickly. In dense graphs, enumerating all paths becomes expensive.
Dijkstra for Weighted Graphs
When edges have costs, BFS is no longer enough. Dijkstra's algorithm finds the lowest total cost path as long as the weights are non-negative.
If the graph can contain negative edge weights, switch to Bellman-Ford instead.
Choosing the Right Representation
Most examples use an adjacency list because it is compact and easy to traverse:
For weighted graphs, store neighbors as (neighbor, weight) pairs. An adjacency matrix is possible, but for sparse graphs it is usually less convenient and more memory-hungry.
Common Pitfalls
The most common mistake is not defining whether you want one path or all paths. Those are very different computational problems.
Another mistake is using DFS when the goal is the shortest path in an unweighted graph. DFS can find a path quickly, but not necessarily the shortest one.
Cycles also cause bugs. If you do not track visited nodes or the current path, a traversal can loop forever. For all paths, check membership in the current path rather than using one global visited set.
Finally, be careful with weighted graphs. BFS ignores weights, so it can return a path with fewer edges but higher total cost.
Summary
- Use BFS for the shortest path in an unweighted graph.
- Use DFS when you want any path or all simple paths.
- Use Dijkstra for lowest-cost paths when edge weights are non-negative.
- Clarify whether the task is
one path,shortest path, orall pathsbefore coding. - Track visited nodes or the current path carefully to avoid infinite loops on cyclic graphs.

