L13b Fibonacci Serach
L13b Fibonacci Serach
(Leonardo Pisano)
1170-1240?
Statue in Pisa Italy
FIBONACCI NUMBERS
GOLDEN RATIO,
RECURRENCES
Fibonacci function
But sequence described
2
much earlier in India:
fib(0) = 0
fib(1) = 1 Virahaṅka 600–800
fib(n) = fib(n-1) + fib(n-2) for n ≥ 2 Gopala before 1135
Hemacandra about 1150
0, 1, 1, 2, 3, 5, 8, 13, 21, … The so-called Fibonacci
numbers in ancient and
medieval India.
Parmanad Singh, 1985
Fibonacci function (year 1202)
3
fib(0) = 0
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2) for n ≥ 2
/** Return fib(n). Precondition: n ≥ 0.*/
public static int f(int n) {
if ( n <= 1) return n;
return f(n-1) + f(n-2);
}
Find the golden ratio when we divide a line into two parts such
that
whole length / long part == long part / short part
a b
(a + b) / a = a / b Solution is called Φ
Golden ratio Φ = 1.61803398…(Divine
proportion)
5
Find the golden ratio when we divide a line into two parts a and
b such that
(a + b) / a = a / b =Φ
Find the golden ratio when we divide a line into two parts a and
b such that
(a + b) / a = a / b =Φ
a/b
8/5 = 1.6
Golden 13/8 = 1.625…
rectangle a 21/13= 1.615…
34/21 = 1.619…
a b 55/34 = 1.617…
360/1.6180339887… = 222.492235…
0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …
fibonacci and bees
11
MB 1
Male bee has only a mother
Female bee has mother and father
FB 1
The number of FB MB 2
ancestors at any
level is a FB MB FB 3
Fibonnaci
number FB MB FB FB MB 5
FB MB FB FB MB FB MB FB 8
You want to grow your leaves so that they all get a good
amount of sunlight. You decide to grow them at
successive angles of 180 degrees
You want to grow your leaves so that they all get a good
amount of sunlight. 90 degrees, maybe?
Where does the
fifth leaf go?
Fibonacci in nature
14 The artichoke uses the
Fibonacci pattern to spiral the
sprouts of its flowers.
Fibonacci search
Fibonacci heap data strcture
Fibonacci cubes: graphs used for
interconnecting parallel and
distributed systems
Fibonacci search of sorted b[0..n-
1]
16
0 e1
__________________ n 0 e1
__________________ 144
e1 = (n-0)/2 e1 = 0 + 89
0 e2
_________ e1 0 e2
___________ e1
e2 = (e1-0)/2 e2 = 0 + 55
e2
_____ e1 e2
_______e1
2 3 5 8 13 21 34 55 89 144
LOUSY WAY TO COMPUTE:
fib(n)
17
19 18
18 17 17 16
17 16 16 15 16 15 15 14
Recursion for fib: f(n) = f(n-1) + f(n-2)
18
ϕ2 = ϕ + 1 so ϕ = (1 + sqrt(5)) /2 = 1.618
…
1.618….
ratio of sum of sides to longer side
a 1
=
b
ratio of longer side to shorter side
Linear algorithm to calculate fib(n)
20
21
Step 1 − As the first step, find the immediate Fibonacci number that is greater than or equal to the size of
the input array. Then, also hold the two preceding numbers of the selected Fibonacci number, that is, we
hold Fm, Fm-1, Fm-2 numbers from the Fibonacci Series.
Step 2 − Initialize the offset value as -1, as we are considering the entire array as the searching range in
the beginning.
to the index of this element. The Fibonacci numbers are also updated with F m = Fm-2.
But if the key element is greater than the element at this index, we remove the elements before this
element from the search range. The Fibonacci numbers are updated as F m = Fm-1. The offset value is set
to the index of this element.
Step 4 − As there are two 1s in the Fibonacci series, there arises a case where your two preceding
numbers will become 1. So if Fm-1 becomes 1, there is only one element left in the array to be searched.
We compare the key element with that element and return the 1st index. Otherwise, the algorithm returns
an unsuccessful search.
Example
22
Step 1
The size of the input array is 10.
The smallest Fibonacci number greater than 10 is 13.
Therefore, F = 13, F = 8, F = 5.
m m-1 m-2
We initialize offset = -1
23
Step 2
In the first iteration, compare it with the element at index =
minimum (offset + F , n – 1) = minimum (-1 + 5, 9) = minimum
m-2
(4, 9) = 4.
The fourth element in the array is 20, which is not a match and
is less than the key element.
24
Step 3
In the second iteration, update the offset value and the Fibonacci numbers.
Since the key is greater, the offset value will become the index of the
element, i.e. 4. Fibonacci numbers are updated as F m = Fm-1 = 8.
Fm-1 = 5, Fm-2 = 3.
Now, compare it with the element at index = minimum (offset + F m-2, n – 1) =
minimum (4 + 3, 9) = minimum (7, 9) = 7.
Element at the 7th index of the array is 43, which is not a match and is also
lesser than the key.
25
Step 4
We discard the elements after the 7th index, so n = 7 and offset value remains 4.
Fibonacci numbers are pushed two steps backward
Fm = Fm-2 = 3.
Fm-1 = 2, Fm-2 = 1.
Now, compare it with the element at index = minimum (offset + Fm-2, n – 1) = minimum
(4 + 1, 6) = minimum (5, 7) = 5.
The element at index 5 in the array is 24, which is our key element. 5th index is returned
as the output for this example array.
The output is returned as 5.
The concept (behind Binary/Fibonacci search)
26
27
28