Top Banner
61A Lecture 13
157

61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Jun 05, 2018

Download

Documents

phungdan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

61A Lecture 13

Page 2: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Announcements

Page 3: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Measuring Efficiency

Page 4: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

Page 5: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 6: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 7: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 8: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(3)

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 9: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)fib(3)

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 10: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 11: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 12: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 13: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 14: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 15: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 16: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 17: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 18: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 19: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 20: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 21: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 22: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 23: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 24: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 25: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 26: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 27: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 28: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Recursive Computation of the Fibonacci Sequence

Our first example of tree recursion:

4

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

(Demo)

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

def fib(n): if n <= 1: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Page 29: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Page 30: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

6

Page 31: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

def memo(f):

6

Page 32: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

def memo(f):

cache = {}

6

Page 33: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

def memo(f):

cache = {} def memoized(n):

6

Page 34: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

def memo(f):

cache = {} def memoized(n):

if n not in cache:

6

Page 35: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

def memo(f):

cache = {} def memoized(n):

if n not in cache: cache[n] = f(n)

6

Page 36: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

def memo(f):

cache = {} def memoized(n):

if n not in cache: cache[n] = f(n)

return cache[n]

6

Page 37: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

def memo(f):

cache = {} def memoized(n):

if n not in cache: cache[n] = f(n)

return cache[n] return memoized

6

Page 38: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

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

6

Page 39: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

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

6

Page 40: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoization

Idea: Remember the results that have been computed before

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

6

(Demo)

Page 41: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 42: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fibfib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 43: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 44: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 45: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 46: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 47: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 48: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 49: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 50: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 51: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 52: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 53: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 54: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 55: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 56: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 57: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 58: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 59: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Memoized Tree Recursion

7

Call to fib

Found in cachefib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Skipped

Page 60: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Space

Page 61: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

The Consumption of Space

9

Page 62: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

The Consumption of Space

Which environment frames do we need to keep during evaluation?

9

Page 63: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

The Consumption of Space

Which environment frames do we need to keep during evaluation?

At any moment there is a set of active environments

9

Page 64: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

The Consumption of Space

Which environment frames do we need to keep during evaluation?

At any moment there is a set of active environments

Values and frames in active environments consume memory

9

Page 65: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

The Consumption of Space

Which environment frames do we need to keep during evaluation?

At any moment there is a set of active environments

Values and frames in active environments consume memory

Memory that is used for other values and frames can be recycled

9

Page 66: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

The Consumption of Space

Which environment frames do we need to keep during evaluation?

At any moment there is a set of active environments

Values and frames in active environments consume memory

Memory that is used for other values and frames can be recycled

9

Active environments:

Page 67: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

The Consumption of Space

Which environment frames do we need to keep during evaluation?

At any moment there is a set of active environments

Values and frames in active environments consume memory

Memory that is used for other values and frames can be recycled

9

Active environments:

• Environments for any function calls currently being evaluated

Page 68: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

The Consumption of Space

Which environment frames do we need to keep during evaluation?

At any moment there is a set of active environments

Values and frames in active environments consume memory

Memory that is used for other values and frames can be recycled

9

Active environments:

• Environments for any function calls currently being evaluated

• Parent environments of functions named in active environments

Page 69: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

The Consumption of Space

Which environment frames do we need to keep during evaluation?

At any moment there is a set of active environments

Values and frames in active environments consume memory

Memory that is used for other values and frames can be recycled

9

Active environments:

• Environments for any function calls currently being evaluated

• Parent environments of functions named in active environments

(Demo)

Interactive Diagram

Page 70: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

10

Page 71: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

10

fib(5)

Page 72: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

10

fib(5)

fib(3)

Page 73: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

10

fib(5)

fib(4)fib(3)

Page 74: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

10

fib(5)

fib(4)fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 75: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

10

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 76: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

10

Assume we have reached this step

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 77: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

11

Assume we have reached this step

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 78: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

11

Assume we have reached this step

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Has an active environment

Page 79: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

11

Assume we have reached this step

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Has an active environmentCan be reclaimed

Page 80: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Fibonacci Space Consumption

11

Assume we have reached this step

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Has an active environmentCan be reclaimedHasn't yet been created

Page 81: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Time

Page 82: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing Implementations

13

Page 83: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing Implementations

Implementations of the same functional abstraction can require different resources

13

Page 84: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing Implementations

Implementations of the same functional abstraction can require different resources

13

Problem: How many factors does a positive integer n have?

Page 85: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing Implementations

Implementations of the same functional abstraction can require different resources

13

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Page 86: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing Implementations

Implementations of the same functional abstraction can require different resources

13

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

