61a lecture 20 - university of california, berkeleytree recursion tree-shaped processes arise...

129
61A Lecture 20 Friday, October 14 Friday, October 14, 2011

Upload: others

Post on 13-Mar-2021

4 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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