Mr. Pranay Meshram
Design and Analysis of Algorithm By Pranay Meshram
Contents:
What is Backtracking
Application of Backtracking
N-queen Problem
Sum of Subsets
Graph coloring Problem
Hamiltonian Cycles
Articulation Point
Design and Analysis of Algorithm By Pranay Meshram
Backtracking is a methodical way of trying out
various sequences of decisions, until you find one that
“works”.
Design and Analysis of Algorithm By Pranay Meshram
Backtracking algorithm solves the problem using two
types of constraints :
i) Explicit Constraint
 ii) Implicit Constraint.
Explicit Constraints :
Explicit Constraints restrict the each element xi to
take a value from given set; these conditions may or
may not depends upon the particular instance of
problem I, being solved. All the tuples which satisfy
these explicit constraints define a solution space for a
particular instance of the problem.
 Implicit Constraints :
Implicit Constraints determine which of the tuples in
the solution space of I actually satisfy the criterion
function, i.e implicit constraints specify how various
xi related to one another.
Design and Analysis of Algorithm By Pranay Meshram
Applications of Backtracking :
1. N - Queen problem
2. Hamiltonian cycle problem
3. Graph colouring problems
4. Subset - Sum problem
5. knapsack problem
6. Constraint satisfaction problem such as
crossword, verbal arithmatic etc.
7. Efficient technique for parsing.
Design and Analysis of Algorithm By Pranay Meshram
N-Queen
Problem
Problem:- The problem is to place n queens
on an n-by-n chessboard so that no
two queens attack each other by
being in the same row, or in the
same column, or in the same
diagonal.
Observation:- Case 1 : n=1
Case 2 : n=2
Case 3 : n=3
Case 4 : n=4
Design and Analysis of Algorithm By Pranay Meshram
.
Algorithm for n - queen
PLACE (k, i) :
1. For j 1 to k - 1
2. do if (x (j) = i) or Abs (x [i] - j) = Abs (j - k) ) or
 i + j = k + l.
