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

0.5Dynamic Programming-1

Uploaded by

PIYUSH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

0.5Dynamic Programming-1

Uploaded by

PIYUSH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Dynamic Programming

Module 3
Origins
• A method for solving complex problems by breaking them into
smaller, easier, sub problems
• Term Dynamic Programming coined by mathematician Richard
Bellman in early 1950s
• employed by Rand Corporation
• Rand had many, large military contracts
• Secretary of Defense, Charles Wilson “against research, especially
mathematical research”
• how could any one oppose "dynamic"?

CS314-slide credits 2
Dynamic Programming

"Thus, I thought dynamic programming was a


good name. It was something not even a
Congressman could object to. So I used it as an
umbrella for my activities"
- Richard E. Bellman
DP……..
• Dynamic programming, like the divide-and-conquer method, solves
problems by combining the solutions to subproblems.
• Build up the answer from smaller subproblems
• More general than “simple” divide & conquer
• Also more powerful
• Generally applies to algorithms where the brute force algorithm would be
exponential.
• We typically apply dynamic programming to optimization problems.
Such problems can have many possible solutions.
• Each solution has a value, and we wish to find a solution with the
optimal (minimum or maximum) value.
Dynamic Programming
• Optimal Substructure
• Overlapping Sub problems
• Recursion
• Memoization
• Iterative Memoization
Concept 1- Optimal Sub-structure
• optimal substructure: optimal solutions to a problem incorporate
optimal solutions to related subproblems, which we may solve
independently.
Elements of Dynamic Programming
Mains Steps to DP
• Recursion Iteration
Ex: Fibonacci Sequence
• 1, 1, 2, 3, 5, 8, 13……..
• Recursion Iteration

• fib(0) = fib(1) = 1
• fib(n) = fib(n-1) + fib(n-2)
Ex: Fibonacci Sequence
• 1, 1, 2, 3, 5, 8, 13……..
• Recursion

• fib(0) = fib(1) = 1
• fib(n) = fib(n-1) + fib(n-2)
Ex: Fibonacci Sequence
• 1, 1, 2, 3, 5, 8, 13……..
• Recursion
Concept 2: Overlapping Sub Problems
Ex: Fibonacci Sequence
Fib(5) calls
Fib (3)- 2 times
Fib (2)- 3 times

Fib (20)
Fib (3)- 2 584 times
Fib (10)- 89 times

Order of this method?


A. O(1) B. O(log N) C. O(N) D. O(N2) E. O(2N)
Ex: Fibonacci Sequence: running time
Fibonacci- Non Recursive way
Fib (N)
1. Let a=1, b=1, cnt=3
2. Let int c No. of function calls made?
3. If n==1 || n==2
4. return 1
5. For cnt=3 to N
6. c= a+b
7. a=b
8. b=c
9. Return c
Example 2
• There are N stations in a route, starting 0 to N-1. A train moves from
first station (0) to last station (N-1) in only forward direction.
• The cost of ticket between any two station is given, then
• Find the min cost to travel from station 0 to N-1.
• Let N=4
Example 2
• There are N stations in a route, starting 0 to N-1. A train moves from
first station (0) to last station (N-1) in only forward direction.
• The cost of ticket between any two station is given, then
• Find the min cost to travel from station 0 to N-1
Example 2
• There are N stations in a route, starting 0 to N-1. A train moves from
first station (0) to last station (N-1) in only forward direction.
• The cost of ticket between any two station is given, then
• Find the min cost to travel from station 0 to N-1
Example 2 – Recursive Formulation
Example 2 – Recursive Base Case
• What are should be the terminating condition?
Example 2
Concept 3 - Memoization
I wish we could choose the memories to be remembered!!

• Overalpping subproblems- exponential


• Recall the Fibonacci Example….
• Cant we store the value of Fib (10) first time we compute it?
• Next time we need it just simply pick up from cache…
Concept 3 –
Memoization
Memoization- Fibonacci
Memoization- Fibonacci
Memoization –Example 2:
Mincost btw stations

The cost matrix btw stations is given.

Here we need a 2D matrix for memo


declared outside the function
So what is memoization?
• Memoization = Recursion+ Cache – Overlapping subproblems
Dynamic Programming
• A method of solving complex problems by breaking it down into
smaller sub problems- solving each sub problem only once, and
storing their solutions.
• Computations of sub problems are top-down in recursion.
• The DP approach is bottom up.
DP on Example 2- MinCost btw Stations
• First Compute mincost to reach station S0, then S1, S2….., Sn-1.
• For that we need only one 1D array of size mincost[N].
• Mincost to reach station - 0 is zero,
• Mincost[0]=0;
• Mincost to reach station 1 is cost[0,1]
• Mincost[1]=cost[0][1]
• Mincost to reach station 2 is
• min (Mincost(0)+ cost[0,2], Mincost(1)+cost[1,2])
DP on Example 2- MinCost btw Stations
• Mincost to reach station 3 is
• min (Mincost(0)+ cost[0,3], Mincost(1)+cost[1,3], Mincost(2), cost[2,3])
DP on Example 2- MinCost btw Stations
DP on Example 2- MinCost btw Stations
• Full code:
Observations
• Solving the problem in bottom-up manner.
Example 3 – Grid Cost
Example 3- Grid Cost
• Current position (i,j)
• We can either move down ↓, i.e. next position is (i+1, j) or
• right ⟶ , (i, j+1)
Does this problem as optimal substructure/
overlapping subproblems?
Example 3- Grid Cost
• Current cell (m, n)
• Let x be mincost uptil left cell (m, n-1)
• Let y be mincot upltil its top cell (m-1, n)

• Mincost(m,n)= cost [m,n] + Min (x, y)



Example 3- Grid Cost
• Recursive Case:
• Mincost(m,n)=cost [m,n]+ Min ( Mincost (m,n-1), Mincost (m-1, n))
• Base Case:
• Mincost(0,0)= cost(0,0)
• Top row -> only right moves
• First Col-> only down moves
Recursive Solution
• Exponential time
• Recomputes same
• sub problems again and again
Next Step
• Devise Memoization to store the sub problems of recursion
• Then Convert Recursion-> iteration using memo tables
Memoization
Eliminate Recursion
Eliminate Recursion
DP solution
Example 3- Modification?
DP Steps
• When developing a dynamic-programming algorithm, we
follow a sequence of four steps:
1. Characterize the structure of an optimal solution.
2. Recursively define the value of an optimal solution.
3. Compute the value of an optimal solution, typically in a
bottom-up fashion.
4. Construct an optimal solution from computed information

You might also like