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

Dynamic Programming

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

Dynamic Programming

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

Dynamic Programming (DP)

Dynamic Programming (DP) is a problem-solving approach used in computer science to


solve problems by breaking them down into smaller subproblems and solving each
subproblem just once, storing its result to avoid redundant computations. DP is particularly
useful for optimization problems and problems with overlapping subproblems and optimal
substructure.

Key Concepts of Dynamic Programming

1. Overlapping Subproblems: Problems can be broken down into smaller subproblems,


and these subproblems are solved multiple times. DP saves the results of these
subproblems to avoid redundant calculations. For instance, In the Fibonacci sequence,
F(n)=F(n−1)+F(n−2), the calculation of F(n−1) and F(n−2) overlaps when calculating
higher terms.
2. Optimal Substructure: A problem exhibits an optimal substructure if the solution of
the overall problem can be constructed from the solutions of its subproblems. For
instance, In the shortest path problem, the shortest path from A to C via B is
composed of the shortest path from A to B and B to C.
3. Memoization vs. Tabulation:

 Memoization (Top-Down Approach): Solves problems recursively and


stores the results of subproblems in a table (cache) to reuse later.
 Tabulation (Bottom-Up Approach): Solves problems iteratively by solving
smaller subproblems first and building up to the final solution.

Common DP Problems:

1. Fibonacci numbers.
2. Longest Common Subsequence (LCS).
3. Longest Increasing Subsequence (LIS).
4. Knapsack problem.
5. Matrix Chain Multiplication.
6. Coin Change problem.
7. Minimum Edit Distance.
Top-Down Approach (Memoization)

The Top-Down approach starts from the problem's largest instance and recursively breaks it
into smaller subproblems. It uses recursion and a cache (or memo table) to store the results of
subproblems so they don’t have to be recomputed. The working of Memoization is as
follows:

1. Start solving the problem recursively.


2. If the result of a subproblem is already stored in the cache (memo table), use it.
3. Otherwise, compute the result, store it in the cache, and return it.

Let us understand the Top-Down Approach by using the Fibonacci problem. We need to find
the n-th Fibonacci number where:

 F(0)=0
 F(1)=1
 F(n)=F(n−1)+F(n−2) for n≥2

For this example, let’s calculate F(6). Now consider the following steps:

1. Start solving the problem recursively from F(6).


2. If a subproblem has already been solved, use its stored result (memoization).
3. Otherwise, compute it recursively.

Calculations:

 Start with F(6):

F(6) = F(5)+F(4)

 Break F(5) and F(4):

F(5) = F(4)+F(3), F(4) = F(3)+F(2)

 Break further:

F(4) = F(3)+F(2), F(3) = F(2)+F(1), F(2) = F(1)+F(0)


Recursive Tree with Memoization
Fibonacci Value Computation Result
F(0) Base case 0
F(1) Base case 1
F(2) F(1) + F(0) 1+0=1
F(3) F(2) + F(1) 1+1=2
F(4) F(3) + F(2) 2+1=3
F(5) F(4) + F(3) 3+2=5
F(6) F(5) + F(4) 5+3=8
F(6) = 8

Memoization Table:
n F(n)
0 0
1 1
2 1
3 2
4 3
5 5
6 8

Bottom-Up Approach (Tabulation)

The Bottom-Up approach solves the problem iteratively by starting from the smallest
subproblems (base cases) and building up the solution to the original problem. It uses a table
to store results. The working of Tabulation is as follows:

1. Define a table (array or matrix) to store results of all subproblems.


2. Solve the smallest subproblems first and fill the table.
3. Use the stored results to solve larger subproblems iteratively.
4. The final result is in the last cell of the table.

Let us understand the Bottom-Up Approach by using the Fibonacci problem. We need to find
the n-th Fibonacci number where:

 Start solving from the smallest subproblem (F(0), F(1)).


 Use these results to build up solutions for larger subproblems.
 Store results in a table iteratively.
