0% found this document useful (0 votes)
2 views

L13b Fibonacci Serach

The document discusses Fibonacci numbers, their mathematical properties, and their historical context, particularly in relation to Leonardo Pisano and ancient Indian mathematicians. It explains the Fibonacci sequence, the golden ratio, and their applications in nature and computer science, including algorithms like Fibonacci search. Additionally, it highlights the connection between Fibonacci numbers and the golden ratio in various aspects of art and nature.

Uploaded by

rv391939
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

L13b Fibonacci Serach

The document discusses Fibonacci numbers, their mathematical properties, and their historical context, particularly in relation to Leonardo Pisano and ancient Indian mathematicians. It explains the Fibonacci sequence, the golden ratio, and their applications in nature and computer science, including algorithms like Fibonacci search. Additionally, it highlights the connection between Fibonacci numbers and the golden ratio in various aspects of art and nature.

Uploaded by

rv391939
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Fibonacci

(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);
}

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55


Golden ratio Φ = (1 + √5)/2 =
1.61803398…
4

Find the golden ratio when we divide a line into two parts such
that
whole length / long part == long part / short part

Call long part a and short part b

a b

(a + b) / a = a / b Solution is called Φ
Golden ratio Φ = 1.61803398…(Divine
proportion)
5

The ideal human figure has its navel


at the golden ratio ( about 1.618).

Dividing the body in the ratio of 0.618 to 0.382


(soles of feet to navel : navel to top of head)
Golden ratio Φ = (1 + √5)/2 =
1.61803398…
6

Find the golden ratio when we divide a line into two parts a and
b such that
(a + b) / a = a / b =Φ

a ratio of two numbers in


which the ratio of the
sum to the larger number
a Golden rectangle is the same as the ratio
of the larger number to
the smaller : golden
a b section

Throughout history, the ratio for length to width of rectangles of


1.61803 39887 49894 84820 has been considered the most
pleasing to the eye.
Golden ratio Φ = (1 + √5)/2 =
1.61803398…
7

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…

For successive Fibonacci numbers a, b , a/b is close to Φ


but not quite it Φ . 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …
Find fib(n) from fib(n-1)
8

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55


Since fib(n) / fib(n-1) is close to the golden ratio. In fact,

limit f(n)/fib(n-1) = golden ratio


n -> ∞

You can see that (golden ratio) * fib(n-1) is close to fib(n)

We can use this formula to calculate fib(n) from fib(n-1)

Golden ratio and Fibonacci numbers: inextricably linked


Fibonacci, golden ratio, golden
angle
9

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

limit f(n)/fib(n-1) = golden ratio = 1.6180339887


n -> ∞

360/1.6180339887… = 222.492235…

360 – 222.492235… = 137.5077 golden angle


Fibonacci function (year 1202)
10

Downloaded from wikipedia

Fibonacci tiling Fibonacci spiral

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

MB: male bee, FB: female bee


Suppose you are a plant
12

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

Pretty stupid plant!


The two bottom leaves get VERY little sunlight!
Suppose you are a plant
13

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.

360/(golden ratio) = 222.492

The artichoke sprouts its leafs at a constant amount of rotation:


222.5 degrees (in other words the distance between one leaf and
the next is 222.5 degrees).
Uses of Fibonacci sequence in CS
15

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

binary search: Fibonnacci search: (n = 144)


cut in half at each step cut by Fibonacci numbers

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

/** Return fib(n). Precondition: n ≥ 0.*/


public static int f(int n) {
if ( n <= 1) return n; Calculates f(15) 8 times!
return f(n-1) + f(n-2); What is complexity of f(n)?
}
20

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

T(0) = a T(n): Time to calculate f(n)


T(1) = a Just a recursive function
T(n) = a + T(n-1) + T(n-2) “recurrence relation”

We can prove that T(n) is O(2n)

It’s a “proof by induction”.


Proof by induction is not covered in this course.
The golden ratio
19

a > 0 and b > a > 0 are in the golden ratio if

(a + b) / b = b/a call that value ϕ

ϕ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

/** Return fib(n), for n >= 0. */


public static int f(int n) {
if (n <= 1) return 1;
int p= 0; int c= 1; int i= 2;
// invariant: p = fib(i-2) and c = fib(i-1)
while (i < n) {
int fibi= c + p; p= c; c= fibi;
i= i+1;
}
return c + p;
}
Fibonacci Search Algorithm

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.

Step 3 − Until Fm-2 is greater than 0, we perform the following steps −


Compare the key element to be found with the element at index [min(offset+Fm-2,n-1)]. If a match is
found, return the index.
If the key element is found to be lesser value than this element, we reduce the range of the input from 0

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

The Fibonacci search algorithm uses the divide and conquer


strategy to eliminate the search spaces that are not likely to
contain the required element.
This elimination is done with the help of the Fibonacci
numbers to narrow down the search range within an input
array.
Implementation

27
28

You might also like