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