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

Slides:



Advertisements
Similar presentations
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Advertisements

Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F.
Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
Fibonacci Sequences Susan Leggett, Zuzana Zvarova, Sara Campbell
FIBONACCI Lecture 23 CS2110 – Spring 2015 Fibonacci (Leonardo Pisano) ? Statue in Pisa Italy And recurrences.
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
ICS103 Programming in C Lecture 11: Recursive Functions
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
Recursion!. Can a method call another method? YES.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Dynamic Programming From An Excel Perspective. Dynamic Programming From An Excel Perspective Ranette Halverson, Richard Simpson, Catherine Stringfellow.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Recursion.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Matrices Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Gaussian elimination Jordi Cortadella Department of Computer Science.
Recursion Jordi Cortadella Department of Computer Science.
Approximate computations Jordi Cortadella Department of Computer Science.
Sequences Jordi Cortadella Department of Computer Science.
Jordi Cortadella Dept. of Computer Science, UPC
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 9. Intermezzo.
Polynomials Jordi Cortadella Department of Computer Science.
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
Sudoku Jordi Cortadella Department of Computer Science.
First steps Jordi Cortadella Department of Computer Science.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Introduction to Programming (in C++) Complexity Analysis of Algorithms
Reusing computations Jordi Cortadella Department of Computer Science.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Numerical algorithms Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Foundations of Algorithms, Fourth Edition
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Vectors Jordi Cortadella Department of Computer Science.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
Functions Jordi Cortadella Department of Computer Science.
FIBONACCI NUMBERS AND RECURRENCES Lecture 26 CS2110 – Spring 2016 Fibonacci (Leonardo Pisano) ? Statue in Pisa Italy.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Approximate computations
Computer Science 4 Mr. Gerb
Reasoning with invariants
Fibonacci numbers and recurrences
For loops, nested loops and scopes
Fibonacci numbers Golden Ratio, recurrences
Fibonacci numbers Golden Ratio, recurrences
Approximate computations
Fibonacci numbers Golden Ratio, recurrences
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Jordi Cortadella Department of Computer Science
Jordi Cortadella Department of Computer Science
Jordi Cortadella Department of Computer Science
CS201: Data Structures and Discrete Mathematics I
Complexity Analysis of Algorithms
Search algorithms for vectors
Jordi Cortadella Department of Computer Science
Fibonacci numbers Golden Ratio, recurrences
CSC 380: Design and Analysis of Algorithms
Dynamic Programming Steps.
Presentation transcript:

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

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