advanced dynamic programming ii

59
Advanced Dynamic Programming II HKOI Training Team 2004

Upload: alda

Post on 20-Jan-2016

60 views

Category:

Documents


1 download

DESCRIPTION

Advanced Dynamic Programming II. HKOI Training Team 2004. In the previous lesson. What is DP? Some examples of DP Probably NOT enough for you to solve DP problems in IOI/NOI Except those classic ones To identify a DP problem, the keys are Recurrence Optimal substructure - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advanced  Dynamic Programming II

Advanced Dynamic

Programming II

HKOI Training Team 2004

Page 2: Advanced  Dynamic Programming II

2

In the previous lesson... What is DP? Some examples of DP Probably NOT enough for you to solve DP

problems in IOI/NOI Except those classic ones

To identify a DP problem, the keys are Recurrence Optimal substructure Experience (Chinglish(?) - “DP feel”)

Page 3: Advanced  Dynamic Programming II

3

In this lesson... Dimension reduction DP on trees, graphs, etc. Game strategy - Minimax

Page 4: Advanced  Dynamic Programming II

4

Dimension Reduction Reduce the memory complexity by one (or

more) dimension Usually a “rolling” array is employed

Page 5: Advanced  Dynamic Programming II

5

Triangle revisited Only a 2x5 array is needed

3

1 4

2 5 8

9 5 6 1

5 2 3 6 6

A F

3

4 7

9 12 15

21 17 21 16

26 23 24 27 22

Page 6: Advanced  Dynamic Programming II

6

4 7

9 12 15

21 17 21 16

26 23 24 27 22

Rolling array

F

33

4 7

9 12 15

21 17 21 16

26 23 24 27 22

F’

Page 7: Advanced  Dynamic Programming II

7

LCS revisited Recall the recurrence F[i,j] = F[i-1,j-1]+1 if A[i]=B[j] F[i,j] = max{F[i-1,j],F[i,j-1]} if A[i]B[j] Note that F[i,?] only depends on F[i,?] and

F[i-1,?] Thus we can just keep 2 rows

Page 8: Advanced  Dynamic Programming II

8

Non-rectangular structures DP can also be applied on graphs, trees,

etc. Usually structures with no cycles

Recurrence should not contain cycles! Rooted tree is a recursive structure Notation

C(v): the set of children of v (in a rooted tree)

Page 9: Advanced  Dynamic Programming II

9

Path Counting A graph is a directed acyclic graph (DAG) if

it is directed and has no cycles

This is a DAG.

This is not a DAG.

Page 10: Advanced  Dynamic Programming II

10

Path Counting Given a DAG G, and two vertices of G, s

and t, count the number of distinct paths from s to t What if I give you a graph with directed cycles?

How is the graph given to you? Adjacency matrix Adjacency list Other ways

Page 11: Advanced  Dynamic Programming II

11

Path (example) s = A, t = E Paths:

ABDE, ACBDE, ACDE Answer = 3

A

B

C

FE

D

Page 12: Advanced  Dynamic Programming II

12

Path (an attempt) Use DFS to find out all paths from s to t

Simple enough, but consider this graph How many paths from s to t?

24 = 16

s

t

Page 13: Advanced  Dynamic Programming II

13

Path (solution) Obviously the three paths shown below

must be distinct Even if they meet at some intermediate

vertices!

C

A

t

...

... ...

s

B

Page 14: Advanced  Dynamic Programming II

14

Path (solution) Topological order

s

t

1

23 4

5

6

7

Page 15: Advanced  Dynamic Programming II

15

Path (solution) Number of paths from vertex to t

s

t 1

0

1

1

2

3

1

23 4

0

5

6

7

Page 16: Advanced  Dynamic Programming II

16

Path (solution) Algorithm

Tsort the verticesSet F[v] = 0 for every vertex vSet F[t] = 1Following topological order, for each vertex v

For each outgoing edge (v, u)F[v] = F[v] + F[u]

Time complexity Tsort – O(V+E) DP – O(V+E) Total – O(V+E)

recurrencerelation

Page 17: Advanced  Dynamic Programming II

17

Path (extensions) Longest path in DAG

Given a weighted DAG G, find the length of a longest path from s to t

Shortest path counting Given a weighted graph G, find the number of

shortest paths from s to t

Page 18: Advanced  Dynamic Programming II

18

Longest Path in Tree I Given a weighted tree T, find the length of

the longest path from a given node s

s7

5

65

3

4

Page 19: Advanced  Dynamic Programming II

19

Longest I (simple solution) Make s the root

s

7

5

65

34

Page 20: Advanced  Dynamic Programming II

20

Longest I (simple solution) Calculate the nodes’ distances from s (in

pre-order/level-order)

s

7

5

65

34

0

5

12 11

10

14 13

