Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views9 pages

Search - Fibonacci Search Algorithm

Uploaded by

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

Search - Fibonacci Search Algorithm

Uploaded by

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

SEARCH ALGORITHM:

FIBONACCI SEARCH ALGORITHM

I. Introduction
II. Fibonacci Search Algorithm
III. Pseudocode of the Algorithm
IV. Step-by-step Example
V. Sample Code for the Algorithm
VI. Advantages and Disadvantages
VII. Conclusion
VIII. References

Design and Analysis of Algorithms (CS243-M)


Prof. Jan Eilbert L. Lee

Eric Jerard A. Collantes


BSCS – 2A
I. Introduction
The Fibonacci sequence consists of a sequence of numbers with the two
fundamental numbers, 0 and 1. The succeeding numbers in the series are the total of
the two numbers that came before them. This Fibonacci search algorithm utilizes these
Fibonacci numbers to search for a certain element in a sorted dataset (Fibonacci Search
Algorithm, n.d.). Similar to the Binary search method, the Fibonacci search method is a
comparison-based algorithm that relies on the divide and conquer strategy (Fibonacci
Search, 2023). The fundamental idea behind the Fibonacci series is eliminating the least
number of locations where the element could be found.

II. Fibonacci Search Algorithm


The Fibonacci search algorithm is a dynamic programming-based, comparison-
based search method. This builds a search tree using the Fibonacci numbers and
locates the key within the tree. The search effectively narrows the search interval by
utilizing the properties of Fibonacci numbers to reduce the array size in each step. The
process is repeated until the desired element is located or the interval reduces to zero.
This search algorithm consists of four main steps:

Step 1: Find a Fibonacci number greater than or equal to the size of the given array in
which we must search for the key. If the array is size n, we should find a Fibonacci
number Fk such that Fk is greater than or equal to n.

Step 2: Compute the two preceding numbers of the selected Fibonacci number Fk,
which are the Fk-1 and Fk-2. Also, initialize the offset value as -1 since the whole array is
this algorithm's search range.

Step 3: Compare the key or the element to be found with the element at index
[min(offset + Fk-2, n-1)]. We will obtain one of the following three results from this
comparison:

1. If a match is found, return the index.


2. If the key is less than the element at the computed index, we narrow down the
search range from 0 to this element's index. The Fibonacci numbers are shifted
back twice, Fk = Fk-2.
3. If the key is greater than the element at the computed index, we narrow down the
search range from this element to the end of the array. The offset value is also
set to the index of this element. The Fibonacci numbers are shifted back once, Fk
= Fk-1.
4. Repeat steps 1 to 3 as long as Fk-2 is greater than or equal to 0. Since the
Fibonacci series has two 1s, there is a chance that the two preceding numbers
will also become 1. Then, just one element remains in the array that needs to be
checked to see if Fk-1 becomes 1. We return the first index after comparing that
element with the key. The program delivers an unsuccessful search otherwise.

Thus, we can observe that the size of array n is decreased by either 1/3 or 2/3 of the
array. The Fibonacci Search algorithm searches for elements with a logarithmic
time complexity. Given its divide-and-conquer approach and resemblance to the concept
of binary search, this algorithm takes O(log n) time to run in the worst-case scenario.

Case Complexity

Best Ω(1)
Average θ(logn)
Worst O(logn)

III. Pseudocode of the Algorithm


3.1 Fibonacci Search Algorithm (Fibonacci Search, 2023a)

Input: Sorted array arr


Input: Element to be searched key
Output: Index of key in dataset or -1

// Initialize Fk-2, Fk-1, and Fk


Fk-2 = 0
Fk-1 = 1
Fk = Fk-2 + Fk-1
while Fk ≤ n do
Fk-2 = Fk-1
Fk-2 = Fk
Fk = Fk-1 + Fk-2
end while
// Initialize offset to -1
offset = -1
while Fk-2 ≥ 0 do
index = min(offset + Fk-2, n – 1)
if key < arr[index] then
Fk = Fk-2
Fk-1 = Fk-1 - Fk-2
Fk-2 = Fk - Fk-1
end if
if key > arr[index] then
Fk = Fk-1
Fk-1 = Fk-2
Fk-2 = Fk - Fk-1
offset = index
end if
else
return index
end else

// Compare the last element of the array with key


if Fk-1 == 1 && arr[offset + 1] == key then
return offset + 1
end if

// key not found in array


return -1
end while

IV. Step-by-step Example


Using this following array, search to find key 99. Blue-shaded cells will represent the
search range and cells shaded with the color orange represent the element where the
key will be compared to (Fibonacci Search, 2023a).

4 16 29 36 47 55 67 88 99 101 119 124

Step 1: Starting with the full array with size 12, n = 12, and offset = -1. Given the size of
the array which is n = 12, therefore, the smallest Fibonacci number that is greater than
or equal to 12 is 13, Fk = 13. Knowing the main Fibonacci number, the two preceding
ones are Fk-1 = 8 and Fk-2 = 5. Then, we compute for the index, which is:

index = min(offset + Fk-2, n – 1)


index = min((-1 + 5), 11)
index = 4

4 16 29 36 47 55 67 88 99 101 119 124

Step 2: Knowing the index, we proceed on comparing the key to the element at index 4,
which is 47. Since 99 > 47, we shift our Fibonacci numbers down by 1 and set the value
of index to the offset. We disregard the numbers on the left side of the element at index
4.
Fk = 8, Fk-1 = 5, Fk-2 = 3
offset = 4

4 16 29 36 47 55 67 88 99 101 119 124

Step 3: Compute the value of the next index:


index = min(offset + Fk-2, n – 1)
index = min((4 + 3), 11)
index = 7

