HamiltonianCycles[g,n]
gives a list of n Hamiltonian cycles.
HamiltonianCycles[g]
gives a list of one Hamiltonian cycle.
HamiltonianCycles
HamiltonianCycles[g,n]
gives a list of n Hamiltonian cycles.
HamiltonianCycles[g]
gives a list of one Hamiltonian cycle.
Details and Options
- HamiltonianCycles functionality is now available in the built-in Wolfram Language function FindHamiltonianCycle.
- To use HamiltonianCycles, you first need to load the Graph Utilities Package using Needs["GraphUtilities`"].
- HamiltonianCycles[g,n] returns an empty list if no Hamiltonian cycle exists.
- HamiltonianCycles considers the input graph as undirected.
- The complexity of the algorithm is such that finding all Hamiltonian cycles for a large graph can take an exponential amount of time.
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]HamiltonianCycles[g]HamiltonianCycles has been superseded by FindHamiltonianCycle:
g = UndirectedGraph[Graph[{1 -> 3, 1 -> 2, 1 -> 4, 2 -> 3, 2 -> 4}]]FindHamiltonianCycle[g][[All, All, 1]]Scope (1)
Needs["GraphUtilities`"]This defines a small graph and finds a Hamiltonian cycle of the graph:
g = {0 -> 1, 2 -> 3, 2 -> 1, 3 -> 0, 4 -> 5, 4 -> 7, 4 -> 3, 5 -> 6, 5 -> 2, 6 -> 7, 6 -> 1, 7 -> 0};GraphPlot[g, VertexLabeling -> True]c = HamiltonianCycles[g]This plots the graph and highlights the cycle in red:
cs = Transpose[{c[[1]], RotateRight[c[[1]]]}];GraphPlot[g, EdgeRenderingFunction -> (If[MemberQ[cs, #2] || MemberQ[cs, Reverse[#2]], {Red, Thickness[.01], Line[#1]}, {Black, Line[#1]}]&), VertexLabeling -> True]This finds all Hamiltonian cycles:
HamiltonianCycles[g, All]Applications (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 = HamiltonianCycles[g, All]This shows the first of these two cycles; the second is just a reversal of the first:
cs = Transpose[{c[[2]], RotateRight[c[[2]]]}];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)
A graph that has a Hamiltonian cycle must be biconnected:
Needs["GraphUtilities`"]g = {1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1, 1 -> 5};Bicomponents[g]HamiltonianCycles[g]A graph that is biconnected does not necessarily have a Hamiltonian cycle:
g = {1 -> 2, 1 -> 4, 1 -> 5, 2 -> 3, 3 -> 6, 4 -> 6, 5 -> 6};Bicomponents[g]HamiltonianCycles[g]GraphPlot[g, VertexLabeling -> True]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), HamiltonianCycles, Wolfram Language function, https://reference.wolfram.com/language/GraphUtilities/ref/HamiltonianCycles.html.
CMS
Wolfram Language. 2007. "HamiltonianCycles." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/GraphUtilities/ref/HamiltonianCycles.html.
APA
Wolfram Language. (2007). HamiltonianCycles. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/GraphUtilities/ref/HamiltonianCycles.html
BibTeX
@misc{reference.wolfram_2026_hamiltoniancycles, author="Wolfram Research", title="{HamiltonianCycles}", year="2007", howpublished="\url{https://reference.wolfram.com/language/GraphUtilities/ref/HamiltonianCycles.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_hamiltoniancycles, organization={Wolfram Research}, title={HamiltonianCycles}, year={2007}, url={https://reference.wolfram.com/language/GraphUtilities/ref/HamiltonianCycles.html}, note=[Accessed: 15-June-2026]}