Backtracking
Backtracking
Sum of Subsets Problem: Given a set of positive integers, find the combination of numbers
that sum to given value M.
Sum of subsets problem is analogous to the knapsack problem. The Knapsack Problem tries
to fill the knapsack using a given set of items to maximize the profit. Items are selected in
such a way that the total weight in the knapsack does not exceed the capacity of the
knapsack. The inequality condition in the knapsack problem is replaced by equality in the
sum of subsets problem. Given the set of n positive integers, W = ,w1, w2, …, wn-, and given
a positive integer M, the sum of the subset problem can be formulated as follows (where wi
and M correspond to item weights and knapsack capacity in the knapsack problem):
Numbers are sorted in ascending order, such that w1 < w2 < w3 < …. < wn. The solution is
often represented using the solution vector X. If the ith item is included, set xi to 1 else set it
to 0. In each iteration, one item is tested. If the inclusion of an item does not violet the
constraint of the problem, add it. Otherwise, backtrack, remove the previously added item,
and continue the same procedure for all remaining items. The solution is easily described by
the state space tree. Each left edge denotes the inclusion of wi and the right edge denotes
the exclusion of wi. Any path from the root to the leaf forms a subset. A state-space tree for
n = 3 is demonstrated in Fig. (a).
The algorithm for solving the sum of subsets problem using recursion is stated below:
Algorithm
// Input :
i: Item index
FEASIBLE_SUB_SET(i) == 1
then
if
(sum == W)
then
print X*1…i+
end
else
X[i + 1] ← 1
FEASIBLE_SUB_SET(i)
if
The second recursive call represents the case when we do not select the current item.
Complexity Analysis
It is intuitive to derive the complexity of sum of the subset problem. In the state-space tree,
at level i, the tree has 2i nodes. So, given n items, the total number of nodes in the tree
would be 1 + 2 + 22 + 23 + .. 2n.
T(n) = 1 + 2 + 22 + 23 + .. 2n = 2n+1 – 1 = O(2n)
Thus, sum of sub set problem runs in exponential order.
Examples
X[i] = X[4] = 1 ⇒ X =[1, 1, 0, 1] A complete state space tree for given data is shown in Fig. (b)
At level i, the left branch corresponds to the inclusion of number w i and the right branch
corresponds to exclusion of number wi. The recursive call number for the node is stated
below the node. Node 8 is the solution node. The bold solid line shows the path to the
output node.
Example:
Analyze sum of subsets algorithm on data :
M = 35 and
i) w = {5, 7, 10, 12, 15, 18, 20}
ii) w = {20, 18, 15, 12, 10, 7, 5}
iii) w = {15, 7, 20, 5, 18, 10, 12} Are there any discernible differences in the computing
time ?
Solution:
Let us run the algorithm on first instance w = {5, 7, 10, 12, 15, 18, 20}.
Example:
Solve the sum of subset problems using backtracking algorithmic strategy for the
following data: n = 4 W =(w1, w2, w3, w4) = (11, 13, 24, 7) and M = 31.
Solution:
Items in sub set Condition Comment
{} 0 Initial condition
{ 11 } 11 < 31 Add next element
{ 11, 13 } 24 < 31 Add next element
{ 11, 13, 24 } 48 < 31 Sub set sum exceeds, so backtrack
{ 11, 13, 7 } 31 Solution Found
State-space tree for a given problem is shown here:
In the above graph, the black circle shows the correct result. The gray node shows where
the algorithm backtracks. Numbers in the leftmost column indicate elements under
consideration at that level. The left and right branches represent the inclusion and exclusion
of that element, respectively.
{11, 13, 7}
{24, 7}
C1 C2 C3 C4 C5
C1 0 1 0 1 0
C2 1 0 1 0 0
C3 0 1 0 1 1
C4 1 0 1 0 1
C5 0 0 1 0 0
Thus, vertices A and C will be colored with color 1, and vertices B and D will be
colored with color 2.
Algorithm
Algorithm for graph coloring is described here:
Algorithm
GRAPH_COLORING(G, COLOR, i)
// Description : Solve the graph coloring problem using backtracking
// Input : Graph G with n vertices, list of colors, initial
vertex i
// COLOR*1…n+ is the array of n different colors
// Output : Colored graph with minimum color
if
CHECK_VERTEX(i) == 1
then
if
i == N
then
print
COLOR*1…n+
else
j←1
while
(j ≤ M)
do
COLOR(i + 1) ← j
j←j+1
end
end
end
Function
CHECK_VERTEX(i)
for
j ← 1 to i – 1
do
if
Adjacent(i, j)
then
if
COLOR(i) == COLOR(j)
then
return
0
end
end
end
return
1
Complexity Analysis
The number of anode increases exponentially at every level in state space tree.
With M colors and n vertices, total number of nodes in state space tree would be
1 + M + M2 + M3 + …. + Mn
Hence, T(n) = 1 + M + M2 + M3 + …. + Mn
=fracMn+1–1M–1
So, T(n) = O(Mn).
Thus, the graph coloring algorithm runs in exponential time.
Solution:
This problem can be solved using backtracking algorithms. The formal idea is to
list down all the vertices and colors in two lists. Assign color 1 to vertex 1. If vertex
2 is not adjacent to vertex 1 then assign the same color, otherwise assign color 2.
The process is repeated until all vertices are colored. The algorithm backtracks
whenever color i is not possible to assign to any vertex k and it selects the next
color i + 1 and the test is repeated. This graph can be colored with 3 colors. The
solution tree is shown in Fig. (i):
Vertex Assigned Color
1 c1
2 c2
3 c3
4 c1
V[i] ← j
HAMILTONIAN(i + 1)
j←j+1
end
end
end
function
FEASIBLE(i)
flag ← 1
for
j ← 1 to i – 1
do
if
Adjacent(Vi, Vj)
then
flag ← 0
end
end
if
Adjacent (Vi, Vi-1)
then
flag ← 1
else
flag ← 0
end
return
flag
Complexity Analysis
Looking at the state space graph, in worst case, total number of nodes in tree
would be,
Solution:
We can start with any random vertex. Let us start with vertex A. Neighbors of A
are {B, C, D}. The inclusion of B does not lead to a complete solution. So explore it
as shown in Figure (c).
Adjacent vertices to B are {C, D}. The inclusion of C does not lead to a complete
solution. All adjacent vertices of C are already members of the path formed so far.
So it leads to a dead end.
The inclusion of D does not lead to a complete solution, and all adjacent vertices
of D are already a member of the path formed so far. So it leads to a dead end.
Backtrack and go to B. Now B does not have any more neighbors, so backtrack
and go to A. Explore the next neighbor of A i.e. C. By repeating the same
procedure, we get the state space trees as shown below. And path
A – C – B – D – A is detected as the Hamiltonian cycle of the input graph.
Example
Example: Explain how to find Hamiltonian Cycle by using Backtracking in a given
graph
Solution:
N Queen Problem
What is N Queen Problem?
N Queen problem is the classical Example of backtracking. N-Queen problem is
defined as, “given N x N chess board, arrange N queens in such a way that no two
queens attack each other by being in same row, column or diagonal”.
Solution tuple for the solution shown in fig (h) is defined as <4, 6, 8, 2, 7, 1, 3,
5>. From observations, two queens placed at (i, j) and (k, l) positions, can be
in same diagonal only if,
(i – j ) = (k – l) or
(i + j) = (k + l)
The arrangement shown in fig. (i) leads to failure. As it can be seen from fig. (i),
Queen Q6 cannot be placed anywhere in the 6th row. So the position of Q5 is
backtracked and it is placed in another feasible cell. This process is repeated until
the solution is found.
Algorithm
The following algorithm arranges n queens on n x n board using a backtracking
algorithm.
Algorithm
N_QUEEN (k, n)
// Description : To find the solution of n x n queen problem using
backtracking
// Input :
n: Number of queen
k: Number of the queen being processed currently, initially set to 1.
for
i ← 1 to n
do
if
PLACE(k , i)
then
x[k] ← i
if
k == n
then
print X*1…n+
else
N_QUEEN(k + 1, n)
end
end
end
Function PLACE (k, i) returns true, if the kth queen can be placed in the
ith column. This function enumerates all the previously kept queen’s positions
to check if two queens are on the same diagonal. It also checks that i is
distinct from all previously arranged queens.
Function abs(a) returns the absolute value of argument a. The array X is the
solution tuple.
Like all optimization problems, the n-queen problem also has some
constraints that it must satisfy. These constraints are divided into two
categories : Implicit and explicit constraints
Function
PLACE(k, i)
// k is the number of queen being processed
Implicit constraints:
5. Find least cost valued node A (i.e. E-node), by computing reduced cost node
matrix with every remaining node.
(c) Reduce A again, except rows and columns having all ∞ entries.
Cost = L + Cost(i, j) + r
Raw Reduction:
Matrix M is called reduced matrix if each of its row and column has at least one
zero entry or entire row or entire column has ∞ value. Let M represents the
distance matrix of 5 cities. M can be reduced as follow:
Find the minimum element from each row and subtract it from each cell of matrix.
Row reduction cost (M) = 10 + 2 + 2 + 3 + 4 = 21
Column reduction:
Matrix MRowRed is row reduced but not the column reduced. Matrix is called column
reduced if each of its column has at least one zero entry or all ∞ entries.
MColRed = {Mji – min {Mji | 1 ≤ j ≤ n, and Mji < ∞ }}
To reduced above matrix, we will find the minimum element from each column
and subtract it from each cell of matrix.
State space tree for 5 city problem is depicted in Fig. 6.6.1. Number within circle
indicates the order in which the node is generated, and number of edge indicates
the city being visited.
Example
= (10 + 2 + 2 + 3 + 4) + (1 + 3) = 25
This means all tours in graph has length at least 25. This is the optimal cost of the
path.
Cost of node 3:
Cost of node 5:
Node 4 has minimum cost for path 1-4. We can go to vertex 2, 3 or 5. Let’s explore
all three nodes.
Cost of node 7: