Dynamic Programming Expanded Content
Dynamic Programming Expanded Content
Dynamic Programming (DP) is an optimization technique used for solving problems by breaking
Unlike divide and conquer, DP solves overlapping subproblems only once and stores their solutions
for reuse.
Key Principles:
1. Overlapping Subproblems: The problem can be divided into smaller, repeated subproblems.
2. Optimal Substructure: The solution to a problem can be constructed from the solutions to its
subproblems.
Approach:
Table:
|n |0|1|2|3|4|5|6|
|------|---|---|---|---|---|---|---|
| F(n) | 0 | 1 | 1 | 2 | 3 | 5 | 8 |
By storing results, we avoid recalculating values, reducing time complexity from O(2^n) (recursive)
to O(n) (DP).
Control Abstraction in Dynamic Programming
Control abstraction defines the general framework for solving problems using DP.
Steps:
3. Transition Relation: Define how the solution for one state is derived from others.
Given weights and values of items, determine the maximum value for a given capacity.
Items:
|------|--------|-------|
|1 |2 |3 |
|2 |3 |4 |
|3 |4 |5 |
Capacity = 5.
DP Formula:
Table:
|i\w|0|1|2|3|4|5|
|--------|---|---|---|---|---|---|
|0 |0|0|0|0|0|0|
|1 |0|0|3|3|3|3|
|2 |0|0|3|4|4|7|
|3 |0|0|3|4|5|7|
Time Analysis of Control Abstraction
General Complexity:
Goal: Minimize scalar multiplications for matrices A1: 10x20, A2: 20x30, A3: 30x40.
Table:
|i\j|1 |2 |3 |
|--------|------|-------|--------|
|1 |0 | 6000 | 18000 |
|2 |- |0 | 24000 |
|3 |- |- |0 |
Recursive Formula:
Binomial coefficients are used to count combinations, represented as C(n, k) = n! / (k! * (n-k)!).
Recursive Relation:
Base Cases:
- C(n, 0) = C(n, n) = 1
|n\k|0|1|2|3|4|5|
|--------|---|---|---|---|---|---|
|0 |1| | | | | |
|1 |1|1| | | | |
|2 |1|2|1| | | |
|3 |1|3|3|1| | |
|4 |1|4|6|4|1| |
|5 | 1 | 5 | 10| 10| 5 | 1 |
Definition:
Given keys {k1, k2, ..., kn} with search probabilities {p1, p2, ..., pn}, construct a binary search tree
Recursive Relation:
Example:
|i\j|1 |2 |3 |
|--------|-----|------|-------|
|2 | - | 0.5 | 1.1 |
|3 |- |- | 0.3 |
Matrix chain multiplication minimizes the number of scalar multiplications for a sequence of
matrices.
Recursive Formula:
Example:
|i\j|1 |2 |3 |
|--------|------|-------|--------|
|1 |0 | 6000 | 18000 |
|2 |- |0 | 24000 |
|3 |- |- |0 |