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

HamiltonianCycles


gives a list of n Hamiltonian cycles.
HamiltonianCycles[g]
gives a list of one Hamiltonian cycle.
  • returns an empty list if no Hamiltonian cycle exists.
  • 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.
This defines a small graph and finds a Hamiltonian cycle of the graph:
This plots the graph and highlights the cycle in red:
This finds all Hamiltonian cycles:
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 finds all Hamiltonian cycles:
In[6]:=
Click for copyable input
Out[6]=
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:
A graph that has a Hamiltonian cycle must be biconnected:
A graph that is biconnected does not necessarily have a Hamiltonian cycle: