FindHamiltonianCycle[g]
attempts to find a Hamiltonian cycle.
FindHamiltonianCycle
FindHamiltonianCycle[g]
attempts to find a Hamiltonian cycle.
Details and Options
- FindHamiltonianCycle functionality is now available in the built-in Wolfram Language function FindHamiltonianCycle.
- To use FindHamiltonianCycle, you first need to load the Graph Utilities Package using Needs["GraphUtilities`"].
- FindHamiltonianCycle[g] returns an empty list if no Hamiltonian cycle is found.
- FindHamiltonianCycle considers the input graph as undirected.
- FindHamiltonianCycle uses heuristic algorithms to find a Hamiltonian cycle; therefore, there is no guarantee that a Hamiltonian cycle will be found even if one exists.
Examples
open all close allBasic Examples (2)
Needs["GraphUtilities`"]This defines a small graph and finds a Hamiltonian cycle of the graph:
g = {1 -> 3, 1 -> 2, 1 -> 4, 2 -> 3, 2 -> 4};GraphPlot[g, VertexLabeling -> True]FindHamiltonianCycle[g]This function has been superseded by FindHamiltonianCycle in the Wolfram System:
g = UndirectedGraph[Graph[{1 -> 3, 1 -> 2, 1 -> 4, 2 -> 3, 2 -> 4}]]FindHamiltonianCycle[g][[1, All, 1]]Options (2)
MaxIterations (1)
Needs["Combinatorica`"]This limits the maximum number of iterations to try to find a Hamiltonian cycle:
FindHamiltonianCycle[GridGraph[8, 8], MaxIterations -> 5]//TimingFindHamiltonianCycle[GridGraph[8, 8]]//TimingFindHamiltonianCycle[GridGraph[8, 8], MaxIterations -> 5]//TimingApplications (1)
This finds all possible Hamiltonian cycles in the graph consisting of bordering countries in South America:
Needs["GraphUtilities`"]g = Flatten[Map[(If[MemberQ[CountryData["SouthAmerica"], #[[1]]] && MemberQ[CountryData["SouthAmerica"], #[[2]]], #, {}])&, Flatten[Thread[# -> CountryData[#, "BorderingCountries"]]& /@ CountryData["SouthAmerica"]]]];c = FindHamiltonianCycle[g]This shows the first of these two cycles; the second is just a reversal of the first:
cs = Transpose[{c, RotateRight[c]}];GraphPlot[g, EdgeRenderingFunction -> (If[MemberQ[cs, #2] || MemberQ[cs, Reverse[#2]], {Red, Thickness[.01], Line[#1]}, {Black, Line[#1]}]&), VertexLabeling -> True, MultiedgeStyle -> False]Properties & Relations (1)
FindHamiltonianCycle uses heuristic algorithms. Unlike HamiltonianCycles, it is not guaranteed to find a Hamiltonian cycle even when one exists. But for large graphs, FindHamiltonianCycle can sometimes be faster at finding one cycle.
This defines a graph of 500 vertices, and uses these two functions to find a Hamiltonian cycle:
SeedRandom[123];
g = RandomGraph[500, 0.3];FindHamiltonianCycle[g]//Short//TimingHamiltonianCycles[g]//Short//TimingPossible Issues (1)
FindHamiltonianCycle uses heuristic algorithms, which are not guaranteed to find a Hamiltonian cycle even when one exists:
Needs["GraphUtilities`"]Needs["Combinatorica`"]FindHamiltonianCycle[GridGraph[10, 10]]HamiltonianCycles[GridGraph[10, 10]]Tech Notes
Related Guides
-
▪
- Graph Utilities Package ▪
- Graphs & Networks ▪
- Graph Visualization ▪
- Computation on Graphs ▪
- Graph Construction & Representation ▪
- Graphs and Matrices ▪
- Graph Properties & Measurements ▪
- Graph Operations and Modifications ▪
- Statistical Analysis ▪
- Social Network Analysis ▪
- Graph Properties ▪
- Mathematical Data Formats ▪
- Discrete Mathematics
Text
Wolfram Research (2007), FindHamiltonianCycle, Wolfram Language function, https://reference.wolfram.com/language/GraphUtilities/ref/FindHamiltonianCycle.html.
CMS
Wolfram Language. 2007. "FindHamiltonianCycle." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/GraphUtilities/ref/FindHamiltonianCycle.html.
APA
Wolfram Language. (2007). FindHamiltonianCycle. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/GraphUtilities/ref/FindHamiltonianCycle.html
BibTeX
@misc{reference.wolfram_2026_findhamiltoniancycle, author="Wolfram Research", title="{FindHamiltonianCycle}", year="2007", howpublished="\url{https://reference.wolfram.com/language/GraphUtilities/ref/FindHamiltonianCycle.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_findhamiltoniancycle, organization={Wolfram Research}, title={FindHamiltonianCycle}, year={2007}, url={https://reference.wolfram.com/language/GraphUtilities/ref/FindHamiltonianCycle.html}, note=[Accessed: 15-June-2026]}