What is a Hamiltonian Path?
A Hamiltonian Path visits every vertex in a graph exactly once, using only existing edges. Unlike an Eulerian path (which is about visiting every edge), there's no simple rule for when a Hamiltonian Path exists - the only general approach is to search for one, which is exactly where backtracking comes in.
The search extends the path one node at a time. Whenever it reaches a node with no unvisited neighbors and the path isn't complete yet, it's a dead end - the algorithm removes the last node and tries a different neighbor instead, exactly like the N-Queens Problem undoing a bad placement.
Time & Space Complexity
- Worst Case TimeO(N!)
- Space (Recursion + Path)O(N)
- Problem ClassNP-Complete
Real-World Use Cases
- Route planning that must visit every stop once
- DNA fragment assembly and sequencing
- Circuit board drilling and PCB routing