New version page

Berkeley COMPSCI 61A - Lecture 21

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more

This preview shows page 1 out of 2 pages.

View Full Document
View Full Document

End of preview. Want to read all 2 pages?

Upload your study docs or become a GradeBuddy member to access this document.

View Full Document
Unformatted text preview:

61A Lecture 21Monday, October 17Space ConsumptionWhich 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 and frames can be reclaimed.2Active environments: •The environment for the current expression being evaluated•Environments for calls that depend upon the value of the current expression•Environments associated with functions referenced by active environmentsFibonacci Environment Diagram3fib(3)if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)fib:fib(n):...n: 3fibfib(n-2)if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)n: 1fib0Fibonacci Environment Diagram4fib(3)if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)fib:fib(n):...n: 3fibfib(n-2)if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)0n: 1fibfib(n-1)if n == 1: return 0if n == 2: return 1return fib(n-2) + fib(n-1)n: 2fibFibonacci Memory Consumption5fib(6)fib(5)fib(3)fib(2)1fib(4)fib(2)1fib(3)fib(1) fib(2)0 1fib(1)0fib(4)fib(2)1fib(3)fib(1) fib(2)0 1Assume we have reached this stepFibonacci Memory Consumption6fib(6)fib(5)fib(3)fib(2)1fib(4)fib(2)1fib(3)fib(1) fib(2)0 1fib(1)0fib(4)fib(2)1fib(3)fib(1) fib(2)0 1Assume we have reached this stepHas an active environmentCan be reclaimedHasn't yet been createdActive Environments for Returned Functions7make_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 environmentn: 1make_adderadder:adder(k):return k + n def adder(k): return k + n return adderTherefore, all frames in this environment must be kept add1:R(n)=Θ(f (n))k1· f (n) ≤ R(n) ≤ k2· f (n)Order of GrowthA method for bounding the resources used by a function as the "size" of a problem increases8n: size of the problemR(n): Measurement of some resource used (time or space)means that there are constants k1 and k2 such thatfor sufficiently large values of n.Iteration vs Memoized Tree RecursionIterative and memoized implementations are not the same.9 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 SpaceΘ(n)Θ(n)Θ(n)Θ(1)Θ(bn)Θ(n)Θ(log n)Θ(1)Comparing orders of growth10Exponential growth! Recursive fib takes Θ(φn)φ =1+√52≈ 1.61828steps, where Incrementing the problem scales R(n) by a factor.Linear growth. Resources scale with the problem.Logarithmic growth. These functions scale well. Doubling the problem increments resources needed.Constant. The problem size doesn't matter.bn=�1 if n =0b · bn−1otherwisebn=1 if n =0(b12n)2if n is evenb · bn−1if n is oddExponentiationGoal: one more multiplication lets us double the problem size.11 def exp(b, n): if n == 0: return 1 return b * exp(b, n-1) def square(x): return x*x def fast_exp(b, n): if n == 0: return 1 if n % 2 == 0: return square(fast_exp(b, n//2)) else: return b * fast_exp(b, n-1)ExponentiationGoal: one more multiplication lets us double the problem size.12 def exp(b, n): if n == 0: return 1 return b * exp(b, n-1) def square(x): return x*x def fast_exp(b, n): if n == 0: return 1 if n % 2 == 0: return square(fast_exp(b, n//2)) else: return b * fast_exp(b, n-1)Time SpaceΘ(n)Θ(n)Θ(log n)Θ(log


View Full Document
Loading Unlocking...
Login

Join to view Lecture 21 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 21 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?