You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notes/dynamic_programming.md
+15-19Lines changed: 15 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -76,9 +76,9 @@ def fibonacci(n, memo={}):
76
76
77
77
**Algorithm Steps**:
78
78
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 tableto 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.
82
82
83
83
**Example**: Computing Fibonacci Numbers with Tabulation
84
84
@@ -95,8 +95,8 @@ def fibonacci(n):
95
95
96
96
**Analysis**:
97
97
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 \).
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.
130
130
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.
134
133
135
134
### State Representation
136
135
137
136
Properly defining the state is crucial for dynamic programming.
138
137
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.
141
140
142
141
**Example**: 0/1 Knapsack Problem
143
142
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 \).
We fill the table $dp[0..n][0..W]$ iteratively based on the state transition.
158
155
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.
162
158
163
159
## Advanced Dynamic Programming Concepts
164
160
@@ -207,8 +203,8 @@ with base case $0! = 1$.
207
203
208
204
**Mathematical Properties**:
209
205
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.
0 commit comments