CS140-lecture-20211110

Memoization #

image_2021-11-10-16-14-23 image_2021-11-10-16-15-25 image_2021-11-10-16-15-53 image_2021-11-10-16-20-07 image_2021-11-10-16-23-43

Elements of dynamic programming #

image_2021-11-10-16-24-36 image_2021-11-10-16-26-10 image_2021-11-10-16-28-30

Longest common subsequence #

image_2021-11-10-16-28-37 image_2021-11-10-16-29-34 image_2021-11-10-16-29-51 image_2021-11-10-16-30-08 image_2021-11-10-16-43-13 image_2021-11-10-16-44-15 image_2021-11-10-16-44-24 image_2021-11-10-16-44-56 image_2021-11-10-16-45-05 image_2021-11-15-14-19-52 image_2021-11-15-14-23-37

  • base cases along left and top edge

image_2021-11-15-16-00-53

  • see which subproblems are needed to solve current sub problem

image_2021-11-15-16-05-09

  • our next level after base cases is the top left corner

image_2021-11-15-16-06-01 image_2021-11-15-16-06-23 image_2021-11-15-16-07-32

  • nested loops

image_2021-11-15-16-08-55

  • another order to solve

image_2021-11-15-16-09-21

  • can also go by columns

image_2021-11-15-16-12-05 image_2021-11-15-16-13-45 image_2021-11-15-16-19-00 image_2021-11-15-16-19-51 image_2021-11-15-16-20-30 image_2021-11-15-16-22-24 image_2021-11-15-16-23-00 image_2021-11-15-16-24-03 image_2021-11-15-16-26-22 image_2021-11-15-16-27-24

  • overall runtime is \( \Theta(n) \) where \( n = i + j \) , the sum of the 2 sequences
My solution in Python

Output

 0  0  0  0  0  0  0 
 0  0  0  0  1  1  1 
 0  1  1  1  1  2  2 
 0  1  1  2  2  2  2 
 0  1  1  2  2  3  3 
 0  1  2  2  2  3  3 
 0  1  2  2  3  3  4 
 0  1  2  2  3  4  4 

b c b a 

Improving the space complexity #

image_2021-11-15-16-29-18 image_2021-11-15-16-33-20

  • we can overwrite the first 2 rows each pass thru