0% found this document useful (0 votes)
8 views31 pages

Daa Unit4

Uploaded by

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

Daa Unit4

Uploaded by

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

UNIT-IV

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.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

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.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

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.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

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.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

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

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

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.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

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:

The solutions for 4-Queens


problem:
1.(2,4,1,3)
2.(3,1,4,2)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

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)

• The eight queens problem has 92 distinct solutions

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

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|

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Algorithm N -Queens (k, n)


{
for i = 1 to n
if Place (k, i) then//Place (k, i) returns true if a queen can be placed in the kth row and ith column
otherwise returnsfalse
{
x [k] = i;
if (k ==n) then
write (x [1:n]);
else
N-Queens (k + 1,n);
}
}
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

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!)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Sum of subsets problem


• Subset sum problem is to find subset of elements that are selected
from a given set S,whose sum adds up to a given number m.
EX: let A be a set A={5,7,10,12,15,18,20} and given sum m=35
• Following are the possible solutions:
1. {15,20},
2. {18,7,10},
3. {5,10,20}, and {18,12,5}

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Sum of subsets problem


• Assumption and considerations of this problem are :
• set contains non-negative values.
• input set is unique (no duplicates are presented).

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

• 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.
• Ex: A= (11, 13, 24, 7) & m = 31
· For Above example the fixed size Tuple is
• (1,1,0,1) or (0,0,1,1)
· the variable sized Tuple is
· (11, 13, 7) , (24, 7)
Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024
Aditya Engineering College (A)

A= (11, 13, 24, 7) & m = 31

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Graph coloring problem


• The problem is, given m colors, find a way of coloring the vertices of a graph G
with minimum number of colors such that no two adjacent vertices have same
color.
• The smallest number of colors needed to color a graph G is called its chromatic
number(X(G)).

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

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.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Hamiltonian cycles problem


• In this problem, we determine whether a given graph contains
Hamiltonian Cycle or not. If it contains, then prints the path.
• In an undirected Graph, Hamiltonian path is a path that visits each
vertex exactly once.
• Hamiltonian cycle or circuit is a Hamiltonian path that ends at the
starting vertex.
• A graph having Hamiltonian cycles is called Hamiltonian Graph.
• we can find the Hamiltonian Circuits using Backtracking approach.

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024


Aditya Engineering College (A)

0/1 Knapsack problem


Item 1 2 3 4
Weight 2 3 4 5
Profit 3 5 6 10

m=8
Optimal solution X={0,1,0,1}
Maximum profit=15

Design&analysisof Algorithms M.Raja Babu Thursday, December 5, 2024

You might also like