Back Tracking
Back Tracking
Backtracking: Idea
• Backtracking is a technique used to solve problems with a large
search space, by systematically trying and eliminating
possibilities.
• A standard example of backtracking would be going through a
maze.
– At some point, you might havePortion A of which direction to go:
two options
Junction
Portion B
Backtracking
o n
cti
One strategy would be to try going through Jun
Portion A of the maze.
If you get stuck before you find your Portion B
way out, then you "backtrack" to the
junction.
Portion A
At this point in time you know that Portion A
will NOT lead you out of the maze,
so you then start searching in Portion B
Backtracking
• Clearly, at a single junction you
could have even more than 2
choices.
?
dead end
dead end
?
start ? ?
dead end
dead end
success!
Backtracking
• Dealing with the maze:
– From your start point, you will iterate through each
possible starting move.
– From there, you recursively move forward.
– If you ever get stuck, the recursion takes you back to
where you were, and you try the next possible move.
• It is an empty 8 x 8
chess board. We have
to place the queens in
this board.
Backtracking EXAMPLE—8 Queens
Problem(cont…)
28
Backtracking
• Partial 3-coloring (3 colors) is solved by the following method:
• Color first vertex with 1st color, color next vertex with the next color, check if
those two vertices are adjacent, if not - coloring is legal, proceed to next
vertex, if yes and color is the same – coloring is illegal, try next color for
second vertex. If all colors tried and all colorings are illegal, backtrack, try next
color for previous vertex etc.
• Note: sometimes solution is impossible.
• Exponential O(3^n) complexity is reduced to O(n) on average.
29
Backtracking
• The goal is to color vertices in a graph G={V,E} so that no 2 adjacent vertices
have the same color. Partial 3-coloring problem means only 3 colors are
considered.
• Direct approach builds the tree of ALL possibilities in exponential time.
30
Backtracking
• Partial 3-coloring (3 colors) is solved by the following method:
• Color first vertex with 1st color, color next vertex with the next color, check if
those two vertices are adjacent, if not - coloring is legal, proceed to next
vertex, if yes and color is the same – coloring is illegal, try next color for
second vertex. If all colors tried and all colorings are illegal, backtrack, try next
color for previous vertex etc.
• Note: sometimes solution is impossible.
• Exponential O(3^n) complexity is reduced to O(n) on average.
31
Recursive backtracking Algorithm:
1. Algorithm Backtrack (k)
2. // This schema describes the backtracking
process using recursion.
3. // On entering, the first k-1 values
4. // X[1]. X[2], ... X[ k-1] of the solution vector
5. // x[1:n] have been assigned. X[] and n are
global
Recursive backtracking Algorithm:
6. {
7. For each x[k] E T (x[1],... X[k-1]) do
8. {
9. if Bk (x[1],x[2],... X[k] !=0) then
10. {
11. if (x[1], x[2],... X[k] is a path to answer the code)
12. then write (x[1:k]);
13. If (k<n) then backtrack (k+1)
14. }
15. }
16. }
Iterative backtracking Algorithm:
1. Algorithm Ibacktrack (n)
2. // This schema describes the backtracking
process.
3. // All the solutions are generated in x[1:n] and
printed
4. // as soon as they are determined.
5. {
6. k=1;
7. while (k!=0)
Iterative backtracking Algorithm:
8. {
9. if (there remains an untried x[k] E T)x[1], x[2],..., x[k-1])
and Bk (x[1],..., x[k]) is true) then
10. {
11. if x[1],... X[k] is a path to answer node)
12. Then write ( x[1:k]);
13. K= k+1// Consider the next set
14. }
15. else k= k-1 // Backtrack to the previous track
16. }
17. }
Queen’s Placement Algorithm
1. Algorithm Place (k,i)
2. // Returns true if a queen can be placed in
kth row and ith column.
3. // Otherwise it returns false.
4. // x[] is a global array whose first (k-1) values
have been set.
5. // Abs (r) returns the absolute value of r.
6. {
Queen’s Placement Algorithm
7. For j=1 to k-1 do
8. if (x[j]=i) // two in the same column
9. or Abs9x[j]-i = Abs(j-k)))
10. // or in same digonal
11. then return false;
12. return true
13. }
N-Queen’s Placement Algorithm
1. Algorithm Nqueens (k,n)
2. // Using backtracking, this procedure prints
all
3. // possible placements of n-queens on nxn
4. //chessboard so that they are nonattacking
5. {
6. for i= 1 to n do
7. {
N-Queen’s Placement Algorithm
8. If place(k,i) then
9. {
10. x[k]= i
11. if (k=n) then write (x[1:n])
12. else Nqueens (k+1, n);
13. }
14.}
15.}
Sum of Subsets:
• Suppose we are given n distinct positive numbers
(usually called weights) and we desire to find all
combinations of these numbers whose sums are m.
• This type of problems are called as Sum of subsets
problem.
• A simple choice for a bounding functions is
Bk (x1,... , xk ) = true iff
k n
Ʃ wi xi + Ʃ wi >=m
i=1 i=k+1
Algorithm Sum of Subsets:
1. Algorithm SumOfSub(s,k,r)
2. // Find all subsets of w[1:n] that sum to m.
3. // The values of x[j], 1<-j<k, have already
n
been determined s= Ʃ
Algorithm Sum of Subsets: