Backtracking PDF
Backtracking PDF
Backtracking PDF
children children
Problems solved by Backtracking
Approach or Algorithms
• Example: Maize Problem, N-Queens Problem, Knight’s Tour Problem
N<4
Cannot use N Queens
3
1
State-Space Tree for 4-Queens Problem
Brute Force Search
Soln X = 2 4 1 3 X 3 1 4 2
=
Column no.
https://www.youtube.com/watch?v=0DeznFqrgAI
5-Queens Problem: Many possible
Solutions…
8-Queens Problem
Solutions:
8-Queens Problem: More solutions…
Solution 13:
900 clockwise rotation
of Solution 3
Solution 15:
1800 clockwise/anti-clockwise
rotation of Solution 3
Solution 14:
900 anti-clockwise
rotation of Solution 3
http://www.hbmeyer.de/backtrack/achtdamen/eight.htm#up
N-Queens Problem: Developing a
Backtracking Algorithm
1. Start from the first row.
2. if all queens are placed return “true”
3. Check all columns in the current row
Do following for every tried column
a) if the queen can be placed safely in this column then mark this position
[row,column] as part of the solution and recursively check if placing
queen here leads to a solution.
b) if placing the queen in [row,column] leads to a solution then return “true”
c) if placing queen doesn’t leads to a solution then unmark this position
[row,column],(means Backtrack) and go to step a) to check other columns.
4. If all columns have been checked and nothing worked,
return “false” to trigger backtracking.
N-Queens Problem: Backtracking Algorithm
Approach (Horowitz and Sahni Book)
You observed from the 8-queens problem that we can let (X1, ... , X ) represent a solution where X is the
n i
column of the ith row where the ith queen is placed. The XiS will all be distinct since no two queens can be
placed in the same column. Now, how do we test if two queens are on the same diagonal?
•
Knight’s Tour
demonstration
on 6 x 6 board
3 Possible Solutions
Knight’s Closed Tour vs. Open Tour
• Finding a knight tour is actually
an application of Hamiltonian
Path or cycle.
8 x 8 Board
Knights Tour Problem
Knights Tour Problem
Knights Tour Problem
Valid Move:
A move is valid if it is inside the chessboard (i.e., i and j are
between 1 to N) and if the cell is not already occupied (i.e., sol[i][j] ==
-1). We will make the value of all the unoccupied cells equal to -1.
IS-VALID(i, j, sol) {
if (i>=1 and i<=N and j>=1 and j<=N and sol[i][j]==-1) {
return TRUE
}
return FALSE
}
• Naïve Solution:
1) Consider city 1 as the starting and ending
point.
2) Generate all (n-1)! Permutation of cities.
3) Calculate cost of every permutation and keep
track of minimum cost permutation.
4) Return the permutation with minimum cost.
Step 1: Finding out all possible Hamiltonian cycle in the Graph (having n vertices
which represents n cities) using backtracking approach.
Step 2: Finally, find out the Minimum cost Hamiltonian cycle among all other
Hamiltonian cycles.
Finding all Hamiltonian Cycles using
Backtracking Approach
Check Hamiltonian cycles (manually) in the following Graph:
a
a b
b e c b
e c a d
d
Pendant Vertices
f
d c Junction point
a -> b -> c -> d -> e -> a
f e
a -> c -> d -> e -> b -> a
a -> b -> e -> d -> c -> a
.
.
9
10
1 2
12 11
20
18
3
15 13
5 4 G is the adjacency matrix
X 1 2 3 4 5
X 1 2 5 4 3
[1] [2] [3] [4] [5]
[1] [2] [3] [4] [5]
1
2
3 4 5
4 3 5 4
5 3 https://www.youtube.com/watch?v=dQr4wZCiJJ4
procedure Hamiltonian(k)
while(true) do
NextVertex(k)
if (X[k] == 0)
return
if (k == n)
print(X[1:n])
else
Hamiltonian(k+1)
endif
end while
end Hamiltonian
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
• Consider the directed graph
which has the adjacency
matrix as follows:
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
• Consider the directed graph
which has the adjacency
matrix as follows:
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
Solving TSP by Dynamic
Programming
• Regard a tour to be a simple path that starts and end at vertex
1.
• Every tour consists of an edge <1,k> for some k ∈V – {1} and a
path from k to vertex 1. The path from vertex k to vertex 1
goes through each vertex in V – {1,k} exactly once.
• Let g(i, S) be the length of a shortest path starting at vertex i,
going through all vertices in S and terminating at vertex 1.
• g(1, V- {1}) is the length of an optimal salesperson tour.
• From the principle of optimality it follows that:
g(1, V – {1}) = min {c1k + g(k, V- {1, k})} (1)
2≤k ≤n
• Generalizing from (1) we obtain
g(i, S) = minj∈S {cij + g(j, S - {j})} (2)
0 10 15 20
5 0 9 10
6 13 0 12
8 8 9 0
• A tour of this length may be constructed if we retain with each g(i, S) the
value of j that minimizes the right hand side of (2). Let J(i, S) be this value.
• J(1, {2,3,4}) = 2. Thus the tour starts from 1 and goes to 2. The remaining
tour may be obtained from g(2, {3,4}). J(2, {3,4}) = 4. Thus the next edge is
<2,4>. The remaining tour is for g(4,{3}). J(4, {3}) = 3. The optimal tour is <1,
2, 4, 3, 1>
Complexity of the method
• Let N be the number of g(i, S) that have to be computed before (1) may be
used to compute g(1, V – {1}). For each value of |S| there are n – 1 choices
for i. The number of distinct sets of sets S of size k not including 1 and i is
C(k, n -2).
n-2
• Hence N = Σ (n - 1) C(k, n - 2) = (n - 1)2n-2
k=0
• An algorithm that proceeds
2 n
to find an optimal tour by making use of (1) and
(2) will require O(n 2 ) time since the computation of g(i, S) with |S| = k
requires k -1 comparisons when solving (2).
• It’s better than enumerating all n! different tours to find the best one.