DAA Unit 4 Backtracking
DAA Unit 4 Backtracking
BACKTRACKING
MCA - I YEAR - II SEMESTER (MR-24)
Prepared By
Dr.Devireddy Srinivasa Kumar
Professor in CSE
MALINENI LAKSHMAIAH
WOMEN’S ENGINEERING COLLEGE
(AUTONOMOUS)
(Approved by AICTE, New Delhi & Affiliated to JNTUK, Kakinada)
How it works:
It's an intuitive, direct, and straightforward technique that enumerates all
possible solutions to a problem.
It doesn't use clever tricks or shortcuts to reduce the search space or
improve efficiency.
Examples:
Finding the divisors of a natural number.
Solving the eight queens puzzle.
Searching for a word in a dictionary by checking each word sequentially.
In cybersecurity, brute-force attacks try all possible password
combinations.
Advantages:
Simple and Easy to Understand: Brute-force algorithms are easy to
implement and understand, making them a good starting point for
problem-solving.
Guaranteed Solution: If a solution exists, a brute-force algorithm will
eventually find it, as it systematically explores all possibilities
Alternatives:
Greedy Algorithms: These algorithms make locally optimal choices at
each step to find a solution.
Divide and Conquer Algorithms: These algorithms break down a
problem into smaller subproblems that are solved independently and
then combined.
Dynamic Programming: This technique uses memoization (storing
previously computed results) to optimize the solution process.
BACKTRACKING
Introduction
When invoked, the algorithm starts with an empty vector. At each stage it
extends the partial vector with a new value. Upon reaching a partial
vector (v1, ..., vi) which can’t represent a partial solution, the algorithm
backtracks by removing the trailing value from the vector, and then
proceeds by trying to extend the vector with alternative values.
ALGORITHM try(v1,...,vi)
{
IF (v1,...,vi) is a solution THEN RETURN (v1,...,vi)
FOR each v DO
IF (v1,...,vi,v) is acceptable vector THEN
sol = try(v1,...,vi,v)
IF sol != () THEN RETURN sol
END
END
RETURN ()
}
Example
For example, consider the following tree. ABD, ABE, AC are the tuples
in solution space.
4)Solution states: These are those problem states S for which the
path from the root to S define a tuple in the solution space.
Here square nodes indicate solution. For the above solution space,
there exists 3 solution states. These solution states represented in
the form of tuples i.e. (1,2,4), (1,3,6) and (1,3,7) are the solution
states.
6)Answer states : These solution states s for which the path from
the root to S defines a tuple which is a member of the set of
solutions.(i.e. it satisfies the implicit constraints) of the problem. Here
3,4, are answer states. (1,3) and (1,2,4) are solution states.
In fig (b) node A is not a live node but B,C are live nodes.
In fig(c) nodes A, B are not live and D,E C are live nodes.
Ex)In figure(a) nodes A,B,C are dead nodes since node A’s children
already generated and Nodes B,C are not expanded.
In figure (b) assumed that node B can generate one more node so
nodes A,D,C are dead nodes.
All possible solutions have been shown in the state space tree. There are
6 possible ways to arrange chairs for 2 boys and 1 girl.
X1 X2 X3
B1 B2 G1
X= B2 B1 G1
G1 B1 B2
G1 B2 B1
The following figure shows a possible tree organization for the case
n = 4. A tree such as this is called a permutation tree. The edges
are labeled by possible values of xi.
Edges from level 1 to level 2 nodes specify the values for x 1. Thus the
left most sub-tree contains all solutions with x 1=1.
Edges from level i to level i+1 are labeled with the values of x i. The
solution space is defined by all paths from the root node to a leaf
node. There are 4!=24 leaf nodes in the permutation tree.
Algorithm place(k,i)
{
for j:=1 to k-1 do
if ((x[j]=i) or (abs(x[j]-i) = abs(j-k)) then
return false;
return true;
}
Algorithm nqueens(k,n)
{
for i:=1 to n do
{
if (place(k,i) then
{
X[k]:=i;
if (k=n) then
write(x[i:n);
else
nqueens(k+1,n);
}
}
}
For an 8x8 chess board there are 64C8 possible ways to place
8 Queens using brute force approach. However, by allowing
only placements of queens on distinct rows and columns, we
require the examination of at most 8! Tuples.
For a 4x4 chess board there are 16C4 possible ways to place
4 Queens using brute force approach. However, by allowing
only placements of queens on distinct rows and columns, we
require the examination of at most 4! Tuples.
1 2 3 4
Go to second row 1 1
Move the queen to another col. 2 - - - 2
Another possibility is column 4. 3
Move to col 4. 4
Now X[2]=4 X[2]=4
All four queens are placed in the 4x4 chess board without
attacking each other.
In the same way it is possible to place all 8 queens in
an 8x8 chess board without attacking each other.
nqueens(k,n) place(k,i)
Algorithm mcoloring(k)
{
repeat
{
nextvalue(k);
if (x[k] = 0) then return;
if (k=n) then
write(x[1:n]);
else
mcoloring(k+1);
}until(false);
}
No of vertices= n
No of colors= m
Solution vector = X[1], X[2], X[3]……..X[n]
The values of solution vector may belongs to {0,1,2,3..m}
Algorithm nextvalue(k)
{
Repeat
{
X[k]=(x[k]+1)mod(m+1); // next highest color
if (x[k]=0) then
return; //all colors have been used
for j:=1 to n do
{
if ((G[k,j]!=0) and (x[k]=x[j])) then break;
//g[k,j] an edge and
//vertices k and j have same color
}
if (j=n+1) then return;
}until (false);
}
----------------------
Coloring first vertex
----------------------
mcoloring(1) i.e. k=1
nextvalue(1)
k=1
x[1]=(x[1]+1) mod (m+1)
x[1]= 0+1 mod 4
x[1]=1
x[1]=1 ,x[2]=0,x[3]=0,x[4]=0
x[1]=1 ,x[2]=2,x[3]=0,x[4]=0
assume that the number mentioned outside the node belongs
to color
-------------------
Coloring 3rd vertex
-------------------
mcoloring(3) i.e. k=3
nextvalue(3)
k=3
x[3]=(x[3]+1) mod (m+1)
x[3]= 0+1 mod 4
x[3]=1
G[k,j]!=0 and x[k]=x[j]
j=1 G[3,1] False and True = False
j=2 G[3,2] True and False = False
j=3 G[3,3] False and True = False
j=4 G[3,4] True and False = False
x[1]=1 ,x[2]=2,x[3]=1,x[4]=0.
x[1]=1 ,x[2]=2,x[3]=1,x[4]=2
Algorithm Hamiltonian(k)
{
Repeat
{
nextvalue(k);
if (x[k]=0) then return;
if (k=n) then write (x[1:n]);
else
Hamiltonian(k+1);
}until(false);
}
Algorithm to find all Hamiltonian cycles.
No of vertices n=8
Adjacency matrix G Solution vertex
1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 1 1 X[1] 1
2 1 0 1 0 0 0 0 1 X[2] 0
3 1 1 0 1 0 1 0 0 X[3] 0
4 0 0 1 0 1 0 0 0 X[4] 0
5 0 0 0 1 0 1 0 0 X[5] 0
6 0 0 1 0 1 0 1 0 X[6] 0
7 1 0 0 0 0 1 0 1 X[7] 0
8 1 1 0 0 0 0 1 0 X[8] 0
Hamiltonian(3) k=3
Nextvalue(3) k=3
X[3]=(x[3]+1) mod (8+1)=(0+1)mod 9 = 1
X[3]=1
Is there an edge between k and k-1
If (G[x[k-1],x[k]]!=0)
G[2,1] edge True
If (G[x[k-1],x[k]]!=0)
G[2,2] edge False
If (G[x[k-1],x[k]]!=0)
G[2,3] edge True
J=1 is x[j]=x[k]
Is 1=2 no false
J=2 is 2=3 no false
Hamiltonian(4) k=4
Nextvalue(4) k=4
If (G[x[k-1],x[k]]!=0)
G[3,2] edge True
J=2 is x[j]=x[k]
is x[2]=x[4]
2=2 True Break
If (G[x[k-1],x[k]]!=0)
Solution
G[3,3] edge False
vector
X[4]=(x[4]+1) mod (8+1)
X[1] 1
X[4]=4
X[2] 2
If (G[x[k-1],x[k]]!=0) X[3] 3
G[3,4] edge True X[4] 4
X[5] 0
J=1 is x[j]=x[k] X[6] 0
X[7] 0
Dr.DSK I/II MCA DAA UNIT-IV Backtracking
X[8]Page
0 24
Is 1=4 no false
J=2 is 2=4 no false
J=3 is 3=4 no false
as k < n return still we need to add vertices
Hamiltonian(5) k=5
Nextvalue(5) k=5
If (G[x[k-1],x[k]]!=0)
G[4,1] no edge False
If (G[x[k-1],x[k]]!=0)
G[4,2] no edge False
J=2 is x[j]=x[k]
is x[2]=x[5]
2=3 False
J=3 is x[j]=x[k]
is x[3]=x[5]
3=3 True duplicate found break
If (G[x[k-1],x[k]]!=0)
G[4,5] edge True
Hamiltonian(6) k=6
Nextvalue(6) k=6
Graph G
No of vertices n=5
Adjacency matrix G Solution vertex
1 2 3 4 5
1 0 1 1 0 1 X 0 0 0 0 0
2 1 0 1 1 1 1 2 3 4 5
3 1 1 0 1 0
4 0 1 1 0 1
5 1 1 0 1 0
4) SUM OF SUBSETS
Now the two solutions are described by the vectors (1, 2, 3) and (1,
4).
The sum of sub set is based on fixed size tuple. Let us draw a tree
structure for fixed tuple size formulation.
All paths from root to a leaf node define a solution space. The left
subtree of the root defines all subsets containing W 1 and the right
subtree defines all subsets not containing W 1 and so on.
Algorithm sumofsubsets(s,k,r)
{
X[k]:=1;
if (s+w[k]=m) then write (x[1:k]); // subset found
else
if (s+w[k]+w[k+1]<=m) then
sumofsubsets(s+w[k],k+1,r-w[k]);
Solution A = {1,1,1,0}
Solution B = {1,0,0,1}
Solution A = (1,1,0,0,1)
Solution B = (1,0,1,1)
Solution C = (0,0,1,0,0,1)
Important Questions
1)Describe problem state, solution state and answer state with examples.
2)Write the control abstraction of backtracking
3)Explain the applications of backtracking
4)Describe 4-queen problem using backtracking
5)Write an algorithm of finding all m-colorings of a graph
6)Draw the state space tree for m-coloring graph using suitable graph
7)write backtracking algorithm for 8-queens problem
8)Apply backtracking to find Hamiltonian cycle in the following graph as
shown in the figure.