11 - 03 feb - from recursion to dynamic programming
TRANSCRIPT
![Page 1: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/1.jpg)
CS 321. Algorithm Analysis & Design Lecture 11
Recursion - Continued.Fibonacci Numbers
![Page 2: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/2.jpg)
![Page 3: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/3.jpg)
(In class we started with a recap of Independent Set, see the previous set of slides.)
![Page 4: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/4.jpg)
![Page 5: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/5.jpg)
![Page 6: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/6.jpg)
![Page 7: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/7.jpg)
How many ways to tile a corridor of length n?
![Page 8: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/8.jpg)
How many ways to tile a corridor of length n?
![Page 9: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/9.jpg)
How many ways to tile a corridor of length n?
![Page 10: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/10.jpg)
How many ways to tile a corridor of length n?
![Page 11: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/11.jpg)
How many ways to tile a corridor of length n?
![Page 12: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/12.jpg)
How many ways to tile a corridor of length n?
![Page 13: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/13.jpg)
How many ways to tile a corridor of length n?
![Page 14: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/14.jpg)
What’s the last tile?
Black Red
![Page 15: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/15.jpg)
Black
![Page 16: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/16.jpg)
![Page 17: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/17.jpg)
![Page 18: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/18.jpg)
![Page 19: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/19.jpg)
![Page 20: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/20.jpg)
![Page 21: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/21.jpg)
![Page 22: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/22.jpg)
![Page 23: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/23.jpg)
![Page 24: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/24.jpg)
![Page 25: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/25.jpg)
![Page 26: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/26.jpg)
C(4) = C(3) + C(2)
C(n) = C(n-1) + C(n-2)
C(1) = 1, C(2) = 2, C(3) = 3
The general recurrence.
![Page 27: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/27.jpg)
Notice that there is redundancy in the recursion tree for computing C(n).
C(n)
C(n-1) C(n-2)
C(n-3)C(n-2) C(n-4)C(n-3)
[If implemented directly]
![Page 28: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/28.jpg)
Memoization - The Basic Idea
![Page 29: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/29.jpg)
Simply store the values when you compute them.
Memoization - The Basic Idea
![Page 30: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/30.jpg)
Simply store the values when you compute them.
Before delving into a recursive rabbit hole, check if the value is already stored somewhere.
Memoization - The Basic Idea
![Page 31: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/31.jpg)
Simply store the values when you compute them.
Before delving into a recursive rabbit hole, check if the value is already stored somewhere.
Get into recursion on a need-to basis.
Memoization - The Basic Idea
![Page 32: 11 - 03 Feb - From Recursion to Dynamic Programming](https://reader031.vdocuments.us/reader031/viewer/2022021921/58efcba51a28ab577e8b4627/html5/thumbnails/32.jpg)
Simply store the values when you compute them.
Before delving into a recursive rabbit hole, check if the value is already stored somewhere.
Get into recursion on a need-to basis.
We’re doing “actual work” for only n distinct subproblems. The rest is only table lookups.
Memoization - The Basic Idea