This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

FindHamiltonianCycle

FindHamiltonianCycle[g]
attempts to find a Hamiltonian cycle.
  • FindHamiltonianCycle[g] returns an empty list if no Hamiltonian cycle is found.
  • considers the input graph as undirected.
  • uses heuristic algorithms to find a Hamiltonian cycle, therefore, there is no guarantee that a Hamiltonian cycle will be found even if one exists.
This defines a small graph and finds a Hamiltonian cycle of the graph:
This plots the graph and highlights the cycle in red:
Needs["GraphUtilities`"]
This defines a small graph and finds a Hamiltonian cycle of the graph:
In[2]:=
Click for copyable input
In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input
Out[4]=
This plots the graph and highlights the cycle in red:
In[5]:=
Click for copyable input
Out[5]=
This limits the maximum number of iterations to try to find a Hamiltonian cycle:
This specifies a random seed different from the default to try to find a Hamiltonian cycle:
This finds all possible Hamiltonian cycles in the graph consisting of bordering countries in South America:
This shows the first of these two cycles; the second is just a reversal of the first:
uses heuristic algorithms. Unlike HamiltonianCycles, it is not guaranteed to find a Hamiltonian cycle even when one exists. But for large graphs, 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:
uses heuristic algorithms, which are not guaranteed to find a Hamiltonian cycle even when one exists: