Dynamic Programming
Recap:
- Greed: process the input in some order, myopically making irrevocable decisions.
- Divide-and-conquer: break up probelms into independent subproblems; solve them; and combine individual solutions to form solution to original problem
- Dyanmic programming (caching intermediate results in a table for later reuse): break up a problem into a series of overlapping subproblems; combine solutions to smaller subproblems to form solutions to a large subproblem
Bellman pioneered the systematic study of DP in 1950s. Etymology
- Dynamic programming = programming over time
- Named this to avoid fear of mathematical research
Weighted Interval Scheduling
Recall the interval scheduling problem. We modify it so that jobs have weights.
- A job starts at , finishes at , and has weight .
- Two jobs are compatible if they don't overlap.
- Goal: find the max-weight subset of mutually compatible jobs.
The greedy algorithm is correct if all weights are 1, but fails spectaculary for the weighted version.
- Recall: it considers jobs in ascending order of finish time, and adds the job to the subset if its compatible with the previously chosen jobs.
Dynamic programming: binary choice
Definition: = max weight of any subset of mutually compatible jobs for subproblem consisting only of jobs . Goal: = max weight of any subset of mutually compatible jobs Case 1: does not select job .
- There must be an optimal solution to problem consisting of remaining jobs . Case 2: selects job .
- Collect profit
- Can't use incompatible jobs
- Must include optimal solution to problem consisting of remaining
compatible jobs .
Bellman equation:
Brute force:
Brute-force():
- sort jobs by finish time so that
- Compute via binary search
- Return Compute-Opt(n)
Compute-Opt(j):
- If (j=0)
- Return 0
- Else
- Return max
Observation: recurisve algorithm is slow because of the overlapping subproblems exponential-time algorithm.
- E.g., the numbe of recursive calls for family of "layered" instances grows like the fibonacci sequence.
Memoization
Top-down dp (memoization).
- Cache result of subproblem in .
- Use to avoid solving subproblem more than once.
Top-Down()
- sort jobs by finish time so that
- Compute via binary search
- Return M-Compute-Opt(n)
M-Compute-Opt(j):
- If ( is uninitialized)
- Return
Running time analysis The memoized version of the algorithm takes time. Proof:
-
sort by finish time with mergesort: .
-
compute for each : via binary search
-
: each invocation takes time and either
-
- returns an initialized value
-
- initializes and makes two recursive calls.
-
-
Progress measures initialized entires among .
- Initially ; throughout
-
- decreases by recursive calls.
-
Overall running time of is .
Those who cannot remember the past are codemned to repeat it.
So now that we have a DP algorithm that computes the optimal value, how do you find the optimal solution?
Find-Solution()
- If
- return
- Else if (
- Return
- Else
- Return .
Botton-up DP
Unwind recursion Buttom-Up()
- sort jobs by finish time so that
- Compute via binary search
- For to :
Those are previously computed value.
The running time of the button-up version takes time.
Maximum subarray problem
Given an array of integer (pos/neg), find a contiguous subarray whose sum is a maximum.
- Let OPT(i) be teh maximum possible sum for any subarray ending at .
- .
The brute-force algorithm involves computing the sum for each i to j of .
- This takes time.
Apply the "cumulative sum" trick
- precompute cumulative sums: .
- Now
- Improves running time to .
Kadane's algorithm: max sum of any subarray of whose rightmost index is
- The goal: max .
Bellman equation:
- in the max statement, we want to either take only element or take the element together with the best subarray ending at index .
This makes the running time .
Segmented Least Squares
This is a multi-choice problem in the sense that you're mkaing the best choice out of many options.
Least square is a foundational problem in statistics.
- Given points in the plane; .
- Find a line that minimizes the sum of the squared error.
Solution: calculus min error is acheived when...
Segmented least squares: points lie roughly on a squence of several line segments. Given points in the plane: , find a sequence of lines that mimimizes . What is the reasonable choice for to balance accuracy (goodness of fit) and parsimony (number of lines)?
- The goal is to mimimize for some constant , where
- = sum of the sums of the squared errors in each segment
- = the number of lines.
Dynamic programming: multiway choice (as compared to binary choice until now). Notation:
- = minimum cost for points .
- = SSE (sum of squared errors) for the points for some .
To compute :
- last segment uses points for some .
- cost = .
Bellman equation:
- ranges from to , making it multiway.
Algorithm
segmented-least-squares()
-
For to :
- For to ,
- compute the SSE for the points
- For to ,
-
-
For to
- where is the previous previously computed value.
-
return .
Analysis
Theorem: DP algorithm solves the segmented least squares problem in time and space.
Proof:
- the bottleneck comes from computing SSE for each and .
- It takes to compute .
It can be improved to time
- for each : pre compute the comulative sums, which can be used to compute in time.
Knapsack Problem
Goal: pack knapsack so as to maximize total value of items taken
- there are items: item provides value and weighs .
- value of a subset of items = sum of values of individual items.
- knapsack has weight limit of
For example, the subset has value and weight .
Assume that all values and weights are integral.
Define = optimal value of knapsack problem with items , subject to weight limit . We want to find meaning that we use all items and are subject to weight limit .
In case 1: does not select item . (possibily because ).
- Then selects the best of subject to weight limit .
In case 2: selects item .
- collect the new value .
- new weight limit = .
- selects bets of subject to the new weight limit.
Bellman equation:
Algorithm
Knapsack()
- For to
- .
- For to
- For to ,
- If ()
- Else
- For to ,
- Return .
Analysis
Theorem: the DP algorithm solves the knapsack problem with items and max weight in time and space. Where weights are integers between and .
Proof:
- Take time per table entry
- There are table entries
- After computing optimal values, can trace back to find solutions: takes item iff .
Remarks:
- algorithm depends critically on assumptions that weights are integral
- assumption that values are integral was not used
Bellman Ford Algorithm
Djikstra's algorithm does not work with edges with negative weight. This is beacuse greedy algorithms do not do backtracking and there could be a shorter path but would not be the smallest initially, which is how greedy algorithms work.
In graphs with negative cycles, the shortest path can be infinitely small, which is why the path must not have negative cycels.
Bellman Ford Algorithm finds the shortest path when there are negative values.
Theorem: If has no negative cycles, then there is a sortest path from to that is simple, i.e., it has nor epeated nodes, and hence at most edges.
We define the bellman equation as OPT(n-1, s) which represents the shortest path from s to t.
Define as the minimum cost of a path from to using at most edges.
If then
Algorithm
Shortest-Path()
-
number of nodes in
-
Array
-
Define and for all other .
-
For
- For in any order
- Compute using recurrence
- Endfor
- For in any order
-
Endfor
-
Return .t
Linked from Algorithms