You are on page 1of 11

Dynamic Programming

Dynamic Programming
Dynamic Programming is a general algorithm design technique
for solving problems defined by or formulated as recurrences with
overlapping sub-instances

• 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

Vaibhav Pandey Department of CSE, TIET, Patiala 2


Example: Fibonacci numbers

• 0, 1, 1, 2, 3, 5, 8, 13, ………

•Recall definition of Fibonacci numbers:

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


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

Vaibhav Pandey Department of CSE, TIET, Patiala 3


Recursive solution

ALGORITHM Fib(n)
//Computes the nth Fibonacci number recursively by using its
definition
//Input: A nonnegative integer n
//Output: The nth Fibonacci number
if n ≤ 1 return n
else return Fib(n − 1) + Fib(n − 2)

Vaibhav Pandey Department of CSE, TIET, Patiala 4


Recursive call tree for n=5

Vaibhav Pandey Department of CSE, TIET, Patiala 5


Recursive call tree (General case )

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


...

Vaibhav Pandey Department of CSE, TIET, Patiala 6


Dynamic Programming: Top-down Memoization

Vaibhav Pandey Department of CSE, TIET, Patiala 7


Recursive call tree for n=5

Vaibhav Pandey Department of CSE, TIET, Patiala 8


Dynamic Programming: Bottom up

Vaibhav Pandey Department of CSE, TIET, Patiala 9


Example: 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)

Vaibhav Pandey Department of CSE, TIET, Patiala 10


Examples of DP algorithms

• Rod cutting problem

• Matrix chain multiplication

• Longest common subsequence

• Warshall’s algorithm for transitive closure

• Floyd’s algorithm for all-pairs shortest paths

• Constructing an optimal binary search tree

• Some instances of difficult discrete optimization problems:


- traveling salesman
- knapsack

Vaibhav Pandey Department of CSE, TIET, Patiala 11

You might also like