 BackTracking
This method is applied to problems which ask for a set of
solutions satisfying certain constraints
The solution to the problem is expressible as an n-tuple (x1,x2,
….xn) where xi’s are chosen from some finite set Si.
If mi is the size of Si then xi can take mi possible values
The total possible tuples are m1xm2xm3….mn
Ex – 8 queens problem
8 queens are to placed on the chess board so that no two
queens kill each other
Let us formulate the solution to this problem as a 8-tuple (x1,x2,
……x8)
Since each Queen has to be in different rows and
different colums
Let xi denote the column in which the Queen is placed
in ith
row
Let (x1,x2,……x8)=(2,5,6,7,3,4,8,1)
Q
Q
Q
Q
Q
Q
Q
Q
Most of the problems solved using backtracking require
to find solutions that satisfy a complex set of
constraints
These constraints can be divided into two types
1. Implicit constraints
2. Explicit constraints
1. Explicit constraints are defined on xi restricting xi to
take values from a given set
Explicit constraints for 8 queen’s problem
1 ≤ xi ≤ 8
All solutions that satisfy explicit constraints define a
possible solution space
The solution space is reduced to 88
from earlier 648
2. Implicit constraints are rules that define the way xi’s
are related to each other
For 8 Queen’s problem implicit constraints are
i) Two Queens cannot be in same column
xi ≠ xj
ii) Two Queens cannot be in the same diagonal
q11 q12 q13 q14
q21 q22 q23 q24 q25
q31 q32 q33 q34 q35
q43 q44
a55
i denotes row and xi
denotes column
Difference is same
xi-i=xj-j or xi-xj= i-j
Sum is same
xi+i=xj+j or xi-xj=j-I
|xi-xj|=|i=j|
The possible solutions to the problem satisfying explicit
and some of the implicit constraints can be arranged
in the form of a tree
Ex 4 queen’s Problem 1
X1=1 X1=2 X1=3
X1=4
2
Q Q
3 4 5
Q
Q
X2=2 X2=3 X2=4
6 7 8
X2=1 X2=2 X2=3
15 16 17
X2=4 X2=2 X2=4
12 13 14
9 10 11
X2=1 X2=3
X2=1
18 19
X3=3
X4=4
The solution space is organized in the form of a tree.
Each node in this tree defines a problem state. All paths from
roots to other nodes define the state space of the problem.
Solution states are those problem states s for which the path
from the root to s defines a tuple in the solution space.
Usually leaf nodes are solution states.
Answer states are those solution states that satisfy implicit
constraints
Backtracking follows a systematic search for answer states in
state space tree.
The search starts at root node. The nodes are generated in
depth first manner. A node is live if all its children are not yet
generated
The implicit constraints in the form of a bounding function are
used to kill a node and that part of the tree need not be
searched thus reducing the effort. The node currently being
explored is called E-node.
1
X1=1
X1=2
Q
Q
3
4
Q
Q
Q
X2=2
7
9
5 6
X2=4
X4=3
2
X3=2
Q
X2=3
X3=4
Q
Q
8 10
X3=2
X3=3
12
13
Q
Q
Q
X2=1
14
16
X2=4
11
Q
X2=3
Q
Q
15
X3=1
Q
Q
Q
Q
X4=3
Basic idea is to build the solution vector one component
at a time, applying bounding function based on
constraints to find whether the partial solution
generated has any chance to succeed
If it is found that partial solution x1,x2,…xi fails with
some value m for xi, then that part of the solution
space is ignored thus reducing the effort and a
second value for xi is tried
Algorithm Backtrack(k)// initially called Backtrack(1)
{ for each xk do
{ if (Bk(x1,x2,….xk))then
{ if (x1 ,x2…xk) is an answer node then
write x[1..k]
if(k<n) then Backtrack(k+1)
}
}// recursive version of Backtrack
Iterative version of backtrack
Algorithm IBacktrack(n)
{ k=1
while(k>0)
{ while (next(xk) and Bk(x1,x2,…,next(xk) ) then
{ if (x1 ,x2…xk) is an answer node then
write x[1..k]
k=k+1
}
}
Algorithm place(k,i)
{//x[ ] is a global array whose first (k-1) values are set.Returns
true if queen k can be placed in ith column.
for j=1 to k-1 do
If (x[ j ] = i) //two in same column
or abs(x[j]-i)=abs(j-k)
then return false
return true}
Algorithm 8Queens(k)
{
for i=1 to 8 do
if (place(k, i)) then
x[k] = i
if k=n then write X
else 8Queens(k+1)
}
Sum of Subsets Problem
Given n distinct positive numbers , to find all
combinations of these numbers whose sum is n
Ex n=4 (w1,w2,w3,w4)=(11,13,24,7) and m=31
The answer states are (11,13,7) and (24,7)
The solution can be represented by giving indices of wi
The answer states are(1,2,4) and (3,4) and are varying
size k tuples
We can represent answer as a fixed n size tuple (x1,x2…
xn) where each xi are either 0 or 1
The answer states are(1,1,0,1) and (0,0,1,1)
Explicit constraints xi=0 or xi=1
Implicit constraints ∑wixi =m
On a partial solution (x1,x2,…xk) we define a bounding
function which tells us whether to proceed or backtrack
Bk(x1,…..xk) is true if
k n
∑ wixi + ∑ wi ≥ m
i=1 i= k+1
If wi’s are arranged in nondecreasing order then additional
condition could be
k
∑ wixi + wk+1 ≤ m
i=1
Thus the bounding function Bk(x1,…xk) is true iff
k n k
∑ wixi + ∑ wi ≥ m and ∑ wixi + wk+1 ≤ m
i=1 i= k+1 i=1
The computing of ∑ wixi or ∑wi each time can be avoided
by storing the computed value in variables s and r
respectively
Let w=(5,7,10,12,15,18,20) and m=35
S=0 and r =∑ wi =5+7+10+12+15+18+20=87
0 ,1, 87
X1=1 X1=0
5 ,2, 82 0 ,2, 82
X2=1 X2=0
12 ,3, 75 5 ,3, 75
X2=1 X2=0
7 ,3, 75 0 ,3, 75
X3=1 X3=1
22 ,4, 65 12 ,4, 65
X3=0
15 ,4, 65 5 ,4, 65
X3=0 X3=1 X3=1
17 ,4, 65 7 ,4, 65
X3=0
10 ,4, 65 12 ,4, 65
X3=0
12 ,5, 53
X4=0
w=(5,7,10,12,15,18,20) and m=35
S=0 and r =∑ wi =87
Algorithm SumofSub(s,k,r)
// initially called with (0,1, ∑wi)
{ x[k]=1
if s+w[k]=m then write x[1..k]
else if s+w[k] +w[k+1] ≤ m then
SumofSub(s+w[k],k+1,r-w[k])
If(s+r-w[k]≥m and s+w[k+1] ≤ m then
{ x[k]=0
sumofSub(s,k+1,r-w[k])
}
}
Graph Coloring problem
G be a graph and m an integer , to find how G can be
colored using m colors so that no two adjacent nodes
have the same color
If d is the degree of graph , then it can becolored with d+1
colors
The m-colorability optimization problem asks for the
smallest intger m for which graph G can be colored
This integer is known as the chromatic number of the
Graph
The 4-color decision problem is famous for planar graphs
The colors are represented by integers 1,2,…m and
solution can be an n-tuple (x1, x2, ….xn) where xi
denotes the value of the color given to the ith node
Explicit constraints
1≤ xi ≤ m
Implicit constraints
The colors should be different if there is an edge
connecting the nodes
xi≠ xj if (xi,xj) ε E
Algorithm mcoloring(k)
{ repeat
{ NextValue(k)
If x(k) =0 then return
If (k=n then write x[i;..n]
else mcoloring(k+1)
} until false
1 2
3 4
x1 1 2
3
x2 2 3 1 3
1 2
x3 2 3
1
x4
3 1
Hamiltonian Cycle
G=(V,E) be connected graph with n vertices.
An Hamiltonian cycle is a round-trip path along n edges
of G that visits every vertex once and returns to its
starting position
1 3
2 4
8 7 6 5
1,2,8,7,6,5,4,3,1 is an Hamiltonian cycle
1 3
2
5 4
There is no Hamiltonian cycle
The solution is a n-tuple (x1,x2,…xn) where xi
represents the ith vertex visited in the cycle
Explicit constraints
1≤ xi ≤ n for all I 1≤ i ≤ n
To avoid printing the same cycle n times
x1=1 and 2 ≤ xi ≤ n for all i 2≤ i ≤ n
Implicit constraints
i) The vertexes in the cycle are all distinct
xi ≠xj if i≠ j
ii) There is an edge connecting adjacent vertices in the
cycle
(xi,xi+1) ε E 1 ≤ i < n ( xn,x1) ε E
1 3
2 4
8 7 6 5
1
X1=1
2
X2=2 X2=7 X2=3
3 3
X3=3 X3=8
4 3
X5=5
X4=6
5
X4=4
X6=6
6
Design and analysis of algorithms_Backtracking.ppt
Design and analysis of algorithms_Backtracking.ppt
Design and analysis of algorithms_Backtracking.ppt
Design and analysis of algorithms_Backtracking.ppt
Design and analysis of algorithms_Backtracking.ppt
Design and analysis of algorithms_Backtracking.ppt

Design and analysis of algorithms_Backtracking.ppt

  • 1.
     BackTracking This methodis applied to problems which ask for a set of solutions satisfying certain constraints The solution to the problem is expressible as an n-tuple (x1,x2, ….xn) where xi’s are chosen from some finite set Si. If mi is the size of Si then xi can take mi possible values The total possible tuples are m1xm2xm3….mn Ex – 8 queens problem 8 queens are to placed on the chess board so that no two queens kill each other Let us formulate the solution to this problem as a 8-tuple (x1,x2, ……x8)
  • 2.
    Since each Queenhas to be in different rows and different colums Let xi denote the column in which the Queen is placed in ith row Let (x1,x2,……x8)=(2,5,6,7,3,4,8,1) Q Q Q Q Q Q Q Q
  • 3.
    Most of theproblems solved using backtracking require to find solutions that satisfy a complex set of constraints These constraints can be divided into two types 1. Implicit constraints 2. Explicit constraints 1. Explicit constraints are defined on xi restricting xi to take values from a given set Explicit constraints for 8 queen’s problem 1 ≤ xi ≤ 8 All solutions that satisfy explicit constraints define a possible solution space The solution space is reduced to 88 from earlier 648
  • 4.
    2. Implicit constraintsare rules that define the way xi’s are related to each other For 8 Queen’s problem implicit constraints are i) Two Queens cannot be in same column xi ≠ xj ii) Two Queens cannot be in the same diagonal q11 q12 q13 q14 q21 q22 q23 q24 q25 q31 q32 q33 q34 q35 q43 q44 a55 i denotes row and xi denotes column Difference is same xi-i=xj-j or xi-xj= i-j Sum is same xi+i=xj+j or xi-xj=j-I |xi-xj|=|i=j|
  • 5.
    The possible solutionsto the problem satisfying explicit and some of the implicit constraints can be arranged in the form of a tree Ex 4 queen’s Problem 1 X1=1 X1=2 X1=3 X1=4 2 Q Q 3 4 5 Q Q X2=2 X2=3 X2=4 6 7 8 X2=1 X2=2 X2=3 15 16 17 X2=4 X2=2 X2=4 12 13 14 9 10 11 X2=1 X2=3 X2=1 18 19 X3=3 X4=4
  • 7.
    The solution spaceis organized in the form of a tree. Each node in this tree defines a problem state. All paths from roots to other nodes define the state space of the problem. Solution states are those problem states s for which the path from the root to s defines a tuple in the solution space. Usually leaf nodes are solution states. Answer states are those solution states that satisfy implicit constraints Backtracking follows a systematic search for answer states in state space tree. The search starts at root node. The nodes are generated in depth first manner. A node is live if all its children are not yet generated The implicit constraints in the form of a bounding function are used to kill a node and that part of the tree need not be searched thus reducing the effort. The node currently being explored is called E-node.
  • 8.
  • 9.
    Basic idea isto build the solution vector one component at a time, applying bounding function based on constraints to find whether the partial solution generated has any chance to succeed If it is found that partial solution x1,x2,…xi fails with some value m for xi, then that part of the solution space is ignored thus reducing the effort and a second value for xi is tried Algorithm Backtrack(k)// initially called Backtrack(1) { for each xk do { if (Bk(x1,x2,….xk))then { if (x1 ,x2…xk) is an answer node then write x[1..k] if(k<n) then Backtrack(k+1) } }// recursive version of Backtrack
  • 10.
    Iterative version ofbacktrack Algorithm IBacktrack(n) { k=1 while(k>0) { while (next(xk) and Bk(x1,x2,…,next(xk) ) then { if (x1 ,x2…xk) is an answer node then write x[1..k] k=k+1 } }
  • 11.
    Algorithm place(k,i) {//x[ ]is a global array whose first (k-1) values are set.Returns true if queen k can be placed in ith column. for j=1 to k-1 do If (x[ j ] = i) //two in same column or abs(x[j]-i)=abs(j-k) then return false return true} Algorithm 8Queens(k) { for i=1 to 8 do if (place(k, i)) then x[k] = i if k=n then write X else 8Queens(k+1) }
  • 12.
    Sum of SubsetsProblem Given n distinct positive numbers , to find all combinations of these numbers whose sum is n Ex n=4 (w1,w2,w3,w4)=(11,13,24,7) and m=31 The answer states are (11,13,7) and (24,7) The solution can be represented by giving indices of wi The answer states are(1,2,4) and (3,4) and are varying size k tuples We can represent answer as a fixed n size tuple (x1,x2… xn) where each xi are either 0 or 1 The answer states are(1,1,0,1) and (0,0,1,1) Explicit constraints xi=0 or xi=1
  • 13.
    Implicit constraints ∑wixi=m On a partial solution (x1,x2,…xk) we define a bounding function which tells us whether to proceed or backtrack Bk(x1,…..xk) is true if k n ∑ wixi + ∑ wi ≥ m i=1 i= k+1 If wi’s are arranged in nondecreasing order then additional condition could be k ∑ wixi + wk+1 ≤ m i=1 Thus the bounding function Bk(x1,…xk) is true iff k n k ∑ wixi + ∑ wi ≥ m and ∑ wixi + wk+1 ≤ m i=1 i= k+1 i=1
  • 14.
    The computing of∑ wixi or ∑wi each time can be avoided by storing the computed value in variables s and r respectively Let w=(5,7,10,12,15,18,20) and m=35 S=0 and r =∑ wi =5+7+10+12+15+18+20=87 0 ,1, 87 X1=1 X1=0 5 ,2, 82 0 ,2, 82 X2=1 X2=0 12 ,3, 75 5 ,3, 75 X2=1 X2=0 7 ,3, 75 0 ,3, 75 X3=1 X3=1 22 ,4, 65 12 ,4, 65 X3=0 15 ,4, 65 5 ,4, 65 X3=0 X3=1 X3=1 17 ,4, 65 7 ,4, 65 X3=0 10 ,4, 65 12 ,4, 65 X3=0 12 ,5, 53 X4=0
  • 15.
  • 19.
    Algorithm SumofSub(s,k,r) // initiallycalled with (0,1, ∑wi) { x[k]=1 if s+w[k]=m then write x[1..k] else if s+w[k] +w[k+1] ≤ m then SumofSub(s+w[k],k+1,r-w[k]) If(s+r-w[k]≥m and s+w[k+1] ≤ m then { x[k]=0 sumofSub(s,k+1,r-w[k]) } }
  • 20.
    Graph Coloring problem Gbe a graph and m an integer , to find how G can be colored using m colors so that no two adjacent nodes have the same color If d is the degree of graph , then it can becolored with d+1 colors The m-colorability optimization problem asks for the smallest intger m for which graph G can be colored This integer is known as the chromatic number of the Graph The 4-color decision problem is famous for planar graphs The colors are represented by integers 1,2,…m and solution can be an n-tuple (x1, x2, ….xn) where xi denotes the value of the color given to the ith node
  • 21.
    Explicit constraints 1≤ xi≤ m Implicit constraints The colors should be different if there is an edge connecting the nodes xi≠ xj if (xi,xj) ε E Algorithm mcoloring(k) { repeat { NextValue(k) If x(k) =0 then return If (k=n then write x[i;..n] else mcoloring(k+1) } until false
  • 22.
    1 2 3 4 x11 2 3 x2 2 3 1 3 1 2 x3 2 3 1 x4 3 1
  • 26.
    Hamiltonian Cycle G=(V,E) beconnected graph with n vertices. An Hamiltonian cycle is a round-trip path along n edges of G that visits every vertex once and returns to its starting position 1 3 2 4 8 7 6 5 1,2,8,7,6,5,4,3,1 is an Hamiltonian cycle 1 3 2 5 4 There is no Hamiltonian cycle
  • 27.
    The solution isa n-tuple (x1,x2,…xn) where xi represents the ith vertex visited in the cycle Explicit constraints 1≤ xi ≤ n for all I 1≤ i ≤ n To avoid printing the same cycle n times x1=1 and 2 ≤ xi ≤ n for all i 2≤ i ≤ n Implicit constraints i) The vertexes in the cycle are all distinct xi ≠xj if i≠ j ii) There is an edge connecting adjacent vertices in the cycle (xi,xi+1) ε E 1 ≤ i < n ( xn,x1) ε E
  • 28.
    1 3 2 4 87 6 5 1 X1=1 2 X2=2 X2=7 X2=3 3 3 X3=3 X3=8 4 3 X5=5 X4=6 5 X4=4 X6=6 6