Rod Cutting Problem Dynamic Programming • A company buys long steel rods (of length n), and cuts them into shorter one to sell CISC5835, Algorithms for Big Data • integral length only CIS, Fordham Univ. • cutting is free • rods of diff lengths sold for diff. price, e.g., • Best way to cut the rods? Instructor: X. Zhang • n=4: no cutting: $9, 1 and 3: 1+8=$9, 2 and 2: 5+5=$10 • n=5: ? 2 Rod Cutting Problem Formulation Rod Cutting Problem Formulation • Input: • // return r_n: max. revenue • int Cut_Rod (int p[1…n], int n) • a rod of length n • a table of prices p[1…n] where p[i] is price for rod of length i • Divide-and-conquer? • Output • how to divide it into smaller one? • determine maximum revenue r n obtained by cutting up • we don’t know we want to cut in half… the rod and selling all pieces • Analysis solution space (how many possibilities?) • how many ways to write n as sum of positive integers? • 4=4, 4=1+3, 4=2+2 • # of ways to cut n: 3 4
Rod Cutting Problem Rod Cutting Problem • // return r n : max. revenue for rod of length n • // return r n : max. revenue for rod size n • int Cut_Rod (int n, int p[1…n]) • int Cut_Rod (int n, int p[1…n]) • Given a rod of length n, consider first rod to cut out • Start from small • if we don’t cut it at all, max. revenue is p[n] • n=1, r 1 =1 //no possible cutting • if first rod to cut is1: max. revenue is p[1]+r n-1 • n=2, r 2 =5 // no cutting (if cut, revenue is 2) • if first rod to cut out is 2: max. revenue is p[2]+r n-2, … • n=3, r 3 =8 //no cutting • max. revenue is given by maximum among all the • r 4 =9 (max. of p[4], p[1]+r 3 , p[2]+r 3, p[3]+r 1 ) above options • r 5 = max (p[5], p[1]+r 4 , p[2]+r 2 , p[3]+r 2 , p[4]+r 1 ) • r n = max (p[n], p[1]+r n-1 , p[2]+r n-2 , …, p[n-1]+r 1 ) • … 5 6 Optimal substructure Rod Cutting Problem • // return r_n: max. revenue for rod size n • // return r n : max. revenue for rod size n • int Cut_Rod (int p[1…n], int n) • int Cut_Rod (int n, int p[1…n]) • r n = max (p[n], p[1]+r n-1 , p[2]+r n-2 , …, p[n-1]+r 1 ) • r n = max (p[n], p[1]+r n-1 , p[2]+r n-2 , …, p[n-1]+r 1 ) • Optimal substructure : Optimal solution to a problem of size n incorporates optimal solutions to problems of smaller size (1, 2, 3, … n-1). 7 8
Recursive Rod Cutting Subproblems Graph • // return r_n: max. revenue for rod size n • Avoid recomputing subproblems Running time T(n) again and again by storing • int Cut_Rod (int p[1…n], int n) subproblems solutions in memory/table (hence “programming”) Closed formula: T(n)=2 n • trade-off between space and time • Overlapping of subproblems Recursive calling tree: n=4 9 10 Dynamic Programming Memoized Cut-Rod • Avoid recomputing subproblems again and again by storing subproblems solutions in memory/ // stores solutions to all problems table (hence “programming”) // initialize to an impossible negative value • trade-off between space and time • Two-way to organize // A recursive function • top-down with memoization // If problem of given size (n) has been • Before recursive function call, check if subproblem solved before, just return the stored result has been solved before • After recursive function call, store result in table • bottom-up method // same as before… • Iteratively solve smaller problems first, move the way up to larger problems 11 12
Memoized Cut-Rod: running time Bottom-up Cut-Rod // stores solutions to all problems // stores solutions to all problems // initialize to an impossible negative value // Solve subproblem j, using // A recursive function solution to smaller subproblems // If problem of given size (n) has been solved before, just return the stored result Running time: 1+2+3+..+n-1=O(n 2 ) // same as before… 13 14 Bottom-up Cut-Rod (2) Recap • We analyze rod cutting problem • Optimal way to cut a rod of size n is found by // stores solutions to all problems • 1) comparing optimal revenues achievable after cutting out the first rod of varying len, • This relates solution to larger problem to solutions to subproblems • 2) choose the one yield largest revenue What if we want to know who to achieve r[n]? i.e., how to cut? i.e., n=n_1+n_2+…n_k, such that p[n_1]+p[n_2]+…+p[n_k]=r n 15 16
maximum (contiguous) subarray Analyze optimal solution • Problem: find the contiguous subarray within an • Problem: find contiguous subarray with largest sum array ( containing at least one number ) which has • Sample Input: [-2,1,-3,4,-1,2,1,-5,4] (array of size n=9) largest sum (midterm lab) • How does solution to this problem relates to smaller subproblem? • If given the array [-2,1,-3,4,-1,2,1,-5,4], • If we divide-up array (as in midterm) • contiguous subarray [4,-1,2,1] has largest sum = 6 • [-2,1,-3,4,-1,2,1,-5,4] //find MaxSub in this array • Solution to midterm lab • brute-force: n 2 or n 3 [-2,1,-3,4,-1] [2,1,-5,4] • Divide-and-conquer: T(n)=2 T(n/2)+O(n), T(n)=nlogn still need to consider subarray that spans both halves • Dynamic programming? This does not lead to a dynamic programming sol. • Need a different way to define smaller subproblems! 17 18 Analyze optimal solution Analyze optimal solution • A • Problem: find contiguous subarray with largest sum • Index A • MSE(k) and optimal substructure • MSE(3): A[2..3], sum is -2 (red box) Index • MSE(4): two options to choose • (1) either grow MSE(3) to include pos 4 How a problem’s optimal • MSE(k), max. subarray ending at pos k, among all solution can be derived from • subarray is then A[2..4], sum is optimal solution to smaller subarray ending at k (A[i…k] where i<=k), the one with MSE(3)+A[4]=-2+A[4]=2 problem largest sum • (2) or start afresh from pos 4 • MSE(1), max. subarray ending at pos 1, is A[1..1], sum is -2 • subarray is then A[4…4], sum is A[4]=4 (better) • MSE(2), max. subarray ending at pos 2, is A[2..2], sum is 1 • Choose the one with larger sum, i.e., • MSE(3) is A[2..3], sum is -2 • MSE(4) = max (A[4], MSE(3)+A[4]) • MSE(4)? 19 20
Analyze optimal solution Analyze optimal solution • A • A • Index • Index • Once we calculate MSE(1) … MSE(9) • MSE(4)=4, array is A[4…4] • MSE(1)=-2, the subarray is A[1..1] • MSE(k) and optimal substructure • MSE(2)=1, the subarray is A[2..2] • Max. subarray ending at k is the larger between A[k…k] and • MSE(3)=-2, the subarray is A[2..3] Max. subarray ending at k-1 extended to include A[k] • MSE(4)=4, the subarray is A[4…4] MSE(k) = max (A[k], MSE(k-1)+A[k]) • … MSE(7)=6, the subarray is A[4…7] • MSE(5)= , subarray is • MSE(9)=4, the subarray is A[9…9] • MSE(6) • What’s the maximum subarray of A? • MSE(7) • well, it either ends at 1, or ends at 2, …, or ends at 9 • MSE(8) • Whichever yields the largest sum! • MSE(9) 21 22 Idea to Pseudocode Running time Analysis • A int MaxSubArray (int A[1…n], int & start, • It’s easy to see that int & end) • Index running time is O(n) { // Use array MSE to store the MSE(i) • a loop that iterates (int, start,end) MaxSubArray (int A[1…n]) MSE[1]=A[1]; • Calculate MSE(1) … MSE(n) for n-1 times { max_MSE = MSE[1]; // Use array MSE to store the MSE(i) • MSE(1)= A[1] • Recall other solutions: MSE[1]=A[1]; for (int i=2;i<=n;i++) • MSE(i) = max (A[i], A[i]+MSE(i-1)); max_MSE = MSE[1]; { • brute-force: n 2 or n 3 • Return maximum among all MSE(i), MSE[i] = ?? for (int i=2;i<=n;i++) • Divide-and-conquer: for i=1, 2, …n { if (MSE[i] > max_MSE) { nlogn MSE[i] = ?? max_MSE = MSE[i]; • Dynamic programming end = i; if (MSE[i] > max_MSE) { } wins! max_MSE = MSE[i]; Practice: } end = i; 1) fill in ?? return max_MSE; 2) How to find out the starting index of } } the max. subarray, i.e., the start parameter? } return (max_MSE, start, end) } 23 24
Recommend
More recommend