Dynamic Programming Flashcards
(Select all that apply.) Dynamic programming is used for problems with:
A) Optimal substructure
B) Non-overlapping subproblems
C) Overlapping subproblems
A & B
Without memoization, a recursive solution for Fibonacci Number would have the time complexity:
O(2^n)
(Each call to F(n) makes 2 addtional calls, to F(n-1) and F(n-2). Those 2 calls generate 4 calls, and so forth.
T/F:
Usually, top-down DP algorithms are faster than bottom-up ones.
False
Usually, a bottom-up algorithm will be faster than the equivalent top-down algorithm as there is some overhead with recursive function calls. However, sometimes, a top-down dynamic programming approach will be the better option, such as when only a fraction of the subproblems need to be solved.
_ approaches can be parallelized while ___ approaches cannot.
(Dynamic programming), (divide and conquer)
(Divide and conquer), (dynamic programming)
Neither can be parallelized.
(Divide and conquer), (dynamic programming)
What must be done when converting a top-down dynamic programming solution into a bottom-up dynamic programming solution?
Find the correct order in which to iterate over the states.
(bottom-up dynamic programming solutions iterate over all of the states (starting at the base case) such that all the results necessary to solve each subproblem for the current state have already been obtained before arriving at the current state. So, to convert a top-down solution into a bottom-up solution, we must first find a logical order in which to iterate over the states.)
[DP Recurrence Relations]
3 key characteristics of a recurrence relation
- A recurrence relation is an equation.
- The equation provides a general rule for the recurrence.
- The rule defines the next term as a function of the previous term(s).
Difference in recursive calls my in DP vs Divide and Conquer
DC recursive calls commits to a single way of diving the input
DP considers multiple ways of defining subproblems and choosing the most optimal
Fibonacci Subproblem
“Let F(i) be the i-th Fibonacci number in the sequence.”
Fibonacci Recurrence
F(0) = 0, F(1) = 1
F(i) = F(i-1) + F(i-2)
where 2 <= i <= n
[LIS]
Subproblem
Let L(i) be the length of LIS of A[1 … i] which includes i
[LIS]
Recurrence
T(1) = 1
T(i) = max{1 + T(j) if a[i] > a[j] : where 1 <= j <= i-1}
= 1 otherwise
where 2 <= i <= n
[LIS]
Implementation Analysis
1. Number of subproblems:
2. Runtime to fill the table:
3. How/where the final return is extracted:
4. Runtime to extract final return:
- Number of subproblems: O(n)
- Runtime to fill the table: O(n^2)
- How/where the final return is extracted: return max{L(*)}
- Runtime to extract final return: O(n)
LCS Subproblem
Let L(i,j) be the length of longest common sequence for x[1…i] and y[1…j].
LCS Recurrence
L(i,0) = 0, 0 <= i <= n L(0,j) = 0, 0 <= j <= m L(i,j) = 1 + L(i-1, j-1) if x[i] = y[j] = max{L(i-1, j), L(i, j-1)} if x[i] != y[j] where 0 <= i <= n, 0 <= j <= m
[LCS]
Implementation Analysis
1. Number of subproblems:
2. Runtime to fill the table:
3. How/where the final return is extracted:
4. Runtime to extract final return:
- Number of subproblems: O(n^2)
- Runtime to fill the table: O(n^2)
- How/where the final return is extracted: return L(n,m)
- Runtime to extract final return: O(1)