# 第四章 Dynamic Programming 技术

## 第四章 Dynamic Programming 技术

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. 第四章 • Dynamic Programming技术 邹权（博士） 计算机科学系

2. 4.1 Introduction • Fibonacci number F(n) 1 if n = 0 or 1 F(n-1) + F(n-2) if n > 1 F(n) = Pseudo code for the recursive algorithm: F(n) 1 ifn=0 or n=1 thenreturn 1 2elsereturn F(n-1) + F(n-2)

3. The execution of F(7) F7 F6 F5 F5 F4 F4 F3 F3 F2 F3 F2 F2 F1 F0 F1 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F0 F1 F1 F0 F2 F1 F1 F0

4. The execution of F(7) F7 F6 F5 F5 F4 F4 F3 F3 F2 F3 F2 F2 F1 F0 F1 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 Computation of F(2) is repeated 8 times! F0 F1 F1 F0 F2 F1 F1 F0

5. The execution of F(7) F7 F6 F5 F5 F4 F4 F3 F3 F2 F3 F2 F2 F1 F0 F1 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 Computation of F(3) is also repeated 5 times! F0 F1 F1 F0 F2 F1 F1 F0

6. The execution of F(7) F7 F6 F5 F5 F4 F4 F3 F3 F2 F3 F2 F2 F1 F0 F1 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 Many computations are repeated!! How to avoid this? F0 F1 F1 F0 F2 F1 F1 F0

7. Memorization Store F1(i) somewhere after we have computed its value Afterward, we don’t need to re-compute F1(i); we can retrieve its value from our memory. F1(n) 1 ifv[n] < 0 then 2 v[n] ←F1(n-1)+F1(n-2) 3 return v[n] Idea for improvement Main() 1 v[0] = v[1] ←1 2 fori ← 2 tondo 3 v[i] = -1 4 output F1(n)

8. Look at the execution of F(7) F(i)=Fi F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

9. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

10. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

11. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

12. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

13. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

14. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

15. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

16. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

17. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

18. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

19. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

20. Look at the execution of F(7) F7 v[0] v[1] v[2] v[3] v[4] v[5] v[6] v[7] F6 F5 F5 F4 F4 F3 F2 F2 F3 F3 F2 F1 F1 F0 F1 F0 F1 F0 F4 F3 F2 F2 F1 F1 F1 F0 F1 F0 F2 F2 F3 F1 F1 F1 F0 F0 F2 F1 F1 F0

21. Can we do even better? • Observation • The 2nd versionstill make many function calls, and each wastes times in parameters passing, dynamic linking, ... • In general, to compute F(i), we need F(i-1) &F(i-2) only • Idea to further improve • Compute the values in bottom-up fashion. • That is, compute F(2) (we already know F(0)=F(1)=1), then F(3), then F(4)… F2(n) 1 F[0] ← 1 2 F[1] ← 1 3 fori← 2 tondo 4 F[i] ← F[i-1] + F[i-2] 5 returnF[n] This new implementation saves lots of overhead.

22. Recursive vs Dynamic programming Too Slow! Recursive version: F(n) 1 ifn=0 or n=1 thenreturn 1 2elsereturn F(n-1) + F(n-2) Dynamic Programmingversion: F2(n) 1 F[0] ← 1 2 F[1] ← 1 3 fori← 2 tondo 4 F[i] ← F[i-1] + F[i-2] 5 returnF[n] Efficient! Time complexity is O(n)

23. Summary of the methodology • Write down a formula that relates a solution of a problem with those of sub-problems.E.g. F(n) = F(n-1) + F(n-2). • Index the sub-problems so that they can be stored and retrieved easily in a table (i.e., array) • Fill the table in some bottom-up manner; start filling the solution of the smallest problem. • This ensures that when we solve a particular sub-problem, the solutions of all the smaller sub-problems that it depends are available. For historical reasons, we call such methodology Dynamic Programming. In the late 40’s (when computers were rare),programming refers to the "tabular method".

24. Dynamic programming VSDivide-and-conquer • Divide-and-conquer method 1. Subproblem is independent. 2. Subproblem is solved repeatedly. • Dynamic programming (DP) 1. Subproblem is not independent. 2. Subproblem is just solved once. • Common:Problem is partitioned into one or more subproblem, then the solution of subproblem is combined. • DP reduces computation by • Solving subproblems in a bottom-up fashion. • Storing solution to a subproblem the first time it is solved. • Looking up the solution when subproblem is met again.

25. S1[1] S1[2] S1[3] S1[n-1] S1[n] Line 1 a1[2] a1[3] a1[n-1] a1[n] a1[1] e1 t1[n-1] x1 t1[1] t1[2] Begin … t2[1] t2[2] t2[n-1] x2 e2 a2[n-1] Line 2 a2[1] a2[2] a2[3] a2[n] S2[1] S2[2] S2[3] S2[n-1] S2[n] 4.2 Assembly-line scheduling Si[j] :The jth station on line i ( i=1 or 2) . ai[j] :The assembly time required at station Si[j]. ei :Entry time. xi: Exit time. End

