Backtracking
Backtracking
General Method:
The solution is based on finding one or more vectors that maximize, minimize, or
satisfy a criterion function P (x1, , xn). Form a solution and check at every step
if this has any chance of success. If the solution at any point seems not promising,
ignore it. All solutions requires a set of constraints divided into two categories: explicit
and implicit constraints.
Definition 1: Explicit constraints are rules that restrict each x i 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.
Definition 2: Implicit constraints are rules that determine which of the tuples in the
solution space of I satisfy the criterion function. Thus, implicit
constraints describe the way in which the xi’s must relate to each other.
Explicit constraints using 8-tuple formation, for this problem are S= {1, 2, 3,
4, 5, 6, 7, 8}.
The implicit constraints for this problem are that no two queens can be the
same (i.e., all queens must be on different columns) and no two queens can be
on the same diagonal.
Backtracking is the procedure whereby, after determining that a node can lead to
nothing but dead end, we go back (backtrack) to the nodes parent and proceed with
the search on the next child.
A backtracking algorithm need not actually create a tree. Rather, it only needs to
keep track of the values in the current branch being investigated. This is the way we
implement backtracking algorithm. We say that the state space tree exists implicitly in
the algorithm because it is not actually constructed.
State space is the set of paths from root node to other nodes. State space tree is the
tree organization of the solution space. The state space trees are called static trees.
This terminology follows from the observation that the tree organizations are
independent of the problem instance being solved. For some problems it is
advantageous to use different tree organizations for different problem instance. In
this case the tree organization is determined dynamically as the solution space is
being searched. Tree organizations that are problem instance dependent are called
42
dynamic trees.
Terminology:
Solution states are the problem states ‘S’ for which the path from the root node to
‘S’ defines a tuple in the solution space.
Answer states are those solution states for which the path from root node to s
defines a tuple that is a member of the set of solutions.
Live node is a node that has been generated but whose children have not yet been
generated.
E-node is a live node whose children are currently being explored. In other words, an
E-node is a node currently being expanded.
Dead node is a generated node that is not to be expanded or explored any further.
All children of a dead node have already been expanded.
Branch and Bound refers to all state space search methods in which all children of
an E-node are generated before any other live node can become the E-node.
Depth first node generation with bounding functions is called backtracking. State
generation methods in which the E-node remains the E-node until it is dead, lead to
branch and bound methods.
N-Queens Problem:
The explicit constraints using this formulation are Si = {1, 2, 3, 4, 5, 6, 7, 8}, 1 < i <
8. Therefore the solution space consists of 88 8-tuples.
The implicit constraints for this problem are that no two xi’s can be the same (i.e., all
queens must be on different columns) and no two queens can be on the same
diagonal.
This realization reduces the size of the solution space from 88 tuples to 8! Tuples.
The promising function must check whether two queens are in the same column or
diagonal:
Suppose two queens are placed at positions (i, j) and (k, l) Then:
Diag 45 conflict: Two queens i and j are on the same 450 diagonal if:
i – j = k – l.
This implies, j – l = i – k
j - l = i – k
Where, j be the column of object in row i for the i th queen and l be the column of
object in row ‘k’ for the kth queen.
To check the diagonal clashes, let us take the following tile configuration:
*
In this example, we have:
*
*
i 1 2 3 4 5 6 7 8
*
* xi 2 5 1 8 4 7 3 6
*
Let us consider for the
*
case whether the queens on 3rd row and 8th row
* are conflicting or not. In this
case (i, j) = (3, 1) and (k, l) = (8, 6). Therefore:
j - l = i – k 1 - 6 = 3 – 8
5 = 5
Example:
*
*
*
*
Step 1:
Add to the sequence the next number in the sequence 1, 2, . . . , 8 not yet
used.
Step 2:
If this new sequence is feasible and has length 8 then STOP with a solution. If
the new sequence is feasible and has length less then 8, repeat Step 1.
Step 3:
If the sequence is not feasible, then backtrack through the sequence until we
find the most recent place at which we can exchange a value. Go back to Step
1.
44
Remarks
1 2 3 4 5 6 7 8
7 5 3 1
j - l = 1 – 2 = 1
7 5 3 1* 2*
i – k = 4 – 5 = 1
7 5 3 1 4
j - l = 7 – 2 = 5
7* 5 3 1 4 2*
i – k = 1 – 6 = 5
j - l = 3 – 6 = 3
7 5 3* 1 4 6*
i – k = 3 – 6 = 3
7 5 3 1 4 8
j - l = 4 – 2 = 2
7 5 3 1 4* 8 2*
i – k = 5 – 7 = 2
j - l = 4 – 6 = 2
7 5 3 1 4* 8 6*
i – k = 5 – 7 = 2
7 5 3 1 4 8 Backtrack
7 5 3 1 4 Backtrack
7 5 3 1 6
j - l = 1 – 2 = 1
7* 5 3 1 6 2*
i – k = 7 – 6 = 1
7 5 3 1 6 4
7 5 3 1 6 4 2
j - l = 3 – 8 = 5
7 5 3* 1 6 4 2 8*
i – k =3 – 8 = 5
7 5 3 1 6 4 2 Backtrack
7 5 3 1 6 4 Backtrack
7 5 3 1 6 8
7 5 3 1 6 8 2
7 5 3 1 6 8 2 4 SOLUTION
*
*
*
*
*
*
*
*
45
4 – Queens Problem:
Let us see how backtracking works on the 4-queens problem. We start with the root
node as the only live node. This becomes the E-node. We generate one child. Let us
assume that the children are generated in ascending order. Let us assume that the
children are generated in ascending order. Thus node number 2 of figure is generated
and the path is now (1). This corresponds to placing queen 1 on column 1. Node 2
becomes the E-node. Node 3 is generated and immediately killed. The next node
generated is node 8 and the path becomes (1, 3). Node 8 becomes the E-node.
However, it gets killed as all its children represent board configurations that cannot
lead to an answer node. We backtrack to node 2 and generate another child, node 13.
The path is now (1, 4). The board configurations as backtracking proceeds is as
follows:
1 1 1 1
. . 2 2 2
. . . . . 3
1 1 1 1
2 . . . 2 2
3 3
. . . . . . 4
(e) (f) (g) (h)
The above figure shows graphically the steps that the backtracking algorithm goes
through as it tries to find a solution. The dots indicate placements of a queen, which
were tried and rejected because another queen was attacking.
In Figure (b) the second queen is placed on columns 1 and 2 and finally settles on
column 3. In figure (c) the algorithm tries all four columns and is unable to place the
next queen on a square. Backtracking now takes place. In figure (d) the second
queen is moved to the next possible column, column 4 and the third queen is placed
on column 2. The boards in Figure (e), (f), (g), and (h) show the remaining steps that
the algorithm goes through until a solution is found.
46
Complexity Analysis:
n 1
n 1
n1
1 n n2 n3 . . . . . . . . . . . n n
47
For the instance in which n = 8, the state space tree contains:
88 1 1 = 19, 173, 961 nodes
8 1
Sum of Subsets:
Given positive numbers wi, 1 ≤ i ≤ n, and m, this problem requires finding all subsets
of wi whose sums are ‘m’.
Explicit constraints:
Implicit constraints:
The above solutions are then represented by (1, 1, 0, 1) and (0, 0, 1, 1).
For both the above formulations, the solution space is 2n distinct tuples.
For example, n = 4, w = (11, 13, 24, 7) and m = 31, the desired subsets are (11,
13, 7) and (24, 7).
The following figure shows a possible tree organization for two possible formulations
of the solution space for the case n = 4.
x 1 =1 1 x 1 =4
x 1 =2 x 1 =3
2 3 4 5
x 2 =4
x 2 =2 x 2 =3 x 2 =4
x 2 =3 x 2 =4
6 7 8 9 10 11
x 3 =3 S
x 3 =4 x 3 =4 x 3 =4
12 13 14 15
S
x 4 =4
16
A p o s s ib le s o lut io n s p ac e org a n is at io n f or t h e
s u m of t h e s u b s et s pro ble m.
The tree corresponds to the variable tuple size formulation. The edges are labeled
such that an edge from a level i node to a level i+1 node represents a value for xi. At
each node, the solution space is partitioned into sub - solution spaces. All paths from
the root node to any node in the tree define the solution space, since any such path
48
corresponds to a subset satisfying the explicit constraints.
The possible paths are (1), (1, 2), (1, 2, 3), (1, 2, 3, 4), (1, 2, 4), (1, 3, 4), (2), (2,
3), and so on. Thus, the left mot sub-tree defines all subsets containing w1, the next
sub-tree defines all subsets containing w2 but not w1, and so on.
49
Graph Coloring (for planar graphs):
Let G be a graph and m be a given positive integer. We want to discover whether 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. This is termed the m-colorabiltiy decision problem.
The m-colorability optimization problem asks for the smallest integer m for which the
graph G can be colored.
Given any map, if the regions are to be colored in such a way that no two adjacent
regions have the same color, only four colors are needed.
For many years it was known that five colors were sufficient to color any map, but no
map that required more than four colors had ever been found. After several hundred
years, this problem was solved by a group of mathematicians with the help of a
computer. They showed that in fact four colors are sufficient for planar graphs.
The function m-coloring will begin by first assigning the graph to its adjacency matrix,
setting the array x [] to zero. The colors are represented by the integers 1, 2, . . . , m
and the solutions are given by the n-tuple (x1, x2, . . ., xn), where xi is the color of
node i.
A recursive backtracking algorithm for graph coloring is carried out by invoking the
statement mcoloring(1);
50
}
if (j = n+1) then return; // New color found
} until (false); // Otherwise try to find another color.
}
Example:
x1
1 3
2
1 2 2 3 1 3 1 2 x2
1 3 1 2 2 x3
3 1 2 2 3 1 3
4 3
Gra p h
x4
2 3 2 2 3 3 1 3 1 3 1 3 1 1 2 2 1 2
Hamiltonian Cycles:
1 2 3 4 1 2 3
8 7 6 5 5 4
Graph G1 Graph G2
51
vertex xn can only be one remaining vertex and it must be connected to
both xn-1 and x1.
}
} until (false);
}
Algorithm Hamiltonian (k)
// This algorithm uses the recursive formulation of backtracking to find all the Hamiltonian
// cycles of a graph. The graph is stored as an adjacency matrix G [1: n, 1: n]. All cycles begin
// at node 1.
{
repeat
{ // Generate values for x [k].
NextValue (k); //Assign a legal Next
value to x [k]. if (x [k] = 0) then return;
if (k = n) then write
(x [1: n]); else
Hamiltonian (k + 1)
} until (false)
52