Algorithms Chapter 15 Dynamic Programming - Rod

19
B98570104 廖廖廖 B98570107 廖 B98570110 廖廖廖 B98570135 廖廖廖 Algorithms Chapter 15 Dynamic Programming - Rod

description

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. - PowerPoint PPT Presentation

Transcript of Algorithms Chapter 15 Dynamic Programming - Rod

Algorithms Chapter 15 Dynamic Programming

B98570104

B98570107

B98570110

B98570135AlgorithmsChapter 15 Dynamic Programming - RodOutlineRod CuttingRecursive top down implementationUsing dynamic programming for optimal rod cuttingSubproblem graphsReconstructing a solutionExercise

Rod - cuttingGiven 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 i12345678910Price pi1589101717202430ExampleConsider the case when n=4.

The optimal strategy is part (c). With length=2, value=10.

91855811111111111555(c)(b)(a)(d)(e)(g)(f)(h)Conclusion If an optimal solution cuts the rod into k pieces, for some 1 k n, then an optimal decomposition n = i1+ i2++ ik of the rod into pieces of lengths i1 , i2 ,, ik provides maximum corresponding revenuern = pi1 + pi2 + + pik More generally,rn = max (pn , r1+rn-1 , r2+rn-2 ,, rn-1+r1)Simpler solution, rn = max (pi + rn-i) 1 i nOutlineRod CuttingRecursive top down implementationUsing dynamic programming for optimal rod cuttingSubproblem graphsReconstructing a solutionExercise

Recursive top down implementationN = i1 + i2 + + ikr [ N ] = p [ i1 ] + + p [ ik ] ----r [ N ] = max i=1..n { p [ i ] + r [ N i ] }r[0]=0CUT ROD ( p , n )if n = = 0return 0q = - for i = 1 to nq = max( q , p [ i ] + CUR ROD ( p , n i ) )return q4210310210010000T ( 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.OutlineRod CuttingRecursive top down implementationUsing dynamic programming for optimal rod cuttingSubproblem graphsReconstructing a solutionExercise

Using dynamic programming for optimal rod cutting

dynamic programming :1. top-down with memoization 2. bottom-up methodtop-down with memoization bottom-up method(n^2)

Top-down with memoizationMemoized-Cut-Rod(p, n)let r[0..n] be a new arrayfor i = 0 to nr[i] =-return Memoized-Cut-Rod-Aux(p,n,r)

Top-down with memoizationMemoized-Cut-Rod-Aux(p,n,r)if r[n] 0return r[n]if n == 0q = 0else q = -for i = 1 to nq = max(q, p[i] + Memoized-Cut-Rod-Aux(p,n-i,r))r[n] = qreturn q

Bottom-up methodBottom-Up-Cut-Rod(p,n)let r[0..n] be a new arrayr[0] = 0for j = 1 to nq = -for i = 1 to jq = max(q, p[i] + r[j-i])r[j] = qreturn r[n]

OutlineRod CuttingRecursive top down implementationUsing dynamic programming for optimal rod cuttingSubproblem graphsReconstructing a solutionExercise

Subproblem graphsDynamic-programming problemEx: rod-cutting problem n=4

Bottom-up methodtop-down method depth-first search

43120OutlineRod CuttingRecursive top down implementationUsing dynamic programming for optimal rod cuttingSubproblem graphsReconstructing a solutionExercise

Reconstructing a solutionBOTTOM-UP-CUT-ROD V.S EXTENDED-BOTTOM-UP-CUT-RODEXTENDED-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 q0 3print s[n] 4n=n-s[n]

i012345678910r[i]015810131718222530s[i]01232261230THANK YOU VERY MUCH!!The End