Shortest path to visit all nodes
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
For an unweighted graph, the shortest path that visits all nodes is usually solved with breadth-first search over both position and visited-state. The key idea is that "where you are" is not enough; the algorithm also needs to know "which nodes have already been visited."
Why Ordinary BFS Is Not Enough
A normal BFS state is just the current node. That works for shortest path from one source to one destination, but it fails here because reaching the same node with different visited sets represents different progress.
For example, arriving at node 3 after visiting only 0 and 3 is not equivalent to arriving at node 3 after visiting 0, 1, 2, 3.
So the real state is:
- current node
- bitmask of visited nodes
The State-Space BFS Idea
If the graph has n nodes, let bit i in a mask indicate whether node i has been visited.
Examples for n = 4:
- mask
0001means only node0visited - mask
1011means nodes0,1, and3visited - mask
1111means all nodes visited
The target mask is:
Once BFS reaches any state with that mask, the corresponding distance is the shortest number of edges needed to visit all nodes.
Start BFS from Every Node
This problem usually allows starting from any node. That means the best path might begin anywhere, so initialize the queue with all nodes as starting states.
That looks like this:
This is the standard and efficient solution for the common interview version of the problem.
Why This Produces the Shortest Path
BFS explores states in increasing order of distance. Since each edge traversal has equal cost, the first time you reach a state whose mask contains all nodes, you know no shorter path exists.
This works because:
- each transition adds exactly one edge to the path
- BFS processes distance
dbefore distanced + 1 - the state includes enough information to distinguish useful revisits
Revisiting nodes is allowed, and that matters. Unlike a Hamiltonian path, the shortest route to cover all nodes may pass through some nodes more than once.
Example Walkthrough
Consider this graph:
One optimal route is 1 -> 0 -> 2 -> 3, which visits all four nodes in three edges. Starting BFS from every node allows the algorithm to discover that quickly.
Time and Space Complexity
There are at most n * 2^n distinct states:
- '
nchoices for the current node' - '
2^npossible visited masks'
For each state, the algorithm iterates over the node's neighbors. So the typical complexity is:
- time:
O(n * 2^n + m * 2^n)or commonly simplified toO(n * 2^n) - space:
O(n * 2^n)
That is feasible for small graphs, which is exactly why this pattern is popular in interview and contest problems.
Common Misunderstandings
This problem is often confused with the Traveling Salesman Problem. They are related, but not identical.
- TSP usually assumes weighted edges and often asks for a cycle returning to the start.
- "Shortest path to visit all nodes" in the common coding-problem form usually uses an unweighted graph and allows revisiting nodes.
That difference is why a BFS with bitmasks is appropriate here, whereas full TSP often requires dynamic programming over weighted distances or approximation methods.
Common Pitfalls
- Using a visited set keyed only by node causes incorrect pruning because different masks matter.
- Starting BFS from one node only can miss the optimal answer when the start is allowed to vary.
- Treating the problem like Hamiltonian path incorrectly forbids revisiting nodes.
- Forgetting the target mask
(1 << n) - 1leads to off-by-one bugs. - Using DFS without memoization usually becomes much slower and more complicated.
Summary
- Model each BFS state as
(current_node, visited_mask). - Start from every node because the best path may begin anywhere.
- Use a bitmask to track which nodes have been visited.
- The first BFS state that reaches the all-visited mask gives the shortest answer.
- This is not the same as general weighted TSP, even though the problems are related.
Related reading
- Shortest path with even number of edges
- Shortest Sudoku Solver in Python - How does it work?
- Shortest uncommon substring shortest substring of one string, that is not a substring of another string
- Should I learn about data structures and algorithms first or the programming language Java first?
- Should an octree be rebuilt every frame?
- Should I use BFS, DFS for tree traversal or in-order, post -order, pre-order?
- Should I use rand N or rand / RAND_MAX / N 1?
- Should one prefer STL algorithms over hand-rolled loops?

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.