Design and Analysis of Algorithms: Graph Coloring Using Backtracking
Design and Analysis of Algorithms: Graph Coloring Using Backtracking
Algorithms
Study material on
Example:
The graph G1 of the following gure contains the Hamiltonian cycle 1, 2, 8, 7, 6, 5, 4, 3, 1. The
graph G2 of contains no Hamiltonian cycle.
Approach:
There is no known easy way to determine whether a given graph contains a Hamiltonian cycle. We
now look at a backtracking algorithm that nds all the Hamiltonian cycles in a graph. The graph
may be directed or undirected. Only distinct cycles are output.
Algorithm:
The backtracking solution vector (x1 , ...xn ) is dened so that xi represents the ith visited vertex
of the proposed cycle. Now all we need do is determine how to compute the set of possible vertices
for xk if x1 ...xk−1 have already been chosen. If k = 1, then x1 can be any of the n vertices. To
avoid printing the same cycle n times, we require that x1 = 1. If 1 < k < n, then xk can be
any vertex v that is distinct from x1 , x2 , ..., xk−1 and v is connected by an edge to xk−1 . The
vertex xn can only be the one remaining vertex and it must be connected to both xn−1 and x1 .
We begin by presenting function NextValue(k), which determines a possible next vertex for
2
the proposed cycle. Using NextValue we can particularize the recursive backtracking schema
to nd all Hamiltonian cycles. This algorithm is started by rst initializing the adjacency matrix
G[l : n, 1 : n], then setting x[2 : n] to zero and x[1] to 1, and then executing Hamiltonian(2).
Algorithm 1: Hamiltonian
(k)
// This algorithm uses the recursive formulation of
backtracking to find all the Hamiltonian cycles of a graph.
The graph is stored as an adjacency matrix G[l : n, 1 : n].
All cycles begin at node 1.
1 repeat
// Generate values for x[k]
2 NextValue (k); // Assign a legal next value to x[k]
3 if x[k] = 0then return ; // no new vertex available
Algorithm 2: NextValue
(k)
// x[1 : k − 1] is a path of k − 1 distinct vertices. If
x[k] = 0, then no vertex has as yet been assigned to x[k].
After execution, x[k] is assigned to the next highest
numbered vertex which does not already appear in x[1 : k − 1]
and is connected by an edge to x[k − 1]. Otherwise x[k] = 0.
If k = n, then in addition x[k] is connected to x[1].
1 repeat
2 x[k] := (x[k] + 1) mod (n + 1) // next vertex
;
7 end
// Check for distinctness.
8 if j = k then
// If true, then the vertex is distinct.
9 if (k < n) ((k = n) G[x[n], x[1]] 6= 0) then return
or and ;
10 end
11 end
12 until false ;
3
Time Complexity:
There are n! possible permutations of the n vertices, and therefore the running time is O(n! )
Travelling Salesman Problem:
A traveller needs to visit all the cities from a list, where distances between all the
cities are known and each city should be visited just once. What is the shortest possi-
ble route that he visits each city exactly once and returns to the origin city?
Travelling salesman problem is a problem of nding tour with minimum cost. This tour is a
Hamiltonian cycle. For the simple case of a graph all of whose edge costs are similar, Hamiltonian
will determine a minimum-cost tour if a tour exists. If the common edge cost is C , the cost of a
tour is Cn as there are n edges in a Hamiltonian cycle.