Download presentation
Presentation is loading. Please wait.
Published byKimberly Thomas Modified over 9 years ago
1
HKOI Training 2009 Kelly Choi 27 June 2009 Acknowledgements: References and slides extracted from 1. [Advanced] Dynamic Programming, 24-04-2004, by Chi-Man Liu 2. [Intermediate] Dynamic Programming, 21-08-2004, by Ng Tung
2
Prerequisites Concepts in Recurrence Concepts in Recurrence Basic Recursion Basic Recursion Functions Functions Divide-and-conquer Divide-and-conquer
3
Introduction Components of DP Components of DP A recurrence formula (with state & state value) A recurrence formula (with state & state value) Memo(r)ization Memo(r)ization Characteristics of DP Problems Characteristics of DP Problems Overlapping Subproblems Overlapping Subproblems Optimal Substructure Optimal Substructure Memoryless Property Memoryless Property
4
Grid Path Counting In a N*M grid, we want to move from the top left cell to the bottom right cell 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 You may only move down or right Some cells may be blocked Some cells may be blocked Example of one path: Example of one path:
5
Naïve Algorithm: DFS DFS(x,y){ 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) return DFS(x+1,y) + DFS(x,y+1)else return 0 {blocked cell} } }
6
Time Complexity Exponential time complexity: Exponential time complexity: Each time the procedure branches into two paths Each time the procedure branches into two paths Alternatively Alternatively The base case is reached as many times as the number of paths, so the time complexity is Ω(number of paths) 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? Have we done anything redundant?
7
Slow... DFS(1,2) DFS(1,3)DFS(2,2) DFS(1,4)DFS(2,3)DFS(3,2)DFS(2,3) DFS(1,1) DFS(2,1) DFS(2,2)DFS(3,1) DFS(3,2)DFS(2,3)
8
Overlapping Subproblems Note that DFS(2,2) is called twice, DFS(2,3) three times, etc. 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 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 Memorize the values these calls return, and avoid the redundant work
9
Memo(r)ization Compute and store the value of DFS(x,y) in the first time we called it. 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 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. This is called recursion with memo(r)ization. Time complexity: O(NM) Time complexity: O(NM)
10
Example : Grid Path Counting DFS(x,y){ 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]; }}
11
Bottom-up approach Iterative, Without the use of function calls Iterative, Without the use of function calls Consider the arrays Grid[x,y] and Memory[x,y]: Consider the arrays Grid[x,y] and Memory[x,y]:6331030211 32101 11111 Treat DFS(x,y) not as a function, but as an array 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 Evaluate the values for DFS[x,y] row-by-row, column-by- column
12
Example : Grid Path Counting DFS[N,M] ← 1 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 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 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]
13
Order of Computation Top-down approach: Recursive function calls with memo(r)ization Top-down approach: Recursive function calls with memo(r)ization Easy to implement Easy to implement Avoid calculating values for impossible states Avoid calculating values for impossible states Further optimization possible Further optimization possible Bottom-up approach Bottom-up approach Easy to code once the order is identified Easy to code once the order is identified Avoid function calls Avoid function calls Usually prefered Usually prefered But we need to see the dependence of states on other states to decide the order But we need to see the dependence of states on other states to decide the order
14
Components of DP It is very important to be able to describe a DP algorithm It is very important to be able to describe a DP algorithm Two essential components of DP Two essential components of DP State( 狀態 ) and the state value: State( 狀態 ) and the state value: State – a description of the subproblem 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. 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 Recurrence – A rule describing the relationship between states, with base cases
15
Grid Path Counting In the above problem, the state is the position – (x,y) In the above problem, the state is the position – (x,y) The state value is defined as The state value is defined as N(x,y) = Number of paths from that position to the destination N(x,y) = Number of paths from that position to the destination Recurrence Formula Recurrence Formula N(x,y) = 1, x=N and y = M 0, Grid[x,y] is impassable N(x+1,y) + N(x,y+1), otherwise
16
Defining States Defining a good state is the key to solving a DP problem Defining a good state is the key to solving a DP problem Affects the recurrence formula Affects the recurrence formula Affects the dimension of the problem and thus the time complexity 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. 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.
17
Defining States For optimization problems : usually the optimal value subject to a set of constraints (i.e. the 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 Sometimes from varying the constraints from the problem e.g. cost(number of students, resource allocated) e.g. cost(number of students, resource allocated)
18
Features of DP Problems Overlapping sub-problems ( 重疊子問題 ) Overlapping sub-problems ( 重疊子問題 ) Optimal Substructure ( 最優子結構 ) Optimal Substructure ( 最優子結構 ) Memoryless Property ( 無後效性 ) Memoryless Property ( 無後效性 ) The future depends only on the present, but not the past. 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. i.e. Decision leading to the subproblem does not affect how we solve the subproblem.
19
Memo(r)ization VS DP Memo(r)ization – speeding up computations by storing previously computed results in memory 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 Dynamic Programming – process of setting up and evaluating a recurrence relation efficiently by employing memo(r)ization
20
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 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 Only the sum is required 7 69 81 4 24 5 3
21
Triangle : Analysis Exhaustion? Exhaustion? How many paths are there in total? How many paths are there in total? Greedy? Greedy? It doesn’t work. Why? It doesn’t work. Why? Graph problem? Graph problem? Possible, but not simple enough Possible, but not simple enough Fail to make use of the special shape of the graph Fail to make use of the special shape of the graph
22
Triangle: Defining the State We need to find the maximum sum on a path from (1,1) to the bottom 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 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
23
Triangle: Formulation Let A[i][j] denote the number in the i-th row and j-th column, (i, j) 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) Let F[i][j] denote the maximum sum of the numbers on a path from the bottom to (i, j) Answer = F[1][1] Answer = F[1][1] The problem exhibits optimal substructure here The problem exhibits optimal substructure here
24
Triangle: Formulation Base cases: F[N][i] = A[N][i] for 1≤ i ≤N Base cases: F[N][i] = A[N][i] for 1≤ i ≤N Recurrence: (i < N, 1 ≤ j ≤ i) Recurrence: (i < N, 1 ≤ j ≤ i) F[i][j] = max{F[i+1][j],F[i+1][j+1]}+A[i][j] F[i][j] = max{F[i+1][j],F[i+1][j+1]}+A[i][j] 7 69 81 4
25
Triangle: Computation Order F[i][*] depends on F[i+1][*] F[i][*] depends on F[i+1][*] Compute F row by row, from bottom to top Compute F row by row, from bottom to top
26
Triangle: Algorithm Algorithm: Algorithm: for i 1 to N do F[N][i] A[N][i] ; for i 1 to N do F[N][i] A[N][i] ; for i N-1 downto 1 do for j 1 to i do F[i][j] max{F[i+1][j],F[i+1][j+1]} + A[i][j] + A[i][j] answer F[1][1]
27
Triangle: Complexity Number of array entries: N(N+1)/2 Number of array entries: N(N+1)/2 Time for computing one entry: O(1) Time for computing one entry: O(1) Thus the total time complexity is O(N 2 ) Thus the total time complexity is O(N 2 ) Memory complexity: O(N 2 ) Memory complexity: O(N 2 )
28
Triangle: Backtracking What if we are asked to output the path? 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 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 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. This is called Backtracking, “back” in the sense that we go in the reversed order of which we obtained the answer.
29
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) 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. 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.
30
Fishing There are N fish ponds and you are going to spend M minutes on 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 Given the time-reward relationship of each pond, determine the time you should spend at each pond in order to get the biggest reward time/pond123 1 minute 0 fish 2 fish 1 fish 2 minutes 3 fish 2 fish 3 minutes 3 fish 4 fish
31
Fishing (example) For example, if N=3, M=3 and the relationships are given in the previous slide, then the optimal schedule is 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 1: 2 minutes Pond 2: 1 minute Pond 2: 1 minute Pond 3: 0 minute Pond 3: 0 minute Reward: 5 fish Reward: 5 fish
32
Fishing (analysis) You can think of yourself visiting ponds 1, 2, 3, …, N in order You can think of yourself visiting ponds 1, 2, 3, …, N in order Why? Why? Suppose in an optimal schedule you spend K minutes on fishing at pond 1 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 So you have M-K minutes to spend at the remaining N-1 ponds The problem is reduced The problem is reduced But how can I know what is K? But how can I know what is K? You don’t know, so try all possible values! You don’t know, so try all possible values!
33
Fishing (formulation) Let F[i][j] be the maximum reward you can get by spending j minutes at the first i ponds 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 Base cases: 0 ≤ i, j ≤ N F[i][0] = 0 F[0][j] = 0 Progress: 1 ≤ i ≤ N, 1 ≤ j ≤ M Progress: 1 ≤ i ≤ N, 1 ≤ j ≤ M F[i][j] = max{F[i-1][k]+R[i][j-k]} 0 ≤ k ≤ j
34
Polygon Triangulation Given an N- sided convex polygon A, find a triangulation scheme with minimum total cut length Given an N- sided convex polygon A, find a triangulation scheme with minimum total cut length 1 2 3 4 5 6
35
Polygon (analysis) Every edge of A belongs to exactly one triangle resulting from the triangulation Every edge of A belongs to exactly one triangle resulting from the triangulation 1 2 4 5 6 4 1 3 4 2 We get two (or one) smaller polygons after deleting a triangle We get two (or one) smaller polygons after deleting a triangle
36
Polygon (analysis) The order of cutting does not matter The order of cutting does not matter Optimal substructure Optimal substructure If the cutting scheme for A is optimal, then the cutting schemes for B and C must also be optimal If the cutting scheme for A is optimal, then the cutting schemes for B and C must also be optimal A B C
37
Best Flying Route 1 Cities from 1 to N, from West to East Cities from 1 to N, from West to East One-way flights connecting some pairs of cities 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 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 Each city, except city 1, can be visited at most once
38
Best Flying Route e.g. When N = 10, 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. 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 Assume at least one such route exists Maximize the number of cities you can visit within the above constraints Maximize the number of cities you can visit within the above constraints
39
Best Flying Route Does the problem demonstrate optimal substructure? Are the states memoryless? 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 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? How to formulate the problem? How are subproblems related? Time Complexity and Memory Complexity Time Complexity and Memory Complexity Outputting the route Outputting the route
40
Review Memoryless Property Memoryless Property Optimal substructure and Overlapping Subproblems Optimal substructure and Overlapping Subproblems State representation and recurrence formula State representation and recurrence formula Bottom-up approach VS top-down approach Bottom-up approach VS top-down approach Tradeoff between memory and runtime Tradeoff between memory and runtime
41
Common Models DP on rectangular arrays DP on rectangular arrays DP on trees DP on trees DP on optimized states (ugly states) DP on optimized states (ugly states) This involves some state representation This involves some state representation
42
Further Optimization Memory optimization – rolling array Memory optimization – rolling array Runtime optimization – reducing the cost in recurrence Runtime optimization – reducing the cost in recurrence
43
Practice Problems 1058 The Triangle II 1058 The Triangle II 3023 Amida De Go II 3023 Amida De Go II 3042 Need for Speed 3042 Need for Speed 6000 Palindrome 6000 Palindrome 6990 Little Shop of Flowers 6990 Little Shop of Flowers
44
References 1. 信息學奧林匹克教程 – 提高篇 吳耀斌, 曹利國, 向期中, 朱全民編著 湖南師范大學出版社 2. 算法藝術與信息學競賽 劉汝佳, 黃亮著 清華大學出版社
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.