def factors(n):

Page 87: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing Implementations

Implementations of the same functional abstraction can require different resources

13

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

def factors(n):

Page 88: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing Implementations

Implementations of the same functional abstraction can require different resources

13

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n):

Page 89: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing Implementations

Implementations of the same functional abstraction can require different resources

13

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n):

Question: How many time does each implementation use division? (Demo)

Page 90: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing Implementations

Implementations of the same functional abstraction can require different resources

13

Time (number of divisions)

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n):

Question: How many time does each implementation use division? (Demo)

Page 91: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

n

Comparing Implementations

Implementations of the same functional abstraction can require different resources

13

Time (number of divisions)

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n):

Question: How many time does each implementation use division? (Demo)

Page 92: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

n

Comparing Implementations

Implementations of the same functional abstraction can require different resources

13

Time (number of divisions)

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n):

Greatest integer less thanpn

Question: How many time does each implementation use division? (Demo)

Page 93: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Orders of Growth

Page 94: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Order of Growth

15

Page 95: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Order of Growth

A method for bounding the resources used by a function by the "size" of a problem

15

Page 96: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Order of Growth

A method for bounding the resources used by a function by the "size" of a problem

15

n: size of the problem

Page 97: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Order of Growth

A method for bounding the resources used by a function by the "size" of a problem

15

n: size of the problem

R(n): measurement of some resource used (time or space)

Page 98: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

R(n) = �(f(n))

Order of Growth

A method for bounding the resources used by a function by the "size" of a problem

15

n: size of the problem

R(n): measurement of some resource used (time or space)

Page 99: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

R(n) = �(f(n))

Order of Growth

A method for bounding the resources used by a function by the "size" of a problem

15

n: size of the problem

R(n): measurement of some resource used (time or space)

means that there are positive constants k1 and k2 such that

Page 100: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

R(n) = �(f(n))

k1 · f(n) � R(n) � k2 · f(n)

Order of Growth

A method for bounding the resources used by a function by the "size" of a problem

15

n: size of the problem

R(n): measurement of some resource used (time or space)

means that there are positive constants k1 and k2 such that

Page 101: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

R(n) = �(f(n))

k1 · f(n) � R(n) � k2 · f(n)

Order of Growth

A method for bounding the resources used by a function by the "size" of a problem

15

n: size of the problem

R(n): measurement of some resource used (time or space)

means that there are positive constants k1 and k2 such that

for all n larger than some minimum m

Page 102: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

R(n) = �(f(n))

k1 · f(n) � R(n) � k2 · f(n)

Order of Growth

A method for bounding the resources used by a function by the "size" of a problem

15

n: size of the problem

R(n): measurement of some resource used (time or space)

means that there are positive constants k1 and k2 such that

for all n larger than some minimum m

Page 103: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

R(n) = �(f(n))

k1 · f(n) � R(n) � k2 · f(n)

Order of Growth

A method for bounding the resources used by a function by the "size" of a problem

15

n: size of the problem

R(n): measurement of some resource used (time or space)

means that there are positive constants k1 and k2 such that

for all n larger than some minimum m

Page 104: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Order of Growth of Counting Factors

Implementations of the same functional abstraction can require different amounts of time

16

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n):

Page 105: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Order of Growth of Counting Factors

Implementations of the same functional abstraction can require different amounts of time

16

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n): Time Space

Page 106: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Order of Growth of Counting Factors

Implementations of the same functional abstraction can require different amounts of time

16

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n): Time Space

�(n) �(1)

Page 107: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Order of Growth of Counting Factors

Implementations of the same functional abstraction can require different amounts of time

16

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n): Time Space

�(n) �(1)

⇥(pn) �(1)

Page 108: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Order of Growth of Counting Factors

Implementations of the same functional abstraction can require different amounts of time

16

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n): Time Space

�(n) �(1)

⇥(pn) �(1)

Assumption: integers occupy a fixed amount of

space

Page 109: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Order of Growth of Counting Factors

Implementations of the same functional abstraction can require different amounts of time

16

Problem: How many factors does a positive integer n have?

A factor k of n is a positive integer that evenly divides n

Slow: Test each k from 1 through n

Fast: Test each k from 1 to square root n For every k, n/k is also a factor!

def factors(n): Time Space

�(n) �(1)

⇥(pn) �(1)

Assumption: integers occupy a fixed amount of

space

(Demo)

Page 110: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Exponentiation

Page 111: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Exponentiation

18

Page 112: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Exponentiation

Goal: one more multiplication lets us double the problem size

18

Page 113: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Exponentiation

Goal: one more multiplication lets us double the problem size

18

