Lecture 20
Lecture 20
• Main idea:
- set up a recurrence relating a solution to a larger instance
to solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
2
Example 1: Fibonacci numbers
• Recall definition of Fibonacci numbers:
F(n)
F(n-1) + F(n-2)
...
3
Example 1: Fibonacci numbers
(cont.)
Computing the nth Fibonacci number using bottom-up iteration
and recording results:
F(0) = 0
F(1) = 1
F(2) = 1+0 = 1
…
F(n-2) =
F(n-1) =
F(n) = F(n-1) + F(n-2)
Efficiency:
- time
4
- space
Example 2: Coin-collecting by robot
Several coins are placed in cells of an n×m board. A robot,
located in the upper left cell of the board, needs to collect
as many of the coins as possible and bring them to the
bottom right cell. On each step, the robot can move either
one cell to the right or one cell down from its current
location. 1 2 3 4 5 6
5
5
Solution to the coin-collecting problem
Let F(i,j) be the largest number of coins the robot can collect
and bring to cell (i,j) in the ith row and jth column.
The recurrence:
F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j
≤m
6
F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n.
Solution to the coin-collecting problem (cont.)
F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1
≤j≤m
F(0, j) = 0 for 1 ≤ j ≤ m and F(i, 0) = 0 for 1 ≤ i ≤ n.
1 2 3 4 5 6
5 7