IB Maths AI HL Graph Theory Paper 1 & 2 Hamiltonian cycles ~7 min read

Travelling Salesman Problem

The Travelling Salesman Problem (TSP) asks: what’s the shortest closed route that visits every vertex exactly once? This closed route is a Hamiltonian cycle. For the classical TSP (graph complete and direct route is shortest), the only general method is brute force — list every Hamiltonian cycle, compute its weight, pick the smallest.

📘 What you need to know

Hamiltonian paths and cycles

A Hamiltonian path visits every vertex once; a Hamiltonian cycle does the same and returns to the start. There is no quick formula to decide whether a Hamiltonian cycle exists in a graph — the only way is to find one. This is the key difference from Eulerian, where counting odd-degree vertices gives an instant answer.

Postman vs Salesman: the postman walks every edge at least once (Eulerian); the salesman visits every vertex exactly once (Hamiltonian). Postman = edges, salesman = vertices.

The classical TSP

The classical TSP assumes the graph is complete (every pair of vertices joined) and satisfies the triangle inequality (a direct edge is always at least as short as any indirect route). Under these conditions:

Classical TSP solution shortest closed route = min weight over all Hamiltonian cycles enumerate all (n − 1)! cycles, compute weights, take minimum
All Hamiltonian cycles in K₄ — pick the smallest total 12 14 8 10 7 6 A B C D shortest: A→B→C→D→A = 31 3 distinct cycles (6 with reverses) ★ A → B → C → D → A = 8 + 10 + 7 + 6 = 31 ← shortest A → B → D → C → A = 8 + 14 + 7 + 12 = 41 A → C → B → D → A = 12 + 10 + 14 + 6 = 42minimum = 31 route: ABCDA (or its reverse ADCBA)
K₄ with edge weights AB=8, AC=12, AD=6, BC=10, BD=14, CD=7. All 3 distinct Hamiltonian cycles are listed on the right; the cheapest is A → B → C → D → A with weight 31.

How many cycles to check?

Vertices nCycles to list = (n−1)!Distinct totals = (n−1)! / 2
321
463
52412
612060
7720360

Brute force is fine up to 5 vertices. Beyond that, use the bounding methods (next page).

🧭 Recipe — solving classical TSP

  1. Fix a starting vertex (any one works).
  2. List all (n − 1)! orderings of the other vertices — each gives a Hamiltonian cycle.
  3. Compute the weight of each cycle by summing its n edge weights.
  4. Pair up reverses (same weight) to halve the work.
  5. Pick the minimum. State the route and its weight.

Worked examples

WE 1

Find a Hamiltonian cycle

A graph G has vertices { A, B, C, D, E } and edges AB, AE, BC, BD, CD, CE, DE. Show that G is Hamiltonian by finding a Hamiltonian cycle.

try a cycle that uses available edges A → B → C → E → D → ? need edge back to A DA not in edge list → try another order try A → B → D → C → E → A edges used: AB ✓, BD ✓, DC ✓, CE ✓, EA ✓ Hamiltonian cycle: A → B → D → C → E → A a Hamiltonian cycle is found ⇒ G is Hamiltonian.
WE 2

Hamiltonian vs Eulerian

The complete graph K4 has 4 vertices each of degree 3. Is K4 Eulerian? Is it Hamiltonian?

Eulerian check (degrees) every vertex has degree 3 (odd) 4 odd vertices → not Eulerian Hamiltonian check (find a cycle) try A → B → C → D → A: all edges exist in K₄ K₄: not Eulerian, but Hamiltonian degrees decide Eulerian; only finding a cycle decides Hamiltonian.
WE 3

Classical TSP on 4 vertices

A complete graph has vertices { A, B, C, D } with edge weights AB(6), AC(4), AD(7), BC(8), BD(3), CD(5). Find the shortest Hamiltonian cycle and state its weight.

list all (4−1)! = 6 cycles from A ABCDA = 6 + 8 + 5 + 7 = 26 ABDCA = 6 + 3 + 5 + 4 = 18 ACBDA = 4 + 8 + 3 + 7 = 22 ACDBA = 4 + 5 + 3 + 6 = 18 (reverse of ABDCA) ADBCA = 7 + 3 + 8 + 4 = 22 (reverse of ACBDA) ADCBA = 7 + 5 + 8 + 6 = 26 (reverse of ABCDA) minimum weight min = 18 shortest route: ABDCA (or ACDBA); weight = 18 3 distinct totals: 18, 22, 26 — each appears twice (forward and reverse).
WE 4

Classical TSP — distinct cycles only

A complete graph has vertices { A, B, C, D } with weights AB(12), AC(8), AD(20), BC(10), BD(15), CD(5). Find the minimum Hamiltonian cycle weight by considering only the 3 distinct cycles.

list 3 distinct cycles ABCDA = 12 + 10 + 5 + 20 = 47 ABDCA = 12 + 15 + 5 + 8 = 40 ACBDA = 8 + 10 + 15 + 20 = 53 minimum shortest route: ABDCA (or its reverse ACDBA); weight = 40 listing 3 distinct cycles is enough — each represents a pair of reverse routes with the same weight.
WE 5

Delivery driver — 4 cities

A driver based in city W must deliver to cities X, Y, Z and return to W. Distances (in km): WX(20), WY(35), WZ(18), XY(22), XZ(30), YZ(14). Find the shortest delivery route.

list 3 distinct cycles starting at W WXYZW = 20 + 22 + 14 + 18 = 74 WXZYW = 20 + 30 + 14 + 35 = 99 WYXZW = 35 + 22 + 30 + 18 = 105 minimum shortest route: W → X → Y → Z → W; distance = 74 km reverse W → Z → Y → X → W has the same total — also valid.
WE 6

Number of cycles for larger graphs

A complete graph has 7 vertices. How many Hamiltonian cycles must be checked by brute force? How many distinct totals? Why is brute force impractical here?

number of cycles = (n − 1)! = (7 − 1)! = 6! = 720 distinct totals = (n − 1)! / 2 = 720 / 2 = 360 720 cycles to check; 360 distinct totals at 360 sums by hand, brute force is impractical. Use bounding methods (next page) instead.

💡 Top tips

⚠ Common mistakes

Next up — Nearest Neighbour & Deleted Vertex Algorithms. For graphs with 6 or more vertices, listing all (n − 1)! cycles is impractical. Instead, sandwich the true minimum between an upper bound (nearest neighbour algorithm) and a lower bound (deleted vertex algorithm).

Need help with Graph Theory?

Get 1-on-1 help from an IB examiner who knows exactly what Paper 1 & 2 are looking for.

Book Free Session →