Rod cutting Decide where to cut steel rods:

Slides:



Advertisements
Similar presentations
Algorithm Design approaches Dr. Jey Veerasamy. Petrol cost minimization problem You need to go from S to T by car, spending the minimum for petrol. 2.
Advertisements

CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Rod cutting Decide where to cut steel rods:
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
CSCI 6212 Design and Analysis of Algorithms Dynamic Programming Dr. Juman Byun The George Washington University Please drop this course if you have not.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Unit 4: Dynamic Programming
Dynamic Programming.
11-1 Elements of Dynamic Programming For dynamic programming to be applicable, an optimization problem must have: 1.Optimal substructure –An optimal solution.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
Dynamic Programming CIS 606 Spring 2010.
Dynamic Programming Code
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 6 Instructor: Paul Beame TA: Gidon Shavit.
Analysis of Algorithms
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
Dynamic Programming Chapter 15 Highlights Charles Tappert Seidenberg School of CSIS, Pace University.
Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized.
COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these.
Dynamic Programming Louis Siu What is Dynamic Programming (DP)? Not a single algorithm A technique for speeding up algorithms (making use of.
Dynamic Programming (Ch. 15) Not a specific algorithm, but a technique (like divide- and-conquer). Developed back in the day when “programming” meant “tabular.
Dynamic Programming. Many problem can be solved by D&C – (in fact, D&C is a very powerful approach if you generalize it since MOST problems can be solved.
Algorithmics - Lecture 121 LECTURE 11: Dynamic programming - II -
CSC5101 Advanced Algorithms Analysis
Dynamic Programming 又稱 動態規劃 經常被用來解決最佳化問題. Dynamic Programming2 Rod cutting problem Given a rod of length N inches and a table of prices p[i] for i=1..N,
D ESIGN & A NALYSIS OF A LGORITHM 13 – D YNAMIC P ROGRAMMING Informatics Department Parahyangan Catholic University.
1 Chapter 15-2: Dynamic Programming II. 2 Matrix Multiplication Let A be a matrix of dimension p x q and B be a matrix of dimension q x r Then, if we.
Alignment, Part II Vasileios Hatzivassiloglou University of Texas at Dallas.
Simplifying Dynamic Programming Jamil Saquer & Lloyd Smith Computer Science Department Missouri State University Springfield, MO USA.
Dynamic Programming Typically applied to optimization problems
Lecture 12.
Dynamic Programming Sequence of decisions. Problem state.
Design & Analysis of Algorithm Dynamic Programming
Dynamic Programming CSE 2320 – Algorithms and Data Structures
Fundamental Structures of Computer Science
David Meredith Dynamic programming David Meredith
Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to the Design and Analysis of Algorithms
Types of Algorithms.
Dynamic Programming CISC4080, Computer Algorithms CIS, Fordham Univ.
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming.
Dynamic Programming.
Data Structures and Algorithms
Unit-5 Dynamic Programming
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming.
Chapter 15: Dynamic Programming II
Ch. 15: Dynamic Programming Ming-Te Chi
Algorithms CSCI 235, Spring 2019 Lecture 28 Dynamic Programming III
Trevor Brown DC 2338, Office hour M3-4pm
Dynamic Programming.
DYNAMIC PROGRAMMING.
Elements of Dynamic Programming
Lecture 4 Dynamic Programming
COMP108 Algorithmic Foundations Dynamic Programming
Longest Common Subsequence
Dynamic Programming II DP over Intervals
Dynamic Programming CISC4080, Computer Algorithms CIS, Fordham Univ.
Algorithms and Data Structures Lecture X
This is not an advertisement for the profession
Matrix Chain Multiplication
Longest Common Subsequence
CSCI 235, Spring 2019, Lecture 25 Dynamic Programming
Dynamic Programming.
Analysis of Algorithms CS 477/677
Data Structures and Algorithms Dynamic Programming
Presentation transcript:

Rod cutting Decide where to cut steel rods: Given a rod of length n inches and a table of prices pi, i=1,2,…,n, find the maximum revenue rn obtainable by cutting up the rod and selling the pieces Rod lengths are integers For i=1,2,…,n we know the price pi of a rod of length i inches

For a rod of length 4: 2+2 is optimal (p2+p2=10) Example For a rod of length 4: 2+2 is optimal (p2+p2=10) In general, can cut a rod of length n 2n-1 ways length i 1 2 3 4 5 6 7 8 9 10 price pi 17 20 24 30

Example: rod of length 4 length i 1 2 3 4 5 6 7 8 9 10 price pi 17 20 24 30 Cuts Revenue 4 9 1, 3 1 + 8 = 9 2, 2 5 + 5 = 10 3, 1 8 + 1 = 9 1, 1, 2 1 + 1 + 5 = 7 1, 2, 1 1 + 5 + 1 = 7 2, 1, 1 5 + 1 + 1 = 7 1, 1, 1, 1 1 + 1 + 1 + 1 = 4 Best: two 2-inch pieces = revenue of p2+p2=5+5=10

Calculating Maximum Revenue length i 1 2 3 4 5 6 7 8 9 10 price pi 17 20 24 30 i ri optimal solution 1 1 (no cuts) 2 5 2 (no cuts) 3 8 3 (no cuts) 4 10 2 + 2 13 2 + 3 6 17 6 (no cuts) 7 18 1 + 6 or 2 + 2 + 3 22 2 + 6

General Solution If optimal solution cuts rod in k pieces then optimal decomposition: n=i1+i2+…+ik Revenue: rn=pi1+pi2+…+pik In general: rn=max{pn,r1+rn-1,r2+rn-2,…,rn-1+r1} Initial cut of the rod: two pieces of size i and n-i Revenue ri and rn-i from those two pieces Need to consider all possible values of i May get better revenue if we sell the rod uncut

A different view of the problem Decomposition in A first, left-hand piece of length i A right-hand reminder of length n-i Only the reminder is further divided Then rn=max{pi+rn-i, 1 <= i <= n} Thus, need solution to only one subproblem

Caclulating Maximum Revenue Better comparison:  rk=max(pi+rk−i) over all 1≤i≤k i ri Maximum of 1 r1 p1+r0 2 r2 p1+r1,p2+r0 3 r3 p1+r2,p2+r1,p3+r0 4 10 p1+r3,p2+r2,p3+r1,p4+r0 ...

Top-down implementation CUT-ROD(p,n) if n==0 return 0 q = -∞ for i=1 to n q=max{q,p[i]+CUT-ROD(p,n-i)} return q Time recurrence: T(n)=1+T(1)+T(2)+…+T(n-1) T(n)=O(2n)

Dynamic Programming Problem with recursive solution: subproblems solved multiple times Must figure out a way to solve each subproblem just once Two possible solutions: solve a subproblem and remember its solution Top Down: Memoize recursive algorithm Bottom Up: Figure out optimum order to fill the solution array

Rod Cutting: Bottom Up Solution Optimality of subproblems is obvious DP-CUT-ROD(p,n) let r[0..n], s[0..n] be new arrays r[0]=0 for j=1 to n q=-∞ for i=1 to j if q < p[i]+r[j-i] s[j]=i; q= p[i]+r[j-i] r[j]=q return r and s

Retrieving an optimal solution PRINT-CUT-ROD (r,s) = DP-CUT-ROD(p,n) while n>0 print s[n] n=n-s[n] Example: i 0 1 2 3 4 5 6 7 r[i] 0 1 5 8 10 13 17 18 s[i] 0 1 2 3 2 2 6 1

Memoize Make functions faster by trading space for time `Memoizing' - caching the return values of the function in a table. If you call the function again with the same arguments, memoize gives you the value out of the table, instead of letting the function compute the value all over again.

Fibonacci int RegularFib(int n) { if (n == 0 || n == 1) return n; else return RegularFib(n - 1) + RegularFib(n - 2); }

Fibonacci

Fibonacci Memoized solution int MemoizeFib(int n){ if(n == 0) return 0; if (n == 1 || n == 2) return 1; if(helperMemory[n] != 0) return helperMemory[n]; else { helperMemory[n] = MemoizeFib(n-1) + MemoizeFib(n-2); }

Fibonacci

Rod cut Top Down Memoized Solution Store solution to subproblem of length i in array element r(i) MemoizedCutRod(p, n) r[n]=(0 => 0, others =>MinInt) return MemoizedCutRodAux(p, n, r) MemoizedCutRodAux(p, n, r) if r[n] = 0 and then n != 0 then q = -∞ for i=1 to n q := max(q, p[i] + MemoizedCutRodAux(p, n-i, r)) r[n] = q return r[n]