def exp(b, n): if n == 0: return 1 else: return b * exp(b, n-1)

Page 114: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

bn =

�1 if n = 0

b · bn�1 otherwise

Exponentiation

Goal: one more multiplication lets us double the problem size

18

def exp(b, n): if n == 0: return 1 else: return b * exp(b, n-1)

Page 115: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

bn =

�1 if n = 0

b · bn�1 otherwise

bn =

���

��

1 if n = 0

(b12 n)2 if n is even

b · bn�1 if n is odd

Exponentiation

Goal: one more multiplication lets us double the problem size

18

def exp(b, n): if n == 0: return 1 else: return b * exp(b, n-1)

Page 116: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

bn =

�1 if n = 0

b · bn�1 otherwise

bn =

���

��

1 if n = 0

(b12 n)2 if n is even

b · bn�1 if n is odd

Exponentiation

Goal: one more multiplication lets us double the problem size

18

def exp(b, n): if n == 0: return 1 else: return b * exp(b, n-1)

def square(x): return x*x

def exp_fast(b, n): if n == 0: return 1 elif n % 2 == 0: return square(exp_fast(b, n//2)) else: return b * exp_fast(b, n-1)

Page 117: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

bn =

�1 if n = 0

b · bn�1 otherwise

bn =

���

��

1 if n = 0

(b12 n)2 if n is even

b · bn�1 if n is odd

Exponentiation

Goal: one more multiplication lets us double the problem size

18

def exp(b, n): if n == 0: return 1 else: return b * exp(b, n-1)

def square(x): return x*x

def exp_fast(b, n): if n == 0: return 1 elif n % 2 == 0: return square(exp_fast(b, n//2)) else: return b * exp_fast(b, n-1)

(Demo)

Page 118: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Exponentiation

19

Time Space

Goal: one more multiplication lets us double the problem size

def exp(b, n): if n == 0: return 1 else: return b * exp(b, n-1)

def square(x): return x*x

def exp_fast(b, n): if n == 0: return 1 elif n % 2 == 0: return square(exp_fast(b, n//2)) else: return b * exp_fast(b, n-1)

Page 119: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Exponentiation

19

Time Space

�(n) �(n)

Goal: one more multiplication lets us double the problem size

def exp(b, n): if n == 0: return 1 else: return b * exp(b, n-1)

def square(x): return x*x

def exp_fast(b, n): if n == 0: return 1 elif n % 2 == 0: return square(exp_fast(b, n//2)) else: return b * exp_fast(b, n-1)

Page 120: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Exponentiation

19

Time Space

�(n) �(n)

�(log n) �(log n)

Goal: one more multiplication lets us double the problem size

def exp(b, n): if n == 0: return 1 else: return b * exp(b, n-1)

def square(x): return x*x

def exp_fast(b, n): if n == 0: return 1 elif n % 2 == 0: return square(exp_fast(b, n//2)) else: return b * exp_fast(b, n-1)

Page 121: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing Orders of Growth

Page 122: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

21

Page 123: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Page 124: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

⇥(n)

Page 125: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

⇥(n) ⇥(500 · n)

Page 126: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

⇥(n) ⇥(500 · n) ⇥(1

500· n)

Page 127: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

⇥(n) ⇥(500 · n) ⇥(1

500· n)

Page 128: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n)

Page 129: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n)

Page 130: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n) ⇥(lnn)

Page 131: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

Nesting: When an inner process is repeated for each step in an outer process, multiply the steps in the outer and inner processes to find the total number of steps

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n) ⇥(lnn)

Page 132: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

Nesting: When an inner process is repeated for each step in an outer process, multiply the steps in the outer and inner processes to find the total number of steps

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n) ⇥(lnn)

def overlap(a, b): count = 0 for item in a: if item in b: count += 1 return count

Page 133: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

Nesting: When an inner process is repeated for each step in an outer process, multiply the steps in the outer and inner processes to find the total number of steps

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n) ⇥(lnn)

def overlap(a, b): count = 0 for item in a: if item in b: count += 1 return count

Outer: length of a

Page 134: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

Nesting: When an inner process is repeated for each step in an outer process, multiply the steps in the outer and inner processes to find the total number of steps

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n) ⇥(lnn)

def overlap(a, b): count = 0 for item in a: if item in b: count += 1 return count

Outer: length of a

Inner: length of b

Page 135: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

Nesting: When an inner process is repeated for each step in an outer process, multiply the steps in the outer and inner processes to find the total number of steps

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n) ⇥(lnn)

def overlap(a, b): count = 0 for item in a: if item in b: count += 1 return count

Outer: length of a

Inner: length of b

If a and b are both length n, then overlap takes steps⇥(n2)

Page 136: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

Nesting: When an inner process is repeated for each step in an outer process, multiply the steps in the outer and inner processes to find the total number of steps

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n) ⇥(lnn)

def overlap(a, b): count = 0 for item in a: if item in b: count += 1 return count

Outer: length of a

Inner: length of b

If a and b are both length n, then overlap takes steps⇥(n2)

Lower-order terms: The fastest-growing part of the computation dominates the total

Page 137: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

Nesting: When an inner process is repeated for each step in an outer process, multiply the steps in the outer and inner processes to find the total number of steps

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n) ⇥(lnn)