Tabulation Table
Step F(0) F(1) F(2) F(3) F(4) F(5) F(6)

Base 0 1
1 0 1 1

2 0 1 1 2

3 0 1 1 2 3
4 0 1 1 2 3 5

5 0 1 1 2 3 5 8

F(6)=8

Coin Change Problem

The coin change problem is a classic dynamic programming problem. You are given a set of
coins of different denominations and a target amount. Your task is to determine the minimum
number of coins required to make up the target amount. If it’s not possible to make up the
amount, return -1.

dp[i]=min(1+dp[i−c])

i: denomination

c: coin

Example 1

 Coins: [1,2,5]
 Target Amount: 11
 11=5+5+1 (3 coins)

Example 2

 Coins: [2]
 Target Amount: 3
 −1. It is impossible to make 3 using only coins of denomination 2.
Example 1:

 Coins: [1,2,5]
 Target: 11
 Goal: Determine the minimum number of coins needed to make each amount i from 0 to
11 using the coins 1,2,5

Base Case: i=0

dp[0]=0 No coins are needed to make the amount 0.

i=1

 Using 1: 1+dp[1−1]=1+0=1. (1 coin of 1 is sufficient.)


 Using 2: Not possible (amount 1−2<0).
 Using 5: Not possible (amount 1−5<0).
 dp[1]=min⁡(1,∞,∞)=1

i=2

 Using 1: 1+dp[2−1]=1+1=2.
 Using 2: 1+dp[2−2]=1+0=1. (1 coin of 2 suffices.)
 Using 5: Not possible (amount 2−5<0).
 dp[2]=min⁡(2,1,∞)=1.

i=3

 Using 1: 1+dp[3−1]=1+1=2.
 Using 2: 1+dp[3−2]=1+1=2.
 Using 5: Not possible (amount 3−5<0).
 dp[3]=min⁡(2,2,∞)=2.

i=4

 Using 1: 1+dp[4−1]=1+2=3.
 Using 2: 1+dp[4−2]=1+1=2
 Using 5: Not possible (amount 4−5<0).
 dp[4]=min⁡(3,2,∞)=2.
i=5

 Using 1: 1+dp[5−1]=1+2=3.
 Using 2: 1+dp[5−2]=1+2=3.
 Using 5: 1+dp[5−5]=1+0=1
 dp[5]=min⁡(3,3,1)=1

i=6

 Using 1: 1+dp[6−1]=1+1=2.
 Using 2: 1+dp[6−2]=1+2=2.
 Using 5: 1+dp[6−5]=1+1=2.
 dp[6] = min(2,2,2)=2.

i=7

 Using 1: 1+dp[7−1]=1+2=3.
 Using 2: 1+dp[7−2]=1+1=2.
 Using 5: 1+dp[7−5]=1+2=2.
 dp[7]=min⁡(3,2,2)=2.

i=8

 Using 1: 1+dp[8−1]=1+2=3.
 Using 2: 1+dp[8−2]=1+2=3.
 Using 5: 1+dp[8−5]=1+1=2.
 dp[8]=min⁡(3,3,2)=2

i=9

 Using 1: 1+dp[9−1]=1+2=3.
 Using 2: 1+dp[9−2]=1+2=3.
 Using 5: 1+dp[9−5]=1+2=3.
 dp[9]=min⁡(3,3,3)=3
i=10

 Using 1: 1+dp[10−1]=1+3=4.
 Using 2: 1+dp[10−2]=1+2=3.
 Using 5: 1+dp[10−5]=1+1=2.
 dp[10]=min⁡(4,3,2)=2

i=11

 Using 1: 1+dp[11−1]=1+2=3.
 Using 2: 1+dp[11−2]=1+3=4.
 Using 5: 1+dp[11−5]=1+2=3.
 dp[11]=min⁡(3,4,3)=3

dp=[0,1,1,2,2,1,2,2,2,3,2,3]

You might also like