Sec15 dynamic programming

Embed Size (px)

DESCRIPTION

Introduction to Algorithms, Section 15, dynamic programing.

Text of Sec15 dynamic programming

  • 1. Introduction To Algorithms. 15. Dynamic Programming 2010 / 06

2. What is DP ? Divide-and-conquer method Programming tabular method 3. Difference () Divide and conquer : + Dynamic Programming : + 4. Step of DP Characterize Recursively define Compute Construct an optimal solutions 5. Step of DP 6. 7. 15.1 Rod Cutting 8. 15.1 Rod Cutting L 1 23 45 678910P 1 58 9 10 17 17 20 24 30 n = 4 4 , 3-1 , 2-2 , 1-3 , 1-1-2 , 1-2-1 , 2-1-1 , 1-1-1-1 9. 15.1 Rod Cutting 4=1+1+2 n = i1 + i2 + + ik rn = pi1 + pi2 + + pik R 2 7=2+5 5 25. Extended Buttom-up Cut Rod 26. Output N1 2 3 45678910 P1 5 89 10 17 17 20 24 30 R1 5 8 10 13 17 18 22 25 30 S1 2 32 2612310 7=1+6=2+2+3 27. Caution Speed memory , trade offMemoize 28. 15.2 Matrix-chain multiplication < A1 , A2 , A3 , A4 > A1 A2 A3 A4 ( A1 ( A2 ( A3 A4 ) ) ) 29. Matrix Multiply (A, B) #01 A of (n,m) * B of (m, s) = AB of (n, s)for I = 1 to nfor J = 1 to s for K = 1 to m c[I,J] += a[I,K] * b[K,J] 30. Matrix Multiply (A, B) #02 12+2 :2 * 3 = 6 31. Matrix Multiply (A, B) #03 A : ( P , Q ) , B : ( Q , R ) , AB : ( P , R ) : PR : Q , PQR : Q : 32. Matrix Multiply (A, B) #04 A1 : ( 10 , 100 ) , A2 : ( 100 , 5 ) , A3 : (5 , 50 ) A1 A2 A3 : ( 10 , 50 ) A1 A2 : 5000 -> * A3 : 2500 A2 A3 : 25000 -> A1 * : 50000 (A1 A2) A3 : 7500 (edge) -> N vertices, N edges / vertex. -> O(n^2)4 3210 59. Cost Rod-cutting n (Length 0,1,.,n-1) N ( n ) (pn) 60. Without optimal substructure Longest Simple Path (P381) Independent(P383) 61. Overlapping Subproblem Overlapping Subproblem Divide-and-conqure 62. Divide-and-conquer ( quick sort ) Tabular DP 63. Memoize P387-389 64. 65. 15.4 LCS Longest Common Subsequence DNALCS Sequence X := < x1 , x2 , , xm > Subsequence Z of X := < z1 , z2 , , zk > Increasingly 66. Example X= Z= Z X Subsequence ( I = < 2 , 3 , 6 >) 67. Common and Longest Common Sequence Z of X and Y Z X Y Subsequence Longest Common Subsequence Z of X and Y CS 68. Step of DP Characterize Recursively define Compute Construct an optimal solutions 69. Characterize LCS #01 X ( Length m ) -> 2^m X = < x1 , x2 , , xm > Xi = < x1 , x2 , , xi > ( I , Y = < y1 , , yn > Z X Y LCS Z = < z1 , , zk > 71. Theorem 15.1 xm=yn -> zk=xm=yn Zk-1 Xm-1 Yn-1 LCS xm != yn zk != xm -> Z Xm-1 Y LCS zk != yn -> Z X Yn-1 LCS P392 72. Step of DP Characterize Recursively define Compute Construct an optimal solutions 73. C[i][j] : LCS (15.9) C[0][0] = 0 xi = yi -> c[i][j] = c[i-1][j-1] + 1 xi != yi -> c[i][j-1]c[i-1][j] 74. Step of DP Characterize Recursively define Compute Construct an optimal solutions 75. Compute / Imprement P394 For for C[i][j]B[i][j] Step4 76. Step of DP Characterize Recursively define Compute Construct an optimal solutions 77. Constructing P395 78. 15.5 Optimal binary search tree K= NN+1 kipi 79. Binary Search Tree k2 n = 5 k1 k3 K d0d1k4k5 ki pi di qi d2d3d4d5 i0 1 2345pi- 0.15 0.10 0.050.10 0.20qi 0.05 0.10 0.05 0.050.05 0.10 80. Problem kidi n n (depth(ki) 1)pi (depth(di) 1)qi i 1 i 0 nn 1 depth(ki)pi depth(di)qii 1 i 0 81. Problem Step1. ~ Step4. (P399~404) 82. 83. Problem. of DP. #01 Ex15-1 : Longest simple path in a directed acyclic graph Ex15-2 : Longest Palindrome subsequence Ex15-3 : Bitonic euclidean traveling-salesman problem Ex15-4 : Printing neatly 84. Problem. of DP. #02 Ex15-5 : Edit distance Ex15-6 : Planning a company party Ex15-7 : Viterbi algorithm Ex15-8 : Image Compression by seam carving 85. Problem. of DP. #03 Ex15-9 :Breaking a string Ex15-10 : Planning a investment strategy Ex15-11 : Inventory planning Ex15-12 : Signing free-agent baseball players 86. 87. History of DP. 1955. R. Bellman (1920-1984)