Week 3 (Dynamic Programming) Flashcards

(10 cards)

1
Q

How does Dynamic Programming work?

A

Dynamic programming builds a solution using sub-problems and reduces recursive calls by storing previously calculated values in an array.

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

What is memorisation of recursion?

A

Where we store each value in an array when it is first found to read later, reducing the number of recursions

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

What are the steps for any dynamic programming problem?

A

1) Explain what is being stored in the array / table
2) State the base case and final product
3) Explain the recurrence (correctness) and how it is built consistently
4) Explain computation for each entry and overall running time which is entries x computational cost

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

How do we solve the weighted interval scheduling problem using dynamic programming?

A
  • Order request in increasing finish time (O(nlogn))
  • For each request j, we define Last(j) to be the last compatible request with j
  • M[j] = value of the set of requests of maximum weight chosen for the sub-instance containing requests {1,…,j}
  • Base case: M[1] = weight(1), final case: M[n]
  • M[j] = max{weight(j) + M[last(j)]; M[j-1]}
  • M[j] operations are constant time, two lookups, one max, one addition
    Overall complexity is O(nlogn)+O(n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the Bellman-Ford algorithm using dynamic programming?

A

We maintain an n ⇒ n table indexed by 0 <= i <= (n - 1) and v ∈ V where OPT[i, v] stores the length of shortest s -> v path which uses at most i edges.
Define OPT[0, s] = 0 and OPT[0, v] = infinity for each v ∈ V, v != s
for i = 1, 2, . . . , n - 1 do
for each v ∈ V do
OPT[i, v] = min{OPT[i - 1, v] , min w∈V (length(w, v) + OPT[i - 1, w])}
end for
end for
return OPT[n - 1, v] for each v ∈ V

Running time = O(n^2) entries x O(n) min and add computations => O(n^3) total

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

Why cant we convert all edge-lengths to be non-negative for shortest path problem?

A

Adding a large positive constant can change the identity of the shortest path

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

What is the theorem for shortest path?

A

For any two vertices s and t, the shortest s->t path has at most n-1 edges

Proof: if a vertex x repeats on a shortest path P with fewest edges, delete x->x. This decreases the number of edges but the length cannot due to no negative cycles

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

What do we assume with the Bellman-Ford algorithm?

A

We assume there are no negative cycles

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

What is the subset problem?

A

Given a set (1,…,n} of n items with weights Weight(1),…,Weight(n) and a bound W, find a set S of items such that ∑ Weight(i) is maximised subject to the constraint ∑ Weight(i) <= W

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

What is the dynamic programming algorithm for subset problem?

A

OPT[i] denotes the max weight of choosing items from {1,..,i} such that ∑ weight(i) is maximised
Base case: OPT[1] = Weight(1) if Weight(1) <= W, else 0
Recurrence = OPT[i,z] = max{OPT[i-1, z]; weight(i) + OPT[i-1,z-weight(i)]}
OPT has O(n.W) entries, filling up the table looks up two existing entries and other constant time operations.
Running time = O(n.W)

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