540 likes | 743 Views
Advanced Dynamic Programming. HKOI Training 2008 Hackson Leung 29 th March 2008. Acknowledgement: References and slides are extracted from: Dynamic Programming, 19-05-2007, by Kelly Choi [Advanced] Dynamic Programming, 24-04-2004, by cx. What should you know?. Concept in Recurrence
E N D
AdvancedDynamic Programming HKOI Training 2008 Hackson Leung 29th March 2008 Acknowledgement: References and slides are extracted from: Dynamic Programming, 19-05-2007, by Kelly Choi [Advanced] Dynamic Programming, 24-04-2004, by cx
What should you know? • Concept in Recurrence • Basic Recursion • [Intermediate] 2008-01-26 • Functions • [Advanced] 2008-03-15 • Divide and Conquer • [Intermediate] 2008-01-26 • Refer to those notes if you are not familiar with
Simple Problem Revisited • You have ∞ valued coins • They are $0.5, $1, $2 and $5 respectively • Tell me how many ways I can make the total amount of N dollar(s) • E.g. N = 1, we have • $0.5+$0.5 • $1 alone • Condition I: Count $1+$2 and $2+$1 are different
Simple Problem Revisited • How can we perform it in EXHAUSTION? • Performance: O(4v) Algorithm Count(Amount v) Begin if (v is zero) return 1; //Base Case Let sum = 0 for i in {0.5, 1, 2, 5} if v – i >= 0 then sum = sum + Count(v-i) return sum End Count
Simple Problem Revisited • How can we perform it in EXHAUSTION? • Performance: O(4v) • When v is reasonably large (say, v = 18), the algorithm is very slow • Cure?
Simple Problem Revisited • Let’s see how the program calls the functions when v=2 2 0 1 1.5 0.5 0 1 0.5 0.5 0 0 0 0
Simple Problem Revisited • Let’s see how redundant calls are invoked when v=2 2 0 1 1.5 0.5 0 1 0.5 0.5 0 0 0 0
Simple Problem Revisited • Does two results for Count(1) differ? • We see that invoking Count(1) from Count(2) and Count(1.5) should give same result • We call this Overlapping Sub-problems(重疊子問題) • Why don’t we STORE the redundant calls?
Simple Problem Revisited • Why don’t we STORE the redundant calls? 2
Simple Problem Revisited • Why don’t we STORE the redundant calls? 2 1 0.5 0 0
Simple Problem Revisited • Why don’t we STORE the redundant calls? 2 1 0.5 0 0
Simple Problem Revisited • Why don’t we STORE the redundant calls? 2 1 1.5 0.5 0 1 0
Simple Problem Revisited • Why don’t we STORE the redundant calls? 2 1 1.5 0.5 0 1 0.5 0
Simple Problem Revisited • Why don’t we STORE the redundant calls? 2 0 1 1.5 0.5 0 1 0.5 0
Simple Problem Revisited • Why don’t we STORE the redundant calls? 2 0 1 1.5 0.5 0 1 0.5 0
Simple Problem Revisited • Why don’t we STORE the redundant calls? • We call that Memo(r)ization • The implementation strategy is Top-Down • Performance?
Simple Problem Revisited • You have ∞ valued coins • They are $0.5, $1, $2 and $5 respectively • Tell me how many ways I can make the total amount of N dollar(s) • Condition I: Count $1+$2 and $2+$1 are different • Condition II: Ignore repetitions
Simple Problem Revisited • You have finite valued coins • They are $0.5, $1, $2 and $5 respectively • Tell me how many ways I can make the total amount of N dollar(s) • Condition I: Count $1+$2 and $2+$1 are different • Condition II: Ignore repetitions
So far… • We know that in solving DP questions it must have the following properties • Overlapping Sub-problems
Simple Problem Revisited II • Diamond Chain [HKOJ 1010] • When considering a diamond di -8 5 10 -2 6 3 -4 -1 di-1 di Let me think about it Hey, tell me the best answer if you are the end of the chain
Simple Problem Revisited II • Diamond Chain [HKOJ 1010] • When considering a diamond di -8 5 10 -2 Calculating… di-1
Simple Problem Revisited II • Diamond Chain [HKOJ 1010] • When considering a diamond di -8 5 10 -2 6 3 -4 -1 di-1 di I tell you, the best answer including me is 13
Simple Problem Revisited II • Diamond Chain [HKOJ 1010] • When considering a diamond di -8 5 10 -2 6 3 -4 -1 di-1 di If I include 13, my best answer should be 19 Otherwise 6
Simple Problem Revisited II • Diamond Chain [HKOJ 1010] • When considering a diamond di -8 5 10 -2 6 3 -4 -1 di di+1 I tell you, the best answer including me is …
Simple Problem Revisited II • Diamond Chain [HKOJ 1010] • When considering a diamond di • We just ask the best answer that includes di-1 • Why do we trust the best answer from di-1? • Prove it by yourselves • We call this property the optimal substructure (最優子結構) • Observing this property is somehow difficult • Caution: Greedy also share this property!
Simple Problem Revisited II • Diamond Chain [HKOJ 1010] • When considering a diamond di • We just ask the best answer that includes di-1 • Why do we trust the best answer beforedi? • Prove it by yourselves • We call this property the memoryless property (無後效性) • Only the past event(s) determine the current event • Observing this property is somehow difficult, too
Simple Problem Revisited II • Variant • What if we consider a RING? • di: My neighbors could be my past events’ candidates!
So far… • We know that in solving DP questions it must have the following properties • Overlapping Sub-problems • Optimal Substructure • Memoryless
Next Step • Sometimes I know a problem that consists of the required properties can be solved in DP, but… • Memo(r)ization may cause Runtime Error due to stack overflow error • Consider in problem I, N = 1,000,000 • I don’t know how to formulate
Next Step • Sometimes I know a problem that consists of the required properties can be solved in DP, but… • Memo(r)ization may cause Runtime Error due to stack overflow error • Consider in problem I, N = 1,000,000 • In top down implementation, we request the previous events (formally, we call it states) • Recall a nice property called memoryless • We can compute all the previous states first, then determine the current • Bottom Up approach
Next Step • An event should be called a state • How to describe a state is very important • Wrong or insufficient info to describe the state would lead to wrong result • e.g. if I want to predict the weather in a particular day, I must know the weather of previous day. So the date and its weather must both be present • In problem I, how to describe the state?
Next Step • An event should be called a state • From previous examples, we know that there are relationships between past and current states • In mathematical term, we call that state transition equation (狀態轉移方程) • In words, we know that Count(N) is combining the case of all reducible values’ count. • In mathematics,
Bottom Up • Usually the procedure of bottom up implementation is • Very often you may see lots of loops in DP solutions • Usually transition takes place within the last loop • Remember the base case! Algorithm Bottom-Up Begin For Any State N as current state For any previous state k If condition is satisfied then Transit state N from k End Bottom-Up
Bottom Up • Problem I, in bottom-up Algorithm Count Begin Let C[N], where C[0] = 1 For i := 1 to N do For j := {0.5, 1, 2, 5} do If i – j >= 0 then C[i] += C[i-j]; End Count
7 6 9 4 8 1 5 2 4 3 Triangle • Given a cell-leveled triangle, with the ith level has i cells, and N levels in total • Traverse from the top cell to the bottom, only by two directions stated in the figure • Find the largest sum • Fulfill the three properties?
Triangle • Defining a state • The ith level? • The ith level, jth cell? • What does a state store? • If you don’t know, please refer to the task description again… • Let S[i][j] stores the optimum value
6 9 1 Triangle • How to relate the current state with previous states? • Refer to the following diagram • State transition equation? • Final answer? • Every cell at the bottom can be the answer • Need extra check ith level jth cell
1 6 9 Triangle • How to relate the current state with previous states? • Refer to the following diagram • State transition equation? • Final answer? • S[1][1] only, hurray! ith level jth cell
1 6 9 1 6 9 Triangle • How to relate the current state with previous states? • Different approaches are possible • Different state transition would lead to different efficiency ith level jth cell ith level jth cell
Edit Distance • Given two strings A and B • You can insert, delete or change a character to A • Find the minimum changes made to transform A to B • e.g. it takes 3 changes to transform “text” to “tree”
Edit Distance • First, define the state (important!) • Think of what makes an event • Can we induce the past events? • Bare in mind: those past events must be sufficient to determine the current state • What are we assuming on previous states? • The past states store the minimal change to transform a substring of A to a substring of B • Formulation? • More complex
Edit Distance • Variants • LCS (Longest Common Subsequence) • Show me how you can transform it!
Non-spacious Writing • You have to write an article, single paragraph • You have typed N words in order, each word has length Wi • Every word must have one space in between for separation • Each line can only have at most L letters • For each line, the unused space shows the emptiness by squaring its count • Minimize the emptiness
Non-spacious Writing • Example • “HKOI” “is” “the” “goodest” “and” “fun” “:o)” • A line can only have 7 letters at most • Best layout • Emptiness = 9 + 1 + 16 = 26
Non-spacious Writing • Determine the state • Is the number of line important? • State transition equation? • Constraints • N< 1,000,000 • L< 100 • Length of any word <L • Algorithm?
Non-spacious Writing • Considering in different order the previous states may lead to different complexities • O(N2) vs O(NL) • Please think if your algorithm can pass the time limit and resources limit or not before implement
Common Mode • DP on rectangular array • Again we use more memory as a trade off for time • 以空間換取時間 • DP on trees • DP on ugly states • Special representation on state is required
Optimization for DP • If we need lots of different states, it probably implies • More for-loops • More space needed for a state • State transition becomes expensive • Cure? • Memory Optimization – Rolling Array (滾動數組) • Runtime Optimization
Challenging Problem -4 1 • Turkish Roulette • A roulette of S slots • Each slot has a number • You are given B balls • A ball occupies 2 slots • Each ball has its number • A slot can be occupied by one ball • For each ball, let L be the sum of the two slots’ values times its number • If the number on ball is negative, you gain $L, otherwise you lose $L 12 0 12 -1 0 0 7 -7 22 -8 3 1 6 -3 1 5 3
Challenging Problem -4 1 • Turkish Roulette • A roulette of S slots • Each slot has a numberXi • You are given B balls • Each ball has its number Yj • Conditions: • 3 <S< 250 • 1 <B<└S / 2┘ • -64 <Xi, Yj< 64, for 1 <i<S, 1 <j<B • Derive the solution by yourself! 12 0 12 -1 0 0 7 -7 22 -8 3 1 6 -3 1 5 3