Vous êtes sur la page 1sur 37

BackTracking Algorithms

Algorithm Design Techniques

 Incremental Approach
 Divide & Conquer Approach
 Greedy Approach
 Dynamic Programming
 Backtracking

2
Backtracking

 Suppose you have to make a series of decisions,


among various choices, where

– You don’t have enough information to know what to choose


– Each decision leads to a new set of choices
– Some sequence of choices (possibly more than one) may be a
solution to your problem

 Backtracking is a methodical way of trying out


various sequences of decisions, until you find one
that “works” 3
Introduction

 Backtracking is used to solve problems in which a


sequence of objects is chosen from a specified set so that
the sequence satisfies some criterion.
 Backtracking is a modified depth-first search of a tree.

 Backtracking involves only a tree search.


 Backtracking is the procedure whereby, after determining
that a node can lead to nothing but dead nodes, we go back
(“backtrack”) to the node’s parent and proceed with the
search on the next child.
4
Introduction …

 We call a node nonpromising if when visiting the node we


determine that it cannot possibly lead to a solution. Otherwise,
we call it promising.

 In summary, backtracking consists of

– Doing a depth-first search of a tree,

– Checking whether each node is promising, and, if it is


nonpromising, backtracking to the node’s parent.
5
State Space Tree

? dead end
dead end
dead end

?
start ? ? dead end
dead end
?
success!

8
N-Queens Problem

 Try to place N queens on an N * N board such


that none of the queens can attack another queen.

 Remember that queens can move horizontally,


vertically, or diagonally any distance.

 Let’s consider the example…

9
The N-Queens Problem

Two queens are not allowed in


the same row...

10
The N-Queens Problem

Two queens are not allowed in


the same column...

11
The N-Queens Problem

Two queens are not allowed


along the same diagonal.

12
The 8-Queens Example

13
Let’s look at it run (1 queen Problem)

14
(2 queen Problem)

X X

2 queen problem ::: No Solution exist

15
(3 queen Problem)

X X X
X X X
X
X
3 queen problem ::: No Solution exist 16
(4 queen Problem)

4 columns
4 rows

17
(4 queen Problem)

4 columns
4 rows

Need Backtrack…..
18
(4 queen Problem)

4 columns
4 rows

Again need backtrack…..


19
(4 queen Problem)

4 columns
4 rows

Solved…..
20
State Space Tree

21
(8 queen Problem)
8 columns
8 rows

22
Algorithm for N Queen problem

1) Divide n by 12. Remember the Remainder


2) Write a list of even numbers from 2 to n in order
(i.e. 2,4,6,8,….,n)
3) If Remainder is 3 or 9 Move 2 to the end of the
list
4) Append the odd numbers from 1 to n in order
but if remainder is 8 then switch pairs (i.e.
3,1,7,5,11,9,….n)
23
Algorithm for N Queen problem

5) If the Remainder is 2 switch the places of 1 and 3,


then move 5 to the end of the list
6) If the Remainder is 3 or 9 move 1 and 3 to the
end of the list
7) Place the first column queen in the row with the
first number in the list
8) Place the second column queen in the row with
second number in the list.
9) etc…. 24
For 8 Queen

1) Divide n by 12. Remember the Remainder i.e. 8/12 =8


2) Write a list of even numbers from 2 to n in order
(i.e. 2,4,6,8)

3) If Remainder is 3 or 9 Move 2 to the end of the


list Not Applicable
4) Append the odd numbers from 1 to n in order
but if remainder is 8 then switch pairs
(i.e. 2,4,6,8, 3,1,7,5)
25
Algorithm for N Queen problem

5) If the Remainder is 2 switch the places of 1 and 3,


then move 5 to the end of the list Not Applicable
6) If the Remainder is 3 or 9 move 1 and 3 to the
end of the list Not Applicable
7) Place the first column queen in the row with the
first number in the list
8) Place the second column queen in the row with
second number in the list.
9) etc…. 26
(8 queen Problem)
8 columns
8 rows

27
Try it for

 14 Queens Remainder = 2

2,4,6,8,10,12,14,3,1,7,9,11,13,5

 15 Queens Remainder = 3
4,6,8,10,12,14,2,5,7,9,11,13,15,1,3

 20 Queens Remainder = 8

2,4,6,8,10,12,14,16,18,20,3,1,7,5,11,9,15,13,19,17

28
29
What is Graph Coloring?

 Graph Coloring is an assignment of colors to


the vertices of a graph such that no two
adjacent vertices have the same color that
too by using the minimum number of colors .
The smallest number of colors needed to
color a graph G is called its chromatic
number , and is often denoted χ(G).

30
Example

Consider the following map and it can be easily decomposed


into the following planner graph beside it

31
State Space Tree

32
Solution

Now the map can be colored as shown here:-

33
Example-2

34
Hamiltonian Cycle

 Hamiltonian cycle 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.

35
State Space Tree

36
Sum of Subset Problem

 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’.
 It is convenient to sort the set’s elements in
increasing order, S1 ≤ S2 ≤ ….. ≤ Sn. And
each set of solutions don’t need to be
necessarily of fixed size.
 For S = {3, 5, 6, 7} and d = 15 37
State Space Tree

38
Chapter Summary

 Backtracking is an algorithm design technique for


solving problems in which the number of choices grows
at least exponentially with their instant size.

 This approach makes it possible to solve many large


instances of NP-hard problems in an acceptable amount
of time.

 Backtracking constructs its state-space tree in the depth-


first search fashion in the majority of its applications.
39

Vous aimerez peut-être aussi