61A Lecture 21 Monday, October 17 Monday, October 17, 2011 Space Consumption 2 Monday, October 17, 2011 Space Consumption Which environment frames do we need to keep during evaluation? 2 Monday, October 17, 2011 Space Consumption Which environment frames do we need to keep during evaluation? Each step of evaluation has a set of active environments. 2 Monday, October 17, 2011 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. 2 Monday, October 17, 2011 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 and frames can be reclaimed. 2 Monday, October 17, 2011 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 and frames can be reclaimed. Active environments: 2 Monday, October 17, 2011 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 and frames can be reclaimed. Active environments: • The environment for the current expression being evaluated 2 Monday, October 17, 2011 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 and frames can be reclaimed. Active environments: • The environment for the current expression being evaluated • Environments for calls that depend upon the value of the current expression 2 Monday, October 17, 2011 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 and frames can be reclaimed. Active 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 environments 2 Monday, October 17, 2011 Fibonacci Environment Diagram fib: fib(n): ... n: 3 fib fib(3) if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) 3 Monday, October 17, 2011 Fibonacci Environment Diagram fib: fib(n): ... n: 3 fib fib(3) if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) n: 1 fib fib(n-2) if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) 3 Monday, October 17, 2011 Fibonacci Environment Diagram fib: fib(n): ... n: 3 fib fib(3) if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) n: 1 fib fib(n-2) 0 if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) 3 Monday, October 17, 2011 Fibonacci Environment Diagram fib: fib(n): ... n: 3 fib fib(3) if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) n: 1 fib fib(n-2) 0 if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) 4 Monday, October 17, 2011 Fibonacci Environment Diagram fib: fib(n): ... n: 3 n: 2 fib fib fib(3) if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) n: 1 fib fib(n-2) 0 if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) fib(n-1) if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) 4 Monday, October 17, 2011 Fibonacci Memory Consumption fib(6) fib(4) fib(2) 1 fib(5) fib(3) fib(1) 0 fib(3) fib(2) 1 fib(4) fib(1) fib(2) fib(2) 0 1 1 fib(3) fib(1) fib(2) 0 1 5 Monday, October 17, 2011 Fibonacci Memory Consumption fib(6) fib(4) fib(2) 1 fib(5) fib(3) fib(1) 0 fib(3) fib(2) 1 fib(4) fib(1) fib(2) fib(2) 0 1 1 Assume we have reached this step fib(3) fib(1) fib(2) 0 1 5 Monday, October 17, 2011 Fibonacci Memory Consumption fib(6) fib(4) fib(2) 1 fib(5) fib(3) fib(1) 0 fib(3) fib(2) 1 fib(4) fib(1) fib(2) fib(2) 0 1 1 Assume we have reached this step fib(3) fib(1) fib(2) 0 1 6 Monday, October 17, 2011 Fibonacci Memory Consumption Has an active environment fib(6) fib(4) fib(2) 1 fib(5) fib(3) fib(1) 0 fib(3) fib(2) 1 fib(4) fib(1) fib(2) fib(2) 0 1 1 Assume we have reached this step fib(3) fib(1) fib(2) 0 1 6 Monday, October 17, 2011 Fibonacci Memory Consumption Has an active environment Can be reclaimed fib(6) fib(4) fib(2) 1 fib(5) fib(3) fib(1) 0 fib(3) fib(2) 1 fib(4) fib(1) fib(2) fib(2) 0 1 1 Assume we have reached this step fib(3) fib(1) fib(2) 0 1 6 Monday, October 17, 2011 Fibonacci Memory Consumption Has an active environment Can be reclaimed Hasn't yet been created fib(6) fib(4) fib(2) 1 fib(5) fib(3) fib(1) 0 fib(3) fib(2) 1 fib(4) fib(1) fib(2) fib(2) 0 1 1 Assume we have reached this step fib(3) fib(1) fib(2) 0 1 6 Monday, October 17, 2011 Active Environments for Returned Functions make_adder: make_adder(n): ... def make_adder(n): def adder(k): return k + n return adder add1 = make_adder(1) 7 Monday, October 17, 2011 Active Environments for Returned Functions make_adder: make_adder(n): ... def make_adder(n): def adder(k): return k + n return adder add1 = make_adder(1) make_adder(1) 7 Monday, October 17, 2011 Active Environments for Returned Functions make_adder: make_adder(n): ... def make_adder(n): def adder(k): return k + n return adder add1 = make_adder(1) n: 1 adder: make_adder adder(k): return k + n make_adder(1) def adder(k): return k + n return adder 7 Monday, October 17, 2011 Active Environments for Returned Functions make_adder: make_adder(n): ... def make_adder(n): def adder(k): return k + n return adder add1 = make_adder(1) n: 1 adder: make_adder adder(k): return k + n make_adder(1) def adder(k): return k + n return adder 7 Monday, October 17, 2011 Active Environments for Returned Functions make_adder: add1: make_adder(n): ... def make_adder(n): def adder(k): return k + n return adder add1 = make_adder(1) n: 1 adder: make_adder adder(k): return k + n make_adder(1) def adder(k): return k + n return adder 7 Monday, October 17, 2011 Active Environments for Returned Functions make_adder: add1: make_adder(n): ... def make_adder(n): def adder(k): return k + n return adder add1 = make_adder(1) n: 1 adder: make_adder adder(k): return k + n Associated with an environment make_adder(1) def adder(k): return k + n return adder 7 Monday, October 17, 2011 Active Environments for Returned Functions make_adder: add1: make_adder(n): ... def make_adder(n): def adder(k): return k + n return adder add1 = make_adder(1) n: 1 adder: make_adder Therefore, all frames in this environment must be kept adder(k): return k + n Associated with an environment make_adder(1) def adder(k): return k + n return adder 7 Monday, October 17, 2011 Order of Growth 8 Monday, October 17, 2011 Order of Growth A method for bounding the resources used by a function as the "size" of a problem increases 8 Monday, October 17, 2011 Order of Growth A method for bounding the resources used by a function as the "size" of a problem increases n: size of the problem 8 Monday, October 17, 2011 Order of Growth A method for bounding the resources used by a function as the "size" of a problem increases n: size of the problem R(n): Measurement of some resource used (time or space) 8 Monday, October 17, 2011 Order of Growth A method for bounding the resources used by a function as the "size" of a problem increases n: size of the problem R(n): Measurement of some resource used (time or space) R(n) = Θ(f (n)) 8 Monday, October 17, 2011 Order of Growth A method for bounding the resources used by a function as the "size" of a problem increases n: size of the problem R(n): Measurement of some resource used (time or space) R(n) = Θ(f (n)) means that there are constants k1 and k2 such that 8 Monday, October 17, 2011 Order of Growth A method for bounding the resources used by a function as the "size" of a problem increases n: size of the problem R(n): Measurement of some resource used (time or space) R(n) = Θ(f (n)) means that there are constants k1 and k2 such that k1 · f (n) ≤ R(n) ≤ k2 · f (n) 8 Monday, October 17, 2011 Order of Growth A method for bounding the resources used by a function as the "size" of a problem increases n: size of the problem R(n): Measurement of some resource used (time or space) R(n) = Θ(f (n)) means that there are constants k1 and k2 such that k1 · f (n) ≤ R(n) ≤ k2 · f (n) for sufficiently large values of n. 8 Monday, October 17, 2011 Iteration vs Memoized Tree Recursion Iterative and memoized implementations are not the same. Time Space 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) 9 Monday, October 17, 2011 Iteration vs Memoized Tree Recursion Iterative and memoized implementations are not the same. Time def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr Space Θ(n) @memo def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) 9 Monday, October 17, 2011 Iteration vs Memoized Tree Recursion Iterative and memoized implementations are not the same. def fib_iter(n): prev, curr = 1, 0 for _ in range(n-1): prev, curr = curr, prev + curr return curr Time Space Θ(n) Θ(1) @memo def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n-2) + fib(n-1) 9 Monday, October 17, 2011 Iteration vs Memoized Tree Recursion Iterative and memoized implementations are not the same. 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) Θ(1) Θ(n) 9 Monday, October 17, 2011 Iteration vs Memoized Tree Recursion Iterative and memoized implementations are not the same. 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) Θ(1) Θ(n) Θ(n) 9 Monday, October 17, 2011 Comparing orders of growth 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) Exponential growth! Recursive fib takes 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) Exponential growth! Recursive fib takes √ 1+ 5 ≈ 1.61828 Θ(φ ) steps, where φ = 2 n 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) Exponential growth! Recursive fib takes √ 1+ 5 ≈ 1.61828 Θ(φ ) steps, where φ = 2 n Incrementing the problem scales R(n) by a factor. 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) Exponential growth! Recursive fib takes √ 1+ 5 ≈ 1.61828 Θ(φ ) steps, where φ = 2 n Incrementing the problem scales R(n) by a factor. Θ(n) 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) Exponential growth! Recursive fib takes √ 1+ 5 ≈ 1.61828 Θ(φ ) steps, where φ = 2 n Incrementing the problem scales R(n) by a factor. Θ(n) Linear growth. Resources scale with the problem. 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) Exponential growth! Recursive fib takes √ 1+ 5 ≈ 1.61828 Θ(φ ) steps, where φ = 2 n Incrementing the problem scales R(n) by a factor. Θ(n) Linear growth. Resources scale with the problem. Θ(log n) 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) Exponential growth! Recursive fib takes √ 1+ 5 ≈ 1.61828 Θ(φ ) steps, where φ = 2 n Incrementing the problem scales R(n) by a factor. Θ(n) Θ(log n) Linear growth. Resources scale with the problem. Logarithmic growth. These functions scale well. 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) Exponential growth! Recursive fib takes √ 1+ 5 ≈ 1.61828 Θ(φ ) steps, where φ = 2 n Incrementing the problem scales R(n) by a factor. Θ(n) Θ(log n) Linear growth. Resources scale with the problem. Logarithmic growth. These functions scale well. Doubling the problem increments resources needed. 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) Exponential growth! Recursive fib takes √ 1+ 5 ≈ 1.61828 Θ(φ ) steps, where φ = 2 n Incrementing the problem scales R(n) by a factor. Θ(n) Θ(log n) Linear growth. Resources scale with the problem. Logarithmic growth. These functions scale well. Doubling the problem increments resources needed. Θ(1) 10 Monday, October 17, 2011 Comparing orders of growth Θ(bn ) Exponential growth! Recursive fib takes √ 1+ 5 ≈ 1.61828 Θ(φ ) steps, where φ = 2 n Incrementing the problem scales R(n) by a factor. Θ(n) Θ(log n) Linear growth. Resources scale with the problem. Logarithmic growth. These functions scale well. Doubling the problem increments resources needed. Θ(1) Constant. The problem size doesn't matter. 10 Monday, October 17, 2011 Exponentiation 11 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. 11 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. def exp(b, n): if n == 0: return 1 return b * exp(b, n-1) 11 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. def exp(b, n): if n == 0: return 1 return b * exp(b, n-1) � 1 n b = b · bn−1 if n = 0 otherwise 11 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. def exp(b, n): if n == 0: return 1 return b * exp(b, n-1) � 1 n b = b · bn−1 1 1 n b = (b 2 n )2 b · bn−1 if n = 0 otherwise if n = 0 if n is even if n is odd 11 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. def exp(b, n): if n == 0: return 1 return b * exp(b, n-1) def square(x): return x*x � 1 n b = b · bn−1 1 1 n b = (b 2 n )2 b · bn−1 if n = 0 otherwise if n = 0 if n is even if n is odd 11 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. 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): � 1 n b = b · bn−1 1 1 n b = (b 2 n )2 b · bn−1 if n = 0 otherwise if n = 0 if n is even if n is odd 11 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. 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 � 1 n b = b · bn−1 1 1 n b = (b 2 n )2 b · bn−1 if n = 0 otherwise if n = 0 if n is even if n is odd 11 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. def exp(b, n): if n == 0: return 1 return b * exp(b, n-1) def square(x): return x*x � 1 n b = b · bn−1 1 1 n b = (b 2 n )2 b · bn−1 def fast_exp(b, n): if n == 0: return 1 if n % 2 == 0: return square(fast_exp(b, n//2)) if n = 0 otherwise if n = 0 if n is even if n is odd 11 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. def exp(b, n): if n == 0: return 1 return b * exp(b, n-1) def square(x): return x*x � 1 n b = b · bn−1 1 1 n b = (b 2 n )2 b · bn−1 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) if n = 0 otherwise if n = 0 if n is even if n is odd 11 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. Time Space 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) 12 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. def exp(b, n): if n == 0: return 1 return b * exp(b, n-1) Time Space Θ(n) Θ(n) 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) 12 Monday, October 17, 2011 Exponentiation Goal: one more multiplication lets us double the problem size. def exp(b, n): if n == 0: return 1 return b * exp(b, n-1) Time Space Θ(n) Θ(n) Θ(log n) Θ(log n) 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) 12 Monday, October 17, 2011
© Copyright 2025 Paperzz