Hamiltonian paths and cycles
Plan routes that visit every place exactly once in Year 12 General Mathematics for Queensland (QCAA). A courier dropping parcels at each shop, a tourist touring every lookout 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 learn the difference between Hamiltonian and semi-Hamiltonian graphs, find paths and cycles by trial and error, and use degree-1 and degree-2 vertices to rule routes in or out. You will list and compare round trips to find the shortest, and contrast these routes with Eulerian trails, before moving on to trees and minimum 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 Queensland students explains Hamiltonian and semi-Hamiltonian graphs, how to find these routes by trial and error, 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 that has a Hamiltonian cycle is a Hamiltonian graph. A graph that has a Hamiltonian path but no Hamiltonian cycle is a semi-Hamiltonian graph. A Hamiltonian graph is not called semi-Hamiltonian, even though it also has Hamiltonian paths.
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 |
| Open only | semi-Eulerian | semi-Hamiltonian |
| How to decide | degrees | trial and error |
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, so the graph is Hamiltonian.
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. There is no Hamiltonian cycle, so the graph is semi-Hamiltonian. (Every vertex is even, so this graph is Eulerian, 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.
What does semi-Hamiltonian mean?
A semi-Hamiltonian graph has a Hamiltonian path but no Hamiltonian cycle. A graph with a Hamiltonian cycle is called Hamiltonian instead.
How do you know if a graph is Hamiltonian?
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 be Eulerian without being Hamiltonian, and the other way round.