Using dynamic programming for optimal rod cutting
Download
Report
Transcript Using dynamic programming for optimal rod cutting
Algorithms
Chapter 15
Dynamic Programming - Rod
B98570104廖翎妤
B98570107方
敏
B98570110李紫綾
B98570135邱郁庭
Outline
Rod Cutting
Recursive top – down implementation
Using dynamic programming for optimal rod cutting
Subproblem graphs
Reconstructing a solution
Exercise
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
1
5
8
9
10
17
17
20
24
30
Example
Consider the case when n=4.
9
1
(a)
1
1
1
1
5
(c)
1
5
1
5
(e)
(d)
(g)
5
(b)
8
5
8
1
1
(f)
1
1
1
(h)
The optimal strategy is part (c). With length=2, value=10.
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 = 5+5+8 = 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
Outline
Rod Cutting
Recursive top – down implementation
Using dynamic programming for optimal rod cutting
Subproblem graphs
Reconstructing a solution
Exercise
Recursive top – down implementation
假設將鐵條切割成k段
N = i 1 + i 2 + … + 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
4
2
3
2
1
1
0
0
0
1
0
1
0
0
0
T ( n ) = 1 + Σn-1j=0 T ( j )
T ( n ) = 2n
0
CUT-ROD explicitly considers all
the 2n-1 possible ways of cutting up a
rod of length n.
Outline
Rod Cutting
Recursive top – down implementation
Using dynamic programming for optimal rod cutting
Subproblem graphs
Reconstructing a solution
Exercise
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)
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)
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
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]
Outline
Rod Cutting
Recursive top – down implementation
Using dynamic programming for optimal rod cutting
Subproblem graphs
Reconstructing a solution
Exercise
Subproblem graphs
4
Dynamic-programming problem
Ex:
rod-cutting problem
假設棍子長度 n=4,
有圖上這些切割方式。
3
2
Bottom-up method
top-down method可視為
1
「depth-first search」
0
Outline
Rod Cutting
Recursive top – down implementation
Using dynamic programming for optimal rod cutting
Subproblem graphs
Reconstructing a solution
Exercise
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= -∞
5
for i=1 to j
6
if q<p[i]+r[j-i]
7
q= p[i]+r[j-i]
8
s[j]=I
9
r[j]=q
10
return r and s
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
0
1
2
3
4
5
6
7
8
9
10
r[i]
0
1
5
8
10
13
17
18
22
25
30
s[i]
0
1
2
3
2
2
6
1
2
3
0
The End
THANK YOU VERY MUCH!!