26. 1 0 0 1 1 1 2 3 4 n 0 if choosing line 2 at step j (= 3) 1 if choosing line 1 at step j (= n) One Solution • Brute force • Enumerate all possibilities of selecting stations • Compute how long it takes in each case and choose the best one • Problem: • There are 2n possible ways to choose stations • Infeasible when n is large

27. fi[j] = the fastest time to get from the starting point through station Si[j] • Let f * denote fastest time to get a chassis all the way through the factory. • j = 1 (getting through station 1) f1[1] = e1 + a1[1] f2[1] = e2 + a2[1] f * = min( f1[n]+x1 , f2[n]+x2 ) Line 1 a1[2] a1[3] a1[n-1] a1[n] a1[1] e1 t1[n-1] x1 t1[1] t1[2] Begin … t2[1] t2[2] t2[n-1] x2 e2 a2[n-1] Line 2 a2[1] a2[2] a2[3] a2[n]

28. 2. A Recursive Solution (cont.) • Compute fi[j] for j = 2, 3, …,n, and i = 1, 2 • Fastest way through S1[j] is either: • fastest way through S1[j-1]then directly through S1[j], or f1[j] = f1[j - 1] + a1[j] • fastest way through S2[j-1], transfer from line 2 to line 1, then through S1[j] f1[j] = f2[j -1] + t2[j-1] + a1[j] f1[j] = min(f1[j - 1] + a1[j], f2[j -1] + t2[j-1] + a1[j]) S1[j-1] S1[j] a1[j-1] a1[j] t2[j-1] a2[j-1] S2[j-1]

29. The recursive equation • For example, n=5: • Solving top-down would result in exponential running time 1 2 3 4 5 f1[j] f1[1] f1[2] f1[3] f1[4] f1[5] f2[j] f2[1] f2[2] f2[3] f2[4] f2[5] 2 times 4 times

30. increasing j 3. Computing the Optimal Solution • For j≥ 2, each value fi[j] depends only on the values of f1[j – 1] and f2[j - 1] • Compute the values of fi[j] • in increasing order of j • Bottom-up approach • First find optimal solutions to subproblems • Find an optimal solution to the problem from the subproblems 1 2 3 4 5 f1[j] f2[j]

31. increasing j Additional Information • To construct the optimal solution we need the sequence of what line has been used at each station: • li[j] – the line number (1, 2) whose station (j - 1) has been used to get in fastest time through Si[j], j = 2, 3, …, n • l* - the line whose station n is used to get in the fastest way through the entire factory 2 3 4 5 l1[j] l2[j]

32. Step 3: Computing the fastest way DPFastestWay(a, t, e, x, n) 1 f1[1] ← e1 + a1[1]; f2[1] ←e2 + a2[1] 2 for j ← 2 to n do 3 if f1[j - 1] + a1[j]≤ f2 [j - 1] + t2[j-1] + a1[j] then 4 f1[j] ← f1[j - 1] + a1[j] 5 l1[j] ← 1 6 else f1[j] ← f2 [j - 1] + t2[j-1] + a1[j] 7 l1[j] ← 2 8 if f2[j - 1] + a2[j]≤ f1[j - 1] + t1[j-1] + a2[j]then 9 f2[j] ← f2[j - 1] + a2[j] 10 l2[j] ← 2 11 else f2 [j] ← f1[j - 1] + t1[j-1]+ a2[j]then 12 l2[j] ← 1 13 if f1[n] + x1≤f2[n] + x2then 14 f* ← f1[n] + x1 15 l* ← 1 16 else f* ← f2[n] + x2 17 l*← 2 Running time: (n)

33. 3 7 9 4 8 4 2 3 2 3 1 3 4 2 1 2 1 2 2 4 6 8 5 4 5 7 For example End Begin 1 j 2 3 4 5 6 j 2 3 4 5 6 9 18 20 24 32 35 1 2 1 1 2 f1[j] l1[j] 12 16 22 25 30 37 1 2 1 2 2 f2[j] l2[j] f *=38 l *=1

34. 4. Construct an Optimal Solution PrintStations(l, n) 1 i ← l* 2 print “line ” i “, station ” n 3 for j ← ndownto 2 do 4 i ←li[j] 5 print “line ” i “, station ” j - 1 line 1, station 6 line 2, station 5 line 2, station 4 line 1, station 3 line 2, station 2 line 1, station 1 j 2 3 4 5 6 1 2 1 1 2 l1[j] l *=1 1 2 1 2 2 l2[j]

35. The fastest assembly way f *=38 3 7 9 4 8 4 3 2 3 1 3 4 2 End Begin 2 1 2 1 2 2 4 6 8 5 4 5 7

36. 4.3 Matrix-chain multiplication • Problem: Given a chain A1, A2, . . . , An of n matrices, where for i = 1, 2, . . . , n , matrix Aihas dimension pi-1 × pi, fully parenthesize the product A1A2...An in a way that minimizes the number of scalar multiplications. A1  A2  Ai  Ai+1  An p0 ×p1p1 ×p2pi-1 ×pipi×pi+1pn-1 ×pn A product of matrices is fully parenthesizedif it is either a single matrix or the product of two fully parenthesized matrix products, surrounded by parentheses.

