Dynamic Programming Flashcards

1
Q

(Select all that apply.) Dynamic programming is used for problems with:

A) Optimal substructure

B) Non-overlapping subproblems

C) Overlapping subproblems

A

A & B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Without memoization, a recursive solution for Fibonacci Number would have the time complexity:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

T/F:
Usually, top-down DP algorithms are faster than bottom-up ones.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

_ approaches can be parallelized while ___ approaches cannot.

(Dynamic programming), (divide and conquer)

(Divide and conquer), (dynamic programming)

Neither can be parallelized.

A

(Divide and conquer), (dynamic programming)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What must be done when converting a top-down dynamic programming solution into a bottom-up dynamic programming solution?

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

[DP Recurrence Relations]
3 key characteristics of a recurrence relation

A
  • 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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Difference in recursive calls my in DP vs Divide and Conquer

A

DC recursive calls commits to a single way of diving the input

DP considers multiple ways of defining subproblems and choosing the most optimal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Fibonacci Subproblem

A

“Let F(i) be the i-th Fibonacci number in the sequence.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Fibonacci Recurrence

A

F(0) = 0, F(1) = 1
F(i) = F(i-1) + F(i-2)
where 2 <= i <= n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

[LIS]

Subproblem

A

Let L(i) be the length of LIS of A[1 … i] which includes i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

[LIS]

Recurrence

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

[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:

A
  1. Number of subproblems: O(n)
  2. Runtime to fill the table: O(n^2)
  3. How/where the final return is extracted: return max{L(*)}
  4. Runtime to extract final return: O(n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

LCS Subproblem

A

Let L(i,j) be the length of longest common sequence for x[1…i] and y[1…j].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

LCS Recurrence

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

[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:

A
  1. Number of subproblems: O(n^2)
  2. Runtime to fill the table: O(n^2)
  3. How/where the final return is extracted: return L(n,m)
  4. Runtime to extract final return: O(1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Knapsack 0-1 Subproblem

A

Let K(i, b) be the max value achievable using a subset of objects x[1 … i] where total weight is less than or equal to b.

17
Q

Knapsack 0-1 Recurrence

A

K(0,b) = 0, 0 <= b <= B
K(i,0) = 0, 0 <= i <= n

K(i,b) = max{v[i] + K(i-1, b-w[i]), K(i-1, b)} if w[i] <= b
= K(i-1, b) otherwise
where 1 <= i <= n, 1 <= b <= B

18
Q

Knapsack with repeat, Subproblem

A

Let K(b) be the max value attainable with total weight <= b

19
Q

Knapsack with repeat, Recurrence

A

K(b) = 0
K(b) = max{v[i] + K(b - w[i]) : w[i] <= b}, 1 <= i <= n
where 0 <= b <= B

20
Q

[Knapsack 0-1]

Explain the greedy approach

A
  1. Sort objects by value per unit of weight, v[i] / w[i]
  2. Starting from highest value item, take item or skip if items doesn’t fit budget
21
Q

[Knapsack 0-1]

Explain why greedy approach fails

A

It can’t make a suboptimal choice to get a better solution in the future.

22
Q

[Knapsack]

Why is the runtime of Knapsack, O(nB), not polynomial?

A

B is simply a number, thus the number of bits required to represent number B takes space O(logB). Thus, run time is polynomial in O(n, log B).

23
Q

[CMM]

How to represent problem in binary tree

A

Root = final result
Leaf = individual matrices
Subtree = parenthetization of the problem (how solving it is structured)

24
Q

CMM Subproblem

A

Let C(i) be the minimum cost to compute A[1 … i]

25
Q

CMM Recurrence

A

Base case
C[i,j] = 0 where 1 <= j <= i <= n

C(i,j) = min{ C(i,l) + C(l+1, j) + m_{i-1} m_l m_j }
where i <= l <= j-1
and 1 <= j < i <= n

26
Q

[CMM]

Why can’t we use prefixes as subproblems?

A

If we set our recurrence on prefixes, when splitting the input into a tree, we will hit substrings where it is neither a prefix or a suffix of the original input.

Our subproblems will only have solutions for prefixes, so we can look up previous entries to solve this substring.

27
Q

[CMM]

Total cost of splitting list of matrices m[1..i] at index l

(hint: 3 components)

A

Sum of the 3 components:

Root: m[i-1], m[l], m[j]
Left: C(i,l)
Right: C(l+1, j)

28
Q

[CMM]

What do the root, left and right components of m[i-1]m[l]m[j] represent?

A

left/right subtree / subproblem
root = cost of new product

29
Q

[Contiguous Subsequence Max Sum]

Subproblem

A

Let T[i] be the max sum from substring of A[1 … i] which includes i

30
Q

[Contiguous Subsequence Max Sum]

Recurrence

A

T(0) = 0
T(i) = a[i] + max{0, a[i-1]}
where 1 <= i <= n

31
Q

[Contiguous Subsequence Max Sum]

Implementation analysis
1. Number of subproblems
2. Runtime to fill the table
3. How/where the final return is extracted from that table
4. Runtime to extract the final return

A
  1. Number of subproblems: O(n)
  2. Runtime to fill the table: O(n)
  3. How/where the final return is extracted from that table: max{L[*]}
  4. Runtime to extract the final return: O(n)
32
Q

[Edit Distance]
Input: x[1 … n], y[1 … m]

Subproblem

A

Subproblem:

Let D(i,j) be the minimum edit distance between x[1 … i] and y[1 … j] up to i and j.

33
Q

[Edit Distance]
Input: x[1…n], y[1…m]

Recurrence

A

D(i, 0) = i, 0 <= i <= n // Cost of deleting i characters
D(0, j) = j, 0 <= j <= m // Cost of inserting j characters

D(i, j) = min{
D(i-1, j-1) + diff(x[i], y[j]) # Cost to replace x[i] to y[j]
D(i-1, j) + 1 # Cost to delete x[i]
D(i, j-1) + 1 # Cost to insert y[j]
}

34
Q

[Edit Distance]

Implementation analysis
1. Number of subproblems
2. Runtime to fill the table
3. How/where the final return is extracted from that table
4. Runtime to extract the final return

A
  • Number of subproblems: O(nm)
  • Runtime to fill the table: O(nm)
  • How/where the final return is extracted: return D(n,m)
  • Runtime to extract final return: O(1)
35
Q

Algos which INCLUDE i

A
  1. LIS
  2. Contiguous Subsequence Max Sum
  3. Chain Matrix Multiply
36
Q

When do algorithms “need” to include i

A

When your subproblem definition T[i] must include input[i].

Ex. LIS, T[i] always includes A[i]