5.3 - Hamiltonian Circuit
5.3 - Hamiltonian Circuit
We use the Depth-First Search algorithm to traverse the graph until all the vertices have been
visited.
We traverse the graph starting from a vertex (arbitrary vertex chosen as starting vertex) and at any
point during the traversal we get stuck (i.e., all the neighbor vertices have been visited), we
backtrack to find other paths (i.e., to visit another unvisited vertex).
If we successfully reach back to the starting vertex after visiting all the nodes, it means the graph
has a Hamiltonian cycle otherwise not.
We mark each vertex as visited so that we don’t traverse them more than once.
5
Example: Find the Hamiltonian circuits of a given graph using Backtracking.
Initially we start out search with vertex ‘1’ the vertex ‘1’ becomes the root of our implicit
tree. Root
(a)
Next we choose vertex ‘2’ adjacent to ‘1’, as it comes first in numerical order (2, 3,
4). Root
(b)
Next vertex ‘3’ is selected which is adjacent to ‘2’ and which comes first in numerical order
(3,5).
Root
(c)
Next we select vertex ‘4’ adjacent to ‘3’ which comes first in numerical order (4, 5).
Root
6
Next vertex ‘5’ is selected. If we choose vertex ‘1’ then we do not get the Hamiltonian cycle.
Root
(e)
The vertex adjacent to 5 is 2, 3, 4 but they are already visited. Thus, we get the dead end. So,
we backtrack one step and remove the vertex ‘5’ from our partial solution.
The vertex adjacent to ‘4’ are 5,3,1 from which vertex ‘5’ has already been checked and we are left
with vertex ‘1’ but by choosing vertex ‘1’ we do not get the Hamiltonian cycle. So, we again
backtrack one step.
Hence we select the vertex ‘5’ adjacent to ‘3’.
Root
7
The vertex adjacent to ‘5’ are (2,3,4) so vertex 4 is selected.
Root
Dead end
The vertex adjacent to ‘4’ are (1, 3, 5) so vertex ‘1’ is selected. Hence we get the
Hamiltoniancycle as all the vertex other than the start vertex ‘1’ is visited only once, 1- 2- 3-
5- 4- 1.
54
1 Dead end
(h)
2 3 4
3 5
Algorithm:
Branch and Bound
9
General method: