Docsity

Dynamic Programming: Maximum Subset and Sequence Alignment, Lecture notes of Computer Science

Partial preview of the text

Download Dynamic Programming: Maximum Subset and Sequence Alignment and more Lecture notes Computer Science in PDF only on Docsity!

Chapter 6

Dynamic Programming

Slides by Kevin Wayne.Copyright Ā© 2005 Pearson-Addison Wesley. All rights reserved.

Algorithmic Paradigms

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.

Dynamic Programming Applications

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.

6.1 Weighted Interval Scheduling

Unweighted Interval Scheduling Review

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

Weighted Interval Scheduling

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

Dynamic Programming: optimal solution

From the previous slide, we can observe that Request j belongs to an

optimal solution on the set {1, 2,... , j}if and only if

vj + OPT(p(j)) ≄ OPT(j āˆ’ 1 )

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)) }

Weighted Interval Scheduling: Brute Force

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

Weighted Interval Scheduling: Memoization

Memoization. Store results of each sub-problem in a cache; lookup as needed.

Weighted Interval Scheduling: Running Time

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

  • (i) returns an existing value M[j]
  • (ii) fills in one new entry M[j] and makes two recursive calls

 Progress measure  = # nonempty entries of M[].

  • initially  = 0, throughout  ļ‚£ n.
  • (ii) increases  by 1 ļƒž at most 2n recursive calls.

 Overall running time of M-Compute-Opt(n) is O(n). ā–Ŗ

Remark. O(n) if jobs are pre-sorted by start and finish times.

Weighted Interval Scheduling: Bottom-Up

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]) }

6.3 Segmented Least Squares

Segmented Least Squares

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

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:

  • the sum of the sums of the squared errors E in each segment
  • the number of lines L  Tradeoff function: E + c L, for some constant c > 0.

x

y