0% found this document useful (0 votes)
13 views48 pages

13.1.1 Backtracking

The document discusses backtracking as a problem-solving technique that optimizes exhaustive search methods, particularly in scenarios like the N-Queens problem. It explains the basic approach of backtracking, which involves a depth-first recursive search to find solutions while managing constraints. Additionally, it covers explicit and implicit constraints, illustrating how they reduce the solution space and improve efficiency in finding valid arrangements.

Uploaded by

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

13.1.1 Backtracking

The document discusses backtracking as a problem-solving technique that optimizes exhaustive search methods, particularly in scenarios like the N-Queens problem. It explains the basic approach of backtracking, which involves a depth-first recursive search to find solutions while managing constraints. Additionally, it covers explicit and implicit constraints, illustrating how they reduce the solution space and improve efficiency in finding valid arrangements.

Uploaded by

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

BACKTRACKING

Preliminaries
 Exhaustive Search
 What is an exhaustive search?
a trivial but very general problem-solving
technique that consists of:
1)systematically enumerating all possible
candidates for the solution, and
2)checking whether each candidate satisfies the
problem's statement
 aka “brute-force” search

 Brute-force is simple to implement


 And given enough time, it will always find
a solution
Backtracking page 2
Preliminaries
 Exhaustive Search
 Example:
 Let’s say you want to find all the possible
divisors of some natural number, n.
 The exhaustive, brute-force approach would
be to enumerate ALL integers from 1 to n
 and then check whether each of them
divides n without any remainder
 Think of brute-force as searching without
a brain

Backtracking page 3
Preliminaries
 Exhaustive Search
 Benefit of Brute-Force:
 You are guaranteed to find a solution
 Since your algorithm will ultimately try EACH AND
EVERY possible candidate solution, you will find
the real solution
 Negative of Brute-Force:
 Ittakes a LOOOOOOOONG time.
 Sure, your algorithm, in theory, will produce a
solution
 But most likely not in your lifetime!
 Even for average size values of n, the running
time is often computationally prohibitive.
Backtracking page 4
Backtracking
 What is backtracking?
 Often no more than a clever
implementation of an exhaustive search
 BUT, the savings over a brute force
algorithm can be significant
 Backtracking could degenerate, in a
worst case, to a brute force, exhaustive
search
 But in most cases, better cases,
backtracking only checks a subset of
possibilities within the search.
Backtracking page 5
The N-Queens Problem

 Suppose you
have 8 chess
queens...
 ...and a chess
board

Backtracking page 6
The N-Queens Problem

Can the queens


be placed on the
board so that no
two queens are
?each
attacking
other

Backtracking page 7
The N-Queens Problem

Two queens are not


allowed in the
same row...

Backtracking page 8
The N-Queens Problem

Two queens are not


allowed in the
same row, or in
the same column...

Backtracking page 9
The N-Queens Problem

Two queens are not


allowed in the
same row, or in
the same column,
or along the same
diagonal.

Backtracking page 10
The N-Queens Problem

The number of N Queens


queens, and the
size of the board
can vary.

N columns
ws
ro
N

Backtracking page 11
The N-Queens Problem

How to place N
queens on an N x N
chess board?

Backtracking page 12
Backtracking
 N-Queens Problem:
 So how would we do this?
 These slides are about backtracking, so the
answer is obvious. But for now, you don’t
know what this means exactly.
 So what would you do?

 Exhaustive brute force approach:


 Find all possible arrangements of queens
 4,426,165,368 possible arrangements of 8 queens
 See which ones are legal
 Your CPU will cry

…really, it will actually cry.


Backtracking page 13
Backtracking Approach

dead end

?
dead end
dead end

?
start ? ?
dead end

dead end

success!

14
Backtracking page 14
Basic approach
 Based on depth-first recursive search
 Approach:
1. Tests whether solution has been found
2. If found solution, return it
3. Else for each choice that can be made
a. Make that choice
b. Recur
c. If recursion returns a solution, return it
4. If no choices remain, return failure

