Resources For Teachers For Tutors For Students & Parents AI New Books Pricing
Year 12 General (Unit 3 & 4) Graphs and networks

Hamiltonian paths and cycles

20 practice questions 0 video lessons Theory + worked examples

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.

Create a free accountTrack your progress and save your work as you go.
Create free account

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.

Key idea. Eulerian is about edges (use every edge once). Hamiltonian is about vertices (visit every vertex once). A route can be one without being the other.
A Hamiltonian cycleHexagon A B C D E F with chords B F and C E. The cycle A B C D E F A is shown in red; the chords are not used. A B C D E F
The red cycle \(A\text{-}B\text{-}C\text{-}D\text{-}E\text{-}F\text{-}A\) visits all \(6\) vertices once and returns to \(A\), so the graph is Hamiltonian. The edges \(BF\) and \(CE\) are not needed.
A semi-Hamiltonian graphDiamond P Q R S with diagonal Q S, and T joined only to R. The path T R Q P S is shown in red. P Q R S T
\(T\) has degree \(1\), so no cycle can pass through it, but the path \(T\text{-}R\text{-}Q\text{-}P\text{-}S\) visits every vertex once. The graph is semi-Hamiltonian.

For a graph with \(n\) vertices:

\[\text{edges in a Hamiltonian path} = n - 1, \qquad \text{edges in a Hamiltonian cycle} = n\]
n−1,n

In a weighted network, the length of a route is the total of its edge weights:

\[\text{length of a Hamiltonian cycle} = \text{sum of the weights of its } n \text{ edges}\]
length=∑weights

Comparing the two ideas:

EulerianHamiltonian
Uses eachedge exactly oncevertex exactly once
Open routeEulerian trailHamiltonian path
Closed routeEulerian circuitHamiltonian cycle
Open onlysemi-Euleriansemi-Hamiltonian
How to decidedegreestrial and error
Counting cycles. A cycle and its reverse are the same cycle, so \(D\text{-}P\text{-}Q\text{-}R\text{-}D\) and \(D\text{-}R\text{-}Q\text{-}P\text{-}D\) count once. With every pair of four vertices joined, there are only \(3\) different Hamiltonian cycles.

How to find a Hamiltonian path or cycle by trial and error

  1. 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.
  2. 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.
  3. Check. Every vertex appears exactly once (the start twice for a cycle), and each step uses a real edge.
  4. For the shortest route, list every possible route from the fixed start, one per line with its total, and choose the smallest.
Example 1 β€” Find a Hamiltonian cycle
Find a Hamiltonian cycle in the graph shown, starting at \(A\), or explain why there is none.
Solution

\(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.

Example 1 graphTwo squares side by side, A B E F and B C D E, with a centre vertex G joined to A, B, D and E. A B C D E F G
\(\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}\)
Example 1 Hamiltonian cycleThe cycle A B C D G E F A shown in red. A B C D E F G
ABCDGEFA

\(A\text{-}B\text{-}C\text{-}D\text{-}G\text{-}E\text{-}F\text{-}A\) is a Hamiltonian cycle, so the graph is Hamiltonian.

Example 2 β€” Semi-Hamiltonian
Two triangles of roads meet at a roundabout \(M\). Show that the graph is semi-Hamiltonian.
Solution

Find a Hamiltonian path, then show that no Hamiltonian cycle exists.

Example 2 graphTwo triangles K L M and M N O meeting at M. The path K L M N O is shown in red. K L M N O
\(\text{path}\)\(:\)\(K\text{-}L\text{-}M\text{-}N\text{-}O\)
\(\text{cycle}\)\(:\)\(\text{must pass through } M \text{ twice}\)
semi-Hamiltonian

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.)

Example 3 β€” Shortest delivery round
A van leaves its depot \(D\) in Townsville, delivers to shops \(P\), \(Q\) and \(R\), and returns to \(D\). Distances are in km. Find the shortest round trip.
Solution

List each different Hamiltonian cycle from \(D\) (a reverse is the same cycle) and total it.

Delivery depot and three shopsDepot D and shops P, Q, R, every pair joined. Distances DP 14, DQ 20, DR 11, PQ 9, PR 18, QR 13 km. D P Q R 14 20 11 9 18 13
\(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}\)
Delivery depot and three shopsDepot D and shops P, Q, R, every pair joined. Distances DP 14, DQ 20, DR 11, PQ 9, PR 18, QR 13 km. The shortest cycle D P Q R D is shown in red. D P Q R 14 20 11 9 18 13
47km

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.

Example 4 β€” Quickest scenic drive
A tourist on the Sunshine Coast hinterland starts at lookout \(A\) and wants to finish at \(E\), visiting every lookout exactly once. Travel times are in minutes. Find the quickest route.
Solution

List every Hamiltonian path from \(A\) to \(E\) and total each one.

Lookouts on a scenic driveTravel times in minutes: AB 8, AC 5, BC 6, BD 7, CD 10, CF 9, DF 4, DE 6, FE 5. A B C D F E 8 5 6 7 9 10 4 6 5
\(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}\)
Lookouts on a scenic driveTravel times in minutes: AB 8, AC 5, BC 6, BD 7, CD 10, CF 9, DF 4, DE 6, FE 5. The quickest route A C B D F E is shown in red. A B C D F E 8 5 6 7 9 10 4 6 5
27min

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

Vertices, not edges. A Hamiltonian route does not need to use every edge. Watch out for mixing it up with an Eulerian trail, which uses every edge.
Semi-Hamiltonian means no cycle. Watch out for calling a graph semi-Hamiltonian just because it has a Hamiltonian path. If it also has a Hamiltonian cycle, it is Hamiltonian.
Reverses are the same cycle. When listing round trips, watch out for counting a cycle and its reverse as two different answers.

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.