0% found this document useful (0 votes)
10 views

Backtracking Part-1 (Nqueen)

Backtracking is an algorithmic technique used to solve computational problems incrementally by building candidates for solutions and abandoning those that cannot lead to a valid solution. It is commonly applied in problems like N-Queens, Sudoku, and the Travelling Salesman Problem, utilizing a depth-first search approach to explore the state space tree. The time complexity of the N-Queens problem using backtracking is O(n!), making it more efficient than brute force methods, which have a complexity of O(N^N).

Uploaded by

rohannijhawan05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Backtracking Part-1 (Nqueen)

Backtracking is an algorithmic technique used to solve computational problems incrementally by building candidates for solutions and abandoning those that cannot lead to a valid solution. It is commonly applied in problems like N-Queens, Sudoku, and the Travelling Salesman Problem, utilizing a depth-first search approach to explore the state space tree. The time complexity of the N-Queens problem using backtracking is O(n!), making it more efficient than brute force methods, which have a complexity of O(N^N).

Uploaded by

rohannijhawan05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Backtracking

Dr. Tarunpreet Bhatia


Associate Professor
CSED, TIET
Applications

• N-Queen
• Graph coloring
• Sudoko
• Hamiltonian Graph
• Sum of subsets
• 0/1 Knapsack
• Travelling Salesman Problem
Backtracking

• Backtracking is a general algorithm for finding all (or some)


solutions to some computational problem, that incrementally
builds candidates to the solutions, and abandons each partial
candidate ‘c’ ("backtracks") as soon as it determines that ‘c’
cannot possibly be completed to a valid solution.
• Backtracking is an important tool for solving constraint
satisfaction problems, such as crosswords, verbal arithmetic,
Sudoku, and many other puzzles.
Backtracking Method

Backtracking consists of:


• Doing a depth-first search of a state space tree.
• Checking whether each node is promising, and, if it is non
promising, backtracking to the node’s parent. We call a
node non promising if when visiting the node we
determine that it cannot possibly lead to a solution.
Otherwise, we call it promising.
• Prunes the sub tree rooted at non-promising node and
continues the depth-first traversal of the tree
State space tree

• We can represent the solution space for the problem


in a tree form.
• The root of the tree represents 0 choices,
• Nodes at depth 1 represent first choice
• Nodes at depth 2 represent the second choice,
etc.
• In this tree a path from a root to a leaf represents
a candidate solution
Example
N-queens problem

• Place n queens on an n by n chess board so that no


two of them are on the same row, column, or
diagonal
• Find a configuration of n queens not attacking each
other.
• Consider chessboard squares are being numbered as
indices of 2D array a[1..n, 1..n] and {x1, x2, …. xn}
represents a solution in which xk is the column where
kth queen is placed.
Graphical steps for 4 Queen problem
a) b) c) d)

e) f) g) h)

i)
j)
Basic Steps

The backtracking strategy is as follows:


1) Place a queen on the first available square in row 1.
2) Move onto the next row, placing a queen on the first available
square there (that doesn't conflict with the previously placed
queens).
3) Continue in this fashion until either:
a) you have solved the problem, or
b) you get stuck.
• When you get stuck, remove the queens that got you there, until you get to a
row where there is another valid square to try.
State space tree for 4 Queen Problem generated
during backtracking
State space tree for 4 Queen Problem generated
during backtracking
How to test 2 queens are non-attacking
Algorithm

• Place(k, i) returns true if kth queen can be placed in


column i. Test whether i is distinct from all previous
values x[1], x[2], …..x[k-1] and also if there is no
other queen on the same diagonal.
• Nqueens(k, n) prints all possible placements of n
queens on an n*n chessboard so that they are non
attacking Using backtracking
Algorithm

NQueens(k, n) { Place(k, i)
for (i=1 to n ) {
{
for (j=1 to k-1) {
if Place(k, i) {
if ((x[j]=i) or (Abs(x[j]-i)=Abs(j-k)))
x[k]=i
//same column or same diagonal
If(k=n) then write (x[1:n]);
return false;
else Nqueens(k+1,n);
}
}
} return true;
} }
Backtracking – Eight Queens Problem
Try for different n values!

• N=1 1 solution
• N=2 No solution
• N=3 No solution
• N=4 2 solutions (2, 4, 1, 3 and 3, 1, 4, 2)
• N=5 1, 3, 5, 2, 4 and possibly more solutions
• N=6 2, 4, 6, 1, 3, 5 and possibly more solutions
Time and Space complexity

• Brute Force: O(N^N). This means it will look through


every position on an NxN board, N times, for N queens.
• Backtracking: O(N!), we have N choices in the first row,
N-1 in the second row, N-2 in the next and so on... which
brings overall time complexity to O(N!)
• Space Complexity: O(N^2), where ‘N’ is the number of
queens as we are using a 2-D array having N rows
and N columns.
Time complexity using backtracking

• In the given algorithm, the main program calls the function Nqueens(k,
n) which calls the function Place(k, i). The function Place(k, i) checks
the already placed queens. So it has O(N-n) worst case running time.
Since O(N-n)<O(N), we can write O(N-n)=O(N)
Since the function Nqueens iterates n times and for each iteration it
calls the function Place(k, i) function the total time complexity would
be O(N^2).
• Besides, Nqueens(k, n) is a recursive function. Thus, the recursive call
of T(n-1) will run n times because it will run only for the safe cells.
Since we have started by filling up the rows, there won't be more than n
(number of queens left) safe cells in the row in any case. Which in turn
results in nT(n-1) times. By adding the results above we get T(n)= nT(n-1) +
O(N^2)
Recursion Tree method

• Replacing T(n−1) with O(N^2)+(n−1)T(n−2):


• T(n) = O(N^2)+n∗(O(N^2)+(n−1)T(n−2))T(n) = O(N^2)+n∗(O(N^2)+(n−1)T(n−2))
=O(N^2)+nO(N^2)+n(n−1)T(n−2)
• Replacing T(n−2) with O(N^2)+(n−2)T(n−3):
• T(n)=O(N^2)+nO(N^2)+n(n−1)(O(N^2)+(n−2)T(n−3))T(n)
=O(N^2)+nO(N^2)+n(n−1)(O(N^2)+(n−2)T(n−3))
=O(N^2)+nO(N^2)+n(n−1)O(N^2)+n(n−1)(n−2)T(n−3)
• Similarly:
• T(n)=O(N^2)(1+n+n(n−1)+n(n−2)+...)+n∗(n−1)∗(n−2)∗(n−3)∗(n−4)∗....∗T(0)T(n)
=O(N^2)(1+n+n(n−1)+n(n−2)+...)+n∗(n−1)∗(n−2)∗(n−3)∗(n−4)∗....∗T(0)T(n)
=O(N^2)(O((n−2)!))+n∗(n−1)∗(n−2)∗(n−3)∗....∗T(0)T(n)
=O(N^2)(O((n−2)!))+n∗(n−1)∗(n−2)∗(n−3)∗....∗T(0) =O(N^2)(O((n−2)!))+O(n!)= O(n!)
According to the definition of Big-Oh notation
• So we can conclude that the time complexity of N Queens Algorithm is O(n!)

You might also like