0% found this document useful (0 votes)
6 views

Lecture 20

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 20

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Dynamic Programming

• Dynamic Programming is an algorithm design technique for optimization problems: often


minimizing or maximizing.
• Like divide and conquer, DP solves problems by combining solutions to subproblems.
• Unlike divide and conquer, subproblems are not independent.
• Subproblems may share subsubproblems,
• However, solution to one subproblem may not affect the solutions to other subproblems of the same
problem. (More on this later.)
• DP reduces computation by
• Solving subproblems in a bottom-up fashion.
• Storing solution to a subproblem the first time it is solved.
• Looking up the solution when subproblem is encountered again.
• Key: determine structure of optimal solutions
Dynamic Programming
Dynamic Programming is a general algorithm design technique
for solving problems defined by recurrences with overlapping
subproblems

• Invented by American mathematician Richard Bellman in the


1950s to solve optimization problems and later assimilated by CS

• “Programming” here means “planning”

• 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)


F(0) = 0
F(1) = 1

• Computing the nth Fibonacci number recursively (top-down):

F(n)

F(n-1) + F(n-2)

F(n-2) + F(n-3) F(n-3) + F(n-4)

...

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)

0 1 1 . . . F(n-2) F(n-1) F(n)

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 largest number of coins that can be brought to cell (i,j):

from the left neighbor ?


from the neighbor above?

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

You might also like