Dynamic Programming
What is the top level decision for the station placement problem?
If the first i towns use the leftmost station, then the station should be (t_i + t_1)/2. If we serve i stations with the leftmost station, then we have n-i towns to serve with k-1 stations.
What is the recipe for a Dynamic Program?
1) Formulate the right subproblem - The subproblem must have the optimal substructure 2) Formulate the recurrence - Identify how the result of the smaller subproblems can lead to that of a larger subproblem 3) State the base cases - The subproblem so small that we know the answer to it 4) Choose the memoization data structure - Where are you going to store already computed values? 5) Identify evaluation order - Identify dependencies: Which subproblem depend on which ones, 6) Analyze space and running time
What are the 4 main ingredients of a DP solution?
1) Notation - Understand the subproblems that need to be solved and come with the appropriate solution 2) Ensure that there are not too many subproblems that need to be solved 3) Recurrence Relation: Express the answer to a subproblem in terms of answers to smaller subproblems, taking max or min of choices.
What is the main point if dynamic programming?
Dynamic Programming is all about reducing the number of recursive calls you make using memoization.
What is dynamic programming and how does it work at a high level?
Dynamic programming is simply an optimization over plain recursion. Whenever we see a recursive solution for the same inputs, we can optimize it using Dynamic programming. The main idea is to simply store the results of the sub-problems so that we don't need to recompute when they are needed later. At a high level, you are looking at all the possible first choices, recursively solving them and then picking the best one.
What questions should you ask to find the running time of a dynamic running time algorithm?
How many subproblems are there and how long does does each subproblem take?
What is the running time for station placement?
In the process, we consider nk subproblems (one for each combination of possible leftmost town and number of stations) Each subproblem takes O(n) time to solve since we take the minimum of n possibilities.
What is the weight activity selection problem and how would you solve it with dynamic programming? Explain the following: - What is P(i)? - What is the recurrence relation?
In the weighted activity selection problem, you have n events e_1, e_2,...e_n. For each event, you have the start time s_1, finish time f_i, and the reward which is r_i. What we want to do is find a non-conflicting subset of events with the highest reward. - The way we approach it with dynamic programming is by considering what you want to schedule last. But since we don't have a greedy strategy, we consider every possible choice for that last position. - If you have an event e_i that you are considering for the last position, you are removing everything after e_i and you are removing anything that is conflicting with e_i. P(i) = j means that there has to be some event e_1 to e_j that are left in the subproblem - Define M(j) as the best reward we can get from the first j events and we ultimately want to computer M(n) - The recurrence relation is M(n) = max(r_i) + M(p(i)). r_i is the reward from event e_i
What is the recurrence of the station placement problem? What is the recurrence relation? - Write the recursive relation and base cases
Locate(i, l) finds the best l stations to serve town t_i through t_n and return the maximum distance from any town to its nearest station.
Explain whether the weighted activity selection problem with dynamic programming is efficient and how do we analyze efficiency?
One way to analyze dynamic programming algorithms is to ask yourself how many subproblems are you trying to solve. There can be n such subproblems We can use memoization to store pre-computer solutions in a table and make sure our run time complexity doesn't explode to n^2 Calculating M(j) the first time requires some work the first time since we do it recursively. But once it is calculated, we store the answer in a table.
What is the station placement problem? What is the cost of any solution?
We want to place k stations along a train line in such a way so that the maximum distance between a town and its nearest station is minimized. The cost of any solution is the maximum distance between any town and its nearest station.