Backtracking page 15
How the program works

The program
uses a stack
to keep track
of where
each queen
is placed.

Backtracking page 16
How the program works

Each time the


program
decides to
place a queen
on the board,
the
position of the
new queen is ROW 1, COL
stored in a 1

record which
is placed in
the stack.
Backtracking page 17
How the program works

We also have
an integer
variable to
keep track of
how many
rows have
been filled ROW 1, COL
1
so far.

1 filled
Backtracking page 18
How the program works

Each time we
try to place a
new queen in
the next row,
we start by ROW 2, COL
placing the 1

queen in the ROW 1, COL


1
first
column...
1 filled
Backtracking page 19
How the program works

...if there is a
conflict with
another
queen, then
we shift the ROW 2, COL
2
new queen
to the next ROW 1, COL
1
column.

1 filled
Backtracking page 20
How the program works

If another
conflict
occurs, the
queen is
shifted ROW 2, COL
3
rightward
again. ROW 1, COL
1

1 filled
Backtracking page 21
How the program works

When there
are no
conflicts, we
stop and add
one to the ROW 2, COL
3
value of
filled. ROW 1, COL
1

2 filled
Backtracking page 22
How the program works

Let's look at
the third row.
The first ROW 3, COL
position we 1

try has a ROW 2, COL


3
conflict...
ROW 1, COL
1

2 filled
Backtracking page 23
How the program works

...so we shift
to column 2.
But another ROW 3, COL
conflict 2

arises... ROW 2, COL


3

ROW 1, COL
1

2 filled
Backtracking page 24
How the program works

...and we
shift to the
third column. ROW 3, COL
Yet another 3

conflict ROW 2, COL


3
arises... ROW 1, COL
1

2 filled
Backtracking page 25
How the program works

...and we
shift to
column 4.
ROW 3, COL
There's still a 4

conflict in ROW 2, COL


column 4, so 3

we try to ROW 1, COL


1
shift
rightward
again... filled
2
Backtracking page 26
How the program works

...but
there's
nowhere ROW 3, COL
else to go. 4

ROW 2, COL
3

ROW 1, COL
1

2 filled
Backtracking page 27
How the program works

When we run out


of room in a row:
 pop the stack,
 reduce filled by
1
ROW 2, COL
 and continue 3

ROW 1, COL
working on the 1
previous row.
 That is we are
backtracking. 1 filled
Backtracking page 28
How the program works

Now we
continue
working on
row 2,
shifting the ROW 2, COL
4
queen to the
right. ROW 1, COL
1

1 filled
Backtracking page 29
How the program works

This position
has no
conflicts, so
we can
increase ROW 2, COL
4
filled by 1,
and move to ROW 1, COL
1
row 3.

2 filled
Backtracking page 30
How the program works

In row 3, we
start again
at the first ROW 3, COL
column.Ther 1

e is a ROW 2, COL
4
conflict.
ROW 1, COL
1

2 filled
Backtracking page 31
How the program works

Shift queen
to the next
column. This ROW 3, COL
position has 2

no conflicts. ROW 2, COL


4

ROW 1, COL
1

3 filled
Backtracking page 32
How the program works

Now, at
row4, place ROW 4, COL
1
queen in 1st ROW 3, COL
column. 2

There is a ROW 2, COL


4
conflict.
ROW 1, COL
1

3 filled
Backtracking page 33
How the program works

Shift queen
to 2nd ROW 4, COL
2
column. ROW 3, COL
Again a 2

conflict. ROW 2, COL


4

ROW 1, COL
1

3 filled
Backtracking page 34
How the program works

Shift queen
to 3rd ROW 4, COL
3
column. ROW 3, COL
Again a 2

conflict. ROW 2, COL


4

ROW 1, COL
1

3 filled
Backtracking page 35
How the program works

Shift queen
to 4th ROW 4, COL
4
column. ROW 3, COL
Again a 2

