Unit 4 Backtracking strategy
General Method
•   Useful technique for optimizing search under some constraints
•   Express the desired solution as an n-tuple (x1; : : : ; xn) where each xi 2 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)
•   Sorting an array a[n]
    – Find an n-tuple where the element xi is the index of ith smallest element in a
    – 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]
                 Brute force approach
Let the size of set Si be mi
   – There are m = m1,m2 ,…,mn n-tuples that satisfy the criterion function P
   – In brute force algorithm, you have to form all the m n-tuples to determine
   the optimal solutions by evaluating against P
                Backtrack approach
Requires less than m trials to determine the solution
   – Form a solution (partial vector) one component at a time, and
   check at every step if this has any chance of success
   –If the solution at any point seems not-promising, ignore it
   –If the partial vector(x1; x2; ______ ; xi) does not yield an optimal
   solution, ignore m i+1_ _ _ mn possible test vectors even without
   looking at them
• Effectively, find solutions to a problem that incrementally builds
  candidates to the solutions, and throw out each partial candidate
  that cannot possibly be completed to a valid solution
• Only applicable to problems which admit the concept of partial
  candidate solution and a relatively quick test of whether the partial
  solution can grow into a complete solution
   – If a problem does not satisfy the above constraint, backtracking
      is not applicable
   – Backtracking is not very efficient to find a given value in an
      unordered list
All the solutions require a set of constraints divided into two
categories: explicit and implicit constraints
                 8-queens problem
•Place eight queens on an 8 *8 chessboard so that no queen attacks
another queen
•A queen attacks another queen if the two are in the same row,
column, or diagonal
• Identify data structures to solve the problem
– First pass: Define the chessboard to be an 8* 8 array
– Second pass: Since each queen is in a different row, define the
   chessboard solution to be an 8-tuple (x1; _____ ; x8),
   where xi is the column for i th queen
• Identify explicit constraints
– Explicit constraints using 8-tuple formulation are Si {1,2,3,4,5,6,7,8},
  1<i<8
– Solution space of 8 8 Tuples
• Identify implicit constraints
– No two xi can be the same, or all the queens must be in different
  columns
– All solutions are permutations of the 8-tuple (1,2,3,4,5,6,7,8)
– Reduces the size of solution space from 8 8 to 8! tuples
• The solution above is expressed as an 8-tuple as 4,6,8,2,7,1,3,5
  Tree Organization of Solution Space
N Queen Problem
                         Sum of Subset
• Given n+1 positive numbers: wi, 1≤ i ≤ n and M, find all subsets of
    wi whose sum is M. For ex.
•    If n = 4, (w1, w2,w3, w4) = (11,13,24,7) and M = 31, then the
    desirable subset are (11,13,7) and (24,7)
• If represented in terms of indices then the solution is
    (1,2,4) and (3,4).
• In general the desired solution is k-tuples (x1, x2,..., xk) ,1≤ i ≤ k and
    different solutions may have different size tuples.
• The explicit constraints require xi to be an integer,
   explicit constraints are xi ={j | j is an integer and 1≤j≤n }
• The implicit constraints require that no two xi be the same (avoid
   (1,2,4) and (1,4,2)) and the sum of the corresponding wi be M.
   Since we wish to avoid generating multiple instances of the same
   set, another implicit constrain imposed is xi < xi+1 , 1≤ i < n.
                   Sum of Subset
Fixed Size Tuple Formulation
Variable Size Tuple Formulation
Terminology
First Method:
As soon as a new child “C”, of a current E-node “R”, is generated, the
   child will become a new Enode. R will become E-node again when
   the subtree has fully been explored. This corresponds to the depth-
   first generation of the problem states.
   DFS with bounding Function Known as Backtracking
Second Method:
In the second method of state generation, E-node remains E-node
   until it is dead. This known as Branch and Bound.
In both methods bounding function is used to kill live nodes without
   generating all their children.
Portion of the tree for 4-queens problems
   while solving it using backtracking
We start with the root node as only live node,
this becomes the E-node and the path is ().
We generated one child. Assume that the
children are generated in the ascending order,
thus node number 2 in the solution space is
generated and path is now (1). This
corresponds to placing queen 1 on column 1.
........
             Recursive Backtracking