4 16 29 36 47 55 67 88 99 101 119 124

Step 4: We proceed on comparing the key to the element at index 7, which is 88. Since
99 > 88, we shift our Fibonacci numbers down by 1 again and set the value of index to
the offset. We disregard the numbers on the left side of the element at index 7.
Fk = 5, Fk-1 = 3, Fk-2 = 2
offset = 7

4 16 29 36 47 55 67 88 99 101 119 124

Step 5: Compute the value of the next index:


index = min(offset + Fk-2, n – 1)
index = min((7 + 2), 11)
index = 9

4 16 29 36 47 55 67 88 99 101 119 124

Step 6: We proceed on comparing the key to the element at index 9, which is 101. Since
99 < 101, we shift our Fibonacci numbers down by 2. We disregard the numbers on the
right side of the element at index 9.
Fk = 2, Fk-1 = 1, Fk-2 = 1

4 16 29 36 47 55 67 88 99 101 119 124

Step 5: Compute the value of the next index:


index = min(offset + Fk-2, n – 1)
index = min((7 + 1), 11)
index = 8

4 16 29 36 47 55 67 88 99 101 119 124

The element at index 8 is 99. Since 99 == 99, the algorithm returns the index and
terminates.

V. Sample Code for the Algorithm


5.1 Fibonacci Search Algorithm in Python (Fibonacci Search Algorithm, n.d.)
1 def fibonacci_search(arr, n, key):
2 offset = -1
3 Fk2 = 0
4 Fk1 = 1
5 Fk = Fk2 + Fk1
6 while (Fm ≤ n):
7 Fk2 = Fk1
8 Fk1 = Fk
9 Fk = Fk2 + Fk1
10 while (Fk2 ≥ 0):
11 index = min(offset + Fk2, n - 1)
12 if (key < arr[index]):
13 Fk = Fk2
14 Fk1 = Fk1 - Fk2
15 Fk2 = Fk - Fk1
16 elif (key > arr[index]):
17 Fk = Fk1
18 Fk1 = Fk2
19 Fk2 = Fk - Fk1
20 offset = index
21 else:
22 return index
23 if (Fk1 == 1 and arr[offset + 1] == key):
24 return offset + 1
25
26 return -1
27 arr = [4, 16, 29, 36, 47, 55, 67, 88, 99, 101, 119, 124]
28 print("Array elements are: ")
29 for i in range(len(arr)):
30 print(arr[i], end = " ")
31
32 n = len(arr);
33 key = 99
34 print("\nThe element to be searched:", key)
35 index = fibonacci_search(arr, n, key)
36
37 if(index >= 0):
38 print("The element is found at index: ", index)
39 else:
40 print("Unsuccessful search!")

VI. Advantages and Disadvantages


Advantages of Fibonacci Search Disadvantages of Fibonacci Search
• Efficient for Large Sorted Arrays: • Limited Applicability: Fibonacci
Fibonacci search offers efficient search is only applicable to sorted
performance comparable to or better arrays.
than binary search for large and sorted • Complex Calculation: The calculation
arrays. of Fibonacci numbers can be
• No Division Operations: It avoids computationally expensive, especially
costly division operations by relying for large values of n.
only on addition and subtraction. • Recursion Overhead: Fibonacci
• Predictable Interval Sizes: The use search uses recursion, which can
of Fibonacci numbers ensures introduce overhead in terms of
predictable and optimized interval memory usage and function calls.
sizes. • Limited Range: Fibonacci numbers
• Natural Fit for Sequential Memory grow exponentially, limiting the efficient
Access: Its access pattern can reduce search range and potentially causing
cache misses compared to binary overflow errors for very large arrays.
search.
• Not Suitable for Real-Time
• Balanced Search Tree Structure:
Applications: Its computational
Fibonacci search inherently creates a
complexity and recursion overhead
balanced search tree structure, leading
make it unsuitable for real-time
to consistent performance across applications requiring fast response
different datasets. times.

VII. Conclusion
Compared to addition and subtraction, division and multiplication are more
expensive processes. The search space is reduced by either two-thirds or one-third
using Fibonacci Search. Binary search, however, always reduces the search space by
half. Using Fibonacci numbers to reduce intervals effectively, the Fibonacci search
method is an effective alternative for binary search in sorted and larger arrays. Because
of its complexity and modest performance benefits, it may not always be the most
optimal strategy, even though it has some advantages, especially in specific
circumstances. When selecting Fibonacci search over alternative search algorithms, it is
crucial to carefully analyze the features of the dataset and the particular requirements of
the application.
VIII. References
Fibonacci Search. (2023, June 13). Baeldung on Computer Science. Retrieved May 20,
2024, from https://www.baeldung.com/cs/fibonacci-search

Fibonacci search. (2023, October 11). GeeksforGeeks. Retrieved May 20, 2024, from
https://www.geeksforgeeks.org/fibonacci-search/

Fibonacci Search Algorithm. (2024, March 16). Computer Geek. Retrieved May 20,
2024, from https://compgeek.co.in/fibonacci-search-algorithm/

Fibonacci Search Algorithm. (n.d.).


https://www.tutorialspoint.com/data_structures_algorithms/fibonacci_search.htm#
:~:text=As%20the%20name%20suggests%2C%20the,in%20a%20sorted%20inp
ut%20array.&text=0%2C%201%2C%201%2C%202,the%20element%20could%2
0be%20found.

What are the disadvantages of Fibonacci search? (2024, February 13). GeeksforGeeks.
Retrieved May 20, 2024, from https://www.geeksforgeeks.org/what-are-the-
disadvantages-of-fibonacci-search/

You might also like