How can I find the number of Hamiltonian cycles in a complete undirected graph?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In a complete undirected graph K_n, every pair of vertices is connected, so Hamiltonian cycles always exist when n is at least three. The counting question is a combinatorics problem, not a search problem in this special case. The final formula is simple, but understanding why the division factors appear prevents common overcounting mistakes.
Build the Count Step by Step
A Hamiltonian cycle visits every vertex exactly once and returns to start.
To count distinct cycles in K_n:
- Fix one vertex as anchor to remove rotational duplicates.
- Arrange the remaining
n - 1vertices in all orders. - Divide by two because each cycle has two traversal directions in undirected graph.
So number of cycles is:
- factorial of
n - 1, divided by2.
Equivalent formula:
(n - 1)! / 2
This is valid for n >= 3.
Why Fixing One Vertex Works
Without fixing a start, each cycle can be rotated into n equivalent representations. Example cycle on vertices 1,2,3,4 can start at 1 or 2 or 3 or 4 and still represent same cycle.
Fixing one vertex removes those rotational duplicates immediately, leaving permutations of the remaining vertices only.
Then reverse-order duplication still remains:
1 -> 2 -> 3 -> 4 -> 11 -> 4 -> 3 -> 2 -> 1
These two are same undirected cycle, so divide by two.
Small-Value Table
Using (n - 1)! / 2:
n = 3gives1cycle.n = 4gives3cycles.n = 5gives12cycles.n = 6gives60cycles.n = 7gives360cycles.
Factorial growth is very fast, so counts become large quickly.
Direct Computation in Python
For counting only, use factorial.
This is constant-time for practical purposes because computation is simple arithmetic.
Verification by Enumeration for Small n
To build intuition, enumerate cycles for very small graphs and deduplicate canonical forms.
This brute-force verification is only for tiny n, but it confirms the formula conceptually.
Relationship to General Hamiltonian-Cycle Problems
In arbitrary graphs, counting Hamiltonian cycles is hard and generally requires exponential-time methods. Complete graphs are a rare case where symmetry gives closed-form count.
That distinction matters in interviews and algorithm design:
- For
K_n, use formula directly. - For sparse or constrained graphs, use search or dynamic programming techniques and expect much higher complexity.
Numeric Growth and Data Types
Counts exceed 32-bit integer limits quickly. Example:
n = 14gives(13)! / 2, already very large.
Languages with fixed-width integers may overflow. Use big-integer support when needed.
Python handles big integers natively, which is useful for combinatorics exploration.
Common Pitfalls
- Forgetting to divide by two for reverse traversal duplication. Fix by applying undirected symmetry correction.
- Forgetting rotational equivalence. Fix by anchoring one vertex before counting permutations.
- Applying complete-graph formula to non-complete graphs. Fix by verifying graph type first.
- Returning non-zero count for
nless than three. Fix by handling small-nbase cases explicitly. - Overflowing fixed-width integers in other languages. Fix by using big-number libraries where required.
Summary
- In complete undirected graph
K_n, Hamiltonian cycle count is(n - 1)! / 2forn >= 3. - Formula comes from anchored permutations and reverse-direction deduplication.
- Use direct factorial computation for exact counts.
- Enumerate only for small
nwhen validating intuition. - Do not reuse this closed-form result for general graph families.

