Dynamic Programming
Conquer the most mathematically intensive algorithmic strategy. Learn how to solve the 0/1 Knapsack problem using tabulation, and master the heavily-tested Matrix Chain Multiplication numericals.
Learning Goals
- Define the two strict requirements for Dynamic Programming: Overlapping Subproblems and Optimal Substructure.
- Explain how a 2D matrix solves the 0/1 Knapsack problem where the Greedy method fails.
- Memorize the Recurrence Relation for Matrix Chain Multiplication (MCM).
- Perform a flawless, mathematically rigorous step-by-step trace of an MCM numerical.
The Two Pillars of Dynamic Programming
Dynamic Programming (DP) is a strategy used to solve optimization problems by breaking them down into simpler subproblems. Unlike Divide and Conquer (which divides problems into independent subproblems), DP is used when subproblems are dependent and repeat themselves.
To use DP, a problem must exhibit two core properties:
- Overlapping Subproblems: The problem can be broken down into subproblems which are reused multiple times. Instead of recalculating the same subproblem (which causes exponential time), DP stores the result in a table (memoization or tabulation) and reuses it in time. Example: In Fibonacci, calculating requires and , but also requires . is overlapping.
- Optimal Substructure: The optimal solution to the overall problem can be constructed directly from the optimal solutions of its subproblems. If you make the best choice at each step, you are guaranteed the global best choice.
Solving 0/1 Knapsack using DP
- 1Step 1
In 0/1 Knapsack, items cannot be broken into fractions. You must take the whole item or leave it. The Greedy method fails because picking an item with the highest Profit/Weight ratio might leave a large, awkward gap in the knapsack that cannot be filled, wasting capacity. We must evaluate combinations to guarantee the global optimum.
- 2Step 2
We create a 2D matrix
dp[i][w].irepresents the items considered so far (from 1 to N).wrepresents the current capacity of the knapsack (from 0 to total capacity W).dp[i][w]stores the maximum profit we can achieve using the firstiitems with a weight limit ofw. - 3Step 3
For each cell, we have two choices for the current item
i(with weightW[i]and profitP[i]):- Leave it: The profit is the same as if we didn't have this item:
dp[i-1][w]. - Take it: We add
P[i]to our profit, but we must subtractW[i]from our capacity:P[i] + dp[i-1][w - W[i]]. We take the maximum of these two choices.
- Leave it: The profit is the same as if we didn't have this item:
- 4Step 4
1For i = 1 to N: 2 For w = 1 to W: 3 If W[i] <= w: 4 dp[i][w] = MAX( dp[i-1][w] , P[i] + dp[i-1][w - W[i]] ) 5 Else: 6 dp[i][w] = dp[i-1][w] - 5Step 5
We fill a table of size
N(items) byW(capacity). Therefore, the Time Complexity is O(N * W) and the Space Complexity is O(N * W).
Matrix Chain Multiplication (MCM)
Matrix Chain Multiplication is the most frequently tested DP problem in the exams. Objective: We are not actually multiplying the matrices. We are trying to find the most efficient way to place parentheses around the matrices to minimize the total number of scalar multiplications.
Rule: Multiplying a matrix of size with a matrix of size costs operations, and the resulting matrix is size .
The MCM Recurrence Relation: To find the minimum cost to multiply a chain of matrices from to , we split the chain at some matrix .
- : Cost of multiplying the left half.
- : Cost of multiplying the right half.
- : Cost of multiplying the resulting left matrix with the resulting right matrix.
Trace: Matrix Chain Multiplication
- 1Step 1
Given matrices: , , , . We extract the common dimensions into an array . .
- 2Step 2
Multiplying a single matrix by itself costs 0 operations.
- 3Step 3
We calculate where .
- 4Step 4
Calculate : We can split at or . If : . If : . Minimum is 1200 (split at ).
Calculate : We can split at or . If : . If : . Minimum is 3000 (split at ).
- 5Step 5
Calculate : We can split at or . If : . If : . If : . Minimum operations = 2200 (split at ).
- 6Step 6
The minimum number of operations is 2200. Because the final split was at , the optimal parenthesization is . The Time Complexity for MCM is and Space Complexity is to store the table.
Knowledge Check
What is the primary advantage of dynamic programming over brute-force algorithms?