conflict. So, ROW 2, COL


4
at this point
we ROW 1, COL
1
backtrack.

Backtracking page 36
How the program works

One of the
solution for ROW 4, COL
3
4-queens ROW 3, COL
problem. 1

ROW 2, COL
4

ROW 1, COL
2

Backtracking page 37
Backtracking- General
Method
 Useful technique for optimizing search under some
constraints
 Express the desired solution as an n-tuple (x1, . . . ,
xn) where each xi ε Si, Si being a finite set
 The solution is based on finding one or more vectors
that maximize, minimize, or satisfy a criterion
function P(x1, . . . , xn)
 Example: Sorting an array a[n]
 Find an n-tuple where the element xi is the index in a of
ith smallest element
 Criterion function is given by a[xi] =< a[xi+1] for 1=< i
<n
 Set Si is a finite set of integers in the range [1,n]
Backtracking page 38
Types of Constraints
 Explicit constraints
 Implicit constraints

Backtracking page 39
Explicit Constraints
 Explicit constraints are rules that restrict each
xi to take on values only from a given set.
 Explicit constraints depend on the particular instance I
of problem being solved
 All tuples that satisfy the explicit constraints define a
possible solution space for I
 Examples of explicit constraints
1. xi >=0, or Si={ all nonnegative real
numbers}
2. xi = 0 or 1 or Si={0, 1}
3. li =< xi =<ui or Si={ a: li =< a =<ui }

Backtracking page 40
Implicit Constraints
 Implicit constraints are rules that
determine which of the tuples in the
solution space of I satisfy the criterion
function.
 Implicit constraints describe the way in which the
xi’s must relate to each other.
 For Example: No two xi’s should have same value
can be one of the constraint.

Backtracking page 41
Example Problem: 4-Queens
Problem
 N-tuple representation: Since each queen is in a different
row, define the chessboard solution to be a 4-tuple (x1, x2, x3,
x4), where xi is the column for ith queen
 Explicit constraints using 4-tuple formulation are Si = {1, 2,
3, 4}, 1=<i=< 4
 Solution space of 44 4-tuples

 Implicit constraints
 No two xi can be the same, or all the queens must be in
different columns
 All solutions are permutations of the 4-tuple (1, 2, 3, 4)

 Reduces the size of solution space from 44 to 4! Tuples.


 No two queens can be on the same diagonal
 The solution discussed earlier for 4 queens problem is
expressed as an 4-tuple as {2,4,1,3}.
Backtracking page 42
State Space Tree
 State Space tree is used to represent solution
space of 4-queens problem.
 This tree is ako permutation tree. So, 4! Leaf
nodes will be there i.e. 24 Possible solutions.
 When we apply implicit constarints to it, number
of nodes are further reduced, Which can be seen
in following diagrams.

Backtracking page 43
x1=1 x1= 4
x1=2
x1= 3

x2= 2 3 4 1 1
3 4 1
2 4 2 3
3 42 4
2 3 3414

4 3 1 2
x1=1 x1= 2

x2=1 X2=4
x2=2 X2=4
X2=3 X2=3

B B X3=1
x3=2 B
x3=4 X3=2 x3=3

B B
B X4=3
X4=3

B
Iterative Algorithm
1. Algorithm Ibacktrack(n)
2. {
3. K:=1;
4. while(k !=0) do
5. {
6. If (there remains untried x[k] ] ε T(x[1], x[2],….x[k-1]) and (Bk(x[1],……x[k]) is
true) then
7. {
8. if (x[1],…..x[k]) is a path to answer node )
9. then write (x[1:k]);
10. k := k+1;
11. }
12. else k:= k-1;
13. }
14. }

Backtracking page 46
References
 http://www.cosc.canterbury.ac.nz
 Wikipedia
 Fundamentals of computer Algorithms by Horowitz,
Sartaj Sahni

Backtracking page 47
Thank You

You might also like