Download presentation
Presentation is loading. Please wait.
Published byChester May Modified over 9 years ago
1
Dynamic Programming Greed is not always good.
2
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm2 Outline Elements of dynamic programming Example –Matrix-chain multiplication Memoization More examples –Longest common subsequence –Shortest path –0/1 knapsack
3
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm3 Elements of Dynamic Programming Optimal substructure –An optimal solution to a problem is created from optimal solutions of subproblems. Overlapping subproblems –The optimal solution to a subproblem is reused in solving more than one problem. –The number of subproblems is usually a polynomial of the input size.
4
Matrix-chain Multiplication
5
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm5 Matrix Chain Multiplication Find the order of multiplication for a chain of matrices which use the minimum number of multiplications. Example: Given M 1 M 2 M 3 M 4 of size (8x1), (1x2), (2x7), and (7x5), respectively. M 1 ((M 2 M 3 ) M 4 ) takes 88 multiplications. (M 1 (M 2 M 3 ) ) M 4 takes 350 multiplications. Which is the minimum?
6
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm6 Two-matrix multiplication Let A and B be 2 matrices of size p q and q r, respectively, and C = A B. C is of size p r. q C i,j = A i,k B k,j k = 1 A and B are compatible.
7
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm7 How many multiplication does it take to multiply 2 matrices ? MATRIX-MULTIPLY(A, B) if columns [A ] = rows [B ] then error “incompatible dimensions” return for i = 1 to rows [A ] do for j = 1 to columns [B ] do C [i, j ]= 0 for k = 1 to columns [A] do C [i, j ] = C [i, j ] + A[i, k] B [k, j ] return C rows [A] columns [B] columns [A]
8
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm8 How many multiplication does it take to multiply a matrix chain ? Let M 1, M 2, …, M n be matrices which are compatible. Let p i-1 p i be the size of matrix M i. One way to multiply this matrix chain is to choose some k 1 k < n: –multiply M 1 M 2 … M k, –multiplying M k+1 M 2 … M n, and –multiplying (M 1 M 2 … M k ) (M k+1 M 2 … M n ). It takes T (1, k) +T (k +1, n) +p 0 p k p n multiplications, where –T (i, j ) is the number of ’s used in multiplying (M i M i+1 … M j ).
9
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm9 M1M1 M2M2 M3M3 M4M4 M6M6 M5M5 M = 5x10 10 x 3 3 x 8 8 x 8 8 x 2 2 x 6 5 x 6 M1M2M1M2 M3M3 M 4 M 5 M6M6 M = 5x3 3 x 8 8 x 2 2 x 6 5 x 6 M1M2M1M2 M 3 M 4 M 5 M6M6 M = 5x3 3 x 2 2 x 6 5 x 6 M 1 M 2 M 3 M 4 M 5 M6M6 M = 5x2 2 x 6 5 x 6
10
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm10 Minimum number of multiplications for a matrix-chain multiplication To find the minimum number of multiplications in M 1 M 2 … M n. Try each k, 1 k < n, –find the min. number of ’s for M 1 M 2 … M k, –find the min. number of ’s for M k+1 M 2 … M n, –find the number of ’s for (M 1 M 2 … M k ) (M k+1 M 2 … M n ), and –sum the three amounts. Choose k which yields the smallest number.
11
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm11 Finding the optimal matrix-chain multiplication Let T (i, j ) be the min. number of ’s used in multiplying (M i M i+1 … M j ), where i < j. j-1 –T (i, j ) = min (T (i, k) + T (k +1, j ) + p i -1 p k p j ) k=i T (i, j ) = 0 if i = j. Optimal substructure Overlapping subproblem: T(2,3) is used in finding both T(1,3) and T(2,4).
12
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm12 Algorithm: Recursive MATRIX-CHAIN-ORDER(p, i, j) mincost = for k = i to j -1 docost = MATRIX-CHAIN-ORDER(p, i, k) + MATRIX-CHAIN-ORDER(p, k +1, j ) + p [i -1] p [k ] p [j ] if cost < mincost then mincost = cost return mincost
13
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm13 Algorithm: parenthesise Global: s [n, n] storing the parenthesis point s [i, j] is the point that yields min. ’s for M i M i+1 … M j MATRIX-CHAIN-ORDER(p, i, j) mincost = for k = 1 to j -1 docost = MATRIX-CHAIN-ORDER(p, i, k) + MATRIX-CHAIN-ORDER(p, k +1, j ) + p [i -1] p [k ] p [j ] if cost < mincost then mincost = cost s [i, j ] = k return mincost
14
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm14 Algorithm: Iterative MATRIX-CHAIN-ORDER(p) n = length [p] − 1 for i = 1 to n do m [i, i ] = 0 for l = 2 to n ► l is the chain length do for i = 1 to n − l + 1 do j = i + l − 1 m [i, j ] = for k = i to j − 1 do q = m [i, k]+m [k + 1,j ]+p [i−1] p [k] p [j] if q < m [i, j ] then m [i, j ] = q s [i, j ] = k ► split point return m and s
15
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm15 Iteration 123456 1016 2014 3070 40105 5060 60 08 11 22 37 45 53 64
16
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm16 Iteration 123456 101672 201449 3070100 40105189 5060 60 08 11 22 37 45 53 64 min(0+16+(8x1x7), (0+14+8x2x7)) min(0+14+(1x7x5), (0+70+1x2x5)) min(0+70+(2x5x3), (0+105+2x7x3)) min(0+60+(7x5x4), (0+105+7x3x4))
17
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm17 Iteration 123456 10167289 20144964 3070100124 40105189 5060 60 08 11 22 37 45 53 64 min(0+49+(8x1x5), (16+70+8x2x5), (72+0+8x7x5)) min(0+100+(1x2x3), (14+105+1x7x3), (49+0+1x5x3)) min(0+189+(2x7x4), (70+60+2x5x4), (100+0+2x3x4))
18
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm18 Iteration 123456 1016728988 2014496476 3070100124 40105189 5060 60 08 11 22 37 45 53 64 min(0+64+(8x1x3), (16+100+8x2x3) ), (72+105+8x7x3), (89+0+8x5x3)) min(0+124+(1x2x4), (14+189+1x7x4), (49+60+1x5x4), (64+0+1x3x4))
19
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm19 Iteration 123456 1016728988108 2014496476 3070100124 40105189 5060 60 08 11 22 37 45 53 64 min(0+76+(8x1x4), (16+124+8x2x4) ), (72+189+8x7x4 ), (89+60+8x5x4), (88+0+8x3x4))
20
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm20 MATRIX-CHAIN-ORDER(p) n = length [p] − 1 for i = 1 to n do m [i, i ] = 0 for l = 2 to n ► l is the chain length do for i = 1 to n − l + 1 do j = i + l − 1 m [i, j ] = for k = i to j − 1 do q = m [i, k]+m [k + 1,j ]+p [i−1] p [k] p [j] if q < m [i, j ] then m [i, j ] = q s [i, j ] = k ► split point return m and s n c(n-l+1)(l-1) O(n 2 ) l=2 n-l-1 c(j-i) i=1 = c(n-l+1)(l-1) Complexity: Iterative j-1 c=c(j-i) k=i c c
21
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm21 Memoization NOT memorization Use for recursive algorithm Store known optimal solution in a table to avoid repeatedly solve the same problem. Improve efficiency
22
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm22 Algorithm: Memoize Global: m [n, n] MEMOIZE-MTX-CHN(p) n = length (p)-1 for i = 1 to n do for j = 1 to n do m [i, j ] = return LOOKUP (p, 1, n) LOOKUP (p, i, j) if m [i, j ] < then return m [i, j ] if i = j then m [i, j ] = 0 else for k = 1 to j -1 doq = LOOKUP(p, i, k) + LOOKUP(p, k +1, j) + p [i -1] p [k ] p [j ] if q < m [i, j ] then m [i, j ] = q return m [i, j ]
23
Longest Common Subsequences
24
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm24 Common Subsequences Given sequences X = x 1, x 2, …, x m and Y = y 1, y 2, …, y n . Z = z 1, z 2, …, z k is a subsequence of X if there is a strictly increasing sequence i 1, i 2, …, i k of X such that x i = z j, for all 1 j k. – A, B, D, A is a subsequence of C, A, B, B, C, C, D, A, B, A (sequence: 2, 3,7, 8). Z is a common subsequence of X and Y if Z is a subsequence of X and Y. – A, B, D, A is a common subsequence of C, A, B, B, C, C, D, A, B, A and A, D, C, B, C, D, C, A, B .
25
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm25 Longest-common-subsequence Problem Given sequences X = x 1, x 2, …, x m and Y = y 1, y 2, …, y n . Find a common subsequence of X and Y, which has maximum length. A, B, C, D, A, B is a longest common subsequence (LCS) of C, A, B, B, C, C, D, A, B, A and A, D, C, B, C, D, C, A, B .
26
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm26 Optimal Substructures Let X = x 1, x 2,..., x m and Y = y 1, y 2,..., y n be sequences, and Z = z 1, z 2,..., z k be an LCS of X and Y. If x m = y n, then z k = x m = y n and Z k-1 is an LCS of X m-1 and Y n-1. If x m y n, then z k x m implies that Z is an LCS of X m-1 and Y. If x m y n, then z k y n implies that Z is an LCS of X and Y n-1.
27
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm27 Recursive Solution Let X = x 1, x 2,..., x m and Y = y 1, y 2,..., y n be sequences, and LCS(i, j) denote the longest common subsequence of x 1, x 2,..., x i and y 1, y 2,..., y j . if x m = y n –LCS(m, n) = LCS(m-1,n-1)+ x m. if x m y n –LCS(m, n) = best(LCS(m,n-1), LCS(m-1,n)).
28
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm28 Algorithm: Recursive LCS(X, Y, m, n) if m 0 or n 0 then return( ε ) else if X [m] = Y [n] then return LCS(X, Y, m-1, n-1) || X [m] else p = LCS(X, Y, m, n-1) q = LCS(X, Y, m-1, n) if length(p)>length(q) then return p else return q
29
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm29 Algorithm: Memoize M-LCS(X, Y, m, n) if m 0 or n 0 then return( ε ) else if lcs[m, n] = undefined then if X [m] = Y [n] ► never been calculated before then lcs[m, n]= M-LCS(X, Y, m-1, n-1) || X [m] return lcs[m, n] else p = M-LCS(X, Y, m, n-1) q = M-LCS(X, Y, m-1, n) if length(p)>length(q) then lcs[m, n]= p else lcs[m, n]= q return lcs[m, n]
30
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm30 Iteration CABBCCDABA A D C B C D C A B
31
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm31 Iteration CABBCCDABA A ε AAAAAAAAA D C B C D C A B
32
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm32 Iteration CABBCCDABA A ε AAAAAAAAA D ε AAAAAAD C B C D C A B
33
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm33 Iteration CABBCCDABA A ε AAAAAAAAA D ε AAAAAAD C CAAAAC B C D C A B
34
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm34 Iteration CABBCCDABA A ε AAAAAAAAA D ε AAAAAAD C CAAAAC B CAAB ACB C CAAB ABC D CAAB ABC ABCD C CAAB ABC ABCCABCD A CCAAB ABC ABCCABCD ABCDA B CCACABABABC ABCCABCD ABCDA ABCDAB
35
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm35 Algorithm: Iteration IT-LCS(X, Y ) m = length [X ] n = length [Y ] for i = 0 to mdo lcs [i, 0] = ε for j = 0 to ndo lcs [0, j ] = ε for i = 1 to m do for j = 1 to n do if x [i ] = y [ j ] then lcs [i, j ] = lcs [i−1, j−1] || x [i ] else if length(lcs [i − 1, j ]) ≥ length(lcs [i, j − 1]) then lcs [i, j ] = lcs [i −1, j ] else lcs [i, j ] = lcs [i, j − 1] return lcs [m, n] O(n m)O(n m)
36
Shortest Paths
37
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm37 Problem Given a graph G = (V, E), and two nodes p and q in V, find the shortest path between p and q. Assume that a graph is represented as an adjacency matrix. 02 0-62 058 01 2 0 1 2 3 4 5 2 1 8 -6 52 2
38
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm38 Example 1 2 3 4 5 2 1 8 -6 52 2 10 821
39
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm39 Shortest Path: Optimal substructure Let G be a graph, w ij be the length of edge (i, j), where 1 i, j n, and d (k) ij be the length of the shortest path between nodes i and j, for 1 i, j, k n, without passing through any nodes numbered greater than k. (k) w ij if k = 0 d ij = (k-1) (k-1) (k-1) min(d ij, d ik + d kj ) if k 1
40
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm40 Proof Prove by induction. Basis: Let k=0. d (k) ij = d 0 ij is the length of the shortest path between nodes i and j, without passing through any node. That is, d 0 ij = w ij. Let m be any integer such that 0<m n. Induction hypothesis: For k < m, (k) w ij if k = 0 d ij = (k-1) (k-1) (k-1) min(d ij, d ik + d kj ) if k 1.
41
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm41 Proof Induction step: Prove that (m) (m-1) (m-1) (m-1) d ij = min(d ij, d im + d mj ). i 1 … m-1 2 j m if the shortest path does not pass through any node numbered greater than m-1
42
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm42 Proof Induction step: i 1 … m-1 2 j m if the shortest path passes through node m, but not any node number greater than m.
43
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm43 Recurrence (k) w ij if k = 0 d ij = (k-1) (k-1) (k-1) min(d ij, d ik + d kj ) if k 1
44
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm44 Algorithm: Recursion STP (i, j, k) if i = j d [i, j, k ]=0 if ( d [i, j, k] is undefined and k!=0 ) dod [i, j, k] = STP(i, j, k-1) d1 = STP(i, k, k-1) d2 = STP(k, j, k-1) if (d [i, j,k] > d1+d2 ) then d [i, j, k] = d1+ d2 return d [i, j, k]
45
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm45 Algorithm: Iteration FLOYD-WARSHALL (W) n = rows[W] d[-,-, 0] = W for k = 1 to n do for i = 1 to n do for j = 1 to n do d [i, j, k] = d [i, j, k-1] if d [i, j, k] > d [i, k, k-1] + d [k, j, k-1] d [i,j,k] = d[ i, k, k-1] + d [k, j, k-1] return d
46
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm46 Algorithm: reduced storage FLOYD-WARSHALL (W) n = rows[W] d = W for k = 1 to n do for i = 1 to n do for j = 1 to n do d [i, j] = min (d [i, j], d [i, k] + d [k, j]) return d
47
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm47 for k = 1 to n for i = 1 to n for j = 1 to n d [i, j] = min (d [i, j], d [i, k] + d [k, j]) Sample Run 12345 102 2 0-62 3 058 4 01 5 2 0 1 2 3 4 5 2 1 8 -6 52 2 -7 1 -44 -2 1 3 2 1 68
48
0/1 Knapsack
49
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm49 Problem Given a knapsack with capacity W, and a set of objects i, 1 i n, such that object i has value v i and weight w i. Find the maximum value of objects which can be fitted in the knapsack. 1 W:4 V:4 2 W:5 V:3 3 W:5 V:7 5 W:7 V:8 4 W:2 V:3 Knapsack Capacity: 13 1 W:4 V:4 5 W:7 V:8 4 W:2 V:3 1 W:4 V:4 3 W:5 V:7 4 W:2 V:3
50
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm50 0/1 Knapsack: Optimal Substructure Let m[k, s] be the maximum value of objects i, for 1 i k, that can be fitted in a knapsack with capacity s. m[k, 0] = 0. m[0, s] = 0. If k,s 0: m[k, s] = max (m[k-1, s], m[k-1, s-w k ] + v k ) if s w k. m[k, s] = m[k-1, s] if s < w k.
51
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm51 Recurrence Let m[k, s] be the maximum value of objects i, for 1 i k, that can be fitted in a knapsack with capacity s. m[k, 0] = 0. m[0, s] = 0. If k,s 0: m[k, s] = max (m[k-1, s], m[k-1, s-w k ] + v k ) if s w k. m[k, s] = m[k-1, s] if s < w k.
52
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm52 Algorithm: recursive Globals: v [i ] and w [i ] store values and weight of object i. m[k][s] stores the maximum values of objects that can be stored in a knapsack of size s. sack(k, s) if k=0 or s=0 then return 0 if m[k, s] is undefined then if s > w[k] then m[k, s] = max(sack(k-1,s), sack(k-1, s-w[k])+v[k]) else m[k, s] = sack(k-1,s) return m[k, s]
53
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm53 Algorithm: Iterative Let n be the number of objects, W be the capacity of the knapsack, and m[i, j ] be the maximum value of objects 1…i which can be fitted in a knapsack of capacity j. for j =1 to W do m[0, j ]=0 for i =1 to n do m[i, 0]=0 for j =1 to W do if j w [i ] then m[i, j ] = max(m[i-1,j ], m[i-1, j –w [i ] ] + v [i ]) else m[i, j ] = m[i-1,j ]
54
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm54 Iteration: Sample Run 012345 0000000 1000000 2000033 3000033 4044444 5044777 6044777 7044710 80447 904711 1004711 047 14 12047111415 13047111415 1 W:4 V:4 2 W:5 V:3 3 W:5 V:7 5 W:7 V:8 4 W:2 V:3 for j =1 to W do m[0, j ]=0 for i =1 to n do m[i, 0]=0 for j =1 to W do if j w[i ] then m[i, j ] = max(m[i-1,j ], m[i-1, j -w[i ] ] + v[i ]) else m[i, j ] = m[i-1,j ]
55
Jaruloj Chongstitvatana2301681 Design and Analysis of Algorithm55 sack(k, s) if k=0 or s=0 then return 0 if m[k, s] is undefined then if s > w[k] then m[k, s] = max(sack(k-1,s), sack(k-1, s-w [k])+v [k]) else m[k, s] = sack(k-1,s) return m[k, s] Memoize: Sample Run 012345 0000000 1000 20 300 40 50 604477 70 8044 90 100 11047 120 13047111415 1 W:4 V:4 2 W:5 V:3 3 W:5 V:7 5 W:7 V:8 4 W:2 V:3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.