Hamiltonian Cycle
Hamiltonian Cycle
Hamiltonian Path in an undirected graph is a path that visits each vertex exactly once. A
Hamiltonian cycle is a Hamiltonian Path such that there is an edge from the last vertex to the first
vertex of the Hamiltonian Path.
A Hamiltonian cycle is a closed loop on a graph where every node (vertex) is visited exactly
once.
A loop is just an edge that joins a node to itself; so a Hamiltonian cycle is a path traveling from a
point back to itself, visiting every node en route.
If a graph with more than one node has a Hamiltonian cycle, we call it a Hamiltonian graph.
There isn’t any equation or general trick to finding out whether a graph has a Hamiltonian cycle;
the only way to determine this is to do a complete and exhaustive search, going through all the
options.
Input:
A 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is
adjacency matrix representation of the graph. A value graph[i][j] is 1 if there is a direct edge
from i to j, otherwise graph[i][j] is 0.
Output:
An array path[V] that should contain the Hamiltonian Path. path[i] should represent the ith
vertex in the Hamiltonian Path. The code should also return false if there is no Hamiltonian
Cycle in the graph.
Examples of Hamiltonian Graphs
Every complete graph with more than two vertices is a Hamiltonian graph. This follows from the
definition of a complete graph: an undirected, simple graph such that every pair of nodes is
connected by a unique edge.
The graph of every platonic solid is a Hamiltonian graph. So the graph of a cube, a tetrahedron,
an octahedron, or an icosahedron are all Hamiltonian graphs with Hamiltonian cycles.
The Hamiltonian cycle was named after Sir William Rowan Hamilton who, in 1857, invented a
puzzle-game which involved hunting for a Hamiltonian cycle. The game, called the Icosian
game, was distributed as a dodecahedron graph with a hole at each vertex. To solve the puzzle or
win the game one had to use pegs and string to find the Hamiltonian cycle — a closed loop that
visited every hole exactly once. The solution is shown in the image above.
A search for Hamiltonian cycles isn’t just a fun game for the afternoon off. It has real
applications in such diverse fields as computer graphics, electronic circuit design, mapping
genomes, and operations research.
For instance, when mapping genomes scientists must combine many tiny fragments of genetic
code, into one single genomic sequence. This can be done by finding a Hamiltonian cycle or
Hamiltonian path, where each of the reads are considered nodes in a graph and each overlap is
considered to be an edge.
In a much less complex application of exactly the same math, school districts use Hamiltonian
cycles to plan the best route to pick up students from across the district. Here students may be
considered nodes, the paths between them edges, and the bus wishes to travel a route that will
pass each students house exactly once.