This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 1/8/2009 1 Lecture 1: Analyzing algorithms A royal mathematical challenge (1202): Suppose that rabbits take exactly one month to become fertile, after which they produce one child per month, forever. Starting with one rabbit, how many are there after n months? Leonardo da Pisa, aka Fibonacci The proliferation of rabbits Fertile Not fertile Initially One month Two months Three months Four months Five months Rabbits take one month to become fertile, after which they produce one child per month, forever. Let F n be the number of rabbits at month n F 1 = 1 F 2 = 1 F n = F n-1 + F n-2 Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … These numbers grow very fast: F 30 > 10 6 ! In fact, F n ≈ 2 0.694n ≈ 1.6 n , exponential growth . Computing Fibonacci numbers function Fib1(n) if n = 1 return 1 if n = 2 return 1 return Fib1(n-1) + Fib1(n-2) A recursive algorithm F(5) F(4) F(3) F(3) F(1) F(2) F(2) F(2) F(1) Two questions we always ask about algorithms: Does it work correctly? Yes – it directly implements the definition of Fibonacci numbers. How long does it take? This is not so obvious… Running time analysis function Fib1(n) if n = 1 return 1 if n = 2 return 1 return Fib1(n-1) + Fib1(n-2) Let T(n) = number of steps needed to compute F(n). Then: T(n) > T(n-1) + T(n-2) But F n = F n-1 + F n-2 . Therefore T(n) > F n ≈ 2 0.694n !...
View Full Document
This note was uploaded on 01/21/2010 for the course CSE 661930 taught by Professor Freund,yoav during the Fall '09 term at UCSD.
- Fall '09