Professional Documents
Culture Documents
Algorithm Design & Analysis Chapter - 03 (Backtracking & Branch and Bound) T.E (Computer)
Algorithm Design & Analysis Chapter - 03 (Backtracking & Branch and Bound) T.E (Computer)
Chapter -03
(Backtracking & Branch and Bound )
T.E(Computer)
By
I.S Borse
SSVP’S BSD COE ,DHULE
0__ 1__
?
dead end
dead end
?
start ? ?
dead end
dead end
success!
n 2 64
4,426,165,368
n
8
(1,2,3,4,5,6,7,8)
ADA Unit -3 I.S Borse 26
1
2
3
4
5
6
7
8
1 2 3 4 5 6 7 8
(1,2,3,4,5,6,8,7)
ADA Unit -3 I.S Borse 27
1
2
3
4
5
6
7
8
1 2 3 4 5 6 7 8
(1,2,3,4,5,8,6,7)
ADA Unit -3 I.S Borse 28
1
2
3
4
5
6
7
8
1 2 3 4 5 6 7 8
(1,2,3,4,5,8,7,6)
ADA Unit -3 I.S Borse 29
Third Solution Idea
• Don’t place 2 queens in the same row or in the same
column.
• Generate all permutations of (1,2…8)
– Now how many positions must be checked?
n ! 8! 40,320
• We went from C(n2, n) to nn to n!
– And we’re happy about it!
1 2 3 4
x2 = 2
x3 = 3
x4 = 4
1 2 3 4
x3 = 3
x4 = 4
1 2 3 4
x3 = 3 x3 = 4
x4 = 4 x4 = 3
1 2 3 4
x3 = 3 x3 = 4
x4 = 4 x4 = 3
1 2 3 4
A better question:
Is any child of this node ever going to be a solution?
x3 = 3 x3 = 4
x4 = 4 x4 = 3
1 2 3 4
A better question:
Is any child of this node ever going to be a solution?
No. 1 is attacking 2. Adding queens won’t fix the problem.
ADA Unit -3 I.S Borse 37
Four Queens Problem
x1 = 1
x2 = 2
1 2 3 4
1 2 3 4
x3 = 2
1 2 3 4
1 2 3 4
Idaho
Wyoming
= 0(n mn)
ADA Unit -3 I.S Borse 58
Graph Coloring
Example
2-coloring problem
No solution!
3-coloring problem
Vertex Color
v1 color1
v2 color2
v3 color3
v4 color2
• Hamiltonian Circuit
• [v1, v2, v8, v7, v6, v5, v4, v3, v2]
• No Hamiltonian Circuit!
Given
n = number of weights
w = weights
P =profits m= knapsack capacity
Using greedy approach Choosing a subset of the weights such that
i pi wi pi / w i
1 $40 2 $20
2 $30 5 $6
3 $50 10 $5
4 $10 5 $2
3 B
$70 $40
Item 2 [$30, 5] 7 8 2
maxprofit =70
maxprofit = 90
4 $120 $70 $90 $40
17 5 7 9 12 12 2
Item 3 [$50, 10]
F B
17>16
$80 $70 10 $90
6 12 7 7 $100 11 12
Item 4 [$10, 5]
17
maxprofit =80
N N F
17>16 Optimal
ADA Unit -3 I.S Borse 76
Worst-case time complexity
Check number of nodes:
n1
1 2 2 2 ... 2 2
2 3 n
1
Time complexity:
(2 n )
Save the sum of those costs in the bound (as a rough draft).
Can we find a tighter lower bound?
ADA Unit -3 I.S Borse 84
Bound on TSP Tour
1 bound=20
9-8=1 1
8-8=0
2 2
5
6 10
4 3
7 12
3 4
4
For a given vertex, subtract the least cost departure from each
edge leaving that vertex.
ADA Unit -3 I.S Borse 85
Bound on TSP Tour
1 bound=20
1 0
0
2 0
5
0 9
2 0
1 6
3 4
1
999 1 999 0 999 999 1 999 0 999 999 1 999 0 999 999 1 999 0 999
999 999 1 999 0 999 999 1 999 0 999 999 1 999 0 999 999 1 999 0
999 0 999 1 999 999 0 999 1 999 999 0 999 1 999 999 0 999 1 999
999 0 0 999 6 999 0 0 999 6 999 0 0 999 6 999 0 0 999 6
0 999 999 9 999 0 999 999 9 999 0 999 999 9 999 0 999 999 9 999
bound = 21+1
999 1 999 0 999 999 1 999 0 999 999 1 999 0 999 999 1 999 0 999
999 999 1 999 0 999 999 1 999 0 999 999 1 999 0 999 999 1 999 0
999 0 999 1 999 999 0 999 1 999 999 0 999 1 999 999 0 999 1 999
999 0 0 999 6 999 0 0 999 6 999 0 0 999 6 999 0 0 999 6
0 999 999 9 999 0 999 999 9 999 0 999 999 9 999 0 999 999 9 999
bound = 21+1
1to2
1 1
999 999 999 999 999 1 0 1 0
999 999 1 999 0 0
999 999 999 1 999 2 0 2 0
999 999 0 999 6
5 5
9 9
0 999 999 9 999
1 0 0 1
0 6 0 6
bound = 21+1 3 4 3 4
1 1
Add extra cost from 1 to 2, exclude edges from 1 or into 2.
ADA Unit -3 I.S Borse 95
Focus: going from 1 to 2
999 1 999 0 999
999 999 1 999 0
999 0 999 1 999 bound = 21
999 0 0 999 6
0 999 999 9 999
1to2
1 1
999 999 999 999 999 1 0 1 0
999 999 1 999 0 0
999 999 999 1 999 2 0 2 0
999 999 0 999 6
5 5
9 9
0 999 999 9 999
1 0 0 1
0 6 0 6
bound = 21+1+1 3 4 3 4
1 1
No edges into vertex 4 w/ 0 reduced cost.
ADA Unit -3 I.S Borse 96
Focus: going from 1 to 2
999 1 999 0 999
999 999 1 999 0
999 0 999 1 999 bound = 21
999 0 0 999 6
0 999 999 9 999
1to2
1
999 999 999 999 999 1 0
999 999 1 999 0
999 999 999 0 999 2 0
999 999 0 999 6
5
0 999 999 8 999 8
1
0 6
bound = 21+1+1 3 4
0
Add cost of reducing edge into vertex 4.
ADA Unit -3 I.S Borse 97
Bounds for other choices.
999 1 999 0 999
999 999 1 999 0
999 0 999 1 999 bound = 21
1to2(23),1to4(21)
999 0 0 999 6
0 999 999 9 999
999 999 999 999 999 999 999 999 0 999 999 999 999 999 999 999 1 999 0 999
999 999 1 999 0 999 999 1 999 0 999 999 1 999 0 999 999 1 999 0
999 999 999 0 999 999 0 999 1 999 999 0 999 999 999 999 0 999 1 999
999 999 0 999 6 999 0 0 999 6 999 0 0 999 6 999 0 0 999 6
0 999 999 8 999 0 999 999 9 999 0 999 999 999 999 0 999 999 9 999
1 1
1 0 0
0
2 0 2 0
5 5
9
1 1 0 0
0 6 0 6
3 4 3 4
1
bound = 23 bound = 21
b=21
b=21 b=999
• Upper bound theory says that for any arbitrary inputs, we can
always sort in time at most U(n). How long it would take to solve a
problem using one of the known Algorithms with worst-case
input gives us a upper bound.
• Improving an upper bound means finding an algorithm with
better worst-case performance.
• U(n) is the minimum over all known algorithms, of the maximum
complexity.
• Both upper and lower bounds are minima over the maximum
complexity of inputs of size n.
• The ultimate goal is to make these two functions coincide. When
this is done, the optimal algorithm will have L(n) = U(n).
>
a, ba c,d
> >
Aaaa Cc
a b c d
a
x: A(1)
failure x: A(1)
failure x: A(n)
failure failure
A comparison tree for a linear search algorithm
ADA Unit -3 I.S Borse 117
Decision-tree model
Example: By using the Decision Tree method, the lower
bound for comparison-based searching on ordered input
is (lgn)
• Proof: Let us consider all possible comparison trees
which model algorithms to solve the searching problem.
– FIND(n) is bounded below by the distance of the longest
path from the root to a leaf in such a tree.