3. then return false
4. return true
Design and Analysis of Algorithm By Pranay Meshram
N - Queen (k,n) :
1. For i 1 to n do
2. if place (k,i)
3. then x[k] i
4. if k = n, then print x [1----n]
5. else N - queen (k + 1, n)
Design and Analysis of Algorithm By Pranay Meshram
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
2
1
3
5
6
4
0
7
8
x x
xxx
x x
x
xxxx
xx
xxx
Design and Analysis of Algorithm By Pranay Meshram
Q
Q
Q
Q
Queen-1
Queen-2
Queen-3
Queen-44
3
2
1
4321
Board for the four-queens problemFigure:-
• Using this above mechanismwe can obtain two
solutions shown in the two consecutive figures:-
Design and Analysis of Algorithm By Pranay Meshram
Q
Q
Q
Q
Queen-1
Queen-2
Queen-3
Queen-44
3
2
1
4321
Board for the four-queens problemFigure:-
Design and Analysis of Algorithm By Pranay Meshram
The "8 Queens" problem
Q
Q
Q
Q
Q
Q
Q
Q
Design and Analysis of Algorithm By Pranay Meshram
Solve 8 queen’s problem for a feasible sequence
< 6, 4, 7,1>
Design and Analysis of Algorithm By Pranay Meshram
The other solution for 8 - queen problem is
< 4, 7, 3, 8, 2, 5, 1, 6>, <7, 2, 6,3, 1,4, 8, 5 >, < 6, 4,7, 1,3, 5, 2, 8>,
< 5, 3, 1, 7, 2, 8, 6, 4 > < 6, 1, 5,2, 8, 3, 7, 4 > < 5, 8, 4, 1, 3,6,2, 7>,
< 8, 2, 5, 3, 1, 7, 4, 6 > etc.
Design and Analysis of Algorithm By Pranay Meshram
• Subset-sumProblem: The problem is to find a subset of a
given set S = {s1, s2,- - -, sn} of ‘n’ positive
integers whose sum is equal to a given
positive integer ‘d’.
•
Example :
For S = {3, 5, 6, 7} and d = 15, the solution
is shown below :-
Solution = {3, 5,
7}
Subset-sumProblem
• Observation : It is convenient to sort the set’s elements
in increasing order, S1 S≤ 2 … . . S≤ ≤ n. And
each set of solutions don’t need to be
necessarily of fixed size.
Design and Analysis of Algorithm By Pranay Meshram
Algorithm
Let S be a set of elements and d is the expected sum
of subsets then:
Step I: Start with an empty set.
Step II: Add to the subset, the next element from the
list.
Step III: If the subset is having sum d than stop with
that subset as a solution.
Step IV: If the subset is not feasible or if we have
reached the end of the set than backtrack through the
subset until. We find most suitable value.
Step V: If the subset is feasible then repeat step 2.
Step VI: If we have visited all the elements without
finding a suitable subset and if no backtracking is
possible then stop without solution.
DDesign and Analysis of Algorithm By Pranay Meshram
15 8
511
05
814 3
8
9
3
0
3
0
with 6
with 5
with 6
with 7
with 6
with 5
with 3
w/o 5
w/o 6
w/o 5
w/o 3
w/o 6
w/o 7
w/o 6
solution
14+7>15 3+7<159+7>15 11+7>15
0+6+7<15
5+7<15
8<15
7
0
3
5
6
Figure : Compete state-space tree of the backtracking algorithm applied to the
instance S = {3, 5, 6, 7} and d = 15 of the subset-sum problem. The number
inside a node is the sum of the elements already included in subsets
represented by the node. The inequality below a leaf indicates the reason
for its termination.
x
xx xxx
x
Design and Analysis of Algorithm By Pranay Meshram
This problem is concern about
finding a Hamiltonian circuit in a
given graph.
Problem:
Hamiltonian Circuit Problem
Hamiltonian circuit is defined as a
cycle that passes to all the vertices
of the graph exactly once except
the starting and ending vertices
that is the same vertex.
Hamiltonian
circuit:
Design and Analysis of Algorithm By Pranay Meshram
Algorithm
1. do x [k] x [k + 1] % (n + 1) // finding next vertex
2. if (x [k] 0) then
3. return 0
4. if (Adj [k - 1], x [k] 1) then // if edge between vertex
k and k – 1 is present.
5. for j 1 to k - 1 do
6. if (x [j] x [k]) then
7. break
8. if (j k) then
9. if (k < n) or (k n) AND Adj [x [n], x [1] 1] then
10. return.
Design and Analysis of Algorithm By Pranay Meshram
Algorithm Hamiltonian(k):
1. k 1
2. do Next - vertex ( k )
3. if (x [k] 0) then
4. return.
5. if (k n) then
6. Write (x [1 : n])
7. Otherwise Hamilntonian (k + 1)
8. return.
Design and Analysis of Algorithm By Pranay Meshram
Explain how to Find Hamiltonian Cycle by using
backtraking in given graph.
Design and Analysis of Algorithm By Pranay Meshram
Design and Analysis of Algorithm By Pranay Meshram
Graph Colouring Problem
Graph colouring problem of states each vertex in a
graph in such a way that no two adjacent vertices
have same colour. This problem is called as m
colouring problem. This is also called a vertex
colouring.
If the degree of a given graph is d then we can colour
it with d + 1 colour.
A graph is a planer if it can be drawn such that no
edges cross when drawn on plane surface A map can
easily be converted to a planner graph as shown in fig.
(1).
Design and Analysis of Algorithm By Pranay Meshram
Design and Analysis of Algorithm By Pranay Meshram
Implement a graph colouring on following graph &
generate the solution space tree if number of
permitted colour = 3.
Design and Analysis of Algorithm By Pranay Meshram
Design and Analysis of Algorithm By Pranay Meshram

