You are on page 1of 23

Algorithms, Data Structures

and Computability
Search
Introduction
Searching is the action of locating a particular item from among a mass of other items.
Present-day computer systems are massively engaged in search. In this unit, we will once
again show how the right algorithm, coupled with the right data structure, can make
processes that would otherwise be hopelessly slow much more efficient.

You learned how naive techniques for searching linear data structures can be vastly improved
by means of algorithms such as binary search , which can drastically reduce search times,
reducing the complexity from O(n) (for sequential search) to O(log n).

We show how efficient string-searching algorithms, in particular the KMP algorithm , can be
used to match specimens against items in very large DNA databases.

A common method of making efficient search possible is to calculate a special key for every
item in the data set – a procedure known as hashing – and to store each item at an address
in a data structure known as a hash table .

In further case studies, we come back to the ADT binary tree and the more specific form of
this, the ADT AVL tree , showing how these can be used in search problems. We will explore
methods of improving efficiency by keeping trees in balance, examine the algorithms for
doing so, and perform complexity analyses on these.
Agenda
❑ Linear Search
❑ Binary Search
❑ Quick Select Part 1
❑ Hashing and hash tables

❑ Basic string search


❑ The Knuth–Morris–Pratt algorithm
Part 2
❑ Search trees
▪ Binary search trees
▪ AVL trees
Linear Search

Linear search is a very simple search algorithm. In this type of search, a sequential search
is made over all items one by one. Every item is checked and if a match is found then that
particular item is returned, otherwise the search continues till the end of the data
collection. O(n)

LinearSearch ( Array A, Value x)


Pseudo code Step 1: Set i to 1
procedure linearSearch (list, value) Step 2: if i > n then go to step 7
for each item in the list Step 3: if A[i] = x then go to step 6
if match item == value Step 4: Set i to i + 1
return the item's location Step 5: Go to Step 2
end if Step 6: Print Element x Found at index i and go to step 8
end for Step 7: Print element not found
end procedure Step 8: Exit
Binary search

Binary search is a fast search algorithm with run-time complexity of Ο(log n).
The search algorithm works on the principle of divide and conquers. For this
algorithm to work properly, the data collection should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item
of the collection. If a match occurs, then the index of item is returned. If the
middle item is greater than the item, then the item is searched in the sub-
array to the left of the middle item. Otherwise, the item is searched for in the
sub-array to the right of the middle item. This process continues on the sub-
array as well until the size of the subarray reduces to zero.
How Binary Search Works?
For a binary search to work, it is mandatory for the target array to be sorted. We
shall learn the process of binary search with a pictorial example. The following is
our sorted array and let us assume that we need to search the location of value 31
using binary search.

First, we shall determine half of the array by using this formula −


mid = low + (high - low) / 2
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.

Now we compare the value stored at location 4, with the value being searched, i.e.
31. We find that the value at location 4 is 27, which is not a match. As the value is
greater than 27 and we have a sorted array, so we also know that the target value
must be in the upper portion of the array.
We change our low to mid + 1 and find the new
mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value
stored at location 7 with our target value 31.

The value stored at location 7 is not a


match, rather it is more than what we are
looking for. So, the value must be in the
lower part from this location.

Hence, we calculate the mid again.


This time it is 5.

We compare the value stored at


location 5 with our target value. We find
that it is a match.

We conclude that the target value 31 is stored at location 5.


Binary search halves the searchable items and thus reduces the count of
comparisons to be made to very less numbers.
The pseudo code of binary search algorithms

Analysis of the algorithm


The maximum number of
comparisons is logarithmic
with respect to the number of
items in the list. Therefore, the
binary search is O(log n)
Binary Search in Python
simple implementation Binary Search in Python
recursive version
Quickselect Algorithm:
Right after learning the Quicksort Algorithm in one of my algorithms classes we learned
the Quickselect Algorithm. The Quickselect Algorithm is a selection algorithm, whereas
the Quicksort Algorithm is a sorting algorithm. The really interesting part about the
Quickselect Algorithm is that it is essentially the Quicksort Algorithm, except that one only
needs to recursively call only one side of the partition to be re-partitioned. This changes
the best case runtime from O(nlogn) to O(n).

The problem is that we have an unordered list of items and want to find the k-th smallest
item. We know we can find the item easily by sorting it first, which will take O(nlogn) to sort
the list and then O(1) to find it. If you will need to do many k-th item lookups you may be
better off just sorting the list, caching it, and then performing many O(1) lookups.
However, if this is not the case, the Quickselect Algorithm will find the kth-smallest item in
an unordered list with a best case runtime of O(n).

