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: Return F[n] B. How fast is Fib2? O(n) operations, linear. Weve gone from exponential O(F n ) -> polynomial O(n). C. Weve been careless in our runtime accounting. Adding numbers is not O(1) time if the numbers are arbitrarily large. F n ~ 1.6 n , ie O(n) bits long. Adding n-bit numbers takes O(n) time. D. Updated running times Fib: O(nF n ) exponential Fib2: O(n 2 ) polynomial Can we do better? A third algorithm a. Matrix vector form F = 0 F 1 1 F 1 = 0 1 F F 2 = 1 1 F 1 2 F 2 = 0 1 F F 3 = 1 1 F 1 So algorithm = n F n = 0 1 F F n+1 = 1 1 F 1 Time to compute F n is then the time to compute M n . We could compute M, M 2 , M 3 , M 4 , M 5 , M n But theres a much faster way repeated squaring M -> M 2 -> M 4-> M 8-> -> M n Just O(log n) marix multiplications Multiplying n-bit numbers like O(n 2 ) time. Time to compute M n time to compute M n/2 one matrix multiplication T(n) = T(n/2) + O(n 2 )...
View Full Document
- Spring '08