Skip to content

Commit 523b80f

Browse files
authored
Update dynamic_programming.md
1 parent 0f693cf commit 523b80f

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

notes/dynamic_programming.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ def fibonacci(n, memo={}):
7676

7777
**Algorithm Steps**:
7878

79-
1. **Initialize the Table**: Set up a table to hold the results of subproblems, initializing base cases.
80-
2. **Iterative Computation**: Use loops to fill the table, ensuring that subproblems are solved before they are needed.
81-
3. **Construct Solution**: Use the filled table to construct the solution to the original problem.
79+
1. **Initialize the table** by setting up a structure to store the results of subproblems, and ensure that base cases are properly initialized to handle the simplest instances of the problem.
80+
2. **Iterative computation** is carried out using loops to fill the table, making sure that each subproblem is solved in the correct order before being used to solve larger subproblems.
81+
3. **Construct the solution** by referencing the filled table, using the stored values to derive the final solution to the original problem efficiently.
8282

8383
**Example**: Computing Fibonacci Numbers with Tabulation
8484

@@ -95,8 +95,8 @@ def fibonacci(n):
9595

9696
**Analysis**:
9797

98-
- **Time Complexity**: $O(n)$, as we iterate from $2$ to $n$.
99-
- **Space Complexity**: $O(n)$, for the table storing Fibonacci numbers.
98+
- The **time complexity** is \( O(n) \), since the algorithm iterates from 2 to \( n \), computing each Fibonacci number sequentially.
99+
- The **space complexity** is \( O(n) \), due to the storage required for the table that holds the Fibonacci numbers up to \( n \).
100100

101101
### Comparison of Memoization and Tabulation
102102

@@ -128,23 +128,20 @@ LCS(i - 1, j - 1) + 1 & \text{if } x_i = y_j \\
128128

129129
We can implement the LCS problem using either memoization or tabulation. With tabulation, we build a two-dimensional table $LCS[0..m][0..n]$ iteratively.
130130

131-
**Time Complexity**: $O(mn)$
132-
133-
**Space Complexity**: $O(mn)$, which can be reduced to $O(n)$ with optimized storage.
131+
- The **time complexity** is \( O(mn) \), as the algorithm processes a grid or matrix of size \( m \times n \), iterating through each cell.
132+
- The **space complexity** is \( O(mn) \), due to the table storing intermediate results, but this can be reduced to \( O(n) \) by optimizing the storage to only keep necessary data for the current and previous rows.
134133

135134
### State Representation
136135

137136
Properly defining the state is crucial for dynamic programming.
138137

139-
- **State Variables**: Parameters that uniquely define a subproblem.
140-
- **State Transition**: Rules that define how to move from one state to another.
138+
- **State variables** are the parameters that uniquely define each subproblem, helping to break down the problem into smaller, manageable components.
139+
- **State transition** refers to the rules or formulas that describe how to move from one state to another, typically using the results of smaller subproblems to solve larger ones.
141140

142141
**Example**: 0/1 Knapsack Problem
143142

144-
- **Problem Statement**: Given $n$ items with weights $w_i$ and values $v_i$, and a knapsack capacity $W$, maximize the total value without exceeding the capacity.
145-
146-
- **State Representation**: $dp[i][w]$ represents the maximum value achievable with the first $i$ items and capacity $w$.
147-
143+
- The **problem statement** focuses on selecting \( n \) items, each with a weight \( w_i \) and value \( v_i \), while ensuring the total weight stays within the knapsack capacity \( W \), in order to maximize the total value.
144+
- In **state representation**, \( dp[i][w] \) represents the maximum value that can be achieved using the first \( i \) items with a total weight capacity of \( w \).
148145
- **State Transition**:
149146

150147
$$ dp[i][w] = \begin{cases}
@@ -156,9 +153,8 @@ dp[i - 1][w] & \text{if } w_i > w \\
156153

157154
We fill the table $dp[0..n][0..W]$ iteratively based on the state transition.
158155

159-
**Time Complexity**: $O(nW)$
160-
161-
**Space Complexity**: $O(nW)$, can be optimized to $O(W)$ since each row depends only on the previous row.
156+
- The **time complexity** is \( O(nW) \), where \( n \) is the number of items and \( W \) is the capacity of the knapsack, as the algorithm iterates through both items and weights.
157+
- The **space complexity** is \( O(nW) \), but this can be optimized to \( O(W) \) because each row in the table depends only on the values from the previous row, allowing for space reduction.
162158

163159
## Advanced Dynamic Programming Concepts
164160

@@ -207,8 +203,8 @@ with base case $0! = 1$.
207203

208204
**Mathematical Properties**:
209205

210-
- **Total Subsets**: A set with $n$ elements has $2^n$ subsets.
211-
- **Power Set**: The set of all subsets of $S$, denoted as $\mathcal{P}(S)$.
206+
- The **total subsets** of a set with \( n \) elements is \( 2^n \), as each element can either be included or excluded from a subset.
207+
- The **power set** of a set \( S \), denoted as \( \mathcal{P}(S) \), is the set of all possible subsets of \( S \), including the empty set and \( S \) itself.
212208

213209
**Relevance to DP**:
214210

0 commit comments

Comments
 (0)