37. For example, if the chain of matrices is <A1, A2, A3, A4>, the product A1A2A3A4 can be fully parenthesized in five distinct ways: (A1 (A2 (A3A4))) , (A1 ((A2A3) A4)) , ((A1A2) (A3A4)) , ((A1 (A2A3)) A4) , (((A1A2) A3) A4).

38. MatrixMultiply(A,B) 1 ifm≠pthen 2 print “Two matrices cannot multiply" 3 else for i←1 ton 4 forj←1 toqdo 5 C[i, j]←0 6 for k ←1 tomdo 7 C[i ,j]←C[i ,j] + A[i, k] B[k, j] 8 return C Running time: (nmq)

39. Consider the problem of a chain A1, A2, A3 of three matrices. Suppose that the dimensions of the matrices are 10×100, 100×5, and 5×50, respectively. 1. ((A1A2) A3): A1A2 = 10×100×5 = 5,000 (10×5) ((A1 A2) A3) = 10×5×50 = 2,500 Total: 7,500 scalar multiplications 2. (A1 (A2A3)): A2A3 = 100×5×50 = 25,000 (100×50) (A1 (A2A3)) = 10×100×50 = 50,000 Total: 75,000 scalar multiplications one order of magnitude difference!!

40. 1. The structure of an optimal parenthesization • Notation: Ai…j= AiAi+1 Aj, i  j • For i < j: Ai…j = AiAi+1 Aj = AiAi+1 AkAk+1  Aj = Ai…kAk+1…j

41. 2. A recursive solution • Subproblem: determine the minimum cost of parenthesizing Ai…j = Ai Ai+1 Ajfor 1  i j  n • Let m[i, j] = the minimum number of multiplications needed to compute Ai…j • Full problem (A1..n): m[1, n]

42. pi-1pkpj m[i, k] m[k+1,j] 2. A Recursive Solution (cont.) • Consider the subproblem of parenthesizing Ai…j = AiAi+1 Ajfor 1  i j  n = Ai…kAk+1…j for i k < j • m[i, j] = the minimum number of multiplications needed to compute the product Ai…j m[i, j] = m[i, k] + m[k+1, j] + pi-1pkpj # of multiplications to compute Ai…kAk…j min # of multiplications to compute Ai…k min # of multiplications to compute Ak+1…j

43. 2. A Recursive Solution (cont.) m[i, j] = m[i, k] + m[k+1, j] + pi-1pk pj • We do not know the value of k • There are j–i possible values for k: k = i, i+1, …, j-1 • Minimizing the cost of parenthesizing the product AiAi+1 Aj becomes:

44. 3. Computing the Optimal Costs How many subproblems do we have? • Parenthesize Ai…j for 1  i  j  n • One problem for each choice of i and j • A recurrent algorithm may encounter each subproblem many times in different branches of the recursion  overlapping subproblems • Compute a solution using a tabular bottom-up approach  (n2)

45. Reconstructing the Optimal Solution • Additional information to maintain: • s[i, j] = a value of k at which we can split the product AiAi+1 Aj in order to obtain an optimal parenthesization

46. 3. Computing the Optimal Costs (cont.) • How do we fill in the tables m[1..n, 1..n] and s[1..n, 1..n] ? • Determine which entries of the table are used in computing m[i, j] • m[i, j] = cost of computing a product of j – i – 1 matrices • m[i, j] depends only on costs for products of fewer than j – i – 1 matrices Ai…j= Ai…kAk+1…j • Fill in m such that it corresponds to solving problems of increasing length

47. second first m[1, n] gives the optimal solution to the problem 3. Computing the Optimal Costs(cont.) • Length = 0: i = j, i = 1, 2, …, n • Length = 1: j = i + 1, i = 1, 2, …, n-1 n 1 2 3 n j Compute rows from bottom to top and from left to right In a similar matrix s we keep the optimal values of k 3 2 1 i

48. Example: min {m[i, k] + m[k+1, j] + pi-1 pk pj} k = 2 m[2, 2] + m[3, 5] + p1p2p5 m[2, 3] + m[4, 5] + p1p3p5 m[2, 4] + m[5, 5] + p1p4p5 m[2, 5] = min k = 3 k = 4 1 2 3 4 5 6 6 5 • Values m[i, j] depend only on values that have been previously computed 4 j 3 2 1 i

49. 2 0 25000 2 1 0 7500 5000 0 Example: Compute A1A2A3 1 2 3 • A1: 10×100 (p0×p1) • A2: 100×5 (p1×p2) • A3: 5×50 (p2×p3) m[i, i] = 0 for i = 1, 2, 3 m[1, 2] = m[1, 1] + m[2, 2] + p0p1p2 (A1A2) = 0 + 0 + 10×100×5 = 5,000 m[2, 3] = m[2, 2] + m[3, 3] + p1p2p3 (A2A3) = 0 + 0 + 100×5×50 = 25,000 m[1,1] + m[2,3] + p0p1p3 = 75,000 (A1(A2A3)) m[1,2] +m[3, 3] + p0p2p3 = 7,500 ((A1A2)A3) 3 2 1 m[1,3] = min