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

Hamiltonian paths and cycles

20 practice questions 0 video lessons Theory + worked examples

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.

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

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\): a Hamiltonian cycle. The edges \(BF\) and \(CE\) are not needed.
A Hamiltonian path but no Hamiltonian cycleDiamond 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: a Hamiltonian path but no Hamiltonian cycle.

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
How to decidedegreestrial 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:

\[\text{number of Hamiltonian cycles} = \dfrac{(n-1) \times (n-2) \times \dots \times 2 \times 1}{2}\]
(n−1)!2
Counting cycles. \(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 \(\dfrac{3 \times 2 \times 1}{2} = 3\) different Hamiltonian cycles; with seven vertices there are already \(360\).

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: it visits all \(7\) vertices once and returns to \(A\).

Example 2 β€” A path but no cycle
Two triangles of roads meet at a roundabout \(M\). Show that the graph has a Hamiltonian path but no Hamiltonian cycle.
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}\)
Hamiltonian path, no Hamiltonian cycle

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

Example 3 β€” Shortest delivery round
A van leaves its depot \(D\) in Warrnambool, 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 in the Grampians 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.
A path is not a cycle. Finding a Hamiltonian path does not mean there is a Hamiltonian cycle. For a cycle, the last vertex must also be joined back to the start.
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.

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.