Post on 17-Jan-2016
HKOI Training 2009Hackson Leung
28th March 2009
Acknowledgement:References and slides are extracted from:1. Dynamic Programming, 19-05-2007, by Kelly Choi2.[Advanced] Dynamic Programming, 24-04-2004, by cx
What should you know?Concept in RecurrenceBasic Recursion
[Intermediate] 2009-01-24Functions
[Advanced] 2009-02-21Divide and Conquer
[Intermediate] 2009-03-14Refer to those notes if you are not familiar
with
Simple Problem RevisitedYou have ∞ valued coins
They are $0.5, $1, $2 and $5 respectivelyTell 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 RevisitedHow can we perform it in EXHAUSTION?
Performance: O(4v)
Algorithm Count(Amount v)Begin
if (v is zero) return 1; //Base CaseLet sum = 0for i in {0.5, 1, 2, 5}
if v – i >= 0 thensum = sum + Count(v-i)
return sumEnd Count
Simple Problem RevisitedHow can we perform it in EXHAUSTION?Performance: O(4v)When v is reasonably large (say, v = 18), the
algorithm is very slowCure?
Simple Problem RevisitedLet’s see how the program calls the functions
when v=22
1 01.5
00.5
10.5
0 00.5
0
0
Simple Problem RevisitedLet’s see how redundant calls are invoked
when v=22
1 01.5
00.5
10.5
0 00.5
0
0
Simple Problem RevisitedDoes two results for Count(1) differ?We see that invoking Count(1) from Count(2)
and Count(1.5) should give same resultWe call this Overlapping Sub-problems (重疊子問題 )
Why don’t we STORE the redundant calls?
Simple Problem RevisitedWhy don’t we STORE the redundant calls?
2V F(v)
0 1
0.5 -1
1 -1
1.5 -1
2 -1
Simple Problem RevisitedWhy don’t we STORE the redundant calls?
2
1
00.5
0
V F(v)
0 1
0.5 1
1 -1
1.5 -1
2 -1
Simple Problem RevisitedWhy don’t we STORE the redundant calls?
2
1
00.5
0
V F(v)
0 1
0.5 1
1 2
1.5 -1
2 -1
Simple Problem RevisitedWhy don’t we STORE the redundant calls?
2
11.5
00.5
1
0
V F(v)
0 1
0.5 1
1 2
1.5 -1
2 -1
Simple Problem RevisitedWhy don’t we STORE the redundant calls?
2
11.5
00.5
10.5
0
V F(v)
0 1
0.5 1
1 2
1.5 -1
2 -1
Simple Problem RevisitedWhy don’t we STORE the redundant calls?
2
1 01.5
00.5
10.5
0
V F(v)
0 1
0.5 1
1 2
1.5 3
2 -1
Simple Problem RevisitedWhy don’t we STORE the redundant calls?
2
1 01.5
00.5
10.5
0
V F(v)
0 1
0.5 1
1 2
1.5 3
2 6
Simple Problem RevisitedWhy don’t we STORE the redundant calls?We call that Memo(r)izationThe implementation strategy is Top-DownPerformance?
Simple Problem Revisited
Simple Problem RevisitedYou have finite valued coins
They are $0.5, $1, $2 and $5 respectivelyTell me how many ways I can make the total
amount of N dollar(s)Condition I: Count $1+$2 and $2+$1 are
differentCondition II: Ignore repetitions
So far…We know that in solving DP questions it must
have the following propertiesOverlapping Sub-problems
Simple Problem Revisited IIDiamond Chain [HKOJ 1010]When considering a diamond di
-8
510
-2
6 3-4
-1
didi-1
Hey, tell me the best answer if you are the end of the
chain
Let me think about it
Simple Problem Revisited IIDiamond Chain [HKOJ 1010]When considering a diamond di
-8
510
-2
di-1
Calculating…
Simple Problem Revisited IIDiamond Chain [HKOJ 1010]When considering a diamond di
-8
510
-2
6 3-4
-1
didi-1I tell you, the best answer
including me is 13
Simple Problem Revisited IIDiamond Chain [HKOJ 1010]When considering a diamond di
-8
510
-2
6 3-4
-1
didi-1If I include 13, my best answer should be 19Otherwise 6
Simple Problem Revisited IIDiamond Chain [HKOJ 1010]When considering a diamond di
-8
510
-2
6 3-4
-1
di+1diI tell you, the best answer
including me is …
Simple Problem Revisited IIDiamond 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 difficultCaution: Greedy also share this property!
Simple Problem Revisited IIDiamond 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 before di?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 IIVariant
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 propertiesOverlapping Sub-problemsOptimal SubstructureMemoryless
Next StepSometimes 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 StepSometimes 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 memorylessWe can compute all the previous states first, then
determine the currentBottom Up approach
Next StepAn event should be called a stateHow 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 StepAn event should be called a stateFrom 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,
}5,2,1,5.0{
)()(i
iNCountNCount
Bottom UpUsually the procedure of bottom up implementation
is
Very often you may see lots of loops in DP solutionsUsually transition takes place within the last loopRemember the base case!
Algorithm Bottom-UpBegin
For Any State N as current stateFor any previous state k
If condition is satisfied thenTransit state N from k
End Bottom-Up
Bottom UpProblem I, in bottom-up
Algorithm CountBegin
Let C[N], where C[0] = 1For i := 1 to N do
For j := {0.5, 1, 2, 5} doIf i – j >= 0 then
C[i] += C[i-j];End Count
TriangleGiven a cell-leveled triangle, with the ith level
has i cells, and N levels in totalTraverse from the top cell to the bottom, only
by two directions stated in the figureFind the largest sumFulfill the three properties?
7
6 9
8 1 4
2 4 5 3
TriangleDefining 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
TriangleHow 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 answerNeed extra check
6 9
1 iithth level level jjthth cell cell
TriangleHow to relate the current state with previous
states?Refer to the following diagram
State transition equation?Final answer?
S[1][1] only, hurray!
6 9
1 iithth level level jjthth cell cell
TriangleHow to relate the current state with previous
states?Different approaches are possible
Different state transition would lead to different efficiency
6 9
1 iithth level level jjthth cell cell6 9
1 iithth level level jjthth cell cell
Edit DistanceGiven two strings A and BYou can insert, delete or change a character
to AFind the minimum changes made to
transform A to Be.g. it takes 3 changes to transform “text” to
“tree”
Edit DistanceFirst, define the state (important!)
Think of what makes an eventCan 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 BFormulation?
More complex
Edit DistanceVariants
LCS (Longest Common Subsequence)Show me how you can transform it!
Non-spacious WritingYou have to write an article, single paragraphYou 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 lettersFor each line, the unused space shows the
emptiness by squaring its countMinimize the emptiness
Non-spacious WritingExample
“HKOI” “is” “the” “goodest” “and” “fun” “:o)”A line can only have 7 letters at most
Best layout
Emptiness = 9 + 1 + 16 = 26
H K O I
I S T H E
G O O D E S T
A N D F U N
: O )
Non-spacious WritingH K O I
I S T H E
G O O D E S T
A N D F U N
: O )
Determine the stateIs the number of line important?
State transition equation?Constraints
N < 1,000,000L < 100Length of any word < L
Algorithm?
Non-spacious WritingConsidering in different order the previous
states may lead to different complexitiesO(N2) vs O(NL)
Please think if your algorithm can pass the time limit and resources limit or not before implementing
Common ModeDP on rectangular array
Again we use more memory as a trade off for time
以空間換取時間DP on treesDP on ugly states
Special representation on state is required
Optimization for DPIf we need lots of different states, it probably
implies More for-loopsMore space needed for a stateState transition becomes expensive
Cure?Memory Optimization – Rolling Array (滾動數
組 )Runtime Optimization
Challenging ProblemTurkish RouletteA roulette of S slotsEach slot has a numberYou are given B ballsA ball occupies 2 slotsEach ball has its numberA slot can be occupied by one ballFor 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
1 -4 12
-10
22
1
-3531
6-8
70
0
-7
12
3
Challenging ProblemTurkish RouletteA roulette of S slotsEach slot has a number Xi
You are given B ballsEach ball has its number Yj
Conditions:3 < S < 2501 < B < └S / 2┘
-64 < Xi, Yj < 64, for 1 < i < S, 1 < j < BDerive the solution by yourself!
1 -4 12
-10
22
1
-3531
6-8
70
0
-7
12
3
Challenging ProblemPainting [HKOI IOI/NOI TFT 2008 Q3]N critical pointsConsecutive up-down-waveMinimize difference between up and down
areaMinimize total area for tie breaks
Practice ProblemsHKOJ 1010 – Diamond RingHKOJ 1053 – Longest Common SubstringHKOJ 1058 – The Triangle II
Last WordsLearning DP should be
Fun! The idea in solving a problem is very interesting
Beneficial! You can deal with more types of problems
Sorrow! Thinking how to solve is painfulSuggestion
Please understand this notes thoroughly in order to attend the next training
Q&AI hear and I forget. I see and I remember. I do and I understand.