# Algorithms Chapter 15 Dynamic Programming - Rod

## Presentation on theme: "Algorithms Chapter 15 Dynamic Programming - Rod"— Presentation transcript:

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

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 17 20 24 30

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)

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

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

Recursive top – down implementation

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.

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

Top-down with memoization

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 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

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= -∞ 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

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]

The End THANK YOU VERY MUCH!!