8/2/2019 Ch 8, Backtracking
1/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
1
Analysis of Algorithms
Code: CS-436
Chapter - 08Backtracking
Dr. Zakir H. Ahmed
Phone No. 2582172
8/2/2019 Ch 8, Backtracking
2/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
2
This Chapter Contains the followingTopics:
8/2/2019 Ch 8, Backtracking
3/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
3
8/2/2019 Ch 8, Backtracking
4/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
4
Introduction
Suppose you have to make a series of
decisions, among various choices, where You dont have enough information to know
what to choose
Each decision leads to a new set ofchoices
Some sequence of choices (possibly morethan one) may be a solution to yourproblem
Backtracking is a methodical way of trying outvarious sequences of decisions, until you findone that works
8/2/2019 Ch 8, Backtracking
5/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
5
Solving a maze
Given a maze, find a path from start to finish
At each intersection, you have to decide betweenthree or fewer choices:
Go straight
Go left
Go right
You dont have enough information to choosecorrectly
Each choice leads to another set of choices
One or more sequences of choices may (or maynot) lead to a solution
Many types of maze problem can be solved with
backtracking
8/2/2019 Ch 8, Backtracking
6/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
6
Coloring a map
You wish to color a map with not more than four
colors red, yellow, green, blue
Adjacent countries must be indifferent colors
You dont have enough information to choosecolors
Each choice leads to another set of choices
One or more sequences of choices may (or maynot) lead to a solution
Many coloring problems can be solved withbacktracking
8/2/2019 Ch 8, Backtracking
7/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
7
Solving a puzzle
In this puzzle, all holes but one are filled withwhite pegs
You can jump over one peg with another
Jumped pegs are removed The object is to remove all but the last peg
You dont have enough information to jumpcorrectly
Each choice leads to another set of choices
One or more sequences of choices may (or maynot) lead to a solution
Many kinds of puzzle can be solved withbacktracking
8/2/2019 Ch 8, Backtracking
8/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
8
Backtracking (animation)
start ?
?
dead end
dead end
??
dead end
dead en
?
success!
dead end
8/2/2019 Ch 8, Backtracking
9/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
9
N-Queens Problem
Place n-queens on an nnboard so that no pairof queens attacks eachother, that is, so that notwo of them are on thesame row, column, or
diagonal.
Let us first consider n=4.
Then Variables:
x1, x2 , x3 , x4
Domains:
{1, 2, 3, 4}
Constraints:
xi x
j and
| xi - xj | | i-j|.4321
x1
x2
x3
x4
8/2/2019 Ch 8, Backtracking
10/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
10
Permutation tree for 4-queens problem
x1
x
2
x3
x4
1 2 3 4
(1,2,3,4) (4,3,2,1)(1,2,4,3) (4,3,1,2)
2 3 4
3 4
4 3
21 3
1
21
2
8/2/2019 Ch 8, Backtracking
11/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
11
Backtrack Solution
1 1. . . 2
x1
x2
x3
x4
1
2B
3 4
B B
1. . 2
. . . .
8/2/2019 Ch 8, Backtracking
12/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
12
Backtrack Solution
x1
x2
x3
x4
1 2
(2,4,1,3)
2B
3 4
B
B
4
1
3
B
2
B
B
B
12
. 3
. . . .
1. . . 2
3
. . 4
1. . . 2
8/2/2019 Ch 8, Backtracking
13/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
13
Backtracking Process
Let (x1, x2, , xi) be a path from the root to a
node in a state space tree. Let T(x1, x2, , xi) be the set of all possible
values for xi+1 such that (x1, x2, , xi+1) also apath to a problem state.
T(x1, x2, , xi) = 0.
We assume the existence of bounding functionBi+1 (expressed as predicates) such that ifBi+1(x1, x2, , xi+1) is false for a path (x1, x2, ,xi+1) from the root not to a problem state, thenthe path cant be extended to reach an answernode.
Thus candidates for position i+1 of the solutionvector (x1, x2, , xn) are those values whichare generated by T and satisfy Bi+1.
The algorithm given in next slide presents arecursive formulation of the backtracking
technique.
8/2/2019 Ch 8, Backtracking
14/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
14
Backtracking Algorithm
Following describes the backtracking process
using recursion. On entering, the first k-1 values x[1], x[2], ,
x[k-1] of solution vector x[1:n] have beenassigned. X[ ] and n are global.
Algorithm Backtrack (k){ for (each x[k] T(x[1], x[2], , x[k-1])) do
{ if (Bk(x[1], x[2], , x[k]) 0) then
{ if (x[1], x[2], , x[k] is a path to an
answer node) then
write (x[1:k]);
if (k
8/2/2019 Ch 8, Backtracking
15/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
15
N-Queens Problem
We observed from the 4-queens problem that we
can let (x1, , xn) represent a solution in which xi isthe column of i-th row where the i-th queen isplaced.
Suppose two queens are placed at positions (i,j)and (k,l).
They are in the same diagonal only if
i-j = k-l or i+j = k+l
=>j-l = i-k =>j-l = k-i
Therefore two queens lie on the same diagonal ifand only if |j-l| = |i-k|.
Algorithm Place(k,i) returns a Boolean value that isTRUE if the k-th queen is placed in column i.
It tests both whether i is distinct from all previousvalues x[1], ., x[k-1] and whether there is noother queen on the same diagonal.
Its computing time is O(k-1). Using Algorithm Place(), we can refine the general
backtrack method given in previous slide, and givea precise solution to the n-queens problem.
8/2/2019 Ch 8, Backtracking
16/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
16
An Algorithm
Algorithm Place(k, i)
{ for j:=1 to k-1 doif ((x[j] = i) or (Abs(x[j] - i) = Abs(j k))) then
return FALSE;
return TRUE;
}
Algorithm NQueens(k, n)
{ for i:=1 to n do
if (Place(k,i)) then{ x[k] := i;
if (k = n) then
write (x[1: n]);
else
NQueens(k+1, n);}
}
8/2/2019 Ch 8, Backtracking
17/17
17/05/1433Prepared by Dr. Zakir H.Ahmed
17
Endof
Chapter-08
Top Related