61a lecture 20 - university of california, berkeleytree recursion tree-shaped processes arise...
TRANSCRIPT
![Page 1: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/1.jpg)
61A Lecture 20
Friday, October 14
Friday, October 14, 2011
![Page 2: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/2.jpg)
Tree Recursion
2
Friday, October 14, 2011
![Page 3: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/3.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
Friday, October 14, 2011
![Page 4: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/4.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
Friday, October 14, 2011
![Page 5: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/5.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
http://en.wikipedia.org/wiki/File:Fibonacci.jpg
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
Friday, October 14, 2011
![Page 6: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/6.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
http://en.wikipedia.org/wiki/File:Fibonacci.jpg
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):
Friday, October 14, 2011
![Page 7: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/7.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
http://en.wikipedia.org/wiki/File:Fibonacci.jpg
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):
... , 35
Friday, October 14, 2011
![Page 8: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/8.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
http://en.wikipedia.org/wiki/File:Fibonacci.jpg
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 5,702,887
... , 35
Friday, October 14, 2011
![Page 9: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/9.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
http://en.wikipedia.org/wiki/File:Fibonacci.jpg
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 5,702,887
... , 35
def fib(n):
Friday, October 14, 2011
![Page 10: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/10.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
http://en.wikipedia.org/wiki/File:Fibonacci.jpg
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 5,702,887
... , 35
def fib(n):
if n == 1:
Friday, October 14, 2011
![Page 11: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/11.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
http://en.wikipedia.org/wiki/File:Fibonacci.jpg
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 5,702,887
... , 35
def fib(n):
if n == 1:
return 0
Friday, October 14, 2011
![Page 12: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/12.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
http://en.wikipedia.org/wiki/File:Fibonacci.jpg
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 5,702,887
... , 35
def fib(n):
if n == 1:
return 0
if n == 2:
Friday, October 14, 2011
![Page 13: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/13.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
http://en.wikipedia.org/wiki/File:Fibonacci.jpg
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 5,702,887
... , 35
def fib(n):
if n == 1:
return 0
if n == 2:
return 1
Friday, October 14, 2011
![Page 14: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/14.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.
2
http://en.wikipedia.org/wiki/File:Fibonacci.jpg
1, 2, 3, 4, 5, 6, 7, 8, 9,n:
0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 5,702,887
... , 35
def fib(n):
if n == 1:
return 0
if n == 2:
return 1
return fib(n-2) + fib(n-1)
Friday, October 14, 2011
![Page 15: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/15.jpg)
A Tree-Recursive Process
3
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 16: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/16.jpg)
A Tree-Recursive Process
3
fib(6)
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 17: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/17.jpg)
A Tree-Recursive Process
3
fib(6)
fib(4)
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 18: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/18.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)fib(4)
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 19: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/19.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 20: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/20.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 21: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/21.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 22: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/22.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 23: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/23.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 24: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/24.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 25: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/25.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 26: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/26.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 27: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/27.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 28: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/28.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 29: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/29.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 30: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/30.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 31: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/31.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 32: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/32.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 33: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/33.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 34: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/34.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 35: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/35.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 36: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/36.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Friday, October 14, 2011
![Page 37: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/37.jpg)
A Tree-Recursive Process
3
fib(6)
fib(5)
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
The computational process of fib evolves into a tree structure
Demo
Friday, October 14, 2011
![Page 38: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/38.jpg)
Repetition in Tree-Recursive Computation
4
Friday, October 14, 2011
![Page 39: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/39.jpg)
Repetition in Tree-Recursive Computation
4
This process is highly repetitive; fib is called on the same argument multiple times
Friday, October 14, 2011
![Page 40: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/40.jpg)
Repetition in Tree-Recursive Computation
4
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
This process is highly repetitive; fib is called on the same argument multiple times
Friday, October 14, 2011
![Page 41: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/41.jpg)
Memoization
Idea: Remember the results that have been computed before
5
Friday, October 14, 2011
![Page 42: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/42.jpg)
Memoization
Idea: Remember the results that have been computed before
5
def memo(f):
Friday, October 14, 2011
![Page 43: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/43.jpg)
Memoization
Idea: Remember the results that have been computed before
5
def memo(f):
cache = {}
Friday, October 14, 2011
![Page 44: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/44.jpg)
Memoization
Idea: Remember the results that have been computed before
5
def memo(f):
cache = {}Keys are arguments that map to return values
Friday, October 14, 2011
![Page 45: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/45.jpg)
Memoization
Idea: Remember the results that have been computed before
5
def memo(f):
cache = {}
def memoized(n):
Keys are arguments that map to return values
Friday, October 14, 2011
![Page 46: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/46.jpg)
Memoization
Idea: Remember the results that have been computed before
5
def memo(f):
cache = {}
def memoized(n):
if n not in cache:
Keys are arguments that map to return values
Friday, October 14, 2011
![Page 47: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/47.jpg)
Memoization
Idea: Remember the results that have been computed before
5
def memo(f):
cache = {}
def memoized(n):
if n not in cache:
cache[n] = f(n)
Keys are arguments that map to return values
Friday, October 14, 2011
![Page 48: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/48.jpg)
Memoization
Idea: Remember the results that have been computed before
5
def memo(f):
cache = {}
def memoized(n):
if n not in cache:
cache[n] = f(n)
return cache[n]
Keys are arguments that map to return values
Friday, October 14, 2011
![Page 49: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/49.jpg)
Memoization
Idea: Remember the results that have been computed before
5
def memo(f):
cache = {}
def memoized(n):
if n not in cache:
cache[n] = f(n)
return cache[n]
return memoized
Keys are arguments that map to return values
Friday, October 14, 2011
![Page 50: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/50.jpg)
Memoization
Idea: Remember the results that have been computed before
5
def memo(f):
cache = {}
def memoized(n):
if n not in cache:
cache[n] = f(n)
return cache[n]
return memoized
Keys are arguments that map to return values
Same behavior as f, if f is a pure function
Friday, October 14, 2011
![Page 51: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/51.jpg)
Memoization
Idea: Remember the results that have been computed before
5
def memo(f):
cache = {}
def memoized(n):
if n not in cache:
cache[n] = f(n)
return cache[n]
return memoized
Demo
Keys are arguments that map to return values
Same behavior as f, if f is a pure function
Friday, October 14, 2011
![Page 52: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/52.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Friday, October 14, 2011
![Page 53: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/53.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Friday, October 14, 2011
![Page 54: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/54.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Friday, October 14, 2011
![Page 55: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/55.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Friday, October 14, 2011
![Page 56: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/56.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Friday, October 14, 2011
![Page 57: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/57.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Friday, October 14, 2011
![Page 58: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/58.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Friday, October 14, 2011
![Page 59: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/59.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Friday, October 14, 2011
![Page 60: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/60.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Friday, October 14, 2011
![Page 61: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/61.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Friday, October 14, 2011
![Page 62: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/62.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Friday, October 14, 2011
![Page 63: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/63.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Friday, October 14, 2011
![Page 64: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/64.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Calls to fib without memoization:
Calls to fib with memoization:
fib(35)
Friday, October 14, 2011
![Page 65: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/65.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Calls to fib without memoization:
Calls to fib with memoization:
fib(35)
35
Friday, October 14, 2011
![Page 66: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/66.jpg)
Memoized Tree Recursion
6
fib(6)
fib(4)
fib(2)
1
fib(5)
fib(3)
fib(1) fib(2)
0 1
fib(3)
fib(1) fib(2)
0 1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Call to fib
Found in cache
Calls to fib without memoization:
Calls to fib with memoization:
fib(35)
35
18,454,929
Friday, October 14, 2011
![Page 67: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/67.jpg)
Iteration vs Memoized Tree Recursion
7
Friday, October 14, 2011
![Page 68: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/68.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
Friday, October 14, 2011
![Page 69: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/69.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n):
Friday, October 14, 2011
![Page 70: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/70.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0
Friday, October 14, 2011
![Page 71: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/71.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0
The first Fibonacci number
Friday, October 14, 2011
![Page 72: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/72.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1):
The first Fibonacci number
Friday, October 14, 2011
![Page 73: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/73.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr
The first Fibonacci number
Friday, October 14, 2011
![Page 74: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/74.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr
The first Fibonacci number
Friday, October 14, 2011
![Page 75: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/75.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr
@memo def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1)
The first Fibonacci number
Friday, October 14, 2011
![Page 76: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/76.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr
@memo def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1)
Time SpaceThe first Fibonacci number
Friday, October 14, 2011
![Page 77: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/77.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr
@memo def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1)
n steps
Time SpaceThe first Fibonacci number
Friday, October 14, 2011
![Page 78: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/78.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr
@memo def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1)
n steps
n steps
Time SpaceThe first Fibonacci number
Friday, October 14, 2011
![Page 79: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/79.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr
@memo def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1)
n steps
n steps
3 names
Time SpaceThe first Fibonacci number
Friday, October 14, 2011
![Page 80: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/80.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr
@memo def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1)
n steps
n steps
3 names
n entries
Time SpaceThe first Fibonacci number
Friday, October 14, 2011
![Page 81: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/81.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr
@memo def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1)
n steps
n steps
3 names
n entries
Time SpaceThe first Fibonacci number
Independent of problem size
Friday, October 14, 2011
![Page 82: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/82.jpg)
Iteration vs Memoized Tree Recursion
Iterative and memoized implementations are not the same.
7
def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr
@memo def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1)
n steps
n steps
3 names
n entries
Time SpaceThe first Fibonacci number
Scales with problem size
Independent of problem size
Friday, October 14, 2011
![Page 83: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/83.jpg)
Counting Change
8
Friday, October 14, 2011
![Page 84: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/84.jpg)
Counting Change
$1 = $0.50 + $0.25 + $0.10 + $0.10 + $0.05
8
Friday, October 14, 2011
![Page 85: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/85.jpg)
Counting Change
$1 = $0.50 + $0.25 + $0.10 + $0.10 + $0.05
$1 = 1 half dollar, 1 quarter, 2 dimes, 1 nickel
8
Friday, October 14, 2011
![Page 86: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/86.jpg)
Counting Change
$1 = $0.50 + $0.25 + $0.10 + $0.10 + $0.05
$1 = 1 half dollar, 1 quarter, 2 dimes, 1 nickel
$1 = 2 quarters, 2 dimes, 30 pennies
8
Friday, October 14, 2011
![Page 87: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/87.jpg)
Counting Change
$1 = $0.50 + $0.25 + $0.10 + $0.10 + $0.05
$1 = 1 half dollar, 1 quarter, 2 dimes, 1 nickel
$1 = 2 quarters, 2 dimes, 30 pennies
$1 = 100 pennies
8
Friday, October 14, 2011
![Page 88: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/88.jpg)
Counting Change
$1 = $0.50 + $0.25 + $0.10 + $0.10 + $0.05
$1 = 1 half dollar, 1 quarter, 2 dimes, 1 nickel
$1 = 2 quarters, 2 dimes, 30 pennies
$1 = 100 pennies
8
How many ways are there to change a dollar?
Friday, October 14, 2011
![Page 89: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/89.jpg)
Counting Change
$1 = $0.50 + $0.25 + $0.10 + $0.10 + $0.05
$1 = 1 half dollar, 1 quarter, 2 dimes, 1 nickel
$1 = 2 quarters, 2 dimes, 30 pennies
$1 = 100 pennies
8
How many ways are there to change a dollar?
How many ways to change $0.11 with nickels & pennies?
Friday, October 14, 2011
![Page 90: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/90.jpg)
Counting Change
$1 = $0.50 + $0.25 + $0.10 + $0.10 + $0.05
$1 = 1 half dollar, 1 quarter, 2 dimes, 1 nickel
$1 = 2 quarters, 2 dimes, 30 pennies
$1 = 100 pennies
8
How many ways are there to change a dollar?
How many ways to change $0.11 with nickels & pennies?
$0.11 can be changed with nickels & pennies by
Friday, October 14, 2011
![Page 91: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/91.jpg)
Counting Change
$1 = $0.50 + $0.25 + $0.10 + $0.10 + $0.05
$1 = 1 half dollar, 1 quarter, 2 dimes, 1 nickel
$1 = 2 quarters, 2 dimes, 30 pennies
$1 = 100 pennies
8
How many ways are there to change a dollar?
How many ways to change $0.11 with nickels & pennies?
$0.11 can be changed with nickels & pennies by
A. Not using any more nickels; $0.11 with just pennies
Friday, October 14, 2011
![Page 92: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/92.jpg)
Counting Change
$1 = $0.50 + $0.25 + $0.10 + $0.10 + $0.05
$1 = 1 half dollar, 1 quarter, 2 dimes, 1 nickel
$1 = 2 quarters, 2 dimes, 30 pennies
$1 = 100 pennies
8
How many ways are there to change a dollar?
How many ways to change $0.11 with nickels & pennies?
$0.11 can be changed with nickels & pennies by
A. Not using any more nickels; $0.11 with just pennies
B. Using at least one nickel; $0.06 with nickels & pennies
Friday, October 14, 2011
![Page 93: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/93.jpg)
Counting Change Recursively
How many ways are there to change a dollar?
9
Friday, October 14, 2011
![Page 94: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/94.jpg)
Counting Change Recursively
How many ways are there to change a dollar?
9
The number of ways to change an amount a using n kinds =
Friday, October 14, 2011
![Page 95: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/95.jpg)
Counting Change Recursively
How many ways are there to change a dollar?
9
The number of ways to change an amount a using n kinds =• The number of ways to change a using all but the first kind
Friday, October 14, 2011
![Page 96: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/96.jpg)
Counting Change Recursively
How many ways are there to change a dollar?
9
The number of ways to change an amount a using n kinds =• The number of ways to change a using all but the first kind
+
Friday, October 14, 2011
![Page 97: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/97.jpg)
Counting Change Recursively
How many ways are there to change a dollar?
9
The number of ways to change an amount a using n kinds =• The number of ways to change a using all but the first kind
+• The number of ways to change (a - d) using all n kinds,
where d is the denomination of the first kind of coin.
Friday, October 14, 2011
![Page 98: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/98.jpg)
Counting Change Recursively
How many ways are there to change a dollar?
9
def count_change(a, kinds=(50, 25, 10, 5, 1)):
The number of ways to change an amount a using n kinds =• The number of ways to change a using all but the first kind
+• The number of ways to change (a - d) using all n kinds,
where d is the denomination of the first kind of coin.
Friday, October 14, 2011
![Page 99: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/99.jpg)
Counting Change Recursively
How many ways are there to change a dollar?
9
def count_change(a, kinds=(50, 25, 10, 5, 1)):
<base cases>
The number of ways to change an amount a using n kinds =• The number of ways to change a using all but the first kind
+• The number of ways to change (a - d) using all n kinds,
where d is the denomination of the first kind of coin.
Friday, October 14, 2011
![Page 100: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/100.jpg)
Counting Change Recursively
How many ways are there to change a dollar?
9
def count_change(a, kinds=(50, 25, 10, 5, 1)):
<base cases>
d = kinds[0]
The number of ways to change an amount a using n kinds =• The number of ways to change a using all but the first kind
+• The number of ways to change (a - d) using all n kinds,
where d is the denomination of the first kind of coin.
Friday, October 14, 2011
![Page 101: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/101.jpg)
Counting Change Recursively
How many ways are there to change a dollar?
9
def count_change(a, kinds=(50, 25, 10, 5, 1)):
<base cases>
d = kinds[0]
return count_change(a, kinds[1:]) + count_change(a-d, kinds)
The number of ways to change an amount a using n kinds =• The number of ways to change a using all but the first kind
+• The number of ways to change (a - d) using all n kinds,
where d is the denomination of the first kind of coin.
Friday, October 14, 2011
![Page 102: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/102.jpg)
Counting Change Recursively
How many ways are there to change a dollar?
9
def count_change(a, kinds=(50, 25, 10, 5, 1)):
<base cases>
d = kinds[0]
return count_change(a, kinds[1:]) + count_change(a-d, kinds)
The number of ways to change an amount a using n kinds =• The number of ways to change a using all but the first kind
+• The number of ways to change (a - d) using all n kinds,
where d is the denomination of the first kind of coin.
Demo
Friday, October 14, 2011
![Page 103: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/103.jpg)
Space Consumption
10
Friday, October 14, 2011
![Page 104: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/104.jpg)
Space Consumption
Which environment frames do we need to keep during evaluation?
10
Friday, October 14, 2011
![Page 105: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/105.jpg)
Space Consumption
Which environment frames do we need to keep during evaluation?
Each step of evaluation has a set of active environments.
10
Friday, October 14, 2011
![Page 106: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/106.jpg)
Space Consumption
Which environment frames do we need to keep during evaluation?
Each step of evaluation has a set of active environments.
Values and frames referenced by active environments are kept.
10
Friday, October 14, 2011
![Page 107: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/107.jpg)
Space Consumption
Which environment frames do we need to keep during evaluation?
Each step of evaluation has a set of active environments.
Values and frames referenced by active environments are kept.
Memory used for other values & frames can be reclaimed.
10
Friday, October 14, 2011
![Page 108: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/108.jpg)
Space Consumption
Which environment frames do we need to keep during evaluation?
Each step of evaluation has a set of active environments.
Values and frames referenced by active environments are kept.
Memory used for other values & frames can be reclaimed.
10
Active environments:
Friday, October 14, 2011
![Page 109: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/109.jpg)
Space Consumption
Which environment frames do we need to keep during evaluation?
Each step of evaluation has a set of active environments.
Values and frames referenced by active environments are kept.
Memory used for other values & frames can be reclaimed.
10
Active environments: • The environment for the current expression being evaluated
Friday, October 14, 2011
![Page 110: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/110.jpg)
Space Consumption
Which environment frames do we need to keep during evaluation?
Each step of evaluation has a set of active environments.
Values and frames referenced by active environments are kept.
Memory used for other values & frames can be reclaimed.
10
Active environments: • The environment for the current expression being evaluated
• All environments for expressions that depend upon the value of the current expression
Friday, October 14, 2011
![Page 111: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/111.jpg)
Space Consumption
Which environment frames do we need to keep during evaluation?
Each step of evaluation has a set of active environments.
Values and frames referenced by active environments are kept.
Memory used for other values & frames can be reclaimed.
10
Active environments: • The environment for the current expression being evaluated
• All environments for expressions that depend upon the value of the current expression
• All environments associated with values referenced by active environments
Friday, October 14, 2011
![Page 112: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/112.jpg)
Fibonacci Environment Diagram
11
fib(3)
if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)
fib:fib(n):
...
n: 3fib
Friday, October 14, 2011
![Page 113: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/113.jpg)
Fibonacci Environment Diagram
11
fib(3)
if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)
fib:fib(n):
...
n: 3fib
fib(n-2)
if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)
n: 1fib
Friday, October 14, 2011
![Page 114: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/114.jpg)
Fibonacci Environment Diagram
11
fib(3)
if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)
fib:fib(n):
...
n: 3fib
fib(n-2)
if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)
n: 1fib
0
Friday, October 14, 2011
![Page 115: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/115.jpg)
Fibonacci Environment Diagram
12
fib(3)
if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)
fib:fib(n):
...
n: 3fib
fib(n-2)
if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)
0
n: 1fib
Friday, October 14, 2011
![Page 116: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/116.jpg)
Fibonacci Environment Diagram
12
fib(3)
if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)
fib:fib(n):
...
n: 3fib
fib(n-2)
if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)
0
n: 1fib
fib(n-1)
if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)
n: 2fib
Friday, October 14, 2011
![Page 117: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/117.jpg)
Fibonacci Memory Consumption
13
fib(6)
fib(5)
fib(3)
fib(2)
1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(1)
0
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Friday, October 14, 2011
![Page 118: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/118.jpg)
Fibonacci Memory Consumption
13
fib(6)
fib(5)
fib(3)
fib(2)
1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(1)
0
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Assume we have reached this step
Friday, October 14, 2011
![Page 119: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/119.jpg)
Fibonacci Memory Consumption
14
fib(6)
fib(5)
fib(3)
fib(2)
1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(1)
0
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Assume we have reached this step
Friday, October 14, 2011
![Page 120: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/120.jpg)
Fibonacci Memory Consumption
14
fib(6)
fib(5)
fib(3)
fib(2)
1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(1)
0
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Assume we have reached this step
Has an active environment
Friday, October 14, 2011
![Page 121: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/121.jpg)
Fibonacci Memory Consumption
14
fib(6)
fib(5)
fib(3)
fib(2)
1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(1)
0
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Assume we have reached this step
Has an active environmentCan be reclaimed
Friday, October 14, 2011
![Page 122: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/122.jpg)
Fibonacci Memory Consumption
14
fib(6)
fib(5)
fib(3)
fib(2)
1
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
fib(1)
0
fib(4)
fib(2)
1
fib(3)
fib(1) fib(2)
0 1
Assume we have reached this step
Has an active environmentCan be reclaimedHasn't yet been created
Friday, October 14, 2011
![Page 123: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/123.jpg)
Active Environments for Returned Functions
15
make_adder: def make_adder(n): def adder(k): return k + n return adderadd1 = make_adder(1)
make_adder(n):...
Friday, October 14, 2011
![Page 124: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/124.jpg)
Active Environments for Returned Functions
15
make_adder: def make_adder(n): def adder(k): return k + n return adderadd1 = make_adder(1)
make_adder(n):...
make_adder(1)
Friday, October 14, 2011
![Page 125: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/125.jpg)
Active Environments for Returned Functions
15
make_adder: def make_adder(n): def adder(k): return k + n return adderadd1 = make_adder(1)
make_adder(n):...
make_adder(1)
n: 1
make_adder
adder:
adder(k):
return k + n
def adder(k): return k + n return adder
Friday, October 14, 2011
![Page 126: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/126.jpg)
Active Environments for Returned Functions
15
make_adder: def make_adder(n): def adder(k): return k + n return adderadd1 = make_adder(1)
make_adder(n):...
make_adder(1)
n: 1
make_adder
adder:
adder(k):
return k + n
def adder(k): return k + n return adder
Friday, October 14, 2011
![Page 127: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/127.jpg)
Active Environments for Returned Functions
15
make_adder: def make_adder(n): def adder(k): return k + n return adderadd1 = make_adder(1)
make_adder(n):...
make_adder(1)
n: 1
make_adder
adder:
adder(k):
return k + n
def adder(k): return k + n return adder
add1:
Friday, October 14, 2011
![Page 128: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/128.jpg)
Active Environments for Returned Functions
15
make_adder: def make_adder(n): def adder(k): return k + n return adderadd1 = make_adder(1)
make_adder(n):...
make_adder(1)
Associated with an environment
n: 1
make_adder
adder:
adder(k):
return k + n
def adder(k): return k + n return adder
add1:
Friday, October 14, 2011
![Page 129: 61A Lecture 20 - University of California, BerkeleyTree Recursion Tree-shaped processes arise whenever executing the body of a function entails making more than one call to that function.2](https://reader036.vdocuments.us/reader036/viewer/2022071417/6115564dfd84c22cd0779d1f/html5/thumbnails/129.jpg)
Active Environments for Returned Functions
15
make_adder: def make_adder(n): def adder(k): return k + n return adderadd1 = make_adder(1)
make_adder(n):...
make_adder(1)
Associated with an environment
n: 1
make_adder
adder:
adder(k):
return k + n
def adder(k): return k + n return adder
Therefore, all frames in this environment must
be kept
add1:
Friday, October 14, 2011