The power of logarithmic computations Jordi Cortadella Department

  • Slides: 31
Download presentation
The power of logarithmic computations Jordi Cortadella Department of Computer Science

The power of logarithmic computations Jordi Cortadella Department of Computer Science

Introduction to Programming © Dept. CS, UPC 2

Introduction to Programming © Dept. CS, UPC 2

Can we reduce the number of multiplications? • Introduction to Programming © Dept. CS,

Can we reduce the number of multiplications? • Introduction to Programming © Dept. CS, UPC 3

 • Introduction to Programming © Dept. CS, UPC 4

• Introduction to Programming © Dept. CS, UPC 4

Introduction to Programming © Dept. CS, UPC 5

Introduction to Programming © Dept. CS, UPC 5

(exponents are powers of 2) Introduction to Programming © Dept. CS, UPC 6

(exponents are powers of 2) Introduction to Programming © Dept. CS, UPC 6

 • Introduction to Programming © Dept. CS, UPC 7

• Introduction to Programming © Dept. CS, UPC 7

Introduction to Programming x 2 4 16 y 19 9 4 z 1 2

Introduction to Programming x 2 4 16 y 19 9 4 z 1 2 8 256 65536 4294967296 2 1 0 8 8 524288 © Dept. CS, UPC 8

Fibonacci numbers Leonardo Fibonacci Pisa, 1170 - 1250 Introduction to Programming © Dept. CS,

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,

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 ©

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.

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

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 Introduction to Programming © Dept. CS, UPC 14

Fibonacci numbers Shallow diagonals of Pascal’s Triangle Introduction to Programming © Dept. CS, UPC

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

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

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 3 4 2 3

Fibonacci numbers 8 7 6 6 5 5 4 3 3 4 2 3 5 3 2 2 1 2 11010 10 10 Introduction to Programming 4 10 4 3 3 2 2 2 11010 10 10 How many recursive calls? © Dept. CS, UPC 18

Fibonacci numbers 8 7 6 6 5 5 4 3 3 2 3 3

Fibonacci numbers 8 7 6 6 5 5 4 3 3 2 3 3 4 2 2 1 3 2 2 11010 10 10 4 5 10 4 3 3 2 2 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

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

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_i 1 = 1; // Inv: f_i is the Fibonacci number of order i. // f_i 1 is the Fibonacci number of order i+1. for (int i = 0; i < n; ++i) { int f = f_i + f_i 1; f_i = f_i 1; f_i 1 = 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.

Fibonacci numbers Algebraic solution: find matrix A such that Introduction to Programming © Dept. CS, UPC 22

Fibonacci numbers Introduction to Programming © Dept. CS, UPC 23

Fibonacci numbers Introduction to Programming © Dept. CS, UPC 23

Fibonacci numbers typedef vector<int> > M 2 x 2; // Pre: A and B

Fibonacci numbers typedef vector<int> > M 2 x 2; // Pre: A and B are 2 x 2 integer matrices // Returns A B M 2 x 2 Matrix. Mul(const M 2 x 2& A, const M 2 x 2& B) { M 2 x 2 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 2 x 2 integer matrix // Returns

Fibonacci numbers // Pre: A is a 2 x 2 integer matrix // Returns An M 2 x 2 power(const M 2 x 2& A, int n) { if (n == 0) return Identity(); // returns I if (n%2 == 0) return power(Matrix. Mul(A, A), n/2); return Matrix. Mul(A, power(Matrix. Mul(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.

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]] M 2 x 2 A(2, vector<int>(2, 1)); A[1][1] = 0; } M 2 x 2 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 • Introduction to Programming © Dept. CS, UPC 27

Fibonacci numbers and golden ratio • Introduction to Programming © Dept. CS, UPC 27

Fibonacci numbers and golden ratio 2 1, 9 Ratio Fn/Fn-1 1, 8 1, 7

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 28

Fibonacci numbers and golden ratio Introduction to Programming © Dept. CS, UPC 29

Fibonacci numbers and golden ratio Introduction to Programming © Dept. CS, UPC 29

Fibonacci numbers and golden ratio • Introduction to Programming © Dept. CS, UPC 30

Fibonacci numbers and golden ratio • Introduction to Programming © Dept. CS, UPC 30

Conclusions • Many naïve algorithms perform repeated computations, often hidden behind the natural computations.

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 31