• Assume that all answer nodes are to be found and not just one
   – Let (x1; x2;___ ; xi) be the path from root to a node in the state space tree
   – Let T(x1; x2; ___; xi+1 )be the set of all possible values for xi+1 such that
      (x1; x2; ___; xi+1)is also a path to a problem state
   – T(x1; x2; v; xn) =ǿ;
   – Assume a bounding function Bi+1 expressed as a predicate such that if
      Bi+1(x1; x2; ___; xi+1) is false for a path (x1; x2; : : : ; xi+1)from root to a
      problem state, then the path cannot be extended to reach an answer
      node
algorithm backtrack ( k )
// Describes a backtracking process using recursion
// Input: First k-1 values x[1], x[2], ..., x[k-1] of the solution vector
// x[1:n] have been assigned
// x and n are global
// Invoked by backtrack ( 1 );
{
    for each x[k] in T(x[1], ..., x[k-1])
    {
         if ( B_k(x[1], x[2], ..., x[[k]) != 0 )
         {
                  if ( x[1], x[2], ..., x[k] is a path to an answer node )
                  write ( x[1:k] );
                  if ( k < n )
                  backtrack ( k + 1 );
         }
    }
}
         Iterative Backtracking
• The last unresolved call start again; the one
  that continues to examine remaining elements
  assuming only k-2 values have been set. The
  algorithm can be modified to quit if just a
  single solution is found Iterative backtracking
  algorithm
algorithm ibacktrack ( n )
// Iterative backtracking process
// All solutions are generated in x[1:n] and printed as soon as they are found
{
     k = 1;
     while ( k != 0 )
     {
            If ( there remains an untried x[k] in T(x[1], x[2], ..., x[k-1]) and
            B_k( x[1], ...,           x[k] ) is true )
            {
                        if ( x[1], ..., x[k] is a path to an answer node )
                        write ( x[1:k] );
                        k = k + 1; // Consider the next set
            }
            else
            k = k - 1; // Backtrack to the previous set
}
  Efficiency of both the Backtracking
               Algorithm
• Four factors for efficiency of the backtracking
  algorithms
  –   Time to generate the next xk
  –   Number of xk satisfying the explicit constraints
  –   Time for the bounding function Bk
  –   Number of xk satisfying Bk
        The first three relatively independent of the
          problem instance, and the last dependent
                   (Example is in NoteBook)
               N Queen Problem
• If we imagine the squares of the chess board being numbered
  as indices of two dimensional array A[1..n,1..n], then for every
  element on same diagonal running from upper left to lower
  right, each element has same “row-column” value.
• Similarly every element on the same diagonal running from
  upper right to lower left has the same row+column” value.
  Suppose that two queens are placed at (i, j) and (k, l)
  positions.
They are on the same diagonal iff
     i-j=k-l
     or i + j = k + l
     or j - l = i - k
     or j - l = k - i.
  function PLACE(k) returns true if kth queen can
  be placed at x[k]. Its computing time is O(k).
                       Algorithm
Algorithm NQueens(k,n)
   //Using backtracking,this procedure prints all possible placements of
   //n queens on an n*n chessboard so that they are non attacking
{
   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);
         }
   }
}
Algorithm Place(k,i)
{
For j=1 to k-1 do
    if((x[j]=i) //same column or (Abs(x[j]-i)=Abs(j-k)))//same diagonal
    then return false;
Return true;
}
                               Sum of Subset
Algorithm sumofsubset(s,k,r)
{
    //generate the left child. note s+w(k)<=M since Bk-1 is true.
    X{k]=1;
    If (S+W[k]=m) then write(X[1:k]);
    // there is no recursive call here as W[j]>0,1<=j<=n.
    Else if (S+W[k]+W[k+1]<=m) then sum of sub (S+W[k], k+1,r- W[k]);
    //generate right child and evaluate Bk.
    If ((S+ r- W[k]>=m)and(S+ W[k+1]<=m)) then
    {
          X{k]=0;
          sum of sub (S, k+1, r- W[k]);
    }
}
       Graph Coloring Problem
• Let G be a graph and m be any given positive
  integer, we want to discover if the nodes of G
  can be colored in such a way that no two
  adjacent nodes have the same color yet only
  m colors are used (m-colorability decision
  problem).
• The m-colorability optimization problem asks
  for the smallest integer m (chromatic
  number) for which graph can be colored.
• A graph is planner if it can be drawn in a
  plane in such a way that no two edges crosses
  each other.
• 4 color problem asks if a map can be colored
  in such a way that no two regions have the
  same color yet only four colors are needed.
A Map and corresponding Graph
A 4-node graph and all possible 3-
            colorings
                                   Procedure
1.   First create the adjacency matrix graph(1:m,1:n) for a graph, if there is an edge
     between i,j then C(i,j) = 1 otherwise C(i,j) =0.
2.   The Colors will be represented by the integers 1,2,…..m and the solutions will
     be stored in the array X(1),X(2),………..,X(n) ,X(index) is the color, index is the
     node.
3.   He formula is used to set the color is,
     X(k) = (X(k)+1) % (m+1)
4.   First one chromatic number is assigned ,after assigning a number for ‘k’ node,
     we have to check whether the adjacent nodes has got the same values if so
     then we have to assign the next value.
5.   Repeat the procedure until all possible combinations of colors are found.
6.   The function which is used to check the adjacent nodes and same color is,
7.   If(( Graph (k,j) == 1) and X(k) = X(j))
//Global G[1..n, 1..n] of boolean;
//m, n:integer; x[1..n] array of integer;
Algorithm MCOLORING(k:integer);
    repeat
    NEXTVALUE(k); //assigns to x[k] a legal color//
    if (x[k] = 0) then exit; //no new color possible//
    if (k = n) then write (x[1:n]) //at most m colors are assigned to n
    vertices//
    else MCOLORING(k+1)
}until(false)
end.
Algorithm Nextvalue(k)
 // X[1],……X[k-1] have been assigned integer values in
 the range[1,m] such that adjacent values have distinct
 integers. A value for X[k] is        determined in the
 range[0,m].X[k] is assigned the next highest numbers //
 color while maintaining distinctness form the adjacent
 vertices of vertex K. If no such color exists, then X[k] is
 0.
{
    repeat
    {
             X[k] = (X[k]+1)mod(m+1); // next highest color.
              If(X[k]=0) then return;           //All colors have been used.
             For j=1 to n do
              {
             // Check if this color is distinct from adjacent color.
                          If((G[k,j] ≠ 0)and(X[k] = X[j]))
              // If (k,j) is an edge and if adjacent vertices have the same color.
                         Then break;
         }
       if(j=n+1) then return; //new color found.
    } until(false); //otherwise try to find another color.
}
Time Complexity
            HAMILTONIAN CYCLES
