2.1 DynamicProgramming
-
Upload
tushar-chauhan -
Category
Documents
-
view
222 -
download
0
Transcript of 2.1 DynamicProgramming
-
8/13/2019 2.1 DynamicProgramming
1/62
-
8/13/2019 2.1 DynamicProgramming
2/62
-
8/13/2019 2.1 DynamicProgramming
3/62
Assembly Line SchedulingProblem
-
8/13/2019 2.1 DynamicProgramming
4/62
-
8/13/2019 2.1 DynamicProgramming
5/62
Step 2: A recursive solution
-
8/13/2019 2.1 DynamicProgramming
6/62
Step 3: Computing the fastest times
-
8/13/2019 2.1 DynamicProgramming
7/62
PRINT-STATIONS (l, n)
-
8/13/2019 2.1 DynamicProgramming
8/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
9/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
10/62
Matrix Chain MultiplicationProblem
-
8/13/2019 2.1 DynamicProgramming
11/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
12/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Step 1: The structure of an optimal parenthesization
let us adopt the notation Aij , where i j, for the matrix that resultsfrom evaluating the product Ai Ai+1 A j.
Ifthe problem is nontrivial, i.e., i < j, then any parenthesization of the
product Ai Ai+1 A j must split the product between A k and A k+1 for someinteger k in the range i k < j. That is, for some value of k, we firstcompute the matrices Aik and A k+1j and then multiply them together to
produce the final product A ij.
The cost of this parenthesization is thus the cost of computing thematrix Aik , plus the cost of computing Ak+1j , plus the cost ofmultiplying them together.
-
8/13/2019 2.1 DynamicProgramming
13/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Step 2: A recursive solution
C i h Th M G Hill C i I P i i i d f d i di l
-
8/13/2019 2.1 DynamicProgramming
14/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Step 3: Computing the optimal costs
-
8/13/2019 2.1 DynamicProgramming
15/62
Cop right The McGra Hill Companies Inc Permission req ired for reprod ction or displa
-
8/13/2019 2.1 DynamicProgramming
16/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright The McGraw Hill Companies Inc Permission required for reproduction or display
-
8/13/2019 2.1 DynamicProgramming
17/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright The McGraw Hill Companies Inc Permission required for reproduction or display
-
8/13/2019 2.1 DynamicProgramming
18/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright The McGraw-Hill Companies Inc Permission required for reproduction or display
-
8/13/2019 2.1 DynamicProgramming
19/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
20/62
Longest Common SubsequenceProblem
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
21/62
Copyright The McGraw Hill Companies, Inc. Permission required for reproduction or display.
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
22/62
Copy g t e cG aw Co pa es, c. e ss o equ ed o ep oduct o o d sp ay.
0 if i=0, or j=0c[i,j] = c[i-1, j-1]+1 if i, j>0 and xi= y j,
max{ c[i-1, j], c[i, j-1]} if i, j>0 and xi y j,
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
23/62
py g p , q p p y
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
24/62
py g p q p p y
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
25/62
-
8/13/2019 2.1 DynamicProgramming
26/62
Optimal Binary Search TreeProblem
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
27/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
28/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
29/62
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
-
8/13/2019 2.1 DynamicProgramming
30/62
-
8/13/2019 2.1 DynamicProgramming
31/62
All Pair Shortest Path
The structure of a shortest path
-
8/13/2019 2.1 DynamicProgramming
32/62
The structure of a shortest path
For the all-pairs shortest-paths problem on a graph G = (V, E), all subpaths of a shortest path should also be shortest paths.
Suppose that the graph is represented by an adjacency matrix W =(wij ). Consider a shortest path p from vertex i to vertex j, and
suppose that p contains at most m edges.
Assuming that there are no negative-weight cycles, m is finite. If i = j, then p has weight 0 and no edges.
If vertices i and j are distinct, then we decompose path p into ,where path p now contains at most m - 1 edges.
p is a shortest path from i to k, and so (i, j) = (i, k) + w kj.
-
8/13/2019 2.1 DynamicProgramming
33/62
A recursive solution to the all-pairs shortest-paths problem
The latter equality follows since w jj = 0 for all j
-
8/13/2019 2.1 DynamicProgramming
34/62
Computing the shortest-path weights bottom up
-
8/13/2019 2.1 DynamicProgramming
35/62
-
8/13/2019 2.1 DynamicProgramming
36/62
-
8/13/2019 2.1 DynamicProgramming
37/62
0/1 Knapsack Problem
-
8/13/2019 2.1 DynamicProgramming
38/62
Given a knapsack with maximum capacity W , and a set S consisting of n items.
Each item i has some weight wi and benefit value bi (all wi , b i and W are integer values)
Problem: How to pack the knapsack to achieve maximum
total value of packed items?
0-1 Knapsack problem
-
8/13/2019 2.1 DynamicProgramming
39/62
W = 20
w i b i
109
85
54
43
32
Items
This is a knapsackMax weight: W = 20
0-1 Knapsack problem:
-
8/13/2019 2.1 DynamicProgramming
40/62
T i
i
T i
i W wb subject tomax
Problem, in other words, is to find
The problem is called a 0 -1 problem, because each itemmust be entirely accepted or rejected.Just another version of this problem is the Fractional
Knapsack Problem , where we can take fractions of items.
-
8/13/2019 2.1 DynamicProgramming
41/62
Defining a Subproblem
If items are labeled 1..n , then a subproblem would be to find anoptimal solution for S k = {items labeled 1, 2, .. k}
Recursive Formula for subproblems
else }],1[],,1[max{ if ],1[
],[k k
k
bwwk Bwk B
wwwk Bwk B
It means, that the best subset of S k that has total weight w is oneof the two:1) the best subset of S k-1 that has total weight w, or 2) the best subset of S k-1 that has total weight w-wk plus the item k
-
8/13/2019 2.1 DynamicProgramming
42/62
The best subset of S k that has the total weight w, either contains item k or not.
First case: wk >w . Item k cant be part of thesolution, since if it was, the total weight would be> w , which is unacceptable
Second case: wk
-
8/13/2019 2.1 DynamicProgramming
43/62
0-1 Knapsack Algorithmfor w = 0 to W
B[0,w] = 0for i = 0 to n
B[i,0] = 0
for w = 0 to Wif w i B[i-1,w]B[i,w] = b
i + B[i-1,w- w
i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
-
8/13/2019 2.1 DynamicProgramming
44/62
Example:
n = 4 (Number of elements)W = 5 (max weight)Elements (weight, benefit):(2,3), (3,4), (4,5), (5,6)
-
8/13/2019 2.1 DynamicProgramming
45/62
11/28/2013 45
Example (2)
for w = 0 to WB[0,w] = 0
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
4
-
8/13/2019 2.1 DynamicProgramming
46/62
11/28/2013 46
Example (3)
for i = 0 to nB[i,0] = 0
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0
4
-
8/13/2019 2.1 DynamicProgramming
47/62
11/28/2013 47
Example (4)
if w i B[i-1,w]
B[i,w] = b i + B[i-1,w- w i]else
B[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0i=1
b i=3w i=2w=1 w-w i =-1
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
-
8/13/2019 2.1 DynamicProgramming
48/62
11/28/2013 48
Example (5)
if w i B[i-1,w] B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0i=1
b i=3w i=2w=2 w-w i =0
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
-
8/13/2019 2.1 DynamicProgramming
49/62
11/28/2013 49
Example (6)
if w i B[i-1,w] B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0i=1
b i=3w i=2w=3 w-w i=1
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
3
-
8/13/2019 2.1 DynamicProgramming
50/62
11/28/2013 50
Example (7)
if w i B[i-1,w] B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0i=1
b i=3w i=2w=4 w-w i=2
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
3
3
-
8/13/2019 2.1 DynamicProgramming
51/62
11/28/2013 51
Example (8)
if w i B[i-1,w] B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0i=1
b i=3w i=2w=5 w-w i=2
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
3
3
3
-
8/13/2019 2.1 DynamicProgramming
52/62
11/28/2013 52
Example (9)
if w i B[i-1,w]B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0i=2
b i=4w i=3w=1 w-w i=-2
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
3
3
3
0
I
-
8/13/2019 2.1 DynamicProgramming
53/62
11/28/2013 53
Example (10)
if w i B[i-1,w]B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0i=2
b i=4w i=3w=2 w-w i=-1
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
3
3
3
0
3
I
-
8/13/2019 2.1 DynamicProgramming
54/62
11/28/2013 54
Example (11)
if w i B[i-1,w] B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0i=2
b i=4w i=3w=3 w-w i=0
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
3
3
3
0
3
4
I
-
8/13/2019 2.1 DynamicProgramming
55/62
11/28/2013 55
Example (12)
if w i B[i-1,w] B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0i=2
b i=4w i=3w=4 w-w i=1
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
3
3
3
0
3
4
4
-
8/13/2019 2.1 DynamicProgramming
56/62
It
-
8/13/2019 2.1 DynamicProgramming
57/62
11/28/2013 57
Example (14)
if w i B[i-1,w]B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W01
2
3
4
5
i 0 1 2 3
0 0 0 0i=3
b i=5w i=4w=1..3
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0
3
3
3
3
00
3
4
4
7
0
3
4
It
-
8/13/2019 2.1 DynamicProgramming
58/62
11/28/2013 58
Example (15)
if w i B[i-1,w] B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W
01
2
3
4
5
i 0 1 2 3
0 0 0 0i=3
b i=5w i=4w=4w- w i=0
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0 00
3
4
4
7
0
3
4
5
3
3
3
3
It
-
8/13/2019 2.1 DynamicProgramming
59/62
11/28/2013 59
Example (15)
if w i B[i-1,w]B[i,w] = b i + B[i-1,w- w i]
else B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W
01
2
3
4
5
i 0 1 2 3
0 0 0 0i=3
b i=5w i=4w=5w- w i=1
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0 00
3
4
4
7
0
3
4
5
7
3
3
3
3
Items:
-
8/13/2019 2.1 DynamicProgramming
60/62
11/28/2013 60
Example (16)
if w i B[i-1,w]B[i,w] = b i + B[i-1,w- w i]
elseB[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // w i > w
00
0
0
0
0
W
01
2
3
4
5
i 0 1 2 3
0 0 0 0i=4
b i=5w i=4w=1..4
Items:1: (2,3)2: (3,4)3: (4,5)4: (5,6)
4
0 00
3
4
4
7
0
3
4
5
7
0
3
4
5
3
3
3
3
-
8/13/2019 2.1 DynamicProgramming
61/62
-
8/13/2019 2.1 DynamicProgramming
62/62