45
HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man,

HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Embed Size (px)

Citation preview

Page 1: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

HKOI2009 Training (Advanced Group)

(Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Page 2: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Review

Recurrence relation Dynamic programming

State & Recurrence FormulaOptimal substructureOverlapping subproblems

2

Page 3: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Outline

Dimension reduction (memory) “Ugly” optimal value functions DP on tree structures Two-person games

3

Page 4: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Dimension reduction Reduce the space complexity by one or

more dimensions “Rolling” array Recall: Longest Common Subsequence

(LCS) Base conditions and recurrence relation:

Fi,0 = 0 for all iF0,j = 0 for all jFi,j = Fi-1,j-1 + 1 (if A[i] = B[j])

max{ Fi-1,j , Fi,j-1 }(otherwise)

4

Page 5: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Dimension Reduction

A: stxc, B: sicxtc

5

0

0

0

0

0 0 0 0 0 0 0

0 1 2 3 4 5 6

1

2

3

4

0

1 1 1

1 1 1

1 1

1 2

1 1 1

1 1 2

2 2

2 2

1

2

2

3

0

0 1 2 3 4 5 6

0 0 0 0 0 0 00

1 1 1 1 1 11

2 1 1 1 1 2 2

3 1 1 1 2 2 2

4 1 1 2 2 2 3

Page 6: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Dimension Reduction We may discard old table entries if they

are no longer needed Instead of “rolling” the rows, we may

“roll” the columns Even less memory (52 entries)

Space complexity: (min{N, M}) Drawback

Backtracking is difficult That means we can get the number but not

the sequence easily.

6

Page 7: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

(Simplified) Cannoneer Base How many non-overlapping cross pieces

can be put onto a HW grid? W ≤ 10, H is arbitrary

A cross piece:

There may be patterns, but we just focus on a DP solution

7

Packing 8 cross pieces onto a 10 6 grid

Page 8: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

(Simplified) Cannoneer Base We place the pieces from top to bottom

Phase k - putting all pieces centered on row k-1

In phase k, we only need to consider the occupied squares in rows k-2 and k-1

8

k -2k -1

k

?

Phase 3Phase 4Phase 5Phase 6Phase 7Phase 8Phase 9Phase 10

Page 9: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

(Simplified) Cannoneer Base The optimal value function C is defined by:

C(k,S) = the max number of pieces after phase k, with rows k-1 and k giving the shape S

How to represent a shape?In a shape, each column can beUse 2, 1, 0 to represent these 3 casesA shape is a W-digit base-3 integerFor example, the following shape is encoded as

010121(3) = 97(10)

9

Page 10: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

(Simplified) Cannoneer Base The recurrence relation is easy to

construct Max possible number of states = H 3W

That’s why W ≤ 10

Cannoneer Base appeared in NOI2001 Bugs Integrated, Inc. in CEOI2002

requires similar techniques

10

Page 11: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Dynamic Programming on Tree Structures States may be (related to) nodes on a

graph Usually directed acyclic graphs

Topological order is the obvious order of recurrence evaluation

Trees are special graphs A lot of graph DP problems are based on trees

Two major types: Rooted tree DP Unrooted tree DP

11

Page 12: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Rooted Tree Dynamic Programming Base cases at the leaves Recurrence at a node involves its child

nodes only Solution

Evaluate the recurrence relation from leaves (bottom) to the root (top)

Top-down implementations work wellTime complexity is often (N) where N is

the number of nodes

12

Page 13: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Unrooted Tree Dynamic Programming No explicit roots given Two cases

The problem can be transformed to a rooted oneIt can’t, so we try root every node

Case 2 increases the time complexity by a factor of N

Sometimes it is possible to root one node in O(N) time and each subsequent node in O(1)overall O(N) time

13

Page 14: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Node Heights Given a rooted tree T The height of a node v in T is the

maximum distance between v and a descendant of v

For example, all leaves have height = 0 Find the heights of all nodes in T Notations

C(v) = the set of children of vp(v) = the parent of v

14

Page 15: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Node Heights Optimal value function

H(v) = height of node v Base conditions

H(u) = 0 for all leaves u Recurrence

H(v) = max { H(x) | x C(v) } + 1 Order of evaluation

All children must be evaluated before selfPost-order

15

Page 16: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Node Heights

Example

16

A

B C

D E F G

I

H

H(I) = 0

H(E) = 0 H(F) = 0 H(G) = 0 H(H) = 0H(D) = 1

H(B) = 2 H(C) = 1

H(A) = 3

Page 17: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Node Heights Time complexity analysis

Naively There are N nodes A node may have up to N-1 children Overall time complexity = O(N2)

A better bound The H-value of a v is at most used by one

other node – p(v) The total number of H-values inside the “max

{}”s = N-1 Overall time complexity = (N)

17

Page 18: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Treasure Hunt

N treasures are hidden at the N nodes of a tree (unrooted)

The treasure at node u has value v(u) You may not take away two treasures

joined by an edge, otherwise missiles will fly to you

Find the maximum value you can take away

18

Page 19: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Treasure Hunt

Let’s see if the problem can be transformed to a rooted one

We arbitrarily root a node, say r How to formulate?

19

Page 20: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Treasure Hunt Optimal value function

Z(u,b) = max value for the subtree rooted at u and it is b that the treasure at u is taken away b = true or false

Base conditions Z(x,false) = 0 and Z(x,true) = v(x) for all leaves

x Recurrence

Z(u,true) = Z(c, false) + v(u) Z(u,false) = max { Z(c,false), Z(c,true) }

Answer = max { Z(r,false), Z(r,true) }

20

c C(u)

c C(u)

Page 21: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Treasure Hunt

Example (values shown in squares)

21

7

2 6

9 3 5 1

1

2

false: 0true: 1

false: 0true: 3

false: 0true: 5

false: 0true: 1

false: 0true: 2

false: 1true: 9

false: 12true: 3

false: 8true: 6

false: 20true: 27

Page 22: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Treasure Hunt

Our formulation does not exploit the properties of a tree root

Moreover the correctness of our formulation can be proven by optimal substructure

Thus the unrooted-to-rooted transformation is correct

Time complexity: (N)

22

Page 23: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Unrooted Tree DP – Basic Idea In rooted tree DP, a node asks (request)

for information from its children; and then provides (response) information to its parent

23

ResponseRequest

Page 24: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Unrooted Tree DP – Basic Idea In unrooted tree DP, a node also makes

a request to its parent and sends response to its children

Imagine B is the root A sends information about the “subtree”

{A,C} to B

24

A

B C

D

ResponseRequest

E

Page 25: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Unrooted Tree DP – Basic Idea Similarly we can root C, D, E and get

different request-response flows These flows are very similar The idea of unrooted tree DP is to root

all nodes without resending all requests and responses every time

25

A

B C

D E

A

B C

D E

Page 26: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Unrooted Tree DP – Basic Idea Root A and do a complete flow A knows about subtrees {B,D,E} and {C} Now B sends a request to A A sends a response to B telling what it

knows about {A,C} B already knows about {D}, {E} Rooting of B completes

26

A

B C

D E

Page 27: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Unrooted Tree DP – Basic Idea Now let’s root D D sends a request to B B knows about {A,C}, {D}, and {E};

combining {A,C}, {E} and B itself, B knows about {B,A,C,E}, and sends a response to D

Rooting of D completes

27

A

B C

D E

Page 28: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Unrooted Tree DP – Basic Idea Rooting a new node requires only one

request and one response if its parent already knows about all its subtrees (including the “imaginary” parent subtree)

Further questions: Fast computation of {B,A,C,E} from

{A,C} and {E}? (rooting of D) Fast computation of {B,A,C,E,D}

from {A,C}, {E}, {D}?

(rooting of B)

28

A

B C

D E

Page 29: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree Given an unrooted tree T, denote its rooted

tree with root r by T(r) Find a node v such that T(v) has the

minimum height among all T(u), u T The height of a tree = the height of its root

Solution Just root every node and find the min height

We know how to find a height of a tree Trivially this is (N2)

Now let’s use what we learnt

29

Page 30: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree

Since parents and children are unclear now, we use slightly different notations

N(v) = the set of neighbors of v H(v, u) = height of the subtree rooted at

v if u is treated as the parent of v H(v, ) = height of the whole tree if v is

root

30

Page 31: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree

Root A, complete flow Height = 3

31

A

B C

D E

F H(F,D) = 0

H(E,B) = 0H(D,B) = 1

H(B,A) = 2 H(C,A) = 0

H(A,) = 3

Page 32: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree

Root BRequest: B asks A for H(A,B)How can A give the

answer in constant

time?

32

A

B C

D E

F H(F,D) = 0

H(E,B) = 0H(D,B) = 1

H(B,A) = 2 H(C,A) = 0

H(A,) = 3

Page 33: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree

Suppose now B asks A for H(A,B), how can A give the answer in constant time?

Two casesB is the only largest subtree of A in T(A)B is not the only largest subtree, or B is not

a largest subtree

33

A

B C D E F G H I J KH(B,A)=7

H(C,A)=8

H(D,A)=1

H(E,A)=2

H(F,A)=9 H(H,A)=4 H(J,A)=3

H(G,A)=5 H(I,A)=6 H(K,A)=0

H(A,)=10

Page 34: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree

(1) B is the only largest subtree of A in T(A)H(A,B) < H(A,)H(A,B) depends on the second largest

subtreeTrick: record the second largest subtree of A

(2) B is not the only largest subtree, or B is not a largest subtreeH(A,B) = H(A,)

34

Page 35: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree

To distinguish case (1) from case(2), we need to record the two largest subtrees of AWhen?

○ When we evaluate H(A,)

Back to our example

35

Page 36: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree

Root BRequest: B asks A for H(A,B)Response: 1

36

A

B C

D E

F H(F,D) = 0

H(E,B) = 0H(D,B) = 1

H(B,A) = 2 H(C,A) = 0

H(A,) = 31st = B, 2nd = C

1st = D, 2nd = E

1st = , 2nd =

1st = F, 2nd =

1st = , 2nd =

1st = , 2nd =

A

H(A,B) = 1

Page 37: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree

Root BH(B,) = 2 can be calculated in constant

time

37

A

B C

D E

F H(F,D) = 0

H(E,B) = 0H(D,B) = 1

H(B,A) = 2 H(C,A) = 0

H(A,) = 31st = B, 2nd = C

1st = D, 2nd = E

1st = , 2nd =

1st = F, 2nd =

1st = , 2nd =

1st = , 2nd =

A

H(A,B) = 1

H(B,) = 2

Page 38: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree Root D

Request: D asks B for H(B,D)Response: 2H(D,) = 3

38

A

B C

D E

F H(F,D) = 0

H(E,B) = 0H(D,B) = 1

H(B,A) = 2 H(C,A) = 0

H(A,) = 31st = B, 2nd = C

1st = D, 2nd = E

1st = , 2nd =

1st = F, 2nd =

1st = , 2nd =

1st = , 2nd =

A

H(A,B) = 1

H(B,) = 2

B F

H(D,) = 3

H(B,D) = 2

Page 39: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Shortest Rooted Tree Root F, E, and C in the same fashion In general, root the nodes in preorder Time complexity analysis

Root A – (N)Root each subsequent nodes – O(1)Overall - (N)

The O(1) is crucial for the linearity of our algorithmIf rooting of a new node cannot be done fast, unrooted

tree DP may not improve running time

39

Page 40: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Two-person Games

Often appear in competitions as interactive tasksPlaying against the judge

Most of them can be solved by the Minimax method

40

Page 41: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Game Tree

A (finite or infinite) rooted tree showing the movements of a game play

41

… … …

… … …

o

xo

xo

o o o

x o x oo x o x

Page 42: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Game Tree

This is a game The boxes at the bottom show your gain

(your opponent’s loss) Your opponent is clever How should you play to maximize your

gain?

42

2 9 4 5 6 6 8 1 7 6End of game

Your turn

Her turn

WHY?

Page 43: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Minimax

You assume that your opponent always try to minimize her loss (minimize your gain)

So your opponent always takes the move that minimize your gain

Of course, you always take the move that maximize your gain

43

2 9 4 5 6 6 8 1 7 6

Your turn

Her turn

9 4 5 6

6

8 7

4 6 7

7

Page 44: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Minimax

Efficient?Only if the tree is small

In fact the game tree may in fact be an expanded version of a directed acyclic graph

Overlapping subproblems memo(r)ization

44

A

B

D

C

1

2

A

B C D

1 2

D

1 22

D

1 2

Page 45: HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)

Past Problems IOI

2001 Ioiwari (game), Score (game), Twofive (ugly) 2005 Rivers(tree)

NOI 2001 炮兵陣地 (ugly), 2002 貪吃的九頭龍 (tree),

2003 逃學的小孩 (tree), 2005 聰聰與可可 IOI/NOITFT

2004 A Bomb Too Far (tree) CEOI

2002 Bugs (ugly), 2003 Pearl (game) Balkan OI

2003 Tribe (tree) Baltic OI

2003 Gems (tree) On HKOI Judge

1074 Christmas Tree

45