• Let G=(V,E) be a connected graph with ‘n’ vertices. A HAMILTONIAN
  CYCLE is a round trip path along ‘n’ edges of G which every vertex
  once and returns to its starting position.
• If the Hamiltonian cycle begins at some vertex V1 belongs to G and
  the vertex are visited in the order of V1,V2…….Vn+1,then the edges
  are in E,1<=I<=n and the Vi are distinct except V1 and Vn+1 which
  are equal.
• Consider an example graph G1.
                                  Procedure:
1.   Define a solution vector X(Xi……..Xn) where Xi represents the I th visited vertex of the
     proposed cycle.
2.   Create a cost adjacency matrix for the given graph.
3.   The solution array initialized to all zeros except X(1)=1,b’coz the cycle should start at
     vertex ‘1’.
4.   Now we have to find the second vertex to be visited in the cycle.
5.   The vertex from 1 to n are included in the cycle one by one by checking 2 conditions,
                    1.There should be a path from previous visited vertex to current vertex.
      2.The current vertex must be distinct and should not have been visited earlier
6.   When these two conditions are satisfied the current vertex is included in the cycle,
     else the next vertex is tried.
7.   When the nth vertex is visited we have to check, is there any path from nth vertex to
     first 8vertex. if no path, the go back one step and after the previous visited node.
8.   Repeat the above steps to generate possible Hamiltonian cycle.
                Algorithm HAMILTONIAN CYCLES