The code is almost identical to the Quicksort Algorithm in Python, except we are only
recursively partitioning one side of the list and a few other minor differences.
Quick-Select
• Quick-select is a randomized selection x
algorithm based on the prune-and-search
paradigm:
• Prune: pick a random element x
(called pivot) and partition S into x
• L: elements less than x
L E G
• E: elements equal x
k > |L|+|E|
k < |L|
• G: elements greater than x k’ = k - |L| - |E|
• Search: depending on k, either answer
is in E, or we need to recur in either L |L| < k < |L|+|E|
(done)
or G
Partition
• We partition an input sequence as in Algorithm partition(S, p)
Input sequence S, position p of pivot
the quick-sort algorithm:
Output subsequences L, E, G of the
• We remove, in turn, each element y elements of S less than, equal to,
from S and or greater than the pivot, resp.
L, E, G  empty sequences
• We insert y into L, E or G, depending on x  S.remove(p)
the result of the comparison with the while S.isEmpty()
pivot x y  S.remove(S.first())
• Each insertion and removal is at the if y < x
L.addLast(y)
beginning or at the end of a else if y = x
sequence, and hence takes O(1) time E.addLast(y)
• Thus, the partition step of quick- else { y > x }
G.addLast(y)
select takes O(n) time return L, E, G
Here is example code for the
Quickselect Algorithm in Python

Here is a short Python Script to test the


Quickselect Algorithm.
Hash Table is a Data Structure

Hash Table is a data structure which stores data in an associative manner. In a


hash table, data is stored in an array format, where each data value has its own
unique index value. Access of data becomes very fast if we know the index of the
desired data.

Thus, it becomes a data structure in which insertion and search operations are
very fast irrespective of the size of the data. Hash Table uses an array as a
storage medium and uses hash technique to generate an index where an
element is to be inserted or is to be located.
We can implement a hash table by using a list with each element initialized to the special
Python value None. The figure below shows a hash table of size m=11. In other words,
there are m slots in the table, named 0 through 10.

The mapping between an item and the slot where that item belongs in the hash table is
called the hash function. The hash function will take any item in the collection and
return an integer in the range of slot names, between 0 and m-1. Assume that we have
the set of integer items 54, 26, 93, 17, 77, and 31. Our first hash function, sometimes
referred to as the “remainder method,” simply takes an item and divides it by the table
size, returning the remainder as its hash value (h(item)=item%11).

Note that 6 of the 11 slots are now occupied. This is referred to as the load factor, and is
commonly denoted by λ=numberofitems/tablesize. For this example, λ=6/11

Visualize the Hashing:


https://visualgo.net/en/hashtable
Issues with Hashing

• Multiple keys can hash to the same slot – collisions are possible.
• Design hash functions such that collisions are minimized.
• But avoiding collisions is impossible.
• Design collision-resolution techniques.

• Search will cost Ө(n) time in the worst case.


• However, all operations can be made to have an expected complexity of
Ө(1).
Methods of Resolution
• Open Addressing: linear Probing
• All elements stored in hash table itself.
• When collisions occur, use a systematic
(consistent) procedure to store elements in free
slots of the table.
• Chaining:
• Store all elements that hash to the same slot in a
0
k1 k4

linked list. k5 k2 k6

• Store a pointer to the head of the linked list in the k7


k8
k3

hash table slot. m–1


Linear Probing

As we can see, it may happen that the hashing technique is used to


create an already used index of the array. In such a case, we can
search the next empty location in the array by looking into the next cell
until we find an empty cell. This technique is called linear probing.
open addressing or linear probing:
is to find the next open slot or address in the hash table. By systematically visiting each
slot one at a time. The figure below shows an extended set of integer items under the
simple remainder method hash function (54,26,93,17,77,31,44,55,20). When we
attempt to place 44 into slot 0, a collision occurs. Under linear probing, we look
sequentially, slot by slot, until we find an open position. In this case, we find slot 1.
Again, 55 should go in slot 0 but must be placed in slot 2 since it is the next open
position. The final value of 20 hashes to slot 9. Since slot 9 is full, we begin to do linear
probing. We visit slots 10, 0, 1, and 2, and finally find an empty slot at position 3.

Once we have built a hash table using open addressing and linear probing, it is essential
that we utilize the same methods to search for items. Assume we want to look up the item
93. When we compute the hash value, we get 5. Looking in slot 5 reveals 93, and we can
return True. What if we are looking for 20? Now the hash value is 9, and slot 9 is currently
holding 31. We cannot simply return False since we know that there could have been
collisions. We are now forced to do a sequential search, starting at position 10, looking
until either we find the item 20 or we find an empty slot.
Chaining:
An alternative method for handling the collision problem is to allow each slot to hold a
reference to a collection (or chain) of items. Chaining allows many items to exist at the
same location in the hash table. When collisions happen, the item is still placed in the
proper slot of the hash table. As more and more items hash to the same location, the
difficulty of searching for the item in the collection increases. The figure below shows the
items (54,26,93,17,77,31,44,55,20) as they are added to a hash table that uses chaining
to resolve collisions.
When we want to search for an
item, we use the hash function to
generate the slot where it should
reside. Since each slot holds a
collection, we use a searching
technique to decide whether the
item is present. The advantage is
that on the average there are likely
to be many fewer items in each slot,
so the search is perhaps more
efficient. We will look at the analysis
for hashing at the end of this
section.
Performance of Hashing

We stated earlier that in the best case hashing would provide a O(1), constant
time search technique. However, due to collisions, the number of
comparisons is typically not so simple. Even though a complete analysis of
hashing is beyond the scope of this text, we can state some well-known
results that approximate the number of comparisons necessary to search for
an item.
Thank You

You might also like