A Hamiltonian path, also called a Hamilton path, is a graph path between two vertices of a graph that visits each vertex exactly once. If a Hamiltonian path exists whose endpoints are adjacent, then the resulting graph cycle is called a Hamiltonian cycle (or Hamiltonian cycle).
A graph that possesses a Hamiltonian path is called a traceable graph.
In general, the problem of finding a Hamiltonian path is NP-complete (Garey and Johnson 1983, pp. 199-200), so the only known way to determine whether a given general graph has a Hamiltonian path is to undertake an exhaustive search
If a bipartite graph has bipartition classes
and
,
then any Hamiltonian path alternates between
and
, so
is necessary. In particular, a connected bipartite
graph whose bipartition classes differ in size by more than 1 is untraceable.
The converse does not hold in general; examples with bipartition sizes differing
by 0 that are untraceable include the E
graph and H graph, and examples with sizes differing
by 1 that are untraceable include the fork
graph and spoke graph
.
More generally, if a graph on
vertices has a Hamiltonian path, then every other edge of
the path gives a matching of size
. Thus traceability requires
, where
is the matching number.
Equivalently, a graph with
is untraceable.
Finding a single Hamiltonian path of a graph is implemented in the Wolfram
Language as FindHamiltonianPath[g].
A precomputed list of all Hamiltonian paths for many named graphs can be obtained
using GraphData[graph,
"HamiltonianPaths"], where and both orientations of paths are
included (so that
1,
2, 3
is considered distinct from
3, 2, 1
). A precomputed count of the corresponding number of Hamiltonian
paths is given by GraphData[graph,
"HamiltonianPathCount"].
The total numbers of directed Hamiltonian paths for all simple graphs of orders , 2, ... are 0, 2, 8, 50, 416, 5616,
117308, 4862736, ... (OEIS A193352).
Rubin (1974) describes an efficient search procedure that can find some or all Hamilton paths and circuits in a graph using deductions that greatly reduce backtracking and
guesswork. A probabilistic algorithm due to Angluin and Valiant (1979), described
by Wilf (1994), can also be useful to find Hamiltonian cycles and paths. A Hamiltonian
path between two vertices and
can be found if an algorithm for Hamiltonian cycles is available.
This can be done by checking if the original graph
contains the edge
and adding it if not to obtain
. Since a Hamiltonian path with adjacent endpoints is a Hamiltonian cycle and since
and
are now adjacent, finding a Hamiltonian cycle and splitting
at the edge gives a Hamiltonian path from
to
in
. Similarly, if no Hamiltonian cycle exists in
, then there is no Hamiltonian path from
to
in
.
The Lovász conjecture states that without exception, every connected vertex-transitive graph is traceable.
The following table summarizes the numbers of (undirected) Hamiltonian paths on various classes of graphs.
| graph | OEIS | sequence |
| Andrásfai graph | A307932 | 1, 5, 72, 3047, 265034, 42442251, ... |
| antiprism graph | A307933 | X, X, 120, 408, 1200, 3240, 8330, 20720, ... |
| barbell graph | A001044 | X, X, 4, 36, 576, 14400, ... |
| A307934 | X, X, X, 96, 25376, 32132352, ... | |
| cocktail
party graph | A307935 | 4, 120, 6912, 631680, ... |
| complete
graph | A001710 | 0, 1, 3, 12, 60, 360, ... |
| complete
bipartite graph | A001044 | 1, 4, 36, 576, 14400, ... |
| complete
tripartite graph | A307936 | 3, 120, 18792, 7547904, ... |
| crossed prism graph | A308136 | X, X, X, 72, X, 288, X, 960, X, 2880, ... |
| crown graph | A259212 | X, X, 6, 72, 1920, 69120, ... |
| cycle graph | A000027 | X, X, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ... |
| folded cube graph | A307948 | 1, 12, 576, 1006080, 28102446858240, ... |
| gear graph | A002378 | X, X, 12, 20, 30, 42, 56, 72, 90, 110, ... |
| grid graph | A120443 | 0, 4, 20, 276, 4324, 229348, 13535280, ... |
| grid graph | A308586 | 0, 72, 2480304, ... |
| halved cube graph | A308130 | 0, 1, 12, 6912, 23710679040, ... |
| Hanoi graph | A308134 | 3, 18, 192, 2730, ... |
| hypercube
graph | A284673 | 1, 4, 72, 45696, 93749829120, ... |
| Keller
graph | A000000 | 0, 1006080, ... |
| A308129 | X, 12, 392, 171592, 364618672, ... | |
| A308131 | X, 0, 0, 0, 864, 3318960, 82787609160, ... | |
| ladder
graph | A003682 | 1, 4, 8, 14, 22, 32, 44, 58, 74, 92, ... |
| Möbius ladder | A020875 | X, X, 36, 72, 140, 228, 364, 528, 756, ... |
| Mycielski graph | A308132 | 0, 1, 5, 490, 31629560, ... |
| odd graph | A301559 | 0, 3, 120, 328345920, ... |
| path
graph | A057427 | 0, 1, 1, 1, 1, 1, 1, 1, ... |
| prism
graph | A308137 | X, X, 30, 72, 130, 228, 350, 528, ... |
| A308139 | 0, 12, 22928, 10221735256, ... | |
| A308141 | 1/2, 4, 756, 11197440, ... | |
| Sierpiński carpet graph | A308138 | 8, 137728, ... |
| Sierpiński gasket graph | A246957 | 3, 12, 552, 6978816, 429714433137180672, ... |
| star graph | A171386 | 0, 1, 1, 0, 0, 0, 0, 0, ... |
| sun graph | A002378 | X, X, 12, 20, 30, 42, 56, 72, 90, 110, ... |
| tetrahedral Johnson graph | A000000 | X, X, X, X, X, 808668610560, ... |
| torus
grid graph | A268838 | X, X, 756, 45696, 2955700, 560028096, ... |
| transposition graph | A307949 | 0, 1, 36, 27267618816, ... |
| triangular graph | A308142 | 0, 3, 120, 47760, ... |
| triangular grid graph | A308144 | 3, 12, 114, 1968, 66312, 4381020, 57826621, ... |
| wheel graph | A046092 | X, X, X, 12, 24, 40, 60, 84, 112, 144, ... |
| A308146 | X, X, X, 96, 12256, 32132352, 392007078912, ... |
Closed forms for some of these classes of graphs are summarized in the following table.
| graph | formula |
| barbell graph | |
| cocktail party graph | |
| complete graph | |
| complete
bipartite graph | |
| cycle graph | |
| gear graph | |
| ladder
graph | |
| Möbius ladder | |
| path graph | 1 |
| prism graph | |
| sun graph | |
| wheel
graph |
Recurrence relations for the number of directed Hamiltonian paths for some graph families are summarized below.
| graph | order | recurrence |
| antiprism graph | 9 | |
| crown graph | 3 | |
| prism graph | 6 |