announcements efficiency
play

Announcements Efficiency Recursive Computation of the Fibonacci - PDF document

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


  1. Announcements Efficiency Recursive Computation of the Fibonacci Sequence Our first example of tree recursion: def fib (n): if n == 0 : return 0 elif n == 1 : fib(5) return 1 else : return fib(n- 2 ) + fib(n- 1 ) Measuring Efficiency fib(3) fib(4) fib(1) fib(2) fib(2) fib(3) 1 fib(0) fib(1) fib(0) fib(1) fib(1) fib(2) 0 1 0 1 1 fib(0) fib(1) (Demo) 0 1 4 http://en.wikipedia.org/wiki/File:Fibonacci.jpg Memoization Idea: Remember the results that have been computed before def memo(f): Keys are arguments that map to return values cache = {} Memoization 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 Memoized Tree Recursion Call to fib fib(5) Found in cache Skipped fib(3) fib(4) Exponentiation 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend