HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are...

54
HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly Choi 2.[Advanced] Dynamic Programming, 24-04- 2004, by cx

Transcript of HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are...

Page 1: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 2: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 3: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 4: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 5: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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?

Page 6: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 7: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 8: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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?

Page 9: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 10: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 11: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 12: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 13: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 14: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 15: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 16: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

Simple Problem RevisitedWhy don’t we STORE the redundant calls?We call that Memo(r)izationThe implementation strategy is Top-DownPerformance?

Page 17: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

Simple Problem Revisited

Page 18: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 19: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

So far…We know that in solving DP questions it must

have the following propertiesOverlapping Sub-problems

Page 20: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 21: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

Simple Problem Revisited IIDiamond Chain [HKOJ 1010]When considering a diamond di

-8

510

-2

di-1

Calculating…

Page 22: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 23: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 24: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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 …

Page 25: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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!

Page 26: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 27: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

Simple Problem Revisited IIVariant

What if we consider a RING?di: My neighbors could be my past events’

candidates!

Page 28: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

So far…We know that in solving DP questions it must

have the following propertiesOverlapping Sub-problemsOptimal SubstructureMemoryless

Page 29: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 30: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 31: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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?

Page 32: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 33: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 34: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 35: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 36: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 37: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 38: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 39: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 40: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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”

Page 41: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 42: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

Edit DistanceVariants

LCS (Longest Common Subsequence)Show me how you can transform it!

Page 43: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 44: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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 )

Page 45: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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?

Page 46: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 47: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 48: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 49: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 50: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 51: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 52: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

Practice ProblemsHKOJ 1010 – Diamond RingHKOJ 1053 – Longest Common SubstringHKOJ 1058 – The Triangle II

Page 53: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

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

Page 54: HKOI Training 2009 Hackson Leung 28 th March 2009 Acknowledgement: References and slides are extracted from: 1. Dynamic Programming, 19-05-2007, by Kelly.

Q&AI hear and I forget. I see and I remember. I do and I understand.