Dynamic Programming (DP) Algorithms
Dynamic Programming (DP) is a powerful algorithmic technique used to solve
optimization problems by breaking them down into simpler overlapping
subproblems. Unlike divide-and-conquer, which solves subproblems
independently, DP stores the results of already-solved subproblems to avoid
redundant computations.
Key Concepts of Dynamic Programming
1. Overlapping Subproblems:
o Problems can be divided into smaller, similar subproblems that are
solved multiple times.
2. Optimal Substructure:
o The solution to a problem can be constructed from solutions to its
subproblems.
3. Memoization vs. Tabulation:
o Memoization: Top-down approach using recursion with caching.
o Tabulation: Bottom-up approach using iterative computations stored
in a table.
Steps to Solve DP Problems
1. Define the problem in terms of states.
2. Identify the recurrence relation to transition between states.
3. Choose between memoization or tabulation.
4. Solve the problem iteratively or recursively.
, Common Dynamic Programming Problems
1. Fibonacci Sequence
Problem: Compute the nnn-th Fibonacci number.
State: dp[i]dp[i]dp[i] stores the iii-th Fibonacci number.
Recurrence Relation: dp[i]=dp[i−1]+dp[i−2]dp[i] = dp[i-1] + dp[i-
2]dp[i]=dp[i−1]+dp[i−2].
Time Complexity: O(n)O(n)O(n).
Space Complexity: O(n)O(n)O(n) (or O(1)O(1)O(1) with space optimization).
2. Knapsack Problem
1. 0/1 Knapsack:
o Problem: Maximize the total value of items that can be placed in a
knapsack of capacity WWW, with each item either included or
excluded.
o State: dp[i][w]dp[i][w]dp[i][w] is the maximum value for the first iii
items with capacity www.
o Recurrence Relation: dp[i][w]=max(dp[i−1][w],dp[i−1][w−weight[i]]
+value[i])dp[i][w] = \max(dp[i-1][w], dp[i-1][w-\text{weight}[i]] + \
text{value}[i])dp[i][w]=max(dp[i−1][w],dp[i−1][w−weight[i]]+value[i])
o Time Complexity: O(n⋅W)O(n \cdot W)O(n⋅W).
o Space Complexity: O(n⋅W)O(n \cdot W)O(n⋅W) (or O(W)O(W)O(W)
with space optimization).
2. Fractional Knapsack:
o Solved using a greedy approach, not DP.
3. Longest Common Subsequence (LCS)
Dynamic Programming (DP) is a powerful algorithmic technique used to solve
optimization problems by breaking them down into simpler overlapping
subproblems. Unlike divide-and-conquer, which solves subproblems
independently, DP stores the results of already-solved subproblems to avoid
redundant computations.
Key Concepts of Dynamic Programming
1. Overlapping Subproblems:
o Problems can be divided into smaller, similar subproblems that are
solved multiple times.
2. Optimal Substructure:
o The solution to a problem can be constructed from solutions to its
subproblems.
3. Memoization vs. Tabulation:
o Memoization: Top-down approach using recursion with caching.
o Tabulation: Bottom-up approach using iterative computations stored
in a table.
Steps to Solve DP Problems
1. Define the problem in terms of states.
2. Identify the recurrence relation to transition between states.
3. Choose between memoization or tabulation.
4. Solve the problem iteratively or recursively.
, Common Dynamic Programming Problems
1. Fibonacci Sequence
Problem: Compute the nnn-th Fibonacci number.
State: dp[i]dp[i]dp[i] stores the iii-th Fibonacci number.
Recurrence Relation: dp[i]=dp[i−1]+dp[i−2]dp[i] = dp[i-1] + dp[i-
2]dp[i]=dp[i−1]+dp[i−2].
Time Complexity: O(n)O(n)O(n).
Space Complexity: O(n)O(n)O(n) (or O(1)O(1)O(1) with space optimization).
2. Knapsack Problem
1. 0/1 Knapsack:
o Problem: Maximize the total value of items that can be placed in a
knapsack of capacity WWW, with each item either included or
excluded.
o State: dp[i][w]dp[i][w]dp[i][w] is the maximum value for the first iii
items with capacity www.
o Recurrence Relation: dp[i][w]=max(dp[i−1][w],dp[i−1][w−weight[i]]
+value[i])dp[i][w] = \max(dp[i-1][w], dp[i-1][w-\text{weight}[i]] + \
text{value}[i])dp[i][w]=max(dp[i−1][w],dp[i−1][w−weight[i]]+value[i])
o Time Complexity: O(n⋅W)O(n \cdot W)O(n⋅W).
o Space Complexity: O(n⋅W)O(n \cdot W)O(n⋅W) (or O(W)O(W)O(W)
with space optimization).
2. Fractional Knapsack:
o Solved using a greedy approach, not DP.
3. Longest Common Subsequence (LCS)