Backtracking

  • 1.
    Mr. Pranay Meshram Designand Analysis of Algorithm By Pranay Meshram
  • 2.
    Contents: What is Backtracking Applicationof Backtracking N-queen Problem Sum of Subsets Graph coloring Problem Hamiltonian Cycles Articulation Point Design and Analysis of Algorithm By Pranay Meshram
  • 3.
    Backtracking is amethodical way of trying out various sequences of decisions, until you find one that “works”. Design and Analysis of Algorithm By Pranay Meshram
  • 4.
    Backtracking algorithm solvesthe problem using two types of constraints : i) Explicit Constraint  ii) Implicit Constraint. Explicit Constraints : Explicit Constraints restrict the each element xi to take a value from given set; these conditions may or may not depends upon the particular instance of problem I, being solved. All the tuples which satisfy these explicit constraints define a solution space for a particular instance of the problem.  Implicit Constraints : Implicit Constraints determine which of the tuples in the solution space of I actually satisfy the criterion function, i.e implicit constraints specify how various xi related to one another. Design and Analysis of Algorithm By Pranay Meshram
  • 5.
    Applications of Backtracking: 1. N - Queen problem 2. Hamiltonian cycle problem 3. Graph colouring problems 4. Subset - Sum problem 5. knapsack problem 6. Constraint satisfaction problem such as crossword, verbal arithmatic etc. 7. Efficient technique for parsing. Design and Analysis of Algorithm By Pranay Meshram
  • 6.
    N-Queen Problem Problem:- The problemis to place n queens on an n-by-n chessboard so that no two queens attack each other by being in the same row, or in the same column, or in the same diagonal. Observation:- Case 1 : n=1 Case 2 : n=2 Case 3 : n=3 Case 4 : n=4 Design and Analysis of Algorithm By Pranay Meshram
  • 7.
    . Algorithm for n- queen PLACE (k, i) : 1. For j 1 to k - 1 2. do if (x (j) = i) or Abs (x [i] - j) = Abs (j - k) ) or  i + j = k + l. 3. then return false 4. return true Design and Analysis of Algorithm By Pranay Meshram
  • 8.
    N - Queen(k,n) : 1. For i 1 to n do 2. if place (k,i) 3. then x[k] i 4. if k = n, then print x [1----n] 5. else N - queen (k + 1, n) Design and Analysis of Algorithm By Pranay Meshram
  • 9.
  • 10.
    Q Q Q Q Queen-1 Queen-2 Queen-3 Queen-44 3 2 1 4321 Board for thefour-queens problemFigure:- • Using this above mechanismwe can obtain two solutions shown in the two consecutive figures:- Design and Analysis of Algorithm By Pranay Meshram
  • 11.
    Q Q Q Q Queen-1 Queen-2 Queen-3 Queen-44 3 2 1 4321 Board for thefour-queens problemFigure:- Design and Analysis of Algorithm By Pranay Meshram
  • 12.
    The "8 Queens"problem Q Q Q Q Q Q Q Q Design and Analysis of Algorithm By Pranay Meshram
  • 13.
    Solve 8 queen’sproblem for a feasible sequence < 6, 4, 7,1> Design and Analysis of Algorithm By Pranay Meshram
  • 14.
    The other solutionfor 8 - queen problem is < 4, 7, 3, 8, 2, 5, 1, 6>, <7, 2, 6,3, 1,4, 8, 5 >, < 6, 4,7, 1,3, 5, 2, 8>, < 5, 3, 1, 7, 2, 8, 6, 4 > < 6, 1, 5,2, 8, 3, 7, 4 > < 5, 8, 4, 1, 3,6,2, 7>, < 8, 2, 5, 3, 1, 7, 4, 6 > etc. Design and Analysis of Algorithm By Pranay Meshram
  • 15.
    • Subset-sumProblem: Theproblem is to find a subset of a given set S = {s1, s2,- - -, sn} of ‘n’ positive integers whose sum is equal to a given positive integer ‘d’. • Example : For S = {3, 5, 6, 7} and d = 15, the solution is shown below :- Solution = {3, 5, 7} Subset-sumProblem • Observation : It is convenient to sort the set’s elements in increasing order, S1 S≤ 2 … . . S≤ ≤ n. And each set of solutions don’t need to be necessarily of fixed size. Design and Analysis of Algorithm By Pranay Meshram
  • 16.
    Algorithm Let S bea set of elements and d is the expected sum of subsets then: Step I: Start with an empty set. Step II: Add to the subset, the next element from the list. Step III: If the subset is having sum d than stop with that subset as a solution. Step IV: If the subset is not feasible or if we have reached the end of the set than backtrack through the subset until. We find most suitable value. Step V: If the subset is feasible then repeat step 2. Step VI: If we have visited all the elements without finding a suitable subset and if no backtracking is possible then stop without solution. DDesign and Analysis of Algorithm By Pranay Meshram
  • 17.
    15 8 511 05 814 3 8 9 3 0 3 0 with6 with 5 with 6 with 7 with 6 with 5 with 3 w/o 5 w/o 6 w/o 5 w/o 3 w/o 6 w/o 7 w/o 6 solution 14+7>15 3+7<159+7>15 11+7>15 0+6+7<15 5+7<15 8<15 7 0 3 5 6 Figure : Compete state-space tree of the backtracking algorithm applied to the instance S = {3, 5, 6, 7} and d = 15 of the subset-sum problem. The number inside a node is the sum of the elements already included in subsets represented by the node. The inequality below a leaf indicates the reason for its termination. x xx xxx x Design and Analysis of Algorithm By Pranay Meshram
  • 18.
    This problem isconcern about finding a Hamiltonian circuit in a given graph. Problem: Hamiltonian Circuit Problem Hamiltonian circuit is defined as a cycle that passes to all the vertices of the graph exactly once except the starting and ending vertices that is the same vertex. Hamiltonian circuit: Design and Analysis of Algorithm By Pranay Meshram
  • 19.
    Algorithm 1. do x[k] x [k + 1] % (n + 1) // finding next vertex 2. if (x [k] 0) then 3. return 0 4. if (Adj [k - 1], x [k] 1) then // if edge between vertex k and k – 1 is present. 5. for j 1 to k - 1 do 6. if (x [j] x [k]) then 7. break 8. if (j k) then 9. if (k < n) or (k n) AND Adj [x [n], x [1] 1] then 10. return. Design and Analysis of Algorithm By Pranay Meshram
  • 20.
    Algorithm Hamiltonian(k): 1. k1 2. do Next - vertex ( k ) 3. if (x [k] 0) then 4. return. 5. if (k n) then 6. Write (x [1 : n]) 7. Otherwise Hamilntonian (k + 1) 8. return. Design and Analysis of Algorithm By Pranay Meshram
  • 21.
    Explain how toFind Hamiltonian Cycle by using backtraking in given graph. Design and Analysis of Algorithm By Pranay Meshram
  • 22.
    Design and Analysisof Algorithm By Pranay Meshram
  • 23.
    Graph Colouring Problem Graphcolouring problem of states each vertex in a graph in such a way that no two adjacent vertices have same colour. This problem is called as m colouring problem. This is also called a vertex colouring. If the degree of a given graph is d then we can colour it with d + 1 colour. A graph is a planer if it can be drawn such that no edges cross when drawn on plane surface A map can easily be converted to a planner graph as shown in fig. (1). Design and Analysis of Algorithm By Pranay Meshram
  • 24.
    Design and Analysisof Algorithm By Pranay Meshram
  • 25.
    Implement a graphcolouring on following graph & generate the solution space tree if number of permitted colour = 3. Design and Analysis of Algorithm By Pranay Meshram
  • 26.
    Design and Analysisof Algorithm By Pranay Meshram