Dynamic Programming
Dynamic Programming
1. Optimal Substructure:
A problem has optimal substructure if the optimal solution to the problem
can be obtained by combining optimal solutions to its subproblems.
Example: In the shortest path problem, the shortest path from node AAA
to node BBB includes the shortest path from AAA to an intermediate node
CCC, and from CCC to BBB.
2. Overlapping Subproblems:
A problem has overlapping subproblems if it repeatedly solves the same
subproblem in different parts of the recursion tree.
Example: Calculating Fibonacci numbers, where F(n)=F(n−1)+F(n−2)F(n)
= F(n-1) + F(n-2)F(n)=F(n−1)+F(n−2), requires computing
F(n−1)F(n-1)F(n−1) and F(n−2)F(n-2)F(n−2), and these computations
overlap frequently.
Advantages of DP
Disadvantages of DP
1. Financial Planning:
Allocating a budget over multiple months to minimize overspending or
maximize savings.
Why DP Works: Decisions for one month depend on previous months
(overlapping subproblems) and optimal allocation ensures overall savings
(optimal substructure).
2. Travel Itinerary Optimization:
Planning the cheapest route between cities while considering various costs
(tolls, fuel, time).
Why DP Works: Costs of visiting cities overlap (overlapping subproblems),
and each segment contributes to the optimal route (optimal substructure).
3. DNA Sequence Alignment (Bioinformatics):
Finding the minimum cost of transforming one DNA sequence into another
through insertions, deletions, or substitutions.
Why DP Works: Subproblem results for smaller sequence segments
contribute to the global alignment solution.
1. Fibonacci Numbers:
○ Question: How does DP improve the computation of Fibonacci
numbers?
○ Solution: Use a table to store results of previously computed
Fibonacci numbers. Time complexity reduces from
O(2n)O(2^n)O(2n) to O(n)O(n)O(n).
2. Longest Palindromic Subsequence:
○ Question: Find the length of the longest palindromic subsequence in
a string.
○ Solution: Use DP to check for matching characters and recursively
build the solution.
3. Subset Sum Problem:
○ Question: Given a set of integers, determine if a subset exists with a
given sum.
○ Solution: Use DP to iteratively check subsets of increasing sizes.
4. Rod Cutting Problem:
○ Question: Maximize the profit from cutting a rod of length nnn.
○ Solution: Use DP to evaluate profits for smaller cuts and combine
results.
1. Climbing Stairs:
○ Question: Find the number of ways to climb nnn stairs, taking 1 or 2
steps at a time.
○ Solution: dp[i]=dp[i−1]+dp[i−2]dp[i] = dp[i-1] +
dp[i-2]dp[i]=dp[i−1]+dp[i−2].
2. House Robber Problem:
○ Question: Maximize the money robbed without alerting police (no
adjacent houses).
○ Solution: dp[i]=max(dp[i−1],dp[i−2]+nums[i])dp[i] = \max(dp[i-1],
dp[i-2] + nums[i])dp[i]=max(dp[i−1],dp[i−2]+nums[i]).
3. Edit Distance:
○ Question: Find the minimum operations required to convert one
string into another.
○ Solution: Use DP to calculate costs for inserting, deleting, or
replacing characters.
Difference Between DP and Greedy
1. Decision-Making:
○ DP: Considers all possibilities and stores solutions to subproblems.
○ Greedy: Makes a local optimal choice at each step.
2. Optimal Substructure:
○ Both DP and greedy require this property.
○ Greedy also requires the greedy-choice property, which DP does
not.
3. When to Use:
○ Use DP when overlapping subproblems exist and greedy fails (e.g.,
0/1 knapsack).
○ Use greedy for simpler problems with a clear local-to-global optimal
solution (e.g., activity selection).
1. Optimal Substructure:
1. Knapsack Problems:
○ 0/1 Knapsack: Find the maximum value by selecting items without
exceeding capacity.
○ Subset Sum: Determine if a subset with a given sum exists.
2. String Problems:
○ Longest Common Subsequence (LCS).
○ Longest Palindromic Substring.
3. Graph Problems:
○ Shortest Path in Graphs (e.g., Bellman-Ford Algorithm).
○ All-Pairs Shortest Path (e.g., Floyd-Warshall Algorithm).
4. Game Theory:
○ Optimal strategies for games like Tic-Tac-Toe or chess endgames.
5. Matrix Chain Multiplication:
○ Minimizing the number of scalar multiplications.
1. Fibonacci Numbers:
○ Derive the recurrence relation for solving the 0/1 knapsack problem.
○ Solution: dp[i][w]=max(dp[i−1][w],dp[i−1][w−wt[i]]+val[i])dp[i][w] =
\max(dp[i-1][w], dp[i-1][w-wt[i]] + val[i]).
4. Matrix Chain Multiplication:
1. Climbing Stairs:
○ Given nn stairs, each time you can climb 1 or 2 steps. How many
distinct ways can you climb to the top?
○ Solution: Use the recurrence dp[i]=dp[i−1]+dp[i−2]dp[i] = dp[i-1] +
dp[i-2].
2. Rod Cutting Problem:
○ Given a rod of length nn and prices for each piece, find the
maximum revenue obtainable.
○ Solution: Use DP to evaluate all possible cuts and store maximum
values.
3. Minimum Edit Distance:
1. Decision-Making: