5.
Backtracking
Ms. D. Retz Mahima
Research Scholar
Dept. of CSE, Andhra University
Contents
• Backtracking - The General Method
• The 8-Queens Problem
• Sum of Subsets
• Graph Colouring
• Hamiltonian Cycles
• Knapsack problem
Backtracking - The General Method
• Problems which deal with searching for a set of solutions or which ask for an optimal
solution satisfying some constraints can be solved using the back tracking formulation.
• In backtracking algorithms, the solution is represented as an n-tuple (x1, x2, … xn), where xi
is chosen from finite set si. The solution maximizes/minimizes/satisfies a criterion function
P(x1, x2, … xn).
• The basic idea of backtracking is to build a vector and test whether the vector being formed
has any chance of success. If the partial vector generated doesn’t lead to an optimal
solution it can be ignored.
• All the solutions using backtracking are required to satisfy a set of constraints, which may be
explicit or implicit.
• Explicit constraints are rules that restrict each xi to take values only from a given set. For
example,
• xi ≥ 0 or Si = {all non-negative real numbers}
• xi = 0 or 1
• Implicit constraints determine which of the tuples in the solution space tree satisfy the
criterion function.
The 8-Queens Problem
• A classic combinatorial problem is to place eight queens on an 8 x 8 chess
board so that no two "attack," that is, so that no two of them are on the
same row, column, or diagonal.
• Let us number the rows and columns of the chessboard 1 through 8.
• The queens can also be numbered 1 through 8.
• Assume queen i is to be placed on row i.
• All solutions to the 8-queens problem can therefore be represented as 8-
tuples(x1,...,x8), where xi is the column on which queen i is placed.
• The explicit constraints - Si = {1,2,3,4,5,6,7,8}, 1<i<8. Therefore the solution
space consists of 88 8-tuples.
• The implicit constraints - 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.
The 4-Queens Problem
Q1 Q2 Q3 Q4
4 x 4 chessboard
The 4-Queens Problem
Q1
Q1 Q2 Q3 Q4
4 x 4 chessboard
The 4-Queens Problem
Q1 Q1 Q1
Q2 Q2 Q2
Q3 Q3 Q3 Q3
Q4 Q4 Q4 Q4
Placing Q2 Cannot place Q3 Cannot place Q4
The 4-Queens Problem
Q1 Q1 Q1
Q2 Q2
Q3
Q4
Backtrack – Placing Q2 Placing Q3,Q4
Repopsition Q1
One possible solution: <x1=2, x2=4, x3=1, x4=3>
State space tree
The 4-Queens Problem – Possible Tree Organization
The 4-Queens Problem
• The solution space consists of all n! permutations of the n-tuple (1,2,... ,n).
• Figure shows a possible tree organization for the case n = 4.
• A tree such as this is called a permutation tree.
• The edges are labeled by possible values of xi.
• Edges from level 1 to level 2 nodes specify the values for x1.
• Thus, the leftmost subtree contains all solutions with x1 = 1; its leftmost
subtree contains all solutions with x1 = 1 and x2 = 2, and so on.
• Edges from level i to level i +1 are labeled with the values of xi.
• The solution space is defined by all paths from the root node to a leaf node.
• There are 4!= 24 leaf nodes in the tree of Figure.
The 4-Queens Problem: Backtracking procedure
• As a bounding function, we use the obvious criteria that if {x1, x2,…,xi) is the
path to the current E-node, then all children nodes with parent-child labelings
xi+1 are such that (x1,..., xi+1) represents a chessboard configuration in which
no two queens are attacking.
The 4-Queens Problem: Backtracking procedure
Nodes are numbered
in the order in which
they are generated.
A node that gets
killed as a result of
the bounding
function has a B
under it. Contrast
this tree with Figure
shown previously,
which contains
31nodes
Recursive backtracking algorithm
We assume the
existence of bounding
function Bi+1
(expressed as
predicates) such that if
Bi+1(x1,x2,.. Xi+1) is
false for a path
(x1,x2,….xi+1) the root
node to a problem
state, then the path
cannot be extended to
reach an answer node.
Algorithm for N-queens
Algorithm for N-queens
Time complexity
• The solution space tree of 8queens problem contains 88 tuple.
• After implicit constraints the size of solution space is 8!.
• Hence the complexity is O(n!).
The 8-Queens Problem
The 8-Queens Problem
With each choice we kept track of the number
of columns a queen could legitimately be
placed on. These numbers are listed in the
vector beneath each chessboard. The total
number of nodes in the 8-queens state space
tree is:
Sum of Subsets Problem
• Suppose we are given n distinct positive numbers (Weights) and we desire to
find all combinations of these numbers whose sum are m. This is called the
sum of subsets problem.
• For example, if n = 4, (w1, w2,w3,w4) = (11,13,24, 7), and m = 31,then the
desired subsets are (11,13,7) and (24,7).
• We represent the solution vector by giving the indices of these Wi. Now the
two solutions are described by the vectors(1,2, 4) and (3, 4).
• The explicit constraints require xi belongs to {j | j is an integer and 1<j < n}.
• The implicit constraints require that no two be the same and that the sum of
the corresponding wi’s be m.
Sum of Subsets Problem
• The element xi of the solution vector is either 0 or 1 depending on whether
the weight wi is included or not.
• For a node at level i the left corresponds to xi = 1 and right corresponds to xi
= 0.
• Bounding function is true iff
Sum of Subsets Problem - Example
Show the portion Subset Sum
of the state space Initially subset S = { } Sum = 0
tree generated by
function SumOfSub Add 1st element S = {5} Sum = 5 <30 Add next element
while working on
the instance n = 6, S = {5, 10} Sum = 15 <30 Add next element
m = 30, and w[1 :
6] = S = {5, 10,12} Sum = 27 <30 Add next element
{5,10,12,13,15,18}
S = {5,10,12,13} Sum = 40 >30 Sum exceeds 30, Backtrack
S = {5, 10, 12, 15} Sum = 42 > 30 Sum exceeds 30, Backtrack
S = {5,10,12,18} Sum = 45 > 30 Sum exceeds 30, Backtrack
S = {5,10,13} Sum = 28 <30 Add next element
S = {5,10,13,15} 43 >30 Backtrack
S = {5,10,13,18} 46 >30 Backtrack
S = {5,10,15} 30 Sum = 30, Solution obtained.
State space tree
Algorithm
Graph Coloring
• 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-
colorability decision problem
• If d is the degree of the given graph, then it can be colored with d + 1 colors.
• The m-colorability optimization problem asks for the smallest integer m for
which the graph G can be colored. This integer is referred to as the chromatic
number of the graph.
• The problem is to determine all the different ways in which a given graph can
be colored using at most m colors.
• We represent a graph by its adjacency matrix G[1 : n, 1 : n], where G[i,j] = 1 if
(i,j) is an edge of G, and G[i,j] = 0 otherwise.
• The colors are represented by the integers 1,2,..., m and the solutions are
given by the n-tuple (x1,.. ,xn), where xi is the color of node i.
Graph Coloring Example
• The graph of Figure can be
colored with three colors 1, 2, and
3.
• The color of each node is
indicated next to it.
• It can also be seen that three
colors are needed to color this
graph and hence this graph’s
chromatic number is 3.
Graph Coloring Example
• Color the graph using m-coloring
1 problem.
• Since the degree of the graph is 2.
• Hence 3 colors are required.
• State space tree for mcoloring
when n= 3 and m = 3
2 3
• The color of each node is
indicated next to it.
• It can also be seen that three
colors are needed to color this
graph and hence this graph’s
chromatic number is 3.
State space tree
Algorithm
Algorithm
Graph Coloring Time Complexity
• FunctionNextValue (Algorithm) produces the possible colors for xk after
x1 through xk-1 have been defined.
• The main loop of m-Coloring repeatedly picks an element from the set
of possibilities, assigns it to xk, and then calls m-Coloring recursively.
• The total time is bounded = O(nmn)
Hamiltonian Cycle
• Let G = (V, E) be a connected graph with n vertices.
• A Hamiltonian cycle (suggested by Sir William Hamilton) is a round-trip
path along n edges of G that visits every vertex once and returns to its
starting position. In other words if a Hamiltonian cycle begins at some
vertex v1 belongs to G and the vertices of G are visited in the order v1,
v2, v3, ……., vn+1, then the edges (vi, vi+1) are in E, 1< i <n, and the vi
are distinct except for v1 and vn+1 which are equal.
• The graph G1 of Figure contains the Hamiltonian cycle 1,2,8,7,6,5,4, 3,1.
• The graph G2 of Figure contains no Hamiltonian cycle. There is no
known easy way to determine whether a given graph contains a
Hamiltonian cycle.
• The graph may be directed or undirected.
• Only distinct cycles are outputs.
Hamiltonian Cycle
Hamiltonian Cycle
Example: Consider a graph G=(V,E), we have to find the Hamiltonian circuit
using backtracking method.
Hamiltonian Cycle
Initially we start out search with vertex ‘1’ the vertex ‘1’ becomes the root of
our implicit tree. Next we choose vertex ‘2’ adjacent to ‘1’, as it comes first in
numerical order (2, 3, 4).
Hamiltonian Cycle
Next vertex ‘3’ is selected which is adjacent to ‘2’ and which comes first in
numerical order (3,5). Next we select vertex ‘4’ adjacent to ‘3’ which comes first
in numerical order (4, 5).
Hamiltonian Cycle
Next vertex ‘3’ is selected which is adjacent to ‘2’ and which comes first in
numerical order (3,5). Next we select vertex ‘4’ adjacent to ‘3’ which comes first
in numerical order (4, 5).
Hamiltonian Cycle
Next vertex ‘5’ is selected. If we choose vertex ‘1’ then we do not get the
Hamiltonian cycle. The vertex adjacent to 5 is 2, 3, 4 but they are already
visited. Thus, we get the dead end. So, we backtrack one step and remove the
vertex ‘5’ from our partial solution.
Hamiltonian Cycle
The vertex adjacent to ‘4’ are 5,3,1 from
which vertex ‘5’ has already been checked
and we are left with vertex ‘1’ but by
choosing vertex ‘1’ we do not get the
Hamiltonian cycle. So, we again backtrack
one step. Hence we select the vertex ‘5’
adjacent to ‘3’.
The vertex adjacent to ‘5’ are (2,3,4) so
vertex 4 is selected.
Hamiltonian Cycle
Hamiltonian Cycle
The vertex adjacent
to ‘4’ are (1, 3, 5) so
vertex ‘1’ is selected.
Hence we get the
Hamiltonian cycle as
all the vertex other
than the start vertex
‘1’ is visited only
once, 1- 2- 3- 5- 4- 1.
Algorithm – Finding all Hamiltonian cycles
Algorithm – Generating a next vertex
Knapsack Problem
• The solution space for this problem consists of the 2n distinct ways to
assign zero or one values to the xi’s. Thus the solution space is the same
as that for the sum of subsets problem.
• Two possible tree organizations are possible.
• One corresponds to the fixed tuple size formulation and the other to
the variable tuple size formulation.
• Backtracking algorithms for the knapsack problem can be arrived at
using either of these two state space trees. Regardless of which is used,
bounding functions are needed.
• A good bounding function for this problem is obtained by using an
upper bound on the value of the best feasible solution obtainable by
expanding the given live node and any of its descendants.
Knapsack Problem
• A dynamic state space tree can be used for the knapsack problem.
• One method for dynamically partitioning the solution space is based on
trying to obtain an optimal solution using the greedy algorithm.
• We first replace the integer constraint xi = 0 or 1 by the constraint 0 ≤ xi ≤ 1.
This yields the relaxed problem,
• If the solution generated by the greedy method has all xi’s equal to 0 or 1,
then it is also an optimal solution to the original 0/1 knapsack problem.
• Partition the solution space of into two subspaces. In one xi = 0 and in the
other xi= 1.
• In general, at each node Z of the state space tree the greedy algorithm is
used to solve.
Knapsack Problem Example
• Sort items by profit/weight.
• Choosing the left child of a node means that you include an item, and
Choosing the right child means that you do exclude an item.
Example: Apply backtracking algorithm on the following data:
p = {11,21,31,33,43,53, 55,65},
w = {1,11,21,23,33,43,45,55},
m = 110, and n = 8
• The greedy solution corresponding to the root node is
x = {1,1,1,1,1,21/45,0,0}. Its value is 164.88.
The two subtrees of the root correspond to x6 = 0 and x6 = 1,respectively
Knapsack Problem Example
Knapsack Problem Algorithm
Knapsack Problem Algorithm
The Backtracking Algorithm for the 0-1 Knapsack Problem
The number of total nodes in the state space tree for n items = 2 n+1 – 1
Time Complexity - O(2n )
Homework/Assignment
1. Solve n-queens problem using Backtracking
2. Show the portion of the state space tree generated by Sum of
Subsets using backtracking while working on the instance n= 4and
W[1:4] = {1,3,4,5} and M = 8.
3. Consider the graph G, Color the graph using m-coloring problem.
4. Consider a graph G=(V,E), we have to find the Hamiltonian circuit
using backtracking method.