Week 3 (Dynamic Programming) Flashcards
(10 cards)
How does Dynamic Programming work?
Dynamic programming builds a solution using sub-problems and reduces recursive calls by storing previously calculated values in an array.
What is memorisation of recursion?
Where we store each value in an array when it is first found to read later, reducing the number of recursions
What are the steps for any dynamic programming problem?
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 do we solve the weighted interval scheduling problem using dynamic programming?
- 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)
What is the Bellman-Ford algorithm using dynamic programming?
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
Why cant we convert all edge-lengths to be non-negative for shortest path problem?
Adding a large positive constant can change the identity of the shortest path
What is the theorem for shortest path?
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
What do we assume with the Bellman-Ford algorithm?
We assume there are no negative cycles
What is the subset problem?
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
What is the dynamic programming algorithm for subset problem?
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)