New version page

# Berkeley COMPSCI 61A - Lecture 21

Pages: 2
Documents in this Course

10 pages

68 pages

5 pages

23 pages

38 pages

4 pages

4 pages

6 pages

12 pages

8 pages

6 pages

13 pages

2 pages

34 pages

3 pages

3 pages

6 pages

250 pages

125 pages

3 pages

5 pages

4 pages

107 pages

159 pages

6 pages

113 pages

46 pages

5 pages

6 pages

35 pages

162 pages

9 pages

30 pages

4 pages

25 pages

3 pages

6 pages

2 pages

13 pages

117 pages

6 pages

13 pages

104 pages

34 pages

97 pages

173 pages

104 pages

3 pages

5 pages

6 pages

6 pages

4 pages

108 pages

3 pages

4 pages

135 pages

10 pages

22 pages

12 pages

35 pages

6 pages

4 pages

3 pages

52 pages

18 pages

86 pages

129 pages

8 pages

132 pages

6 pages

16 pages

2 pages

3 pages

12 pages

114 pages

43 pages

4 pages

57 pages

3 pages

3 pages

5 pages

2 pages

95 pages

2 pages

108 pages

6 pages

13 pages

43 pages

15 pages

6 pages

106 pages

26 pages

4 pages

Load more

## This preview shows page 1 out of 2 pages.

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
Unlocking...
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?