Download + A[1] - Computer Science Department

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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