Module5_BCS402
Module5_BCS402
MODULE 5
BACKTRACKING
Syllabus:
Principal idea of backtracking: is to construct solutions by selecting one component at a time and
evaluate such partially constructed candidates as follows:
• If a partially constructed solution can be developed further without violating the problem’s
constraints, it is done by taking the first remaining legitimate option for the next component
• If there is no legitimate option for the next component, then remaining alternate components need
not be considered. In this case the algorithm backtracks to replace the last component of the
partially constructed solution with its next option
The following pseudo code gives the general form of a backtracking algorithm
Algorithm Backtrack(X[1…..i])
// Gives a template of a generic backtracking algorithm
// input: X[1…i] specifies first i promising components of a solution
// output: All the tuples representing the problems solutions
N-Queens problem
Definition: The problem is to place n queens on an n × n chessboard so that no two queens attack each
other by being in the same row or in the same column or on the same diagonal.
For n = 1, the problem has a trivial solution.
For n=2 and n=3, no solution.
For n=4, there are 2 solutions as follows:
Soln 1: (x1, x2, x3, x4)=(2, 4, 1, 3) Soln 2: (x1, x2, x3, x4)=(3, 1, 4, 2)
Solution: The four queen’s problem can be solved by starting with the empty board and then place queen
1 in the first possible position of its row, which is in column 1 of row 1. (1, 1)
To place queen 2 in 2nd row, the first acceptable position is column 3. Queen 2 cannot be placed in
column 1 (because of straight column attack from queen 1) and in column 2 (because of diagonal attack).
(2, 3)
This proves to be a dead end because there is no acceptable position for queen 3. So, the algorithm
backtracks and puts queen 2 in the next possible position at (2, 4). Then queen 3 is placed at (3, 2),
which proves to be another dead end. The algorithm then backtracks all the way to queen 1 and moves it
to (1, 2). Queen 2 then goes to (2, 4), queen 3 to (3, 1), and queen 4 to (4, 3), which is a solution to the
problem. The state-space tree of this search is shown below:
For example: Solve the given subset problem. A = {1, 2, 5, 6, 8} and d = 9. There are two solutions: {1,
2, 6} and {1, 8}.
The root of the tree represents the starting point, with no decisions about the given elements made as yet.
Its left and right children represent, respectively, inclusion and exclusion of s1 in a set being sought.
Similarly, going to the left from a node of the first level corresponds to inclusion of s2 while going to the
right corresponds to its exclusion, and so on. Thus, a path from the root to a node on the ith level of the
tree indicates which of the first I numbers have been included in the subsets represented by that node.
We record the value of s’, the sum of these numbers, in the node. If s’ is equal to d, we have a solution to
the problem. If s’ is not equal to d, we can terminate the node as non-promising if either of the following
two inequalities holds:
Traversal Backtracking traverses the state space tree Branch-and-Bound traverse the tree either
by DFS manner. in DFS or BFS.
Function Backtracking involves feasibility function. Branch-and-Bound involves a bounding
function.
Efficiency Backtracking is more efficient. Branch-and-Bound is less efficient.
Next move Next move from current state can lead to Next move is always towards better
bad choice. solution.
Solution On successful search of solution in state Entire state space tree is search in order to
space tree, search stops. find optimal solution.
Applications Useful in solving N-Queen Problem, Sum Useful in solving Knapsack Problem,
of subset, Hamilton cycle problem, graph Travelling Salesman Problem.
coloring problem
TSP is an example for minimization problem. So we need to calculate lower bound by the formula
lb(lower bound)= s/2 where ‘s’ is sum of two smallest distances from each city.
Lower bound(lb) =
(a,b) lb=14
(a, d) lb= [ (1+5) + (3+6) + (1+2) + (5+3) + (2 +3) ] / 2 = 16
(a, e) lb = [ (1+8) + (3+6) + (1 + 2) + (3 + 4) + (2 + 8)] / 2 = 19
a, b, c lb = [(1+3) + (3+6) + (1 + 6) + (3 + 4) + (2 + 3)] / 2 = 16
a, b, d lb = [(1+3) + (3+7) + (1 + 2) + (3 + 7) + (2 + 3)] / 2 = 16
a, b, e lb = [(1+3) + (3+9) + (1 + 2) + (3 + 4) + (2 + 9)] / 2 = 19
Note: Definitions:
Live node: A node which has been generated and whose children have not yet been generated.
E-node: The live node whose children are currently being generated is called the E-node.
Dead node: A node which is not expanded further.
c^(x): approximate cost used for computing the least cost c(x).
u(x): denotes the upper bond.
LC Branch and Bound solution
LC stands for Least Cost
Problem 1: Consider the knapsack instance n=4, (p1, p2, p3, p4) = (10, 10, 12, 18), (w1, w2, w3, w4) =
(2, 4, 6, 9), and m=15.
Construct state space tree:
The c^(.) and u(.) of the first node is calculated as follows:
To get u(1), the function scans through the objects from left to right. It adds all objects into the knapsack
until the first object that doesn’t fit is encountered. So u(1) = Items 1, 2 and 3 are added to bag.
– = – (10 + 10 + 12)
u(1) = -32
Item 4 cannot be selected, since it exceeds the knapsack capacity.
With x1=1 (with item 1): u(2) and c^(2) = same as u(1) and c^(1)
u(2) = -32
c^(2) = -38
Without item 1: u(2) = -(10 +12) = -22
c^(2) = -22 – (15- (4 + 6))/9 * 18
= -22 – 5 * 2 = -22- 10
= -32
With item 2: Since item 2 is included in all previous steps, it is same as u(2) and c^(2).
u(2) = -32
c^(2) = -38
Without item 2: u(2) = -(10 + 12) = -22
c^(2) = -22 – (15- (2 + 6))/9 * 18
= -22 – 7 * 2 = -22- 14
= -36
With item 3: Since item 2 is included in all previous steps, it is same as u(2) and c^(2).
u(2) = -32
c^(2) = -38
Without item 3: u(2) = -(10 + 10) = -20
c^(2) = -20 – (15- (2 + 4))/9 * 18
= -20 – 9 * 2 = -20- 18
= -38
With item 1, 2, 3 and 4. It is not feasible to add item 4 since it exceeds the maximum capacity of the
knapsack.
With item 1, 2, without item 3 and item 4 : u(2) = -(10 + 10 + 18) = -38
c^(2) = -20 – (15- (2 + 4))/9 * 18
= -20 – 9 * 2 = -20- 18
= -38
With item 1 and 2 and Without item 3 and 4: u(2) = -(10 + 10) = -20
c^(2) = -20 – (15- (2 + 4))/9 * 0
= -20 – 0 = -20
= -20
Soln: node 8 gives the answer (node with minimum –u(.) value). We get maximum profit with item 1, 2,
and 4.
So the answer is (1, 1, 0, 1) with the maximum profit 38.
– = – (10 + 10 + 12)
u(1) = -32
With x1=1 (with item 1): u(2) and c^(2) = same as u(1) and c^(1)
u(2) = -32
c^(2) = -38
Without item 1: u(2) = -(10 +12) = -22
c^(2) = -22 – (15- (4 + 6))/9 * 18
= -22 – 5 * 2 = -22- 10
= -32
With item 2: Since item 2 is included in all previous steps, it is same as u(2) and c^(2).
u(2) = -32
c^(2) = -38
Without item 2: u(2) = -(10 + 12) = -22
c^(2) = -22 – (15- (2 + 6))/9 * 18
= -22 – 7 * 2 = -22- 14
= -36
With item 1, 2, and With item 3: Since item 2 is included in all previous steps, it is same as u(2) and
c^(2).
u(2) = -32
c^(2) = -38
With item 1, 2, 3 and 4: It is not feasible to add item 4 since it exceeds the maximum capacity of the
knapsack.
With item 1, 2, 3 and without 4: same as parent node
u(2) = -32
c^(2) = -38
With item 1, 2, without item 3 and item 4 : u(2) = -(10 + 10 + 18) = -38
c^(2) = -20 – (15- (2 + 4))/9 * 18
= -20 – 9 * 2 = -20- 18
= -38
With item 1 and 2 and Without item 3 and 4: u(2) = -(10 + 10) = -20
c^(2) = -20 – (15- (2 + 4))/9 * 0
= -20 – 0 = -20
= -20
Complexity Classes
In computer science, problems are divided into classes known as Complexity Classes. In complexity
theory, a Complexity Class is a set of problems with related complexity. Complexity classes are useful in
organizing similar types of problems. The problems set can be divided into following types
• Problems that cannot be solved by computers.
• Problems that can be efficiently solved (solved in Polynomial time) by computers.
• Problems for which no efficient solution (only non-polynomial (exponential time) algorithms)
exist.
The common resources required by computers to obtain solution are time and space i.e, how much time
the algorithm takes to solve a problem and the corresponding memory usage.
• The time complexity of an algorithm is used to describe the number of steps required to solve a
problem, but it can also be used to describe how long it takes to verify the answer.
• The space complexity of an algorithm describes how much memory is required for the algorithm
to operate.
• An algorithm having time complexity of the form O(nk) for input n and constant k is called
polynomial time solution. These solutions scale well. On the other hand, time complexity of the
form O(kn) is exponential time.
1. P –class Problems: P class stands for Polynomial Time. It is the collection of decision problems
(problems with a "yes" or "no" answer) that can be solved by a deterministic machine (computers) in
polynomial time. Problems whose solution times are bounded by polynomials of small degree.
Features:
• The solution to P problems is easy to find.
• P is often a class of computational problems that are solvable and tractable. Tractable means that
the problems can be solved in theory as well as in practice. But the problems that can be solved in
theory but not in practice are known as intractable.
Example: Ordered search O(n log n), sorting O(n log n), polynomial evaluation O(n), string editing
O(mn) etc.
• These problems requires vast amount of time to execute even for moderate values of n. Such
problems cannot be solved for large values of ‘n’.
Problems which don’t have polynomial time algorithm are computationally related and can be
classified as follows:
1. NP-Hard Problems: An NP-hard problem is at least as hard as the hardest problem in NP and it is
a class of problems such that every problem in NP reduces to NP-hard. If an NP-hard problem
can be solved in polynomial time, then all NP-complete problems can be solved in polynomial
time.
Features:
• All NP-hard problems are not in NP.
• It takes a long time to check them. This means if a solution for an NP-hard problem is given then
it takes a long time to check whether it is right or not.
• A problem A is in NP-hard if, for every problem L in NP, there exists a polynomial-time
reduction from L to A.
Some of the examples of problems in Np-hard are:
• Halting problem.
• Qualified Boolean formulas.
• No Hamiltonian cycle.
The relation between these problems are explained in the following diagram
Figure: Commonly believed relationship among P, NP, NP-complete, and NP-hard problems
Non-deterministic algorithms
Non deterministic algorithm contains operations whose outcomes are limited to specified sets of
possibilities. The following three functions are used:
1. Choice (S) arbitrarily chooses one of the elements of set S.
2. Failure () signals an unsuccessful completion.
3. Success () signals a successful completion.