def overlap(a, b): count = 0 for item in a: if item in b: count += 1 return count

Outer: length of a

Inner: length of b

If a and b are both length n, then overlap takes steps⇥(n2)

Lower-order terms: The fastest-growing part of the computation dominates the total

⇥(n2)

Page 138: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

Nesting: When an inner process is repeated for each step in an outer process, multiply the steps in the outer and inner processes to find the total number of steps

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n) ⇥(lnn)

def overlap(a, b): count = 0 for item in a: if item in b: count += 1 return count

Outer: length of a

Inner: length of b

If a and b are both length n, then overlap takes steps⇥(n2)

Lower-order terms: The fastest-growing part of the computation dominates the total

⇥(n2 + n)⇥(n2)

Page 139: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Properties of Orders of Growth

Constants: Constant terms do not affect the order of growth of a process

21

Logarithms: The base of a logarithm does not affect the order of growth of a process

Nesting: When an inner process is repeated for each step in an outer process, multiply the steps in the outer and inner processes to find the total number of steps

⇥(n) ⇥(500 · n) ⇥(1

500· n)

⇥(log2 n) ⇥(log10 n) ⇥(lnn)

def overlap(a, b): count = 0 for item in a: if item in b: count += 1 return count

Outer: length of a

Inner: length of b

If a and b are both length n, then overlap takes steps⇥(n2)

Lower-order terms: The fastest-growing part of the computation dominates the total

⇥(n2 + n)⇥(n2) ⇥(n2+ 500 · n+ log2 n+ 1000)

Page 140: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

Page 141: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

Page 142: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn) Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Page 143: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn) Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Page 144: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Page 145: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Quadratic growth. E.g., overlap

Page 146: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

Page 147: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

Page 148: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Linear growth. E.g., slow factors or exp

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

Page 149: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Linear growth. E.g., slow factors or exp

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

⇥(pn)

Page 150: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Linear growth. E.g., slow factors or exp

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

⇥(pn) Square root growth. E.g., factors_fast

Page 151: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

�(log n)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Linear growth. E.g., slow factors or exp

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

⇥(pn) Square root growth. E.g., factors_fast

Page 152: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

�(log n)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Linear growth. E.g., slow factors or exp

Logarithmic growth. E.g., exp_fast

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

⇥(pn) Square root growth. E.g., factors_fast

Page 153: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

�(log n)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Linear growth. E.g., slow factors or exp

Logarithmic growth. E.g., exp_fastDoubling the problem only increments R(n).

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

⇥(pn) Square root growth. E.g., factors_fast

Page 154: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

�(log n)

�(1)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Linear growth. E.g., slow factors or exp

Logarithmic growth. E.g., exp_fastDoubling the problem only increments R(n).

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

⇥(pn) Square root growth. E.g., factors_fast

Page 155: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

�(log n)

�(1)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Linear growth. E.g., slow factors or exp

Logarithmic growth. E.g., exp_fastDoubling the problem only increments R(n).

Constant. The problem size doesn't matter

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

⇥(pn) Square root growth. E.g., factors_fast

Page 156: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

�(log n)

�(1)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Linear growth. E.g., slow factors or exp

Logarithmic growth. E.g., exp_fastDoubling the problem only increments R(n).

Constant. The problem size doesn't matter

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

⇥(pn) Square root growth. E.g., factors_fast

Page 157: 61A Lecture 13 - inst.eecs.berkeley.educs61a/fa17/assets/slides/13-Growth... · Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: 4 def fib(n):

Comparing orders of growth (n is the problem size)

22

�(bn)

�(n)

�(log n)

�(1)

⇥(n2)

Exponential growth. Recursive fib takes

�(�n) � =1 +

�5

2� 1.61828steps, where

Incrementing the problem scales R(n) by a factor

Linear growth. E.g., slow factors or exp

Logarithmic growth. E.g., exp_fastDoubling the problem only increments R(n).

Constant. The problem size doesn't matter

Quadratic growth. E.g., overlapIncrementing n increases R(n) by the problem size n

⇥(pn) Square root growth. E.g., factors_fast