Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
The power of logarithmic computations Jordi Cortadella Department of Computer Science Calculate π₯ π¦ // Pre: π β π, π β₯ π // Returns ππ int power(int x, int y) { int p = 1; for (int i = 0; i < y; ++i) p = pοͺx; return p; } The algorithm performs π¦ multiplications. Introduction to Programming © Dept. CS, UPC 2 Can we reduce the number of multiplications? Let us consider the calculation of π₯ 32 : For π₯ π¦ we will need about log 2 π¦ multiplications Introduction to Programming © Dept. CS, UPC 3 Recursive π₯ π¦ β’ Basic case (π¦ = 0): π₯ 0 = 1 β’ Recursive cases (π¦ > 0): ο§ π¦ is even: ο§ π¦ is odd: π₯ π¦ = π₯2 π¦ 2 π₯ π¦ = π₯ β π₯ π¦β1 = π₯ β π₯ 2 (π¦β1) 2 β’ For integer division: π¦β1 π¦ = 2 2 Introduction to Programming when π¦ is odd © Dept. CS, UPC 4 Recursive π₯ π¦ // Pre: π β π, π β₯ π // Returns ππ int power(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return power(xοͺx, y/2); return xοͺpower(xοͺx, y/2); } Introduction to Programming © Dept. CS, UPC 5 19 Example: 2 (exponents are powers of 2) Introduction to Programming © Dept. CS, UPC 6 Iterative π₯ π¦ Key strategy: find a good invariant. Let π be the result of the computation. int power(int x, int y) { int z = 1; // Invariant: π = ππ β π while (y != 0) { if (y%2 == 0) { x = xοͺx; y = y/2; } else { z = zοͺx; y = y β 1; } } return z; } Introduction to Programming © Dept. CS, UPC 7 π¦ Iterative π₯ (improved version) // Pre: π β π, π β₯ π // Returns ππ int power(int x, int y) { int z = 1; // Invariant: π = ππ β π while (y != 0) { if (y%2 == 1) z = zοͺx; x = xοͺx; y = y/2; } return z; Example: 219 x 2 4 16 y 19 9 4 z 1 2 8 256 65536 4294967296 2 1 0 8 8 524288 } Introduction to Programming © Dept. CS, UPC 8 Fibonacci numbers Leonardo Fibonacci Pisa, 1170 - 1250 Introduction to Programming © Dept. CS, UPC 9 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, UPC 10 Fibonacci numbers Tiling with Fibonacci squares https://en.wikipedia.org/wiki/Fibonacci_number Introduction to Programming © Dept. CS, UPC 11 Fibonacci numbers The Fibonacci spiral https://en.wikipedia.org/wiki/Fibonacci_number Introduction to Programming © Dept. CS, UPC 12 Fibonacci numbers Number of petals in flowers Introduction to Programming © Dept. CS, UPC 13 Fibonacci numbers Introduction to Programming © Dept. CS, UPC 14 Fibonacci numbers Shallow diagonals of Pascalβs Triangle Introduction to Programming © Dept. CS, UPC 15 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, UPC 16 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, UPC 17 Fibonacci numbers 8 7 6 6 5 5 4 3 4 3 3 4 2 3 5 3 2 2 1 2 2 1 2 110 2 11010 2 11010 10 10 Introduction to Programming 10 4 3 4 3 3 2 2 2 1 2 110 2 11010 10 10 How many recursive calls? © Dept. CS, UPC 18 Fibonacci numbers 8 7 6 6 5 5 4 3 4 3 3 2 3 3 2 2 1 2 2 1 2 110 2 11010 2 11010 10 10 4 5 10 4 3 4 3 3 2 2 2 1 2 110 2 11010 10 10 For example, fib(5) is re-calculated 3 times. Introduction to Programming © Dept. CS, UPC 19 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·1010 times Introduction to Programming © Dept. CS, UPC 20 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; Complexity: O(n) } Introduction to Programming © Dept. CS, UPC 21 Fibonacci numbers Algebraic solution: find matrix A such that Introduction to Programming © Dept. CS, UPC 22 Fibonacci numbers Complexity: O(log n) Introduction to Programming © Dept. CS, UPC 23 Fibonacci numbers typedef vector< vector<int> > M2x2; // Pre: A and B are 2x2 integer matrices // Returns Aο΄B M2x2 MatrixMul(const M2x2& A, const M2x2& B) { M2x2 C(2, vector<int>(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, UPC 24 Fibonacci numbers // Pre: A is a 2x2 integer matrix // Returns An 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, UPC 25 Fibonacci numbers // Pre: n ο³ 0 // Returns the Fibonacci number of order n. int fib(int n) { if (n <= 1) return n; // Creates the Fibonacci matrix [[1,1],[1,0]] M2x2 A(2, vector<int>(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, UPC 26 Fibonacci numbers and golden ratio β’ Two quantities π > π > 0 are in the golden ratio if π+π π = =π π π β’ The golden ratio is: 1+ 5 π= = 1.6180339887 β¦ 2 β’ The ratio of two consecutives Fibonacci numbers converges to the golden ratio: πΉπ+1 lim =π πββ πΉπ Introduction to Programming β πΉπ+1 β π β πΉπ © Dept. CS, UPC 27 Fibonacci numbers and golden ratio β’ Let π΄ be the Fibonacci matrix 1 π΄= 1 1 0 β’ The eigenvalues of π΄ are (Ax=ο¬x): ο¬1 = π, ο¬2 = 1 β π β’ Example (πΉ15 = 610, πΉ16 = 987, πΉ17 = 1597): π β πΉ15 = 987.00073313 β¦ β πΉ16 π β πΉ16 = 1596.99954689 β¦ β πΉ17 Introduction to Programming © Dept. CS, UPC 28 Fibonacci numbers and golden ratio 2 1.9 Ratio Fn/Fn-1 1.8 1.7 1.6 1.5 1.4 1 2 Introduction to Programming 3 5 8 13 21 34 © Dept. CS, UPC 55 89 144 233 377 610 987 29 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, UPC 30