DAA#3
DAA#3
N-Queens Problem
N - Queens problem is to place n - queens in such a manner on an n x
n chessboard that no queens attack each other by being in the same
row, column or diagonal.
It can be seen that for n =1, the problem has a trivial solution, and no
solution exists for n =2 and n =3. So first we will consider the 4
queens problem and then generate it to n - queens problem.
4
Using Backtracking to Solve N Queens
Now, we can see that there is no safe place where we can put the
last queen. So, we will just change the position of the previous
queen i.e., backtrack and change the previous decision.
Also, there is no other position where we can place the third queen,
so we will go back one more step and change the position of the
second queen.
And now we will place the third queen again in a safe position other
than the previously placed position in the third row.
We will continue this process and finally, we will get the solution as
shown below.
State Space Tree of the Four-queens Problem
4 - Queen's problem
function .in use the criterion that if (x1, x2, ……., xi) is a path to a current E-node,
then all the children nodes with parent-child labelings x (i+1) are such that (x1, x2,
attacking.
So we start with the root node as the only live node. This time this node becomes
the E-node and the path is (). We generate the next child. Suppose we are
generating the child in ascending order. Thus the node number 2 is generated and
path is now 1 i.e., the queen 1 is placed in the first row and in the first column.
Now, node 2 becomes the next E-node or line node. Further, try the next node in the
ascending nodes i.e., the node 3 which is having x2 = 2 means queen 2 is placed in
the second column but by this the queen 1 and 2 are on the same diagonal, so node
3 becomes dead here so we backtrack it and try the next node which is possible.
Here, the x2 = 3 means the queen 2 is placed in the 3rd column. As it satisfies all the
After this try for next node 9 having x3 = 2 which means the queen 3 placed in the
2nd column, but by this the 2 and 3 queen are on the same diagonal so it becomes
dead. Now we try for next node 11 with x3 = 4, but again the queens 2 and 3 are on
the 4th column. Move to the next node 14. It becomes the next live node with x3 =
2 means the queen 3 is placed in the 2nd column. Further, we move to the next
node 15 with x4 = 3 as the live node. But this makes the queen 3 and 4 on the same
diagonal resulting this node 15 is the dead node so we have to backtrack to the
node 14 and then backtrack to the node 13 and try the other possible node 16 with
x3 = 3 by this also we get the queens 2 and 3 on the same diagonal so the node is
So we further backtrack to the node 2 but no other node is left to try so the node 2
is killed so we backtrack to the node 1 and try another sub-tree having x1 = 2 which
the node 29 with x2 = 4 means the queen 2 is placed in the 4th column then we
try for the node 30 with x3 = 1 as a live node and finally we proceed to next node
Here, all the constraints are satisfied, so the desired result for 4 queens is {2, 4, 1,
3}.
N - Queen's problem
problem. Here, the n – queens are placed on a n * n chess board, which means
that the chessboard has n rows and n columns and the n queens are placed on
thus n * n chessboard such that no two queens are placed in the same row or in
the same column or in same diagonal. So that, no two queens attack each other.
Here, we suppose that the queen i is to be placed in row i. We can say that 1
queen is placed in the first row only but can have columns from 1, 2... n so that
tuples (x1, x2... xn) where xi is the column on which queen i is to be placed.
The explicit constraints using this formulation are Si = {1, 2, 3... n-1, n}, where 1 <=
I <= n. Therefore, the solution space consists of nˆn n- tuples. Now, while
considering the implicit constraints, that no two xi’s can be same i.e., two queens
cannot be in the same row, same column, or in same diagonal. So, each xi should
be different. Now by the above constraint, the solution is the permutations of the
Step 2: Given n queens, read n from user and let us denote the queen number by
k. k=1,2,..,n.
Step 3: We start a loop for checking if the k<sup>th</sup> queen can be placed in
the respective column of the k<sup>th</sup> row.
Step 4: For checking that whether the queen can be placed or not, we check if the
previous queens are not in diagonal or in same row with it.
Step 5: If the queen cannot be placed backtracking is done to the previous queens
until a feasible solution is not found.
Step 6: Repeat the steps 3-5 until all the queens are placed.
Step 7: The column numbers of the queens are stored in an array and printed as a
n-tuple solution
Step 8: Stop
Backtracking
Backtracking name itself suggests that we are going back and coming forward; if
it satisfies the condition, then return success, else we go back again. It is used to
solve a problem in which a sequence of objects is chosen from a specified set so
that the sequence satisfies some criteria.
Suppose another path exists from node A to node C. So, we move from node A to node C. It
is also a dead-end, so again backtrack from node C to node A. We move from node A to the
starting node.
Now we will check any other path exists from the starting node. So, we move
from start node to the node D. Since it is not a feasible solution so we move from
node D to node E. The node E is also not a feasible solution. It is a dead end so we
backtrack from node E to node D.
Suppose another path exists from node D to node F. So, we move from node D to
node F. Since it is not a feasible solution and it's a dead-end, we check for
another path from node F.
Suppose there is another path exists from the node F to node G so move from
node F to node G. The node G is a success node.
The terms related to the backtracking are:
Live node: The nodes that can be further generated are known as live nodes.
E node: The nodes whose children are being generated and become a success
node.
Success node: The node is said to be a success node if it provides a feasible
solution.
Dead node: The node which cannot be further generated and also does not
provide a feasible solution is known as a dead node.
Many problems can be solved by backtracking strategy, and that problems satisfy
complex set of constraints, and these constraints are of two types:
Implicit constraint: It is a rule in which how each element in a tuple is related.
Explicit constraint: The rules that restrict each element to be chosen from the
given set.
Sum of Subset Problem
Sum of Subset Problem
W[1:6] = {5,10,12,13,15,18}
Total 6 weights are there
n= 6
We have to take subset of weight such that their sum/ total is exactly
equal to 'm'
m= 30
X=
1 2 3 4 5 6
Note : X1= 1 means First weight is included X1=0 means its not included
Like the above if I continue there are 6 weights there will be 7 levels including
the root (Height will be 6 ). So total how many paths = 2^6 = 2^n.
So this is time consuming, its exponential. Amount of time taken to solve will
be 2^n
When we apply backtracking we try to kill the nodes if they are not satisfying
bounding function
We have to define the bounding function and we follow the bounding function
Now we can see the solution using backtracking and also we can define the
bounding function
0, 73
W[1:6] = {5,10,12,13,15,18}
5, 68
0, 73
X1= 1
5, 68
X2= 1
5, 68
X2= 1
15, 58
X3= 1
5, 68
X2= 1
W[1:6] = {5,10,12,13,15,18}
15, 58
X3= 1
27, 46
X4= 1
15, 58
X3= 1 One more bounding function is
there
X4= 1
27, 46 So for what all you have included
X4= 0
and remaining weights should be
27, 33
40, 33 greater than m.
X5= 1 X5= 0
B See 27 + 0 is not greater than 30
42,18 27,18
X6= 1 X6= 0
B
45,0 27,0
B
0, 73
X1= 1
W[1:6] = {5,10,12,13,15,18}
5, 68 1 2 3 4 5 6
X2= 1
X 1 1 0 0 1 0
15, 58
First weight is included
X3= 1 X3= 0 second is also included third
and fourth weights is not
included fifth weight is
27, 46 15,46
X4= 1 X4= 0 included sixth weight is not
X4= 0 X4= 1
included
40, 33 27, 33 28,33 15, 33
B B
An iterative backtracking method is shown below
In this problem, there is a given set with some integer elements. And another
some value is also provided, we have to find a subset of the given set whose sum
is the same as the given sum value.
Here backtracking approach is used for trying to select a valid subset when an
item is not valid, we will backtrack to get the previous subset and add another
element to get the solution.
Output − All possible subsets whose sum is the same as the given sum.
Begin
if total = sum, then
display the subset
//go for finding next subset
subsetSum(set, subset, , subSize-1, total-set[node], node+1, sum)
return
else
for all element i in the set, do
subset[subSize] := set[i]
subSetSum(set, subset, n, subSize+1, total+set[i], i+1, sum)
done
End
Lower Bounds: The Decision Tree method,
Lower Bounds for Comparison based Sort
and Searching
Lower Bound Theory
Lower Bound Theory Concept is based upon the calculation of minimum time
that is required to execute an algorithm is known as a lower bound theory or
Base Bound Theory.
Decision Tree: A decision tree is a full binary tree that shows the comparisons
between elements that are executed by an appropriate sorting algorithm
operating on an input of a given size. Control, data movement, and all other
conditions of the algorithm are ignored.
In a decision tree, there will be an array of length n.
So, total leaves will be n! (I.e. total number of comparisons)
If tree height is h, then surely
n! ≤2n (tree will be binary)
Taking an Example of comparing a1, a2, and a3.
Left subtree will be true condition i.e. ai ≤ aj
Right subtree will be false condition i.e. ai >aj
So from above, we got
N! ≤2n
Taking Log both sides
Comparison tree for Binary Search:
Example: Suppose we have a list of items according to the following Position:
1,2,3,4,5,6,7,8,9,10,11,12,13,14
2, 4, 6, 8, 10, 12, 14
Thus, we will consider all the midpoints and we will make a tree of it by having
stepwise midpoints.
According to Mid-Point, the tree will be:
Step1: Maximum number of nodes up to k level of the internal node is 2k-1
For Example
2k-1
N! ≤ 2k-1
Here, Internal Nodes will always be less than 2k in the Binary Search.
Lower bound of Comparison based Sorting
The problem of sorting can be viewed as following.
Output: A permutation (reordering) <a‘1, a‘2, . . . , a‘n> of the input sequence such that a‘1 <= a‘2 ….. <= a’n.
A sorting algorithm is comparison based if it uses comparison operators to find the order between two
numbers. Comparison sorts can be viewed abstractly in terms of decision trees. A decision tree is a full binary
tree that represents the comparisons between elements that are performed by a particular sorting algorithm operating
on an input of a given size. The execution of the sorting algorithm corresponds to tracing a path from the root of the
decision tree to a leaf. At each internal node, a comparison ai <= aj is made. The left subtree then dictates subsequent
comparisons for ai <= aj, and the right subtree dictates subsequent comparisons for ai > aj. When we come to a leaf,
the sorting algorithm has established the ordering. So we can say following about the decision tree.
1) Each of the n! permutations on n elements must appear as one of the leaves of the decision tree for the sorting
2) Let x be the maximum number of comparisons in a sorting algorithm. The maximum height of the decision tree