Title: Advanced Dynamic Programming II
1Advanced Dynamic Programming II
2In 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)
3In this lesson...
- Dimension reduction
- DP on trees, graphs, etc.
- Game strategy - Minimax
4Dimension Reduction
- Reduce the memory complexity by one (or more)
dimension - Usually a rolling array is employed
5Triangle revisited
- Only a 2x5 array is needed
3
1 4
2 5 8
9 5 6 1
5 2 3 6 6
3
4 7
9 12 15
21 17 21 16
26 23 24 27 22
A
F
6Rolling array
3
3
9 12 15
26 23 24 27 22
4 7
4 7
21 17 21 16
9 12 15
F
21 17 21 16
26 23 24 27 22
F
7LCS revisited
- Recall the recurrence
- Fi,j Fi-1,j-11 if AiBj
- Fi,j maxFi-1,j,Fi,j-1 if Ai?Bj
- Note that Fi,? only depends on Fi,? and
Fi-1,? - Thus we can just keep 2 rows
8Non-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)
9Path Counting
- A graph is a directed acyclic graph (DAG) if it
is directed and has no cycles
This is not a DAG.
This is a DAG.
10Path 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
11Path (example)
- s A, t E
- Paths
- ABDE, ACBDE, ACDE
- Answer 3
12Path (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
13Path (solution)
- Obviously the three paths shown below must be
distinct - Even if they meet at some intermediate vertices!
s
C
B
A
...
...
...
t
14Path (solution)
7
s
6
5
t
2
3
4
1
15Path (solution)
- Number of paths from vertex to t
3
7
s
6
2
1
5
t
1
2
3
4
1
0
1
0
16Path (solution)
- Algorithm
- Tsort the vertices
- Set Fv 0 for every vertex v
- Set Ft 1
- Following topological order, for each vertex v
- For each outgoing edge (v, u)
- Fv Fv Fu
- Time complexity
- Tsort O(VE)
- DP O(VE)
- Total O(VE)
17Path (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
18Longest Path in Tree I
- Given a weighted tree T, find the length of the
longest path from a given node s
s
5
7
4
5
6
3
19Longest I (simple solution)
s
5
7
5
6
3
4
20Longest I (simple solution)
- Calculate the nodes distances from s (in
pre-order/level-order)
0
s
5
5
7
5
6
10
3
4
12
11
14
13
21Longest I (another solution)
- A longest path must end at one of the leaves
s
5
7
5
6
3
4
22Longest I (another solution)
- Let Fv be the longest distance between v to one
of its descendant leaves - For example, Fx 9
s
x
5
7
5
6
3
4
23Longest I (another solution)
- Compute F in post-order
- Fx 0 for every leaf x
- Fv max Fulength(v,u)
u ? C(v)
s
14
answer
5
9
7
5
6
4
3
4
0
0
0
0
24Longest I (another solution)
- Algorithm
- Longest_One(vertex v)
- if (v is a leaf)
- Fv ? 0
- else
- Fv ? 0
- for each child u of v do
- Longest_One(u)
- if (Fulength(v,u)
- Fv ? Fulength(v,u)
-
25Longest I (another solution)
- Time complexity O(V)
- No overlapping subproblems
- F is redundant!
26Longest Path in Tree II
- Given a weighted tree T (all weights positive),
find the length of the longest path in T
5
7
4
5
6
3
27Longest II (solution)
- Take any node and make it root
28Longest II (solution)
- A longest path must be a leaf-to-leaf or a
root-to-leaf path - Must it pass the root?
29Longest II (solution)
- Let z be the uppermost node in the longest path
- Only two cases
the only common node is z
30Longest II (solution)
- As in Longest I, let Fv be the longest distance
between v to one of its descendant leaves - Define G as follows
- Gv Fv if v has less than 2 children
- Gv maxFulength(v,u) second_max
Fwlength(v,w) - Note that max may equal second_max
u ? C(v)
w ? C(v)
31Longest II (demonstration)
12
(75)(04) 16
7
(07)(06) 13
0
0
0
0
0
0
0
0
0
0
32Longest II (demonstration)
- Computing G from F (again)
14
14
9
(45)(07) 16
4
(04)(03) 7
0
0
0
0
0
0
0
0
33Longest 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
34Simplified 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?
35Gems (example)
36Gems (attempt)
- Make the tree a rooted one first
37Gems (attempt)
- Let Gv be the minimum cost to cover all nodes
in the subtree rooted at v - How to set up the recurrence?
38Gems (solution)
- Let Fv,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
- Fx,c c for every leaf x and 1 ? c ? M
- Progress
- Fv,c ? min Fu,d c
- Post-order traversal
u ? C(v)
1?d?M,d?c
39Gems (demostration)
12
11
11
12
7
5
6
7
1
2
3
4
1
2
3
4
1
2
3
4
1
2
3
4
1
2
3
4
1
2
3
4
40Gems (solution)
- Algorithm (recursive, non-DP)
- Gems(vertex v,integer c)
- if (v is leaf) return c
- value ? c
- for each child u of v do
- temp ? 8
- for d ? 1 to M do
- if (d ? c)
- temp ? mintemp, Gems(u,d)
- value ? value temp
- return value
41Gems (solution)
- Algorithm (DP)
- Gems_DP(vertex v)
- if (v is a leaf)
- set base case and exit
- for each child u of v do
- Gems_DP(u)
- for c ? 1 to M do
- Fv,c ? c
- for each child u of v do
- temp ? 8
- for d ? 1 to M do
- if (d ? c)
- temp ? mintemp, Fu,d
- Fv,c ? temp c
-
42Gems (solution)
- Time complexity
- Computing Fv,c O(M children of v)
- Computing Fv,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??
43Game 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
44Game-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
45Game 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
46Card 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
47Card (example)
A
B
2
1
9
7
1
4
3
17
14
4
48Card (game tree)
- N 4
- Only 5 different states
As move
1
2
3
4
Bs move
2
3
4
3
4
4
3
4
4
NULL
4
NULL
NULL
4
NULL
NULL
NULL
NULL
49Minimax
- 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
50Minimax
- Suppose A wants to maximize his final score
(value), which move should he make?
1
max
-1
1
-2
min
min
min
51Minimax
As move
Bs move
1
3
9
8
1
-1
2
8
-4
-1
2
-2
7
9
7
5
52Minimax
As move
Bs move
2
2
1
1
2
3
8
1
-1
9
8
1
-1
2
3
8
9
8
-1
-2
9
1
-1
2
8
-4
-1
2
-2
7
9
7
5
53Tic-tac-toe
- O wants to maximize the value
- Is this a winning state for O?
O X
O O
X X
O X
O O O
X X
O X
O 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
O
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 0
value 1
54Card Picking revisited
- Let the F-value of a state be the maximum
difference (preserve /- sign) between your score
and your opponents score if the game starts from
this state (assume that your opponent plays
perfectly)
55Card Picking revisited
- A transition may alter the F-value
- Two states that appear the same may have
different F-values!
56Card Picking revisited
- We can still apply the concept of Minimax
1
2
3
4
2
1
6
3
2
3
4
3
4
-9
-7
4
-4
-2
-9
-3
-7
-4
-5
3
4
7
4
NULL
4
4
4
NULL
NULL
0
0
0
7
3
4
4
4
NULL
NULL
-4
NULL
0
0
0
-4
NULL
0
57Card 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
58Conclusion
- 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
59The end
- Prepare for TFT (19 June)..
- ..as well as your exam
- Have a nice holiday!