Algorithm Hamiltonian (k)
{
    repeat
    {
         Next value (k)
         If (x (k)=0) then return;
         If k=n then
         write (x1:xn)
         Else Hamiltonian (k+1);
         End if
}until(false)
Algorithm Nextvalue (k)
{
    Repeat
    {
           X [k]=(X [k]+1) mod (n+1); //next vertex
           If (X [k]=0) then return;
           If (G [X [k-1], X [k]] 0) then
          {
                     For j=1 to k-1 do if (X [j]=X [k]) then break; // Check for
    distinction.
                     If (j=k) then      //if true then the vertex is distinct.
                     If ((k<n) or ((k=n) and G [X [n], X [1]] 0)) then return;
          }
} Until (false);
}
       Algorithm 0/1 KnapSack Problem
Algorithm Bknap(k,cp,cw)
// ‘m’ is the size of the knapsack; ‘n’  no.of weights & profits. W[]&P[] are
   the weights & profits
// P[I]/W[I] >P[I+1]/W[I+1].
//fwFinal weights of knapsack.
//fp final max.profit.
//x[k] = 0 if W[k] is not the knapsack,else X[k]=1.
{
        // Generate left child.
         If((W+W[k] m) then
         {
             Y[k] =1;
              If(k<n) then Bnap(k+1,cp+P[k],Cw +W[k])
                If((Cp + p[w] > fp) and (k=n)) then
                  {
                       fp = cp + P[k];
                       fw = Cw+W[k];
                      for j=1 to k do X[j] = Y[j];
              }
    }
//generate Right child
  if(Bound(cp,cw,k) fp) then
  {
        y[k] = 0;
      if(k<n) then Bnap (K+1,cp,cw);
    if((cp>fp) and (k=n)) then
       {
           fp = cp;
            fw = cw;
             for j=1 to k do X[j] = Y[j];
         }
    }
}
    5.7 The 0-1 Knapsack Problem
Backtracking for the 0-1 Knapsack Problem
void checknode (node v)
{ node u;
    if (value(v) is better than best)
        best = value(v);
    if (promising(v))
        for (each child u of v)
             checknode(u);
}
                        Backtracking        5-45
            Nonpromising Nodes
weight:the sum of the weights of the items that
   have been included up to some node.
        weight  W
profit:the sum of the profits of the items
   included up to some node.
Initialization: bound = profit, totweight = weight
                           k 1
                                         If bound 
   totweight  weight   w j
   maxprofit              j i 1
                      k 1                  
   bound   profit   p j   W  totweight  
                                                     pk
   Nonpromising                     
                
                 j i 1
                                                 Capacity availablefor kth item
                                                                                        w
                                                                                        k
           Profit from first k 1 items taken                                     Profit per unit
                                                                                  weight for kth item
                                         Backtracking                                             5-46
                                  Example 5.6
Suppose that n = 4, W=16, and we have the following
                                                              (0,0)
     i    pi      wi         pi/wi                             $0
   1   $40     2       $20                                      0
  2   $30     5       $6                              (1,1)   $115
  3   $50 10          $5                               $40
  4   $10     5       $2                                 2
                                                       $115
Find the solutions.
Sol:
1.  Set maxprofit = 0
2.  Visit node(0,0)
    profit = ?
   weight = ?
3. Visit node (1,1) :profit = ?        weight = ?
                                     bound = ?
                                                                      maxprofit = ?
               bound = ?               Backtracking                               5-47
                      Example 5.6 (Cont’d)
n = 4, W=16,             $40       $30     $50      $10
                  item 1   item 2   item 3   item 4               (0,0)
                          2         5       10       5             $0
4. Visit node(2,1)                                                          0
     profit = ?                                       (1,1)               $115                (1,2)
    weight = ?           maxprofit = ?                 $40                                     $0
5. Visit node (3,1)       bound = ?                      2                                      0
                                                  (2,1)        $115               (2,2)       $82
                                                  $70                             $40
                                                    7                               2
                                                  $115                            $98
                                     (3,1)                 (3,2)          (3,3)                  (3,4)
                                     $120                 $70              $90                $40
                                      17                   7                12                 2
                                                          $80              $98                $50
                                           (4,1)              (4,2) (4,3)             (4,4)
                                            $80            $70         $100         $90
                                             12             7           17           12
                                            $80            $70         $115         $90
                                   Backtracking                                           5-48