














































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















































Slides by Kevin Wayne.Copyright Ā© 2005 Pearson-Addison Wesley. All rights reserved.
Greedy. Build up a solution incrementally, myopically optimizing some local criterion.
Divide-and-conquer. Break up a problem into sub-problems, solve each sub-problem independently, and combine solution to sub-problems to form solution to original problem.
Dynamic programming. Break up a problem into a series of overlapping sub-problems, and build up solutions to larger and larger sub-problems.
Areas. ļ® Bioinformatics. ļ® Control theory. ļ® Information theory. ļ® Operations research. ļ® Computer science: theory, graphics, AI, compilers, systems, ā¦.
Some famous dynamic programming algorithms. ļ® Unix diff for comparing two files. ļ® Viterbi for hidden Markov models. ļ® Smith-Waterman for genetic sequence alignment. ļ® Bellman-Ford for shortest path routing in networks. ļ® Cocke-Kasami-Younger for parsing context free grammars.
Recall. Greedy algorithm works if all weights are 1. ļ® Consider jobs in ascending order of finish time. ļ® Add job to subset if it is compatible with previously chosen jobs.
Observation. Greedy algorithm can fail spectacularly if arbitrary weights are allowed.
0 1 2 3 4 5 6 7 8 9 10 11 Time
b a
weight = 999
weight = 1
Notation. Label jobs by finishing time: f 1 ļ£ f 2 ļ£... ļ£ fn. Def. p(j) = largest index i < j such that job i is compatible with j.
Ex: p(8) = 5, p(7) = 3, p(2) = 0.
Time 0 1 2 3 4 5 6 7 8 9 10 11
6 7 8
4
3
1 2
5
Input: n, s 1 ,ā¦,s n , f 1 ,ā¦,f n , v 1 ,ā¦,v n Sort jobs by finish times so that f 1 ļ£ f 2 ļ£ ... ļ£ fn. Compute p(1), p(2), ā¦, p(n) Compute-Opt(j) { if (j = 0) return 0 else return max(vj + Compute-Opt(p(j)), Compute-Opt(j-1)) }
Brute force algorithm.
Input: n, s 1 ,ā¦,s n , f 1 ,ā¦,f n , v 1 ,ā¦,v n Sort jobs by finish times so that f 1 ļ£ f 2 ļ£ ... ļ£ fn. Compute p(1), p(2), ā¦, p(n) for j = 1 to n M[j] = empty M[0] = 0 M-Compute-Opt(j) { if (M[j] is empty) M[j] = max(vj + M-Compute-Opt(p(j)), M-Compute-Opt(j-1)) return M[j] }
global array
Memoization. Store results of each sub-problem in a cache; lookup as needed.
Claim. Memoized version of algorithm takes O(n log n) time. ļ® Sort by finish time: O(n log n). ļ® Computing p(ļ) : O(n log n) via sorting by start time.
ļ® M-Compute-Opt(j): each invocation takes O(1) time and either
ļ® Progress measure ļ = # nonempty entries of M[].
ļ® Overall running time of M-Compute-Opt(n) is O(n). āŖ
Remark. O(n) if jobs are pre-sorted by start and finish times.
Bottom-up dynamic programming. Unwind recursion.
Input: n, s 1 ,ā¦,s n , f 1 ,ā¦,f n , v 1 ,ā¦,v n Sort jobs by finish times so that f 1 ļ£ f 2 ļ£ ... ļ£ fn. Compute p(1), p(2), ā¦, p(n) Iterative-Compute-Opt { M[0] = 0 for j = 1 to n M[j] = max(vj + M[p(j)], M[j-1]) }
Segmented least squares. ļ® Points lie roughly on a sequence of several line segments. ļ® Given n points in the plane (x 1 , y 1 ), (x 2 , y 2 ) ,... , (xn, yn) with ļ® x 1 < x 2 < ... < xn, find a sequence of lines that minimizes f(x).
Q. What's a reasonable choice for f(x) to balance accuracy and parsimony?
x
y
goodness of fit number of lines
Segmented least squares. ļ® Points lie roughly on a sequence of several line segments. ļ® Given n points in the plane (x 1 , y 1 ), (x 2 , y 2 ) ,... , (xn, yn) with ļ® x 1 < x 2 < ... < xn, find a sequence of lines that minimizes:
x
y