M5_Backtracking Branch-and-Bound
M5_Backtracking Branch-and-Bound
Backtracking, Branch-and-Bound
Hemantha T D
Assistant Professor
2
Introduction cont’d…
3
• terminate a node as soon as it can be guaranteed that no solution to the
problem can be obtained by considering choices that correspond to the
node’s descendants.
• Branch-and-bound is applicable only to optimization problems.
• it is based on computing a bound on possible values of the problem’s
objective function.
• Backtracking applies to non-optimization problems.
• Backtracking constructs tree in DFS order
• Branch-and-bound constructs tree in BFS order
3
Backtracking
4
• The problems that cannot be solved in polynomial time they can be solved
by exhaustive search technique.
• Backtracking is an intelligent variation of exhaustive search , which
constructs solution one component at a time and evaluates such partially
constructed candidates.
• 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, no alternatives for
any remaining component need to be considered.
• the algorithm backtracks to replace the last component of the
partially constructed solution with its next option
Department of ISE Acharya Institute of Technology
4
Backtracking cont’d…
5
• In state space tree the root represents an initial state before the search for
a solution begins. The nodes of the first level in the tree represent the
choices made for the first component of a solution, the nodes of the second
level represent the choices for the second component, and so on.
• A node is said to be promising if it corresponds to a partially constructed
solution that may still lead to a complete solution
• Otherwise its called nonpromising
• Leaves represent either nonpromising dead ends or complete solutions
found by the algorithm.
• statespace tree for a backtracking algorithm is constructed in the manner
of depth first search(DFS).
5
n-Queens Problem
6
• n-queens problem asks us 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.
n=4
n=1 n=2 n=3 1 2 3 4
Q 1 queen 1
2 queen 2
Solution no Solution
3 queen 3
no Solution
4 queen 4
6
N-Queen’s Problem
7
Q Q
x x x x x
Q Q Q
Q Q Q
x x x x x x x
dead node Q
Q
Q
Q
Q
Q
x x x x x x
dead node Q
Q
Q
Department of ISE Acharya Institute of Technology
Q
7 Solution
n-Queens Problem
8
Solution 1 Solution 2
Q Q
Q Q
Q Q
Q Q
8
Hamiltonian Circuit Problem
9
• Hamiltonian Cycle of a given graph with ‘n’ vertices is a path of n+1
vertices in which except first and last vertex all other vertices are distinct.
• Ex: If graph has n vertices- v1,v2, . . ., vn
Then Hamiltonian cycle at v1 is: v1, v2,v3, . . ., vn-1, vn, v1
9
Hamiltonian Circuit Example
10 a
c f
d e
e
Hamiltonian Cycle is
a, b, f, e, c, d, a
e d f c
dead node dead node
d
f
dead node
Department of ISE Acharya Institute of Technology a
10 Solution
Hamiltonian Circuit Example a
11
b
d e
g
f g
Hamiltonian Cycle is
d a, b, e, g, d, f, c, a
e f
c g
dead node
dead node
f
e c
dead node dead node
c
12
Subset-Sum Problem cont’d…
• 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. We can either report this
result and stop or, if all the solutions need to be found, continue by
backtracking to the node’s parent.
• If s is not equal to d, we can terminate the node as nonpromising if either
of the following two inequalities holds:
13
Subset-Sum Problem
S={3, 5, 6, 7} d=15
0
14
3 0
8 3 5 0
14 8 9 3 11 5 6 0
X X X
14+7>15 11+7>15
9+7>15
15 8 10 3
12 5 13 6 7 0
Solution
15
Backtracking-General Remarks
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 problem’s solutions
16
Branch-and-Bound
• An optimization problem seeks to minimize or maximize some objective
function usually subject to some constraints
• A feasible solution is a point in the problem’s search space that satisfies
all the problem’s constraints
• An optimal solution is a feasible solution with the best value of the
objective function
• branch-and-bound requires two additional items:
1. a way to provide, for every node of a state-space tree, a bound on
the best value of the objective function on any solution that can be
obtained by adding further components to the partially constructed
solution represented by the node
2. the value of the best solution seen so far
17
Branch-and-Bound cont’d…
• we can compare a node’s bound value with the value of the best solution
seen so far.
• If the bound value is not better than the value of the best solution seen so
far the node is nonpromising and can be terminated/pruned
• we terminate a search path at the current node in a state-space tree of a
branch-and-bound algorithm for any one of the following three reasons:
1. The value of the node’s bound is not better than the value of the best
solution seen so far.
2. The node represents no feasible solutions because the constraints of the
problem are already violated.
3. The subset of feasible solutions represented by the node consists of a
single point
18
Assignment Problem
• Assigning ‘n’ people to ‘n’ jobs so that the total cost of the assignment
is as small as possible.
• An instance of the assignment problem is specified by an n × n cost matrix
C
• select one element in each row of the matrix so that no two selected
elements are in the same column and their sum is the smallest possible
19
Assignment Problem cont’d…
start
lb=2+3+1+4=10
21
Knapsack Problem cont’d…
• We record the total weight w and the total value v of this selection in the
node, along with some upper bound ub on the value of any subset that
can be obtained by adding zero or more items to this selection.
• upper bound ub is calculated as add v, the total value of the items already
selected, the product of the remaining capacity of the knapsack W − w
and the best per unit payoff among the remaining items, which is
vi+1/wi+1
ub = v + (W − w)(vi+1/wi+1)
22
Knapsack Problem Example
Item Weight Value Value/
Item Weight Value
1 7 42 Weight
2 3 12 1 7 42 6
3 4 40 2 3 12 4
4 5 25 3 4 40 10
Capacity W=10 4 5 25 5
23
Knapsack Problem Example cont’d…
w=0, v=0
ub = v + (W − w)(vi+1/wi+1) ub=100 0+10*10
40+6*6
w=4, v=40 w=0, v=0
Valu
ub=76 ub=60 0+10*6 Ite Weig Val
e/W
X m ht ue
eight
w=11 w=4, v=40 3 4 40 10
ub=70 40+6*5 1 7 42 6
X 4 5 25 5
Not feasible 2 3 12 4
w=9, v=65 w=4, v=40 Capacity W=10
65+1*4 ub=69 ub=64 40+6*4
X
Optimal Solution: {item3, item4}
w=12 w=9, v=65
Total weight=4+5=9
value=65 Total Profit= 40+25=65
X Optimal Solution
Not feasible
Department of ISE Acharya Institute of Technology
24
Traveling Salesman Problem
• We will be able to apply the branch-and-bound technique to instances of
the traveling salesman problem if we come up with a reasonable lower
bound on tour lengths.
• lower bound can be obtained by finding the smallest element in the
intercity distance matrix D and multiplying it by the number of cities n
• For each city i, 1≤ i ≤ n, find the sum si of the distances from city i to the
two nearest cities; compute the sum s of these n numbers, divide the
result by 2, and, if all the distances are integers, round up the result to the
nearest integer:
• For any subset of tours that must include particular edges of a given
graph, we can modify lower bound in above statement accordingly
Department of ISE Acharya Institute of Technology
25
TSP Example a
lb=14
a, b a, c a, d a, e
lb=14 lb=14 lb=16 lb=19
X X X
a, b, c a, b, d a, b, e
lb=16 lb=16 lb=19
X
26
Graph Colouring Problem
• Let G be a graph and m be a positive integer, it asks us to find whether
nodes of graph G can be colored in such a way that no two adjacent nodes
have same color yet only m colors used
27
Graph Colouring Problem cont’d…
• If d is the degree of given graph G, then it can be colored with
(d+1) colors
28
Graph Colouring Example n=4, m=3
1 2 {Red, Green, Blue}
0
4 3 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
29
P, NP and NP-Complete Problems
30
P, NP and NP-Complete Problems cont’d…
• Deterministic Algorithm- an algorithm which, given a particular input,
will always produce the same output, with the underlying machine
always passing through the same sequence of states.
• Nondeterministic algorithm- an algorithm that, even for the same input,
can exhibit different behaviors on different runs
31
P, NP and NP-Complete Problems cont’d…
• choice(X) : chooses any value randomly from the set X.
• failure( ) : denotes the unsuccessful solution.
• success( ) : Solution is successful and current thread terminates.
• Problem Statement : Search an element x on A[1:n] where n>=1, on successful
search return j if a[j] is equals to x otherwise return 0.
Algorithm Nsearch(A,n,key)
{
j= choice(A, n)
if(A[j]==key) then
{
write(j);
success();
}
write(0);
failure();
}
Department of ISE Acharya Institute of Technology
32
P, NP and NP-Complete Problems cont’d…
• Nondeterministic Polynomial(NP) Algorithms- Problem which can't be
solved in polynomial time. But NP problems are checkable/verifiable in
polynomial time
33
P, NP and NP-Complete Problems cont’d…
• Reduction- If we can convert one instance of a problem A into problem B (NP problem) then
it means that A is reducible to B.
34
P, NP and NP-Complete Problems cont’d…
• Now suppose we have a NP-Complete problem R and it is reducible to Q
then Q is at least as hard as R and since R is an NP-hard problem.
therefore Q will also be at least NP-hard , it may be NP-complete also.
35
36
36