Page 21: Advanced  Dynamic Programming II

21

Longest I (another solution) A longest path must end at one of the

leaves

s

7

5

65

34

Page 22: Advanced  Dynamic Programming II

22

Longest I (another solution) Let F[v] be the longest distance between v

to one of its descendant leaves For example, F[x] = 9

s

7

5

65

34

x

Page 23: Advanced  Dynamic Programming II

23

Longest I (another solution) Compute F in post-order F[x] = 0 for every leaf x F[v] = max {F[u]+length(v,u)}

s

7

5

65

34

u C(v)

00

00

4

9

14 answer

Page 24: Advanced  Dynamic Programming II

24

Longest I (another solution) Algorithm

Longest_One(vertex v) {if (v is a leaf)

F[v] 0else

F[v] 0for each child u of v do

Longest_One(u)if (F[u]+length(v,u)

F[v] F[u]+length(v,u)}

Page 25: Advanced  Dynamic Programming II

25

Longest I (another solution) Time complexity – O(V) No overlapping subproblems F[] is redundant!

Page 26: Advanced  Dynamic Programming II

26

Longest Path in Tree II Given a weighted tree T (all weights

positive), find the length of the longest path in T

7

5

65

3

4

Page 27: Advanced  Dynamic Programming II

27

Longest II (solution) Take any node and make it root

756

53

4

7

56

5

34

Page 28: Advanced  Dynamic Programming II

28

Longest II (solution) A longest path must be a leaf-to-leaf or a

root-to-leaf path Must it pass the root?

756

53

4

7

56

5

34

Page 29: Advanced  Dynamic Programming II

29

Longest II (solution) Let z be the uppermost node in the longest

path Only two cases

z z

the only common node is z

Page 30: Advanced  Dynamic Programming II

30

Longest II (solution) As in Longest I, let F[v] be the longest

distance between v to one of its descendant leaves

Define G as follows G[v] = F[v] if v has less than 2 children G[v] = max{F[u]+length(v,u)} +

second_max {F[w]+length(v,w)}

Note that max may equal second_max

u C(v)

w C(v)

Page 31: Advanced  Dynamic Programming II

31

Longest II (demonstration) Computing G from F

75

6

5

3

4

0 0

0 0

7

12

0 000

0 0

(0+7)+(0+6) = 13

(7+5)+(0+4) = 16

Page 32: Advanced  Dynamic Programming II

32

Longest II (demonstration) Computing G from F (again)

5

7

65

340 0

0 0

0 0

0 0

4

9

14

(0+4)+(0+3) = 7

(4+5)+(0+7) = 16

14

Page 33: Advanced  Dynamic Programming II

33

Longest II (solution) Time complexity

Computing F – O(V) Computing G – O(V) Total – O(V)

F and G can be computed together Not quite a DP problem

Page 34: Advanced  Dynamic Programming II

34

Simplified Gems Given a tree T with N nodes Each node is to be covered by a gemstone Costs of gemstones: 1, 2, 3, …, M There is an unlimited supply of gemstones Two adjacent nodes must contain

gemstones of different costs What is the minimum total cost?

Page 35: Advanced  Dynamic Programming II

35

Gems (example) N = 8, M = 4

2

3

1

1

1

1

1

1

Page 36: Advanced  Dynamic Programming II

36

Gems (attempt) Make the tree a rooted one first

Page 37: Advanced  Dynamic Programming II

37

Gems (attempt) Let G[v] be the minimum cost to cover all

nodes in the subtree rooted at v How to set up the recurrence?

Page 38: Advanced  Dynamic Programming II

38

Gems (solution) Let F[v,c] be the minimum cost to cover all

nodes in the subtree rooted at v and the cost of the gemstone covering v is c

Base cases F[x,c] = c for every leaf x and 1 c M

Progress F[v,c] = min {F[u,d]} + c

Post-order traversalu C(v) 1dM,dc

Page 39: Advanced  Dynamic Programming II

39

Gems (demostration) M = 4

1

2

3

4

1

2

3

4

1

2

3

4

1

2

3

4

7

5

6

7

1

2

3

4

1

2

3

4

12

11

11

12

Page 40: Advanced  Dynamic Programming II

40

Gems (solution) Algorithm (recursive, non-DP)

Gems(vertex v,integer c) {if (v is leaf) return cvalue cfor each child u of v do temp ∞ for d 1 to M do if (d c) temp min{temp, Gems(u,d)} value value + tempreturn value }

Page 41: Advanced  Dynamic Programming II

41

Gems (solution) Algorithm (DP)

Gems_DP(vertex v) {if (v is a leaf) set base case and exitfor each child u of v do Gems_DP(u)for c 1 to M do F[v,c] c for each child u of v do temp ∞ for d 1 to M do if (d c) temp min{temp, F[u,d]} F[v,c] temp + c

}

Page 42: Advanced  Dynamic Programming II

42

Gems (solution) Time complexity

Computing F[v,c] – O(M × #children of v) Computing F[v,c] for all vertices – O(MN) Computing all entries – (M2N)

The time complexity can be reduced to O(MN) with a “trick”

The original problem allows N to be as large as 10000 and M arbitrarily large Even O(N2) is too slow How to solve it??

Page 43: Advanced  Dynamic Programming II

43

Game strategies Not closely related to DP Almost all game-type problems in

IOI/BOI/CEOI requires the concept of Minimax

DP is needed in most of these problems

Page 44: Advanced  Dynamic Programming II

44

Game-type problems Usually interactive problems Write a program to play a simple two-

player game with a judging program e.g. play tic-tac-toe with the judging program

Often the judging program uses an optimal strategy

Page 45: Advanced  Dynamic Programming II

45

Game tree A (finite or infinite) rooted tree showing

the movements of a game play

O

O O O

X

O

X

O

O X O X

… …X O X O

… … …

Page 46: Advanced  Dynamic Programming II

46

Card Picking A stack of N cards with numbers on them Two players take turns to take cards from

the top of the stack 1, 2, or 3 cards can be taken in each turn Game ends when all cards have been

taken The player with a higher total score (sum

of numbers) wins

Page 47: Advanced  Dynamic Programming II

47

Card (example)

2

1

9

7

1

4

3

4

A B

17 14

Page 48: Advanced  Dynamic Programming II

48

Card (game tree) N = 4 Only 5 different states

1

2

3

4

2

3

4 4

3

4

3

4 4 NULL 4 NULL NULL

4 NULL NULL

NULL

A’s moveB’s move

NULL

Page 49: Advanced  Dynamic Programming II

49

Minimax A recursive algorithm for choosing the

next move in a two-player game A value is associated with each state

e.g. in tic-tac-toe, all winning states may have value 1

We assume that the other player always chooses his best move

Page 50: Advanced  Dynamic Programming II

50

Minimax Suppose A wants to maximize his final

score (value), which move should he make?

1 -1 4 3 2 1 7 -2

A’s moves

B’s moves

-1 1 -2

1

min min min

max

Page 51: Advanced  Dynamic Programming II

51

Minimax Again!

1

3 9 8

-1 2 8 -4 -1 2 -2 7 97 5

A’s moveB’s move

1

Page 52: Advanced  Dynamic Programming II

52

Minimax Answer: left move

2

2 1 1

2 3 8 1 -1 98 1

-1 2 3 8 9 8-1 -2 9

-1 2 8 -4 -1 2 -2 7 97 5

A’s moveB’s move

1

Page 53: Advanced  Dynamic Programming II

53

Tic-tac-toe O wants to

maximize the value Is this a winning

state for O? O X

O O O

X X

O X

O O

O X X

O X

O O

X X

O O X

O O

X X

value: 1

X O X

O O

O X X

O X

X O O

O X X

O O X

X O O

X X

O O X

O O

X X X

value: -1

O O X

X O O

O X X

X O X

O O O

O X X

O O X

X O O

O X X

value: 0 value: 1 value: 0

O

X

Page 54: Advanced  Dynamic Programming II

54

Card Picking revisited Let the F-value of a state be the maximum

difference (preserve +/- sign) between your score and your opponent’s score if the game starts from this state (assume that your opponent plays perfectly)

Page 55: Advanced  Dynamic Programming II

55

Card Picking revisited A transition may alter the F-value

Two states that appear the same may have different F-values!

9

NULLF-value: 0

F-value: 9

9

NULLF-value: 0

F-value: -9

Page 56: Advanced  Dynamic Programming II

56

Card Picking revisited We can still apply the concept of Minimax

1

2

3

4

2

3

4 4

3

4

3

4 4 NULL 4 NULL

4 NULL NULL

NULL

NULL

0

0 0

0 0 0

-4

47

NULL0

4

+7+3 +4 +4

-4

-2

-5 -9

-9

-3 -7 -4

-7-4

+1+3 +6

2

Page 57: Advanced  Dynamic Programming II

57

Card Picking revisited A recurrence can be set up Many overlapping sub-problems, so DP! Find the optimal move by backtracking Most game-type problems in OI

competitions are similar to this game

Page 58: Advanced  Dynamic Programming II

58

Conclusion Many DP problems discussed are now

classics More and more atypical DP problems in

competitions (esp. on trees) Still not enough for solving some difficult

IOI/NOI/BOI/CEOI DP problems We hope that those problems can be

covered in the summer vacation Practice, practice and practice

Page 59: Advanced  Dynamic Programming II

59

The end Prepare for TFT (19 June)..

..as well as your exam Have a nice holiday!