DAA Unit4 Part-1c (Backtracking)
DAA Unit4 Part-1c (Backtracking)
BACKTRACKING
The root node is considered both a live node and an E-node (i.e., expansion node).
At any point of time, only one node is designated as an E-node. From the E-node,
we try to move to (or, generate) a new node (i.e., child of E-node).
If it is possible to move to a child node from the current E-node (i.e., if there is any
component yet to be included in the solution vector) then that child node will be
generated by adding the first legitimate choice for the next component of a solution
vector and the new node becomes a live node and also the new E-node. The old E-
node remains as a live node.
At the newly generated node, we apply the constraint function (or, criterion) to
determine whether this node can possibly lead to a solution. If it cannot lead to a
solution (i.e., if it doesn’t satisfy constraints) then there is no point in moving into
any of its subtrees and so this node is immediately killed and we move back (i.e.,
backtrack) to the most recently seen live node (i.e., its parent) to consider the next
possible choice for the previous component of a solution vector. If there is no such
choice, we backtrack one more level up the tree, and so on. The final live node
becomes new E-node.
Finally, if the algorithm reaches a complete solution, it either stops (if just one
solution is required) or continues searching for other possible solutions.
N-QUEENS PROBLEM: -
We are given an nxn chessboard and we need to place n queens on the chess board
such that they are non-attacking each another (i.e., no two queens should lie on the
same row, or same column, or same diagonal).
Due to the first two restrictions, it is clear that each row and column of the board
will have exactly one queen.
Let us number the rows and columns of the chessboard 1 through n. The queens
can also be numbered 1 through n.
Since, each queen must be on a different row, we can assume that queen i will be
placed on row i.
Each solution to the n-queens problem can therefore be represented as an n- tuple
(x1, x2, …, xn) where xi is the column number on which queen i is placed.
The xi values should be distinct since no two queens can be placed on the same
column.
A B A B
D C D C
E
Let us number the vertices of the graph 1 through n and the colors 1 through m.
So, a solution to the graph coloring problem can be represented as an n-tuple (x1,
x2, …, xn) where xi is the color of vertex i.
Problem: Find all possible ways of coloring the below graph with m=3.
A B
D C
Solution:
Let A=1, B=2, C=3, and D=4.
Example:
Consider the following graph:
1—3—4—5—6—7—8—2—1
Note: If a graph has an articulation point, then there will be no Hamiltonian cycles.
We can write a backtracking algorithm that finds all the Hamiltonian cycles in a
graph. We will output only distinct cycles.
We assume that the vertices of the graph are numbered from 1 to n.
The backtracking solution vector (x1, x2, …, xn) is defined so that xi represents the
ith visited vertex of the proposed cycle.
The graph is represented by its adjacency matrix G[1:n, 1:n].
x[2:n] are initialized to zero. And, x[1] is initialized to 1 because we assume that
cycles start from vertex 1.
For 2≤k≤n-1, xk can be assigned any vertex v in the range from 1 to n provided it is
distinct from x1, x2, …, xk-1 and there exists an edge between v and xk-1.
Now, xn can be assigned the remaining vertex, provided there exists an edge to it
from both x1 and xn-1.
Algorithm 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[1:n, 1:n].
// All cycles begin at node 1.
{
while(TRUE)
{
// Generate values for x[k].
NextValue(k); // Assign a legal next value to x[k].
if (x[k] = 0) then return;
if (k = n) then write (x[1:n]);
else Hamiltonian(k+1);
}
}
Algorithm 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].
{
while(TRUE)
{
x[k] := (x[k]+1) mod (n+1); // Next vertex.
if (x[k] = 0) then return;
if (G[x[k-1], x[k]] ≠ 0) then // Is there an edge?
{
for j:= 1 to k – 1 do // Check for distinctness.
if (x[j] = x[k]) then break;
if (j = k) then //If true, then the vertex is distinct.
if ((k < n) or ((k = n) and G[x[n], x[1]] ≠ 0)) then
return;
}
}
}
Example: Find the Hamiltonian cycles for the following graph using backtracking.
Solution:
Portion of State-space tree generated for solving Hamiltonian cycles problem: -
Example: Find the Hamiltonian cycles for the following graph using backtracking.
Solution:
Portion of State-space tree generated for solving Hamiltonian cycles problem: -
SUM OF SUBSETS: -
Suppose we are given n distinct positive numbers (usually called weights) wi,
1≤i≤n and we need to find all combination (subsets) of these numbers whose sums
are equal to a given integer m.
Each solution subset is represented by an n-tuple (x1, x2, …, xn) such that xi ∈ {0,1},
1 ≤ i ≤ n.
If wi is not included in subset, then xi =0.
If wi is included in subset, then xi =1.
Example: For n=4, (w1, w2, w3, w4) = (5,3,4,6), and m=9,
(x1, x2, x3, x4) = (1,0,1,0)
= (0,1,0,1)
Size of solution search space is 2×2×2×2×… n times = 2n.
Note: In the above figure, the number in the circle denotes the sum of the weights
considered till now.
Example:
Let w= {3,4,5,6} and m=9. Find all possible subsets of w that sum to m. Draw the
portion of the state space tree that is generated.
Solution:
Note: In the state space tree, the rectangular nodes list the values of s, k, and r on
each call to SumOfSub. Initially s=0, k=1, r=18.
Portion of the state space tree that is generated:
Example: Let w = {5, 7, 10, 12, 15, 18, 20} and m=35. Find all possible subsets of w that sum
to m. Draw the portion of the state space tree that is generated.
Solution:
Initially, sum of weights, r=5+7+10+12+15+18+20=87.
One solution is: (x1, x2, x3, x4, x5, x6, x7) = (1,0,1,0,0,0,1).
Example:
Let w= {5,10,12,13,15} and m=30. Find all possible subsets of w that sum to m.
Draw the portion of the state space tree that is generated.
Solution:
Note: In the state space tree, the rectangular nodes list the values of s, k, and r on
each call to SumOfSub. Answer nodes are represented in circles. Initially s=0,
k=1, r=73.