efficiency announcements measuring efficiency recursive
play

Efficiency Announcements Measuring Efficiency Recursive - PowerPoint PPT Presentation

Efficiency Announcements Measuring Efficiency Recursive Computation of the Fibonacci Sequence def fib (n): Our first example of tree recursion: if n == 0 : return 0 elif n == 1 : fib(5) return 1 else : return fib(n- 2 ) + fib(n- 1 ) fib(3)


  1. Efficiency

  2. Announcements

  3. Measuring Efficiency

  4. Recursive Computation of the Fibonacci Sequence def fib (n): Our first example of tree recursion: if n == 0 : return 0 elif n == 1 : fib(5) return 1 else : return fib(n- 2 ) + fib(n- 1 ) fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) fib(0) fib(1) 1 fib(0) fib(1) fib(1) fib(2) 0 1 fib(0) fib(1) 0 1 1 0 1 (Demo) 4 http://en.wikipedia.org/wiki/File:Fibonacci.jpg

  5. Memoization

  6. Memoization Idea: Remember the results that have been computed before def memo(f): Keys are arguments that map to return values cache = {} def memoized(n): if n not in cache: cache[n] = f(n) return cache[n] return memoized Same behavior as f, 
 if f is a pure function (Demo) 6

  7. Memoized Tree Recursion Call to fib fib(5) Found in cache Skipped fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) fib(0) fib(1) 1 fib(0) fib(1) fib(1) fib(2) 0 1 fib(0) fib(1) 0 1 1 0 1 7

  8. Exponentiation

  9. Exponentiation Goal: one more multiplication lets us double the problem size def exp(b, n): � if n == 0: 1 if n = 0 b n = return 1 b · b n − 1 otherwise else: return b * exp(b, n-1) def exp_fast(b, n): if n == 0: return 1 elif n % 2 == 0:  1 if n = 0 return square(exp_fast(b, n//2))  b n =  1 else: 2 n ) 2 ( b if n is even return b * exp_fast(b, n-1)  b · b n − 1 if n is odd  def square(x): return x * x (Demo) 9

  10. Exponentiation Goal: one more multiplication lets us double the problem size def exp(b, n): Linear time: if n == 0: • Doubling the input 
 return 1 doubles the time else: • 1024x the input takes 
 return b * exp(b, n-1) 1024x as much time def exp_fast(b, n): Logarithmic time: if n == 0: • Doubling the input 
 return 1 increases the time 
 elif n % 2 == 0: by a constant C return square(exp_fast(b, n//2)) • 1024x the input 
 else: increases the time 
 return b * exp_fast(b, n-1) by only 10 times C def square(x): return x * x 10

  11. Orders of Growth

  12. Quadratic Time Functions that process all pairs of values in a sequence of length n take quadratic time 3 5 7 6 def overlap(a, b): count = 0 0 0 0 0 4 for item in a: for other in b: 0 1 0 0 5 if item == other: count += 1 0 0 0 1 return count 6 overlap([3, 5, 7, 6], [4, 5, 6, 5]) 0 1 0 0 5 (Demo) 12

  13. Exponential Time def fib (n): Tree-recursive functions can take exponential time if n == 0 : return 0 elif n == 1 : fib(5) return 1 else : return fib(n- 2 ) + fib(n- 1 ) fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) fib(0) fib(1) 1 fib(0) fib(1) fib(1) fib(2) 0 1 fib(0) fib(1) 0 1 1 0 1 13 13 http://en.wikipedia.org/wiki/File:Fibonacci.jpg

  14. <latexit sha1_base64="VxC4Ky3frwSgEWdLXRZ0iEtjZH4=">ACv3icbVHbSsMwGE7rac5T1UtvgkPZGIy2CHqhMPFCLye4A6zbSLNshqVpSVJhlL2kF4JvY7p17viTwJfvkMfP2JUKtv+Ncyd3b39g9xh/uj45PTMOr9oyDAWmNRxyELR8pEkjHJSV1Qx0oEQYHPSNMfvaR684sISUP+ocYR6QRoyOmAYqQ01bN+EPRwP1TQ7ya87Ezg7RMsLjhemkPoebORn6tF7S913ZUE7olWIb/FpfrlVPaFp4JK+Esu71GNc7LUzLmVTjy0emhAt7VsGu2NOCm8DJQAFkVetZ314/xHFAuMIMSdl27Eh1EiQUxYxM8l4sSYTwCA1JW0OAiI7ybT/E3ijmT4chEJPruCUXU4kKJByHPjaGSD1Kde1lNymtWM1eOgklEexIhzPDhrEDKoQp8J+1QrNhYA4QF1XeF+BMJhJX+8rxugrP+5E3QcCuOXHe7wrV16wdOXAFrkEROAeVMEbqIE6wMaj4Rsjg5nP5tDkZjSzmkaWuQrZY7/ABqfyBM=</latexit> <latexit sha1_base64="VxC4Ky3frwSgEWdL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kZjSzmkaWuQrZY7/ABqfyBM=</latexit> <latexit sha1_base64="VxC4Ky3frwSgEWdLXRZ0iEtjZH4=">ACv3icbVHbSsMwGE7rac5T1UtvgkPZGIy2CHqhMPFCLye4A6zbSLNshqVpSVJhlL2kF4JvY7p17viTwJfvkMfP2JUKtv+Ncyd3b39g9xh/uj45PTMOr9oyDAWmNRxyELR8pEkjHJSV1Qx0oEQYHPSNMfvaR684sISUP+ocYR6QRoyOmAYqQ01bN+EPRwP1TQ7ya87Ezg7RMsLjhemkPoebORn6tF7S913ZUE7olWIb/FpfrlVPaFp4JK+Esu71GNc7LUzLmVTjy0emhAt7VsGu2NOCm8DJQAFkVetZ314/xHFAuMIMSdl27Eh1EiQUxYxM8l4sSYTwCA1JW0OAiI7ybT/E3ijmT4chEJPruCUXU4kKJByHPjaGSD1Kde1lNymtWM1eOgklEexIhzPDhrEDKoQp8J+1QrNhYA4QF1XeF+BMJhJX+8rxugrP+5E3QcCuOXHe7wrV16wdOXAFrkEROAeVMEbqIE6wMaj4Rsjg5nP5tDkZjSzmkaWuQrZY7/ABqfyBM=</latexit> Time for n+n Time for input n+1 Time for input n Common Orders of Growth Exponential growth . E.g., recursive fib a · b n +1 = ( a · b n ) · b Incrementing n multiplies time by a constant Quadratic growth . E.g., overlap a · ( n + 1) 2 = ( a · n 2 ) + a · (2 n + 1) Incrementing n increases time by n times a constant Linear growth . E.g., slow exp a · ( n + 1) = ( a · n ) + a Incrementing n increases time by a constant Logarithmic growth . E.g., exp_fast a · ln(2 · n ) = ( a · ln n ) + a · ln 2 Doubling n only increments time by a constant Constant growth . Increasing n doesn't affect time 14

  15. Order of Growth Notation

Recommend


More recommend