Hamiltonian paths and cycles
Plan routes that visit every place exactly once in Year 12 VCE General Mathematics. A courier dropping parcels at each shop, a tour bus calling at every goldfields town, or a technician servicing each mobile tower does not care about using every road, only about reaching every stop once. That is the idea of a Hamiltonian path or cycle.
You will find Hamiltonian paths and Hamiltonian cycles by trial and error, use vertices of degree 1 and 2 to rule routes in or out, and count the tours in a complete graph. You will list and compare round trips to find the shortest, and contrast these routes with Eulerian trails, before moving on to trees and spanning trees.
Theory
A Hamiltonian path visits every vertex of a graph exactly once, and a Hamiltonian cycle also returns to its start. This Year 12 General Mathematics guide for students in Victoria shows how to find these routes by trial and error, how to rule a cycle out, how to count the cycles in a complete graph, and how to choose the shortest route that visits every place.
A Hamiltonian path is a path that visits every vertex of a graph exactly once. It starts and finishes at different vertices, and it does not need to use every edge. A Hamiltonian cycle visits every vertex exactly once and then returns to its starting vertex, so the start is the only vertex written twice. On \(n\) vertices a Hamiltonian path uses \(n - 1\) edges and a Hamiltonian cycle uses \(n\) edges.
A graph can have a Hamiltonian path without having a Hamiltonian cycle. If a graph has a Hamiltonian cycle, removing any one edge of that cycle leaves a Hamiltonian path, so every graph with a Hamiltonian cycle also has a Hamiltonian path.
There is no simple degree test, so Hamiltonian paths and cycles are found by trial and error. Two facts help. A vertex of degree \(1\) can never be on a cycle. A vertex of degree \(2\) forces both of its edges into any Hamiltonian cycle.
For a graph with \(n\) vertices:
In a weighted network, the length of a route is the total of its edge weights:
Comparing the two ideas:
| Eulerian | Hamiltonian | |
|---|---|---|
| Uses each | edge exactly once | vertex exactly once |
| Open route | Eulerian trail | Hamiltonian path |
| Closed route | Eulerian circuit | Hamiltonian cycle |
| How to decide | degrees | trial and error |
When every pair of \(n\) vertices is joined (a complete graph), fix the start, count the orders of the other \(n - 1\) vertices, then halve, because a cycle and its reverse are the same cycle:
How to find a Hamiltonian path or cycle by trial and error
- Look at low degrees first. A degree-\(1\) vertex rules out a cycle and must be an end of any path. A degree-\(2\) vertex forces both its edges into a cycle.
- Build the route. Start at a forced or low-degree vertex and move to an unvisited neighbour each time. If you get stuck, go back a step and try another choice.
- Check. Every vertex appears exactly once (the start twice for a cycle), and each step uses a real edge.
- For the shortest route, list every possible route from the fixed start, one per line with its total, and choose the smallest.
\(C\) and \(F\) have degree \(2\), so \(B\text{β}C\text{β}D\) and \(E\text{β}F\text{β}A\) must be part of the cycle. \(G\) still has to be visited.
| \(\text{forced}\) | \(:\) | \(B\text{β}C\text{β}D,\ E\text{β}F\text{β}A\) |
| \(\text{try}\) | \(:\) | \(A\text{β}B\text{β}C\text{β}D\text{β}E\text{β}F\text{β}A\ \text{misses } G\) |
| \(\text{try}\) | \(:\) | \(A\text{β}B\text{β}C\text{β}D\text{β}G\text{β}E\text{β}F\text{β}A\) |
| \(\text{check}\) | \(:\) | \(7 \text{ vertices once},\ 7 \text{ edges}\) |
\(A\text{β}B\text{β}C\text{β}D\text{β}G\text{β}E\text{β}F\text{β}A\) is a Hamiltonian cycle: it visits all \(7\) vertices once and returns to \(A\).
Find a Hamiltonian path, then show that no Hamiltonian cycle exists.
| \(\text{path}\) | \(:\) | \(K\text{β}L\text{β}M\text{β}N\text{β}O\) |
| \(\text{cycle}\) | \(:\) | \(\text{must pass through } M \text{ twice}\) |
The path \(K\text{β}L\text{β}M\text{β}N\text{β}O\) visits every vertex once. A cycle would have to go from the left triangle to the right triangle and back, and both crossings are through \(M\), so \(M\) would be visited twice. So the graph has a Hamiltonian path but no Hamiltonian cycle. (Every vertex is even, so this graph has an Eulerian circuit, which shows the two ideas are different.)
List each different Hamiltonian cycle from \(D\) (a reverse is the same cycle) and total it.
| \(D\text{β}P\text{β}Q\text{β}R\text{β}D\) | \(:\) | \(14 + 9 + 13 + 11 = 47\ \text{km}\) |
| \(D\text{β}P\text{β}R\text{β}Q\text{β}D\) | \(:\) | \(14 + 18 + 13 + 20 = 65\ \text{km}\) |
| \(D\text{β}Q\text{β}P\text{β}R\text{β}D\) | \(:\) | \(20 + 9 + 18 + 11 = 58\ \text{km}\) |
The shortest round trip is \(D\text{β}P\text{β}Q\text{β}R\text{β}D\) (or the same trip in reverse), a total of 47 km.
List every Hamiltonian path from \(A\) to \(E\) and total each one.
| \(A\text{β}C\text{β}B\text{β}D\text{β}F\text{β}E\) | \(:\) | \(5 + 6 + 7 + 4 + 5 = 27\ \text{min}\) |
| \(A\text{β}B\text{β}C\text{β}D\text{β}F\text{β}E\) | \(:\) | \(8 + 6 + 10 + 4 + 5 = 33\ \text{min}\) |
| \(A\text{β}B\text{β}C\text{β}F\text{β}D\text{β}E\) | \(:\) | \(8 + 6 + 9 + 4 + 6 = 33\ \text{min}\) |
| \(A\text{β}B\text{β}D\text{β}C\text{β}F\text{β}E\) | \(:\) | \(8 + 7 + 10 + 9 + 5 = 39\ \text{min}\) |
These are the only \(4\) Hamiltonian paths from \(A\) to \(E\). The quickest is \(A\text{β}C\text{β}B\text{β}D\text{β}F\text{β}E\), taking 27 minutes.
Common pitfalls
Frequently asked questions
What is a Hamiltonian path?
A Hamiltonian path is a path that visits every vertex of a graph exactly once. It starts and finishes at different vertices and may leave some edges unused.
What is the difference between a Hamiltonian path and a Hamiltonian cycle?
A Hamiltonian cycle also visits every vertex exactly once, but it then returns to the starting vertex. A Hamiltonian path stops at a different vertex from where it began.
How many Hamiltonian cycles does a complete graph have?
Fix the starting vertex, count the orders of the other n - 1 vertices, then halve because a cycle and its reverse are the same. With 5 vertices that is 4 x 3 x 2 x 1 = 24 orders, so 12 cycles.
How do you know if a graph has a Hamiltonian cycle?
There is no simple test like the degree rule for Eulerian graphs. Use trial and error to look for a cycle through every vertex. A vertex of degree 1, or a vertex every route would have to pass through twice, means there is no Hamiltonian cycle.
What is the difference between Eulerian and Hamiltonian?
Eulerian routes use every edge exactly once, and Hamiltonian routes visit every vertex exactly once. A graph can have an Eulerian circuit without a Hamiltonian cycle, and the other way round.