Daa Unit4
Daa Unit4
Backtracking
By
Dr.M.Rajababu
Associate Professor
Dept of Information Technology
Aditya Engineering College(A)
Surampalem.
Aditya Engineering College (A)
Backtracking
• Backtracking is a systematic method for searching one or more solutions of the
problem among all available options.
• It is used for finding all possible solutions (solutions space)for the given problem.
• This approach is used to solve problems that have multiple solutions. If you want
an optimal solution, you must go for dynamic programming.
• In this approach , we use the following steps to give the solution to the problem:
1.start with a sub solution
2.check if the sub solution leads to the final solution or not.
3.If not come back and change the sub solution and continue again.
Backtracking
• We can represent the solution space for the problem using a state space tree.
• A space state tree is a tree representing all the possible states(solution or non
solution) of the problem from the root as an initial state to the leaf as a terminal
state.
Backtracking
• In this method the desired solution is expressed as an n-tuple x1,x2,……,xn where
xi chosen from some finite set S.
• All solutions satisfy two types of constraints:
1.Explicite constraints
-These are the rules that restrict each xi to take values from finite set S.
2.Implicite constraints
-These are the rules that determine which solution set satisfies the criteria
function or bounding function.
Backtracking
• We formulate this solution using either Fixed or variable sized Tuple
representations.
· In Fixed Sized Tuple representation the solution subset is represented
by n-Tuple where the elements in this solution vector are either 0 or
1.
· In Variable Size representation we represent our solution set with
the elements in the actual set.
Backtracking
• Some problems solved with Backtracking technique are:
• N-Queens problem
• Sum of subsets problem
• Graph coloring problem
• Hamiltonian cycle problem
• Knapsack problem
• Sudoku Puzzle
• Rat in a Maze problem
N-Queens problem
• The N-Queens is the problem of placing N chess queens on an N×N chessboard so
that no two queens attack each other.
• Two queens attack each other if they are in same row,column or diagonal.
4-Queens problem
• 4-Queens problem is the problem of placing 4-Queens on a 4X4 chessboard such
that no two queens attack each other.
• following are two solutions for 4 Queen problem:
8-Queens problem
• 8-Queens problem is the problem of placing 8-Queens on an 8X8 chessboard
such that no two queens attack each other.
• following are three possible solutions for 8- Queens problem:
1.(1,5,8,6,3,7,2,4)
2.(1,6,8,3,7,4,2,5)
3. (2,4,6,8,3,1,7,5)
N-Queens problem
• In N-Queens problem the two queens Q1,Q2 at positions (i,j) and (k,l) are on the
same diagonal
If i-j=k-l-----(1)
i+j=k+l-----(2)
• From equation(1) j-l=i-k ----(3)
• From equation(2) j-l=k-i ---(4)
• From equation (3) and(4)
• |j-l|=|i-k|
• Therefore, two queens lie on the duplicate diagonal if and only if |j-l|=|i-k|
Place (k, i)
{
for j = 1 to k - 1
if (x [j] = i) //same column
or (Abs( x [j] - i) = (Abs (j - k)) //same diagonal
return false;
return true;
}
• Time complexity T(n)=n*T(n-1)+c
• T(n)=O(n!)
M-Coloring problem
• The problem is to find if it is possible to assign nodes with m different colors,
such that no two adjacent vertices of the Graph G are of the same colors. If
the solution exists, then display which color is assigned on which vertex.
m=8
Optimal solution X={0,1,0,1}
Maximum profit=15