The power of logarithmic computations Jordi Cortadella Department of Computer Science
Introduction to Programming© Dept. CS, UPC2
Can we reduce the number of multiplications? Introduction to Programming© Dept. CS, UPC3
Introduction to Programming© Dept. CS, UPC4
Introduction to Programming© Dept. CS, UPC5
(exponents are powers of 2) Introduction to Programming© Dept. CS, UPC6
Introduction to Programming© Dept. CS, UPC7
Introduction to Programming© Dept. CS, UPC8 xyz
Fibonacci numbers Leonardo Fibonacci Pisa, Introduction to Programming© Dept. CS, UPC9
Fibonacci numbers The Fibonacci sequence is defined as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, … In mathematical terms, it is defined by the following recurrence relation: Introduction to Programming© Dept. CS, UPC10
Fibonacci numbers Tiling with Fibonacci squares Introduction to Programming© Dept. CS, UPC11
Fibonacci numbers The Fibonacci spiral Introduction to Programming© Dept. CS, UPC12
Fibonacci numbers Number of petals in flowers Introduction to Programming© Dept. CS, UPC13
Fibonacci numbers Introduction to Programming© Dept. CS, UPC14
Fibonacci numbers Shallow diagonals of Pascal’s Triangle Introduction to Programming© Dept. CS, UPC15
Fibonacci numbers // Pre: n 0 // Post: Returns the Fibonacci number of order n. int fib(int n); Basic case: n = 0 ⇒ return 0. n = 1 ⇒ return 1. General case: n > 1 ⇒ return fib(n - 1) + fib(n – 2) Introduction to Programming© Dept. CS, UPC16
Fibonacci numbers: recursive version // Pre: n 0 // Returns the Fibonacci number of order n. int fib(int n) { // Recursive solution if (n <= 1) return n; else return fib(n - 1) + fib(n - 2); } Introduction to Programming© Dept. CS, UPC17
Fibonacci numbers How many recursive calls? Introduction to Programming© Dept. CS, UPC18
Fibonacci numbers For example, fib(5) is re-calculated 3 times. Introduction to Programming© Dept. CS, UPC19
Fibonacci numbers When fib(8) is calculated: – fib(7) is called once – fib(6) is called twice – fib(5) is called 3 times – fib(4) is called 5 times – fib(3) is called 8 times – fib(2) is called 13 times – fib(1) is called 21 times – fib(0) is called 13 times When fib(n) is calculated, how many times will fib(1) and fib(0) be called? Example: fib(50) calls fib(1) and fib(0) about 2.4·10 10 times Introduction to Programming© Dept. CS, UPC20
Fibonacci numbers: iterative version // Pre: n 0 // Returns the Fibonacci number of order n. int fib(int n) { // iterative solution int f_i = 0; int f_i1 = 1; // Inv: f_i is the Fibonacci number of order i. // f_i1 is the Fibonacci number of order i+1. for (int i = 0; i < n; ++i) { int f = f_i + f_i1; f_i = f_i1; f_i1 = f; } return f_i; } Introduction to Programming© Dept. CS, UPC21 Complexity: O(n)
Fibonacci numbers Algebraic solution: find matrix A such that Introduction to Programming© Dept. CS, UPC22
Fibonacci numbers Complexity: O(log n) Introduction to Programming© Dept. CS, UPC23
Fibonacci numbers typedef vector > M2x2; // Pre: A and B are 2x2 integer matrices // Returns AB M2x2 MatrixMul(const M2x2& A, const M2x2& B) { M2x2 C(2, vector (2)); C[0][0] = A[0][0]B[0][0] + A[0][1]B[1][0]; C[0][1] = A[0][0]B[0][1] + A[0][1]B[1][1]; C[1][0] = A[1][0]B[0][0] + A[1][1]B[1][0];; C[1][1] = A[1][0]B[0][1] + A[1][1]B[1][1]; return C; } Introduction to Programming© Dept. CS, UPC24
Fibonacci numbers // Pre: A is a 2x2 integer matrix // Returns A n M2x2 power(const M2x2& A, int n) { if (n == 0) return Identity(); // returns I if (n%2 == 0) return power(MatrixMul(A, A), n/2); return MatrixMul(A, power(MatrixMul(A, A), n/2)); } Complexity: O(log n) Introduction to Programming© Dept. CS, UPC25
Fibonacci numbers // Pre: n 0 // Returns the Fibonacci number of order n. int fib(int n) { if (n (2, 1)); A[1][1] = 0; M2x2 Fn = power(A, n - 1); // Complexity O(log n) return Fn[0][0]; } Introduction to Programming© Dept. CS, UPC26
Fibonacci numbers and golden ratio Introduction to Programming© Dept. CS, UPC27
Fibonacci numbers and golden ratio Introduction to Programming© Dept. CS, UPC28
Fibonacci numbers and golden ratio Introduction to Programming© Dept. CS, UPC29
Conclusions Many naïve algorithms perform repeated computations, often hidden behind the natural computations. Identify repeated computations and re-design algorithms accordingly. A deep knowledge of the problem is required. Doubly-recursive functions usually generate an explosion of computations (see Fibonacci). Try to avoid them whenever possible. Introduction to Programming© Dept. CS, UPC30