13.1.1 Backtracking
13.1.1 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
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
Backtracking page 7
The N-Queens Problem
Backtracking page 8
The N-Queens Problem
Backtracking page 9
The N-Queens Problem
Backtracking page 10
The N-Queens Problem
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?
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
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
...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
2 filled
Backtracking page 23
How the program works
...so we shift
to column 2.
But another ROW 3, COL
conflict 2
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
2 filled
Backtracking page 25
How the program works
...and we
shift to
column 4.
ROW 3, COL
There's still a 4
...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
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
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
3 filled
Backtracking page 33
How the program works
Shift queen
to 2nd ROW 4, COL
2
column. ROW 3, COL
Again a 2
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
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
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)
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