Professional Documents
Culture Documents
Tut 6
Tut 6
Tut 6
Shiyuan DENG
1/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Pitfall of Recursion
2/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Problem 1
algorithm f (x)
1. if x = 0 then return 0
2. max = −∞
3. for i = 1 to x
4. v = A[i] + f (x − i)
5. if v > max then max = v
6. return max
3/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
g (0) ≥ 1,
g (1) ≥ 1,
n−1
X
g (n) ≥ g (i) for n ≥ 2.
i=0
4/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
As k + 1 ≥ 2, we have:
k
X
g (k + 1) ≥ g (k).
i=0
5/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Principle of Dynamic Programming
6/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Problem 2
7/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
b
a 1 2 3 4 5 6 7 8 9 10 11 12
1
2
3 f (5, 8)
4
5
6
7
8
9
10
11
12
8/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
f (a, b) = 0 when a ≥ b.
b
a 1 2 3 4 5 6 7 8 9 10 11 12
1
2
3
4 our goal: f (1, n)
5
6
7
8 =0
9
10
11
12
9/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
Pb
f (a, b) = ( c=a A[c]) + minb−1
c=a+1 {f (a, c) + f (c, b)} when a < b.
b
a 1 2 3 4 5 6 7 8 9 10 11 12
1
2
f (4, 10)
3
4
5
6
7
8
9
10
11
12
10/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
Pb
f (a, b) = ( c=a A[c]) + minb−1
c=a+1 {f (a, c) + f (c, b)} when a < b.
Let us start with the gray cells — they correspond to f (a, b) where
a = b − 1. These cells depend on no other cells.
b
a 1 2 3 4 5 6 7 8 9 10 11 12
1
2
3
4
5
6
7
8
9
10
11
12
11/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
Let us continue with the green cells — they correspond to f (a, b) where
a = b − 2. Every such cell depends on two gray cells, which have already
been computed.
b
a 1 2 3 4 5 6 7 8 9 10 11 12
1
2
3
4
5
6
7
8
9
10
11
12
12/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
Let us continue with the red cells — they correspond to f (a, b) where
a = b − 3. Every such cell depends on two gray cells and two green cells,
all of which have been computed.
b
a 1 2 3 4 5 6 7 8 9 10 11 12
1
2
3
4
5
6
7
8
9
10
11
12
13/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
14/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Problem 3 (Space Consumption)
0 1 2 3 4
y B D C A
0 x
1 A
2 B
3 C
15/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
We can calculate the values in the row-major order, i.e., row 0 to row 3
and left to right in each row. We used O(mn) space because we stored
all the values. Observe, however, that only two rows need to be stored at
any moment .
0 1 2 3 4
y B D C A
0 x
1 A
2 B
3 C
16/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Solution
0 1 2 3 4
y B D C A
0 x
1 A
2 B
3 C
17/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions
Think: Can this trick be used to reduce the space in Problem 2?
18/1
Shiyuan DENG Dynamic Programming: Evaluating Recursive Functions