Presentation is loading. Please wait.

Presentation is loading. Please wait.

B98570104 廖翎妤 B98570107 方 敏 B98570110 李紫綾 B98570135 邱郁庭 Algorithms Chapter 15 Dynamic Programming - Rod.

Similar presentations


Presentation on theme: "B98570104 廖翎妤 B98570107 方 敏 B98570110 李紫綾 B98570135 邱郁庭 Algorithms Chapter 15 Dynamic Programming - Rod."— Presentation transcript:

1

2 B 廖翎妤 B 方 敏 B 李紫綾 B 邱郁庭 Algorithms Chapter 15 Dynamic Programming - Rod

3 Outline  Rod Cutting  Recursive top – down implementation  Using dynamic programming for optimal rod cutting  Subproblem graphs  Reconstructing a solution  Exercise

4 Rod - cutting  Given a rod of length n inches and a table of prices p i for i = 1, 2, …, n.  Determine the maximum revenue r n obtainable by cutting up the rod and selling the pieces. Length i Price p i

5 Example  Consider the case when n=4.  The optimal strategy is part (c). With length=2, value= (c)(b)(a) (d) (e) (g) (f) (h)

6 Conclusion  If an optimal solution cuts the rod into k pieces, for some 1 ≦ k ≦ n, then an optimal decomposition n = i 1 + i 2 +…+ i k of the rod into pieces of lengths i 1, i 2,…, i k provides maximum corresponding revenue r n = p i1 + p i2 + …+ p ik  More generally, r n = max (p n, r 1 +r n-1, r 2 +r n-2,…, r n-1 +r 1 )  Simpler solution, r n = max (p i + r n-i ) 1 ≦ i ≦ n

7 Outline  Rod Cutting  Recursive top – down implementation  Using dynamic programming for optimal rod cutting  Subproblem graphs  Reconstructing a solution  Exercise

8 Recursive top – down implementation 假設將鐵條切割成k段 N = i 1 + i 2 + … + i k r [ N ] = p [ i 1 ] + … + p [ i k ] ---- 總價格 r [ N ] = max i=1..n { p [ i ] + r [ N – i ] } r[0]=0 CUT – ROD ( p, n ) if n = = 0 return 0 q = - ∞ for i = 1 to n q = max( q, p [ i ] + CUR – ROD ( p, n – i ) ) return q

9 T ( n ) = 1 + Σ n-1 j=0 T ( j ) T ( n ) = 2 n CUT-ROD explicitly considers all the 2 n-1 possible ways of cutting up a rod of length n.

10 Outline  Rod Cutting  Recursive top – down implementation  Using dynamic programming for optimal rod cutting  Subproblem graphs  Reconstructing a solution  Exercise

11 Using dynamic programming for optimal rod cutting  算出子問題的答案,並將結果記下來,若再遇到重複的子問題, 就不必重複計算,也因此能提高效率,但要多花一些記憶體來檢 查此時要算的子問題是不是已經算過了。  使用 dynamic programming 的演算法有兩種做法 :  1. top-down with memoization  2. bottom-up method  top-down with memoization 和 bottom-up method 都是 Θ(n^2)

12 Top-down with memoization  此作法為遞迴,先檢查子問題是否有算過,若沒算過,就先算再 將答案記下來(給之後可能會重複出現的子問題使用),若有算 過,就將之前算過的答案拿出來使用。  Memoized-Cut-Rod(p, n)  let r[0..n] be a new array  for i = 0 to n  r[i] =-∞  return Memoized-Cut-Rod-Aux(p,n,r)

13 Top-down with memoization  Memoized-Cut-Rod-Aux(p,n,r)  if r[n] ≥ 0  return r[n]  if n == 0  q = 0  else q = -∞  for i = 1 to n  q = max(q, p[i] + Memoized-Cut-Rod-Aux(p,n-i,r))  r[n] = q  return q

14 Bottom-up method  按照子問題的大小,從最小的問題做到最大的問題。因較大 的問題的最佳解須包含子問題的最佳解。  Bottom-Up-Cut-Rod(p,n)  let r[0..n] be a new array  r[0] = 0  for j = 1 to n  q = -∞  for i = 1 to j  q = max(q, p[i] + r[j-i])  r[j] = q  return r[n]

15 Outline  Rod Cutting  Recursive top – down implementation  Using dynamic programming for optimal rod cutting  Subproblem graphs  Reconstructing a solution  Exercise

16 Subproblem graphs  Dynamic-programming problem  Ex: rod-cutting problem 假設棍子長度 n=4 , 有圖上這些切割方式。  Bottom-up method  top-down method 可視為 「 depth-first search 」  Dynamic-programming problem  Ex: rod-cutting problem 假設棍子長度 n=4 , 有圖上這些切割方式。  Bottom-up method  top-down method 可視為 「 depth-first search 」

17 Outline  Rod Cutting  Recursive top – down implementation  Using dynamic programming for optimal rod cutting  Subproblem graphs  Reconstructing a solution  Exercise

18 Reconstructing a solution  BOTTOM-UP-CUT-ROD V.S EXTENDED-BOTTOM-UP-CUT-ROD  EXTENDED-BOTTOM-UP-CUT-ROD(p,n) 1Let r[0..n] and s[0..n] be new arrays 2r[0]=0 3for j=1 to n 4 q= -∞ 5 for i=1 to j 6 if q

19 Reconstructing a solution  PRINT-CUT-ROD-SOLUTION(p,n) 1 (r,s) = EXTENDED-BOTTOM-UP-CUT-ROD(p,n) 2 while n>0 3print s[n] 4n=n-s[n] i r[i] s[i]

20 THANK YOU VERY MUCH!! The End


Download ppt "B98570104 廖翎妤 B98570107 方 敏 B98570110 李紫綾 B98570135 邱郁庭 Algorithms Chapter 15 Dynamic Programming - Rod."

Similar presentations


Ads by Google