more dynamic programming
play

More Dynamic Programming Lecture 14 Wednesday, March 11, 2020 L A - PowerPoint PPT Presentation

Algorithms & Models of Computation CS/ECE 374 B, Spring 2020 More Dynamic Programming Lecture 14 Wednesday, March 11, 2020 L A T EXed: January 19, 2020 04:18 Miller, Hassanieh (UIUC) CS374 1 Spring 2020 1 / 48 What is the running


  1. Algorithms & Models of Computation CS/ECE 374 B, Spring 2020 More Dynamic Programming Lecture 14 Wednesday, March 11, 2020 L A T EXed: January 19, 2020 04:18 Miller, Hassanieh (UIUC) CS374 1 Spring 2020 1 / 48

  2. What is the running time of the following? Consider computing f ( x , y ) by recursive function + memoization. x + y − 1 � f ( x , y ) = x ∗ f ( x + y − i , i − 1) , i =1 f (0 , y ) = y f ( x , 0) = x . The resulting algorithm when computing f ( n , n ) would take: (A) O ( n ) (B) O ( n log n ) (C) O ( n 2 ) (D) O ( n 3 ) (E) The function is ill defined - it can not be computed. Miller, Hassanieh (UIUC) CS374 2 Spring 2020 2 / 48

  3. Recipe for Dynamic Programming Develop a recursive backtracking style algorithm A for given 1 problem. Identify structure of subproblems generated by A on an instance 2 I of size n Estimate number of different subproblems generated as a 1 function of n . Is it polynomial or exponential in n ? If the number of problems is “small” (polynomial) then they 2 typically have some “clean” structure. Rewrite subproblems in a compact fashion. 3 Rewrite recursive algorithm in terms of notation for subproblems. 4 Convert to iterative algorithm by bottom up evaluation in an 5 appropriate order. Optimize further with data structures and/or additional ideas. 6 Miller, Hassanieh (UIUC) CS374 3 Spring 2020 3 / 48

  4. A variation Input A string w ∈ Σ ∗ and access to a language L ⊆ Σ ∗ via function IsStringinL( string x ) that decides whether x is in L , and non-negative integer k Goal Decide if w ∈ L k using IsStringinL( string x ) as a black box sub-routine Example Suppose L is English and we have a procedure to check whether a string/word is in the English dictionary. Is the string “isthisanenglishsentence” in English 5 ? Is the string “isthisanenglishsentence” in English 4 ? Is “asinineat” in English 2 ? Is “asinineat” in English 4 ? Is “zibzzzad” in English 1 ? Miller, Hassanieh (UIUC) CS374 4 Spring 2020 4 / 48

  5. Recursive Solution When is w ∈ L k ? Miller, Hassanieh (UIUC) CS374 5 Spring 2020 5 / 48

  6. Recursive Solution When is w ∈ L k ? k = 0 : w ∈ L k iff w = ǫ k = 1 : w ∈ L k iff w ∈ L k > 1 : w ∈ L k if w = uv with u ∈ L and v ∈ L k − 1 Miller, Hassanieh (UIUC) CS374 5 Spring 2020 5 / 48

  7. Recursive Solution When is w ∈ L k ? k = 0 : w ∈ L k iff w = ǫ k = 1 : w ∈ L k iff w ∈ L k > 1 : w ∈ L k if w = uv with u ∈ L and v ∈ L k − 1 Assume w is stored in array A [1 .. n ] IsStringinLk( A [1 .. n ] , k ) : If ( k = 0 ) If ( n = 0 ) Output YES Else Ouput NO If ( k = 1 ) Output IsStringinL( A [1 .. n ]) Else For ( i = 1 to n − 1 ) do If ( IsStringinL( A [1 .. i ]) and IsStringinLk( A [ i + 1 .. n ] , k − 1) ) Output YES Output NO Miller, Hassanieh (UIUC) CS374 5 Spring 2020 5 / 48

  8. Analysis IsStringinLk( A [1 .. n ] , k ) : If ( k = 0 ) If ( n = 0 ) Output YES Else Ouput NO If ( k = 1 ) Output IsStringinL( A [1 .. n ]) Else For ( i = 1 to n − 1 ) do If ( IsStringinL( A [1 .. i ]) and IsStringinLk( A [ i + 1 .. n ] , k − 1) ) Output YES Output NO How many distinct sub-problems are generated by IsStringinLk( A [1 .. n ] , k ) ? Miller, Hassanieh (UIUC) CS374 6 Spring 2020 6 / 48

  9. Analysis IsStringinLk( A [1 .. n ] , k ) : If ( k = 0 ) If ( n = 0 ) Output YES Else Ouput NO If ( k = 1 ) Output IsStringinL( A [1 .. n ]) Else For ( i = 1 to n − 1 ) do If ( IsStringinL( A [1 .. i ]) and IsStringinLk( A [ i + 1 .. n ] , k − 1) ) Output YES Output NO How many distinct sub-problems are generated by IsStringinLk( A [1 .. n ] , k ) ? O ( nk ) Miller, Hassanieh (UIUC) CS374 6 Spring 2020 6 / 48

  10. Analysis IsStringinLk( A [1 .. n ] , k ) : If ( k = 0 ) If ( n = 0 ) Output YES Else Ouput NO If ( k = 1 ) Output IsStringinL( A [1 .. n ]) Else For ( i = 1 to n − 1 ) do If ( IsStringinL( A [1 .. i ]) and IsStringinLk( A [ i + 1 .. n ] , k − 1) ) Output YES Output NO How many distinct sub-problems are generated by IsStringinLk( A [1 .. n ] , k ) ? O ( nk ) How much space? Miller, Hassanieh (UIUC) CS374 6 Spring 2020 6 / 48

  11. Analysis IsStringinLk( A [1 .. n ] , k ) : If ( k = 0 ) If ( n = 0 ) Output YES Else Ouput NO If ( k = 1 ) Output IsStringinL( A [1 .. n ]) Else For ( i = 1 to n − 1 ) do If ( IsStringinL( A [1 .. i ]) and IsStringinLk( A [ i + 1 .. n ] , k − 1) ) Output YES Output NO How many distinct sub-problems are generated by IsStringinLk( A [1 .. n ] , k ) ? O ( nk ) How much space? O ( nk ) Running time? Miller, Hassanieh (UIUC) CS374 6 Spring 2020 6 / 48

  12. Analysis IsStringinLk( A [1 .. n ] , k ) : If ( k = 0 ) If ( n = 0 ) Output YES Else Ouput NO If ( k = 1 ) Output IsStringinL( A [1 .. n ]) Else For ( i = 1 to n − 1 ) do If ( IsStringinL( A [1 .. i ]) and IsStringinLk( A [ i + 1 .. n ] , k − 1) ) Output YES Output NO How many distinct sub-problems are generated by IsStringinLk( A [1 .. n ] , k ) ? O ( nk ) How much space? O ( nk ) Running time? O ( n 2 k ) Miller, Hassanieh (UIUC) CS374 6 Spring 2020 6 / 48

  13. Another variant Question: What if we want to check if w ∈ L i for some 0 ≤ i ≤ k ? That is, is w ∈ ∪ k i =0 L i ? Miller, Hassanieh (UIUC) CS374 7 Spring 2020 7 / 48

  14. Exercise Definition A string is a palindrome if w = w R . Examples: I , RACECAR , MALAYALAM , DOOFFOOD Miller, Hassanieh (UIUC) CS374 8 Spring 2020 8 / 48

  15. Exercise Definition A string is a palindrome if w = w R . Examples: I , RACECAR , MALAYALAM , DOOFFOOD Problem: Given a string w find the longest subsequence of w that is a palindrome. Example MAHDYNAMICPROGRAMZLETMESHOWYOUTHEM has MHYMRORMYHM as a palindromic subsequence Miller, Hassanieh (UIUC) CS374 8 Spring 2020 8 / 48

  16. Exercise Assume w is stored in an array A [1 .. n ] LPS ( A [1 .. n ]) : length of longest palindromic subsequence of A . Recursive expression/code? Miller, Hassanieh (UIUC) CS374 9 Spring 2020 9 / 48

  17. Part I Edit Distance and Sequence Alignment Miller, Hassanieh (UIUC) CS374 10 Spring 2020 10 / 48

  18. Spell Checking Problem Given a string “exponen” that is not in the dictionary, how should a spell checker suggest a nearby string? Miller, Hassanieh (UIUC) CS374 11 Spring 2020 11 / 48

  19. Spell Checking Problem Given a string “exponen” that is not in the dictionary, how should a spell checker suggest a nearby string? What does nearness mean? Question: Given two strings x 1 x 2 . . . x n and y 1 y 2 . . . y m what is a distance between them? Miller, Hassanieh (UIUC) CS374 11 Spring 2020 11 / 48

  20. Spell Checking Problem Given a string “exponen” that is not in the dictionary, how should a spell checker suggest a nearby string? What does nearness mean? Question: Given two strings x 1 x 2 . . . x n and y 1 y 2 . . . y m what is a distance between them? Edit Distance: minimum number of “edits” to transform x into y . Miller, Hassanieh (UIUC) CS374 11 Spring 2020 11 / 48

  21. Edit Distance Definition Edit distance between two words X and Y is the number of letter insertions, letter deletions and letter substitutions required to obtain Y from X . Example The edit distance between FOOD and MONEY is at most 4 : FOOD → MOOD → MONOD → MONED → MONEY Miller, Hassanieh (UIUC) CS374 12 Spring 2020 12 / 48

  22. Edit Distance: Alternate View Alignment Place words one on top of the other, with gaps in the first word indicating insertions, and gaps in the second word indicating deletions. F O O D M O N E Y Miller, Hassanieh (UIUC) CS374 13 Spring 2020 13 / 48

  23. Edit Distance: Alternate View Alignment Place words one on top of the other, with gaps in the first word indicating insertions, and gaps in the second word indicating deletions. F O O D M O N E Y Formally, an alignment is a set M of pairs ( i , j ) such that each index appears at most once, and there is no “crossing”: i < i ′ and i is matched to j implies i ′ is matched to j ′ > j . In the above example, this is M = { (1 , 1) , (2 , 2) , (3 , 3) , (4 , 5) } . Miller, Hassanieh (UIUC) CS374 13 Spring 2020 13 / 48

  24. Edit Distance: Alternate View Alignment Place words one on top of the other, with gaps in the first word indicating insertions, and gaps in the second word indicating deletions. F O O D M O N E Y Formally, an alignment is a set M of pairs ( i , j ) such that each index appears at most once, and there is no “crossing”: i < i ′ and i is matched to j implies i ′ is matched to j ′ > j . In the above example, this is M = { (1 , 1) , (2 , 2) , (3 , 3) , (4 , 5) } . Cost of an alignment is the number of mismatched columns plus number of unmatched indices in both strings. Miller, Hassanieh (UIUC) CS374 13 Spring 2020 13 / 48

  25. Edit Distance Problem Problem Given two words, find the edit distance between them, i.e., an alignment of smallest cost. Miller, Hassanieh (UIUC) CS374 14 Spring 2020 14 / 48

  26. Applications Spell-checkers and Dictionaries 1 Unix diff 2 DNA sequence alignment . . . but, we need a new metric 3 Miller, Hassanieh (UIUC) CS374 15 Spring 2020 15 / 48

  27. Similarity Metric Definition For two strings X and Y , the cost of alignment M is [Gap penalty] For each gap in the alignment, we incur a cost δ . 1 [Mismatch cost] For each pair p and q that have been matched 2 in M , we incur cost α pq ; typically α pp = 0 . Miller, Hassanieh (UIUC) CS374 16 Spring 2020 16 / 48

Recommend


More recommend