# HKOI Training 2009 Kelly Choi 27 June 2009 Acknowledgements: References and slides extracted from - PowerPoint PPT Presentation

1 / 43

HKOI Training 2009 Kelly Choi 27 June 2009 Acknowledgements: References and slides extracted from [Advanced] Dynamic Programming, 24-04-2004, by Chi-Man Liu [Intermediate] Dynamic Programming, 21-08-2004, by Ng Tung. Prerequisites. Concepts in Recurrence Basic Recursion Functions

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

HKOI Training 2009 Kelly Choi 27 June 2009 Acknowledgements: References and slides extracted from

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

#### Presentation Transcript

HKOI Training 2009

Kelly Choi

27 June 2009

Acknowledgements:

References and slides extracted from

[Advanced] Dynamic Programming, 24-04-2004, by Chi-Man Liu

[Intermediate] Dynamic Programming, 21-08-2004, by Ng Tung

### Prerequisites

• Concepts in Recurrence

• Basic Recursion

• Functions

• Divide-and-conquer

### Introduction

• Components of DP

• A recurrence formula (with state & state value)

• Memo(r)ization

• Characteristics of DP Problems

• Overlapping Subproblems

• Optimal Substructure

• Memoryless Property

### Grid Path Counting

• In a N*M grid, we want to move from the top left cell to the bottom right cell

• You may only move down or right

• Some cells may be blocked

• Example of one path:

### Naïve Algorithm: DFS

• DFS(x,y){

if (x <= N) and (y <= M) {

if (x = N) and (y = M)

return 1 {base case}

else if (x,y) is not blocked

return DFS(x+1,y) + DFS(x,y+1)

else

return 0 {blocked cell}

}

}

### Time Complexity

• Exponential time complexity:

• Each time the procedure branches into two paths

• Alternatively

• The base case is reached as many times as the number of paths, so the time complexity is Ω(number of paths)

• Have we done anything redundant?

DFS(1,1)

DFS(1,2)

DFS(2,1)

DFS(1,3)

DFS(2,2)

DFS(2,2)

DFS(3,1)

DFS(1,4)

DFS(2,3)

DFS(2,3)

DFS(3,2)

DFS(2,3)

DFS(3,2)

### Overlapping Subproblems

• Note that DFS(2,2) is called twice, DFS(2,3) three times, etc.

• But the work performed by these DFS(2,2) calls are unaffected by what called them, and thus are redundant

• Memorize the values these calls return, and avoid the redundant work

### Memo(r)ization

• Compute and store the value of DFS(x,y) in the first time we called it.

• Afterwards, retrieve the value of DFS(x,y) from the table directly, without calling DFS(x+1,y) and DFS(x,y+1) again

• This is called recursion with memo(r)ization.

• Time complexity: O(NM)

### Example : Grid Path Counting

• DFS(x,y){

If (x <= N) and (y <= M){

If (Memory[x,y] = -1) {

If (x = N) and (y = M)

Memory[x,y]← 1

else if (x,y) is not blocked

Memory[x,y] ← DFS(x+1,y) + DFS(x,y+1)

else

Memory[x,y] ← 0

}

return Memory[x,y];

}

}

### Bottom-up approach

• Iterative, Without the use of function calls

• Consider the arrays Grid[x,y] and Memory[x,y]:

• Treat DFS(x,y) not as a function, but as an array

• Evaluate the values for DFS[x,y] row-by-row, column-by-column

### Example : Grid Path Counting

• DFS[N,M] ← 1

For x ← 1 to N Do

If (x,M) is blocked Then DFS[x,M] ← 0 Else DFS[x,M] ← 1

For y ← 1 to M Do

If (N,y) is blocked Then DFS[N,y] ← 0 Else DFS[N,y] ← 1

For x ← N-1 downto 1 Do

For y ← M-1 downto 1 Do

If (x,y) is blocked Then

DFS[x,y] ← 0

Else

DFS[x,y] ← DFS[x+1,y] + DFS[x,y+1]

### Order of Computation

• Top-down approach: Recursive function calls with memo(r)ization

• Easy to implement

• Avoid calculating values for impossible states

• Further optimization possible

• Bottom-up approach

• Easy to code once the order is identified

• Avoid function calls

• Usually prefered

• But we need to see the dependence of states on other states to decide the order

### Components of DP

• It is very important to be able to describe a DP algorithm

• Two essential components of DP

• State(狀態) and the state value:

• State – a description of the subproblem

• State value – the value we need according to the subproblem, usually optimal in some sense, but can be defined flexibly.

• Recurrence – A rule describing the relationship between states, with base cases

### Grid Path Counting

• In the above problem, the state is the position – (x,y)

• The state value is defined as

• N(x,y) = Number of paths from that position to the destination

• Recurrence Formula

### Defining States

• Defining a good state is the key to solving a DP problem

• Affects the recurrence formula

• Affects the dimension of the problem and thus the time complexity

• Should include all information relevant to the computation of the value (except those static to the test cases). Recall what a “function” means in the mathematical sense.

### Defining States

• For optimization problems : usually the optimal value subject to a set of constraints (i.e. the states).

• Sometimes from varying the constraints from the problem

• e.g. cost(number of students, resource allocated)

### Features of DP Problems

• Overlapping sub-problems (重疊子問題)

• Optimal Substructure (最優子結構)

• Memoryless Property (無後效性)

• The future depends only on the present, but not the past.

• i.e. Decision leading to the subproblem does not affect how we solve the subproblem.

### Memo(r)ization VS DP

• Memo(r)ization – speeding up computations by storing previously computed results in memory

• Dynamic Programming – process of setting up and evaluating a recurrence relation efficiently by employing memo(r)ization

7

6

9

4

8

1

5

2

4

3

### Triangle (IOI ’94)

• Given a triangle with N levels like the one on the left, find a path with maximum sum from the top to the bottom

• Only the sum is required

### Triangle : Analysis

• Exhaustion?

• How many paths are there in total?

• Greedy?

• It doesn’t work. Why?

• Graph problem?

• Possible, but not simple enough

• Fail to make use of the special shape of the graph

### Triangle: Defining the State

• We need to find the maximum sum on a path from (1,1) to the bottom

• We can attempt to define the state by the cell (i,j) and state value F[i][j] to be the maximum sum from (i,j) to be bottom

### Triangle: Formulation

• Let A[i][j] denote the number in the i-th row and j-th column, (i, j)

• Let F[i][j] denote the maximum sum of the numbers on a path from the bottom to (i, j)

• The problem exhibits optimal substructure here

7

6

9

4

8

1

### Triangle: Formulation

• Base cases: F[N][i] = A[N][i] for 1≤ i ≤N

• Recurrence: (i<N, 1 ≤j≤i)

• F[i][j] = max{F[i+1][j],F[i+1][j+1]}+A[i][j]

### Triangle: Computation Order

• F[i][*] depends on F[i+1][*]

• Compute F row by row, from bottom to top

### Triangle: Algorithm

• Algorithm:

• for i 1 to N doF[N][i]  A[N][i] ;

for i N-1downto 1 do

for j 1 to i do

F[i][j]  max{F[i+1][j],F[i+1][j+1]}

+ A[i][j]

### Triangle: Complexity

• Number of array entries: N(N+1)/2

• Time for computing one entry: O(1)

• Thus the total time complexity is O(N2)

• Memory complexity: O(N2)

### Triangle: Backtracking

• What if we are asked to output the path?

• We can store the optimal decision, i.e. left or right to go for each cell

• We then travel from (1,1) down to the bottom to obtain the path

• This is called Backtracking, “back” in the sense that we go in the reversed order of which we obtained the answer.

### Triangle

• If you refer to past training notes, there is another definition of the state value F[i][j] – the maximum sum of a path from (1,1) to (i,j)

• Both algorithm gives the same time complexity. But the second definition (from the top to bottom) probably gives you some insights for Triangle II on the HKOI Online Judge. Think about it. 

### Fishing

• There are N fish ponds and you are going to spend M minutes on fishing

• Given the time-reward relationship of each pond, determine the time you should spend at each pond in order to get the biggest reward

### Fishing (example)

• For example, if N=3, M=3 and the relationships are given in the previous slide, then the optimal schedule is

• Pond 1: 2 minutes

• Pond 2: 1 minute

• Pond 3: 0 minute

• Reward: 5 fish

### Fishing (analysis)

• You can think of yourself visiting ponds 1, 2, 3, …, N in order

• Why?

• Suppose in an optimal schedule you spend K minutes on fishing at pond 1

• So you have M-K minutes to spend at the remaining N-1 ponds

• The problem is reduced

• But how can I know what is K?

• You don’t know, so try all possible values!

### Fishing (formulation)

• Let F[i][j] be the maximum reward you can get by spending j minutes at the first i ponds

• Base cases: 0 ≤ i, j ≤ N

F[i][0] = 0

F[0][j] = 0

• Progress: 1 ≤ i ≤ N, 1 ≤ j ≤ M

F[i][j] = max{F[i-1][k]+R[i][j-k]}

0 ≤ k ≤ j

1

2

6

5

3

4

### Polygon Triangulation

• Given an N-sided convex polygon A, find a triangulation scheme with minimum total cut length

1

1

2

2

6

5

3

4

4

4

### Polygon (analysis)

• Every edge of A belongs to exactly one triangle resulting from the triangulation

• We get two (or one) smaller polygons after deleting a triangle

A

C

B

### Polygon (analysis)

• The order of cutting does not matter

• Optimal substructure

• If the cutting scheme for A is optimal, then the cutting schemes for B and C must also be optimal

### Best Flying Route1

• Cities from 1 to N, from West to East

• One-way flights connecting some pairs of cities

• To fly from city 1 to city N, always flying to the East, then back to city 1, always flying to the West

• Each city, except city 1, can be visited at most once

### Best Flying Route

• e.g. When N = 10,

• You can fly in the sequence {1, 2, 5, 8, 10, 7, 3, 1} (if there are flights connecting each adjacent pair of them), but not in the sequence {1, 4, 2, 5, 10, 1}, nor {1, 3, 5, 10, 5, 1}, even if there are flights connectin each adjacent pair of them.

• Assume at least one such route exists

• Maximize the number of cities you can visit within the above constraints

### Best Flying Route

• Does the problem demonstrate optimal substructure? Are the states memoryless?

• Unfortunately, after flying from 1 to N, how we can fly back to 1 depends on what cities we visited in flying to N

• How to formulate the problem? How are subproblems related?

• Time Complexity and Memory Complexity

• Outputting the route

### Review

• Memoryless Property

• Optimal substructure and Overlapping Subproblems

• State representation and recurrence formula

• Bottom-up approach VS top-down approach

• Tradeoff between memory and runtime

### Common Models

• DP on rectangular arrays

• DP on trees

• DP on optimized states (ugly states)

• This involves some state representation

### Further Optimization

• Memory optimization – rolling array

• Runtime optimization – reducing the cost in recurrence

### Practice Problems

• 1058 The Triangle II

• 3023 Amida De Go II

• 3042Need for Speed

• 6000 Palindrome

• 6990 Little Shop of Flowers