Presentation is loading. Please wait.

Presentation is loading. Please wait.

The power of logarithmic computations Jordi Cortadella Department of Computer Science.

Similar presentations


Presentation on theme: "The power of logarithmic computations Jordi Cortadella Department of Computer Science."— Presentation transcript:

1 The power of logarithmic computations Jordi Cortadella Department of Computer Science

2 Introduction to Programming© Dept. CS, UPC2

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

4 Introduction to Programming© Dept. CS, UPC4

5 Introduction to Programming© Dept. CS, UPC5

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

7 Introduction to Programming© Dept. CS, UPC7

8 Introduction to Programming© Dept. CS, UPC8 xyz 2191 492 1648 25628 6553618 42949672960524288

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

10 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

11 Fibonacci numbers https://en.wikipedia.org/wiki/Fibonacci_number Tiling with Fibonacci squares Introduction to Programming© Dept. CS, UPC11

12 Fibonacci numbers https://en.wikipedia.org/wiki/Fibonacci_number The Fibonacci spiral Introduction to Programming© Dept. CS, UPC12

13 Fibonacci numbers Number of petals in flowers Introduction to Programming© Dept. CS, UPC13

14 Fibonacci numbers Introduction to Programming© Dept. CS, UPC14

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

16 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

17 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

18 Fibonacci numbers8 7 6 5 4 3 2 10 1 2 10 3 2 10 1 4 3 2 10 1 2 10 5 4 3 2 10 1 2 10 3 2 10 1 6 5 4 3 2 10 1 2 10 3 2 10 1 4 3 2 10 1 2 10 How many recursive calls? Introduction to Programming© Dept. CS, UPC18

19 Fibonacci numbers8 7 6 5 4 3 2 10 1 2 10 3 2 10 1 4 3 2 10 1 2 10 5 4 3 2 10 1 2 10 3 2 10 1 6 5 4 3 2 10 1 2 10 3 2 10 1 4 3 2 10 1 2 10 For example, fib(5) is re-calculated 3 times. Introduction to Programming© Dept. CS, UPC19

20 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

21 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)

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

23 Fibonacci numbers Complexity: O(log n) Introduction to Programming© Dept. CS, UPC23

24 Fibonacci numbers typedef vector > M2x2; // Pre: A and B are 2x2 integer matrices // Returns AB 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

25 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

26 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

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

28 Fibonacci numbers and golden ratio Introduction to Programming© Dept. CS, UPC28

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

30 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


Download ppt "The power of logarithmic computations Jordi Cortadella Department of Computer Science."

Similar presentations


Ads by Google