Return graded responses -------------------------------------------------------------------- Efficiency of programs. We saw two different programs for computing the Fibonacci number sequence. Here they are: (define (fib1 n) (if (< n 2) n (+ (fib1 (- n 1)) (fib1 (- n 2))))) (define (fib2 n) (if (< n 2) n (loop (- n 2) 1 0))) (define (loop k f1 f2) (if (= k 0) (+ f1 f2) (loop (- k 1) (+ f1 f2) f1))) We noticed that program fib1 is much slower than fib2. Even for n = 30, (fib1 30) was taking time of the order of tens of seconds. On the other hand, fib2 was very fast and worked well for extremely large numbers. In real-world scenarios, when we have several options to implement a particular functionality, we need to choose the right one based on a number of different factors, including: compatibility: does the option fit well with rest of the software package? performance: does the option run fast and has low memory usage? extensibility and maintenance: is the option easily extensible? Let us consider the two fibonacci programs and reason out their performance differences. Here is a profile and chart of their performance characteristics. In terms of number of function calls. (fib1 n): 1 + number of calls in (fib1 n-1) + number of calls in (fib1 n-2) (fib2 n): 1 + number of calls in (loop n-1 * *) (loop k * *): 1 + number of calls in (loop k-1 * *) = k. We see that the time for (fib2 n) is linear in n. Let us consider (fib1 n). Let us see how much more does (fib1 n) take than (fib1 n-1). Consider the ratios. Hmm, the ratio seems to be around 1.61. Interesting. This suggests that we have something like the following. T_1(n) = 1.61^n. Let phi = 1.61. Let us try to prove this claim. We will show that Theorem: T_1(n) >= phi^n - 1 for n >= 1. Proof: By induction on n. We will prove the following claim. IH(n): For 1 <= i <= n, T_1(i) >= phi^i - 1. Base case: n = 1. T_1(1) = 1. While phi^1 - 1 = 1.61 - 1 = 0.61. Induction step: Let us suppose the claim is true for all i <= n. We will prove the claim for n+1. We have T_1(n+1) = T_1(n) + T_1(n-1) + 1 >= phi^n - 1 + phi^{n-1} - 1 + 1 = phi^n + phi^{n-1} - 1 = phi^{n+1} - 1, since phi^2 = phi + 1. End of proof. ------------------------------------------------------------------------- Question: Are these two algorithms efficient? Can we construct a more efficient algorithm? In order to answer this, we are going to take a long detour. During this detour, we will try to address issues such as: what do we mean by efficient? what is the precise notion of complexity? why is it important in practice? -------------------------------------------------------------------------