Hamiltonian Paths and Why They Are Hard
Learning Objectives
- Define Hamiltonian paths and cycles and contrast them with the Euler trails and circuits of
euler-paths-and-circuits. - Explain, with two 3-regular graphs on 10 vertices, why no test based on degrees can decide whether a graph is Hamiltonian, and what NP-completeness adds to that.
- State and prove Ore's theorem, derive Dirac's theorem from it, and show what goes wrong when a hypothesis is missing.
- Recognize Dirac's and Ore's conditions as sufficient but not necessary, using Hamiltonian graphs that violate both.
- Connect Hamiltonian cycles to the traveling salesman problem and count the work of brute force against the Held-Karp dynamic program.
Context & Motivation
William Rowan Hamilton outlined his icosian game in letters to John T. Graves in late 1856, showed it at the 1857 Dublin meeting of the British Association, and the toy maker Jaques and Son sold it from 1859. The goal was a closed route along the edges of a dodecahedron through each of its 20 corners exactly once. Hamilton was not the first: knight's tours on a chessboard are centuries older, and Thomas Kirkman was studying such cycles on polyhedra at about the same time.
This is Euler's question with vertices in place of edges. Euler's version has a complete answer: a pass over the degrees plus a connectivity check. The vertex version has no such answer, and the reason is not that nobody looked: in 1972 Richard Karp put it on his list of NP-complete problems, so a fast test of any kind would settle P versus NP. The theory offers instead sufficient conditions, which certify some graphs and say nothing about the rest, and exact algorithms that are exponential but far better than trying every order.
Core Theory
Definitions, and the contrast with Euler
In a graph on vertices, a Hamiltonian path is a path (no repeated vertex) that visits every vertex. A Hamiltonian cycle is a cycle through every vertex, which needs . A graph with a Hamiltonian cycle is Hamiltonian. Graphs here are simple: a cycle on at least 3 vertices never uses a loop or two edges between the same pair, so multigraphs add nothing.
The two properties are independent. The Königsberg multigraph from euler-paths-and-circuits has no Euler trail, yet its underlying simple graph has the Hamiltonian cycle (a cycle uses at most one edge of each parallel pair). The bowtie, two triangles sharing one vertex, has all degrees even (2, 2, 2, 2, 4) and so has an Euler circuit, but no Hamiltonian cycle: the shared vertex separates the two triangles, and a cycle covering both sides would have to pass through it twice.
This is why Euler's proof does not carry over: an Euler circuit may visit a vertex any number of times, the local constraint is only a parity, and Hierholzer glues closed trails at shared vertices. A Hamiltonian cycle uses exactly two edges per vertex, the choice at one vertex constrains all the others, and gluing at a shared vertex is exactly what is forbidden.
Why there is no degree test
Degrees do not determine the answer. The Petersen graph and the pentagonal prism (two 5-cycles joined by a perfect matching, the circular ladder on 10 vertices) both have 10 vertices and 15 edges, are connected, and every vertex has degree 3. Example 1 counts their Hamiltonian cycles by exhaustive search: the prism has 5, the Petersen graph has none. Any test that looks only at the degree sequence must give both graphs the same answer, so it is wrong on one of them. This argument is unconditional, but it only rules out tests that read degrees.
No fast test of any kind, unless P = NP. Deciding whether a graph has a Hamiltonian cycle is in NP: the certificate is the vertex order, and checking it means confirming that each vertex appears once and that consecutive pairs are edges. In his 1972 paper Reducibility Among Combinatorial Problems, Karp proved 21 problems NP-complete by reductions starting from satisfiability; two of them are Directed Hamilton Circuit (reduced from vertex cover) and Undirected Hamilton Circuit (reduced from the directed one). Hamiltonian path is NP-complete as well, through simple reductions between the path and cycle versions. Euler's condition is checked in time. A polynomial-time test for Hamiltonicity, reading degrees or anything else, would put an NP-complete problem in P and give P = NP. So unless P = NP, no characterization is as easy to check as Euler's.
Ore's theorem and Dirac's theorem
Theorem (Ore, 1960). Let be a simple graph with vertices such that for every pair of distinct non-adjacent vertices . Then is Hamiltonian.
Proof. Suppose satisfies the condition but is not Hamiltonian. Add edges one at a time as long as the graph stays non-Hamiltonian, until no more can be added; call the result . Adding edges only raises degrees, so still satisfies the condition. is not complete, because is Hamiltonian for . Pick non-adjacent in . Adding creates a Hamiltonian cycle, which must use , so has a Hamiltonian path .
Let and . Both are subsets of , with and . Since , some index lies in both. Then
is a Hamiltonian cycle in : it uses the edges and and otherwise follows the path. That contradicts the choice of . ∎
Theorem (Dirac, 1952). Every simple graph with vertices and minimum degree is Hamiltonian.
Proof. Any two vertices have degree sum at least , so Ore's condition holds. ∎ Diestel proves it directly from a longest path.
When a hypothesis is missing, the conclusion can fail.
- : has and no cycle at all.
- The bound : for odd , take two copies of sharing one vertex. Then , just below , and the shared vertex separates the graph, so there is no Hamiltonian cycle. For this is the bowtie, whose non-adjacent pairs have degree sum ; for it is two sharing a vertex, with . , with and degree sum 4 on its non-adjacent pairs of degree-2 vertices, is not Hamiltonian either.
Sufficient, not necessary. The cycle with is Hamiltonian, yet and its non-adjacent pairs have degree sum . Hamilton's own dodecahedron violates both conditions badly (degree 3 everywhere, ) and has 30 Hamiltonian cycles, a count computed in Example 2. Both theorems certify dense graphs; sparse Hamiltonian graphs slip past them.
Hamiltonian cycles and the traveling salesman problem
The traveling salesman problem (TSP) gives a complete graph with edge weights and asks for a Hamiltonian cycle of minimum total weight. Hamiltonian cycle reduces to it: give weight 1 to the edges of and 2 to the missing pairs, and is Hamiltonian exactly when the optimum tour costs . the-traveling-salesman-problem-a-case-study-in-np-hardness carries out that reduction in full.
Brute force. Fix vertex 0 as the start. Each of the orders of the other vertices gives a tour, and each tour appears twice (once per direction), so there are tours. Summing a tour's weights takes additions.
Held-Karp. Proposed independently in 1962 by Bellman and by Held and Karp, it solves subproblems instead of tours. For a set of vertices not containing 0 and some , let be the cheapest path that starts at 0, visits exactly the vertices of , and ends at . Then
and the optimum is . Only where the path ends matters, not its order inside , and that is the whole saving. There are values ; a set of size costs additions per end vertex, and summing over gives , plus for the final step. That is time and memory. Replacing "min of sums" by "or of ands" decides Hamiltonicity in the same time.
Worked Examples
Example 1: same degrees, different answers
plaintextimport itertools, networkx as nx def hamiltonian_cycles(G): first, *rest = sorted(G) count = 0 for order in itertools.permutations(rest): if order[0] > order[-1]: # each cycle appears twice, once per direction continue cycle = (first, *order, first) count += all(G.has_edge(a, b) for a, b in zip(cycle, cycle[1:])) return count for name, G in [("prism", nx.circular_ladder_graph(5)), ("Petersen", nx.petersen_graph())]: degrees = sorted({d for _, d in G.degree()}) print(name, G.number_of_nodes(), G.number_of_edges(), degrees, hamiltonian_cycles(G)) # prism 10 15 [3] 5 # Petersen 10 15 [3] 0
Example 2: testing Ore and Dirac
Here is the smallest degree sum over non-adjacent pairs, so Ore's condition is and Dirac's is . "Hamiltonian" comes from exhaustive search, and for the dodecahedron from a subset dynamic program that counts paths with the Held-Karp recurrence.
| Graph | Dirac | Ore | Hamiltonian cycles | |||
|---|---|---|---|---|---|---|
| on plus vertex 5 joined to 3 and 4 | 5 | 2 | 5 | no | yes | 2 |
| bowtie | 5 | 2 | 4 | no | no | 0 |
| 5 | 2 | 4 | no | no | 0 | |
| 6 | 2 | 4 | no | no | 1 | |
| 6 | 3 | 6 | yes | yes | 6 | |
| dodecahedron | 20 | 3 | 6 | no | no | 30 |
The first row is where Ore beats Dirac: vertex 5 has degree , but its only non-adjacent partners, 1 and 2, have degree 3, so both sums are . One of its two cycles is .
How much do the conditions cover? The networkx graph atlas lists every graph up to 7 vertices, and the same search over the connected ones gives:
| connected graphs | Hamiltonian | satisfy Ore | satisfy Dirac | |
|---|---|---|---|---|
| 5 | 21 | 8 | 5 | 3 |
| 6 | 112 | 48 | 21 | 19 |
| 7 | 853 | 383 | 68 | 29 |
At , Ore certifies 68 of the 383 Hamiltonian graphs and Dirac 29. Every graph that satisfies a condition is Hamiltonian, as the theorems promise, and for and most Hamiltonian graphs satisfy neither.
Example 3: brute force against Held-Karp, counted
plaintextfrom itertools import combinations, permutations def held_karp(d): n = len(d) g = {(1 << k, k): d[0][k] for k in range(1, n)} # paths 0 -> k for size in range(2, n): for S in combinations(range(1, n), size): mask = sum(1 << k for k in S) for e in S: prev = mask ^ (1 << e) g[(mask, e)] = min(g[(prev, p)] + d[p][e] for p in S if p != e) full = (1 << n) - 2 # every vertex except 0 return min(g[(full, e)] + d[e][0] for e in range(1, n)) def brute_force(d): def length(order): tour = (0, *order, 0) return sum(d[a][b] for a, b in zip(tour, tour[1:])) return min(length(p) for p in permutations(range(1, len(d))) if p[0] < p[-1])
For each from 4 to 10, take points from numpy.random.default_rng(n).random((n, 2)) with Euclidean distances. Both functions return the same optimum on all seven instances. Counting additions with a counter in each loop (and per brute-force tour) matches the formulas exactly:
| tours | brute-force additions | Held-Karp values | Held-Karp additions | |
|---|---|---|---|---|
| 4 | 3 | 9 | 12 | 15 |
| 5 | 12 | 48 | 32 | 52 |
| 6 | 60 | 300 | 80 | 165 |
| 8 | 2520 | 17640 | 448 | 1351 |
| 10 | 181440 | 1632960 | 2304 | 9225 |
Both columns count the same operation, adding an edge weight to a partial sum; comparisons and subset enumeration are left out of both. Brute force does fewer additions up to , and Held-Karp wins from on. From the formulas, at brute force needs additions and Held-Karp ; at the figures are and . At Held-Karp also stores values, about 62 GB at 8 bytes each: memory runs out before time does.
Common Misconceptions & Pitfalls
- "The graph fails Dirac and Ore, so it is not Hamiltonian." Both are one-way certificates. and the dodecahedron fail both and are Hamiltonian.
- "Connected with minimum degree 2 is enough." The bowtie and the Petersen graph are connected, have minimum degree at least 2, and have no Hamiltonian cycle.
- "NP-complete means unsolvable." It means no polynomial-time algorithm is known. Held-Karp solves 20 vertices exactly with additions.
- "Held-Karp is polynomial." is exponential; it beats , not .
- "A Hamiltonian path gives a Hamiltonian cycle." A path graph has a Hamiltonian path and no cycle at all.
Summary
Hamiltonian cycles and Euler circuits are independent properties, and only the second is governed by a local parity condition. No degree test exists: the prism and the Petersen graph share every degree and differ in the answer, and since Karp (1972) put Hamilton circuit among the NP-complete problems, any polynomial-time test would imply P = NP. Ore's theorem (non-adjacent degree sums at least , with ) and its corollary, Dirac's theorem (minimum degree at least ), guarantee a Hamiltonian cycle, and two cliques sharing a vertex show the guarantee failing just below the bound. They are sufficient, not necessary: at they certify 68 and 29 of the 383 Hamiltonian graphs. For the TSP, Held-Karp replaces brute force's tours with additions: better from on, still exponential.
Documentation Links
- Diestel (2025): Graph Theory, 6th ed., Chapter 10 (free preview)
- Wikipedia: Hamiltonian path
- Wikipedia: Ore's theorem
- Wikipedia: Icosian game
- Wikipedia: Karp's 21 NP-complete problems
- Erickson (2019): Algorithms, Chapter 12: NP-Hardness
- Wikipedia: Held-Karp algorithm
- Levin (2025): Discrete Mathematics: An Open Introduction, 3rd ed., Euler Paths and Circuits