dynamic programming
play

Dynamic Programming Algorithm Design 6.1, 6.2, 6.3 Thank you to - PowerPoint PPT Presentation

Dynamic Programming Algorithm Design 6.1, 6.2, 6.3 Thank you to Kevin Wayne for inspiration to slides Applications In class (today and next time) 4 Applications In class (today and next time) Weighted interval scheduling Set of


  1. Dynamic Programming Algorithm Design 6.1, 6.2, 6.3 Thank you to Kevin Wayne for inspiration to slides

  2. Applications • In class (today and next time) 4

  3. Applications • In class (today and next time) • Weighted interval scheduling • Set of weighted intervals with start and finishing times • Goal: find maximum weight subset of non-overlapping intervals j 1 2 j 2 4 1 j 3 j 4 10 j 5 7 j 6 5 6 j 7 j 8 4 5

  4. Applications • In class (today and next time) • Weighted interval scheduling • Segmented least squares • Given n points in the plane find a small sequence of lines that minimizes the squared error. y x 6

  5. Applications • In class (today and next time) • Weighted interval scheduling • Segmented least squares • Sequence alignment • Given two strings A and B how many edits (insertions, deletions, relabelings) is needed to turn A into B? A C A A G T C A C A A - G T C - C A T G T - - C A - T G T - 1 mismatch, 2 gaps 0 mismatches, 4 gaps 7

  6. Applications • In class (today and next time) • Weighted interval scheduling • Segmented least squares • Sequence alignment • Shortest paths with negative weights • Given a weighted graph, where edge weights can be negative, find the shortest path between two given vertices. 3 -1 10 3 -6 t 5 -4 s 3 9 5 1 8 8

  7. Applications • In class (today and next time) • Weighted interval scheduling • Segmented least squares • Sequence alignment • Shortest paths with negative weights • Some other famous applications • Unix di ff for comparing 2 files • Vovke-Kasami-Younger for parsing context-free grammars • Viterbi for hidden Markov models • …. 9

  8. Dynamic Programming • Greedy. Build solution incrementally, optimizing some local criterion. • Divide-and-conquer. Break up problem into independent subproblems, solve each subproblem, and combine to get solution to original problem. • Dynamic programming. Break up problem into overlapping subproblems, and build up solutions to larger and larger subproblems. • Can be used when the problem have “optimal substructure”: Solution can be constructed from optimal solutions to subproblems Use dynamic programming when subproblems overlap. 10

  9. Computing Fibonacci numbers • Fibonacci numbers: if n = 0 0 if n = 1 1 F n = otherwise F n − 1 + F n − 2 • First try: Fib(n) time Θ ( ϕ n ) if n = 0 6 return 0 else if n = 1 5 X 4 return 1 else X X X 4 3 3 2 return Fib(n-1) + Fib(n-2) X X X X X X X 3 2 2 1 2 1 1 0 X X X X X X 2 1 1 0 1 0 1 0 1 0 Avoid recomputation?

  10. Memoized Fibonacci numbers • Fibonacci numbers: if n = 0 0 6 if n = 1 1 F n = otherwise F n − 1 + F n − 2 5 • Remember already computed values: 4 for j=1 to n F[j] = null Mem-Fib(n) 3 Mem-Fib(n) time Θ ( n ) if n = 0 2 return 0 else if n = 1 1 return 1 else 0 if F[n] is empty F[n] = Mem-Fib(n-1) + Mem-Fib(n-2) return F[n]

  11. Bottom-up Fibonacci numbers • Fibonacci numbers: if n = 0 0 if n = 1 1 F n = otherwise F n − 1 + F n − 2 • Remember already computed values: Iter-Fib(n) F[0] = 0 time Θ ( n ) F[1] = 1 for i = 2 to n space Θ ( n ) F[n] = F[n-1] + F[n-2] return F[n]

  12. Bottom-up Fibonacci numbers - save space • Fibonacci numbers: if n = 0 0 if n = 1 1 F n = otherwise F n − 1 + F n − 2 • Remember last two computed values: Iter-Fib(n) previous = 0 time Θ ( n ) current = 1 for i = 1 to n space Θ (1) next = previous + current previous = current current = next return current

  13. Weighted Interval Scheduling 15

  14. Weighted interval scheduling • Weighted interval scheduling problem • n jobs (intervals) • Job i starts at s i , finishes at f i and has weight/value v i . • Goal: Find maximum weight subset of non-overlapping (compatible) jobs. j 1 v 1 = 2 v 2 = 4 j 2 v 3 = 1 j 3 j 4 v 4 = 9 j 5 v 5 = 7 j 6 v 6 = 5 v 7 = 6 j 7 j 8 v 8 = 4 16

  15. Weighted interval scheduling • Weighted interval scheduling problem • n jobs (intervals) • Job i starts at s i , finishes at f i and has weight/value v i . • Goal: Find maximum weight subset of non-overlapping (compatible) jobs. j 1 2 4 j 2 Optimal? 1 j 3 j 4 9 j 5 7 j 6 5 6 j 7 j 8 4 17

  16. Weighted interval scheduling • Weighted interval scheduling problem • n jobs (intervals) • Job i starts at s i , finishes at f i and has weight/value v i . • Goal: Find maximum weight subset of non-overlapping (compatible) jobs. j 1 2 4 j 2 1 j 3 j 4 9 j 5 7 j 6 5 6 j 7 j 8 4 18

  17. Weighted interval scheduling • Label/sort jobs by finishing time: f 1 ≤ f 2 ≤ … ≤ f n 2 4 1 9 7 5 6 4 19

  18. Weighted interval scheduling • Label/sort jobs by finishing time: f 1 ≤ f 2 ≤ … ≤ f n • Greedy? j 1 1 1 j 2 4 2 j 3 j 4 7 7 j 5 9 5 j 6 6 j 7 j 8 4 20

  19. Weighted interval scheduling • Label/sort jobs by finishing time: f 1 ≤ f 2 ≤ … ≤ f n • p(j) = largest index i < j such that job i is compatible with j . • Optimal solution OPT: • Case 1. OPT selects last job OPT = v n + optimal solution to subproblem on 1,…,p(n) • Case 2. OPT does not select last job OPT = optimal solution to subproblem on 1,…,n-1 j 1 1 p(1) = 0 j 2 p(2) = 0 4 2 p(3) = 0 j 3 p(4) = 2 j 4 7 p(5) = 1 j 5 9 5 j 6 p(6) = 2 6 j 7 p(7) = 3 p(8) = 5 j 8 4 4 21

  20. Weighted interval scheduling • OPT(j) = value of optimal solution to the problem consisting job requests 1,2,.., j . • Case 1. OPT( j ) selects job j OPT(j) = v j + optimal solution to subproblem on 1,…,p(j) • Case 2. OPT( j ) does not select job j OPT = optimal solution to subproblem 1,…j-1 • Recursion: OPT ( j ) = { if j = 0 0 otherwise max{ v j + OPT ( p ( j )), OPT ( j − 1)} 22

  21. Weighted interval scheduling: brute force OPT ( j ) = { if j = 0 0 otherwise max{ v j + OPT ( p ( j )), OPT ( j − 1)} Input: n, s[1..n], f[1..n], v[1..n] Sort jobs by finish time so that f[1] ≤ f[2] ≤ … ≤ f[n] time Θ (2 n ) Compute p[1], p[2], …, p[n] Compute-BruteForce—Opt(n) 5 Compute-Brute-Force-Opt(j) if j = 0 return 0 3 4 else return max(v[j] + Compute-Brute-Force-Opt(p[j]), 2 3 1 2 Compute-Brute-Force-Opt(j-1)) 1 0 0 0 1 0 1 1 2 2 3 0 0 1 0 0 0 0 0 4 0 0 5 23

  22. Weighted interval scheduling: memoization Input: n, s[1..n], f[1..n], v[1..n] Sort jobs by finish time so that f[1] ≤ f[2] ≤ … ≤ f[n] 5 Compute p[1], p[2], …, p[n] 4 for j=1 to n M[j] = null 3 M[0] = 0. Compute-Memoized-Opt(n) 2 Compute-Memoized-Opt(j) if M[j] is empty 1 M[j] = max(v[j] + Compute-Memoized-Opt(p[j]), Compute-Memoized-Opt(j-1)) 0 return M[j] • Running time O(n log n): • Sorting takes O(n log n) time. • Computing p(n): O(n log n) - use log n time to find each p(i). • Each subproblem solved once. • Time to solve a subproblem constant. • Space O(n) 24

  23. Weighted interval scheduling: memoization Input: n, s[1..n], f[1..n], v[1..n] 8 Sort jobs by finish time so that f[1] ≤ f[2] ≤ … ≤ f[n] 7 5 Compute p[1], p[2], …, p[n] for j=1 to n 1 4 6 M[j] = empty M[0] = 0. Compute-Memoized-Opt(n) i M[i] 2 3 Compute-Memoized-Opt(j) if M[j] is empty 0 0 M[j] = max(v[j] + Compute-Memoized-Opt(p[j]), Compute-Memoized-Opt(j-1)) 1 1 return M[j] 2 4 j 1 1 p(1) = 0 3 4 j 2 p(2) = 0 4 p(3) = 0 j 3 2 11 4 p(4) = 2 j 4 7 11 5 p(5) = 1 9 j 5 6 11 j 6 5 p(6) = 2 7 11 6 j 7 p(7) = 3 15 8 p(8) = 5 j 8 4 25

  24. Weighted interval scheduling: memoization Input: n, s[1..n], f[1..n], v[1..n] i M[i] Sort jobs by finish time so that f[1] ≤ f[2] ≤ … ≤ f[n] Compute p[1], p[2], …, p[n] 0 0 for j=1 to n M[j] = empty 1 1 M[0] = 0. Compute-Memoized-Opt(n) 4 2 Compute-Memoized-Opt(j) 4 3 if M[j] is empty M[j] = max(v[j] + Compute-Memoized-Opt(p[j]), 4 11 Compute-Memoized-Opt(j-1)) return M[j] 5 11 j 1 1 p(1) = 0 11 6 j 2 p(2) = 0 4 11 7 p(3) = 0 j 3 2 p(4) = 2 15 8 j 4 7 p(5) = 1 9 j 5 j 6 5 p(6) = 2 6 j 7 p(7) = 3 p(8) = 5 j 8 4 26

  25. Weighted interval scheduling: bottom-up 5 Compute-Bottom-Up—Opt(n, s[1..n], f[1..n], v[1..n]) 4 Sort jobs by finish time so that f[1] ≤ f[2] ≤ … ≤ f[n] Compute p[1], p[2], …, p[n] 3 M[0] = 0. for j=1 to n 2 M[j] = max(v[j] + M(p[j]), M(j-1)) return M[n] 1 • Running time O(n log n): 0 • Sorting takes O(n log n) time. • Computing p(n): O(n log n) • For loop: O(n) time • Each iteration takes constant time. • Space O(n) 27

  26. Weighted interval scheduling: bottom-up Compute-Bottom-Up—Opt(n, s[1..n], f[1..n], v[1..n]) i M[i] Sort jobs by finish time so that f[1] ≤ f[2] ≤ … ≤ f[n] Compute p[1], p[2], …, p[n] 0 0 1 1 M[0] = 0. for j=1 to n 2 4 M[j] = max(v[j] + M(p[j]), M(j-1)) return M[n] 3 4 4 11 1 p(1) = 0 j 1 5 11 j 2 4 p(2) = 0 11 6 p(3) = 0 j 3 2 7 11 p(4) = 2 j 4 7 8 15 p(5) = 1 j 5 9 5 j 6 p(6) = 2 6 j 7 p(7) = 3 p(8) = 5 j 8 4 28

  27. Weighted interval scheduling: find solution Find-Solution(j) i M[i] if j=0 Return emptyset 0 0 else if M[j] > M[j-1] return {j} ∪ Find-Solution(p[j]) 1 4 else 2 4 return Find-Solution(j-1) 3 4 Solution = 8 , 4 , 2 4 11 1 p(1) = 0 j 1 5 11 j 2 p(2) = 0 4 4 6 11 p(3) = 0 j 3 2 7 11 p(4) = 2 j 4 7 7 8 15 p(5) = 1 j 5 9 5 j 6 p(6) = 2 6 j 7 p(7) = 3 p(8) = 5 j 8 4 4 29

  28. Segmented Least Squares 30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend