Download presentation
Presentation is loading. Please wait.
Published byMallory Norrington Modified over 9 years ago
1
Algorithms Chapter 15 Dynamic Programming - Rod
B 廖翎妤 B 方 敏 B 李紫綾 B 邱郁庭
2
Outline Rod Cutting Recursive top – down implementation
Using dynamic programming for optimal rod cutting Subproblem graphs Reconstructing a solution Exercise
3
Rod - cutting Given a rod of length n inches and a table of prices pi for i = 1, 2, …, n. Determine the maximum revenue rn obtainable by cutting up the rod and selling the pieces. Length i 1 2 3 4 5 6 7 8 9 10 Price pi 17 20 24 30
4
Example Consider the case when n=4.
The optimal strategy is part (c). With length=2, value=10. 9 1 8 5 5 (a) (b) (c) 1 1 5 1 5 1 8 1 (e) (f) (d) 1 1 1 1 5 1 1 (h) (g)
5
Conclusion If an optimal solution cuts the rod into k pieces, for some 1 ≦k ≦n, then an optimal decomposition n = i1+ i2+…+ ik <7=2+2+3> of the rod into pieces of lengths i1 , i2 ,…, ik provides maximum corresponding revenue rn = pi1 + pi2 + …+ pik <r7 = p2+p2+p3 = = 18> More generally, rn = max (pn , r1+rn-1 , r2+rn-2 ,…, rn-1+r1) Simpler solution, rn = max (pi + rn-i) <r7 = p2+r5 = 5+13 = 18> 1 ≦i ≦n
6
Outline Rod Cutting Recursive top – down implementation
Using dynamic programming for optimal rod cutting Subproblem graphs Reconstructing a solution Exercise
7
Recursive top – down implementation
假設將鐵條切割成k段 N = i1 + i2 + … + ik r [ N ] = p [ i1 ] + … + p [ ik ] ----總價格 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
8
4 2 1 3 T ( n ) = 1 + Σn-1j=0 T ( j ) T ( n ) = 2n CUT-ROD explicitly considers all the 2n-1 possible ways of cutting up a rod of length n.
9
Outline Rod Cutting Recursive top – down implementation
Using dynamic programming for optimal rod cutting Subproblem graphs Reconstructing a solution Exercise
10
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)
11
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)
12
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
13
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]
14
Outline Rod Cutting Recursive top – down implementation
Using dynamic programming for optimal rod cutting Subproblem graphs Reconstructing a solution Exercise
15
Subproblem graphs Dynamic-programming problem Ex: rod-cutting problem
4 Dynamic-programming problem Ex: rod-cutting problem 假設棍子長度 n=4, 有圖上這些切割方式。 Bottom-up method top-down method可視為 「depth-first search」 3 2 1
16
Outline Rod Cutting Recursive top – down implementation
Using dynamic programming for optimal rod cutting Subproblem graphs Reconstructing a solution Exercise
17
Reconstructing a solution
BOTTOM-UP-CUT-ROD V.S EXTENDED-BOTTOM-UP-CUT-ROD EXTENDED-BOTTOM-UP-CUT-ROD(p,n) 1 Let r[0..n] and s[0..n] be new arrays 2 r[0]=0 3 for j=1 to n 4 q= -∞ for i=1 to j if q<p[i]+r[j-i] q= p[i]+r[j-i] s[j]=I 9 r[j]=q 10 return r and s
18
Reconstructing a solution
PRINT-CUT-ROD-SOLUTION(p,n) 1 (r,s) = EXTENDED-BOTTOM-UP-CUT-ROD(p,n) 2 while n>0 3 print s[n] 4 n=n-s[n] i 1 2 3 4 5 6 7 8 9 10 r[i] 13 17 18 22 25 30 s[i]
19
The End THANK YOU VERY MUCH!!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.