Docsity

Hashing functionality, Lecture notes of Computer Architecture and Organization

Toggle sidebar
bg1
Hashing:
◊
Hashing in the data structure is a technique of mapping a large chunk of data into small tables using a hashing function. It is done for faster access to elements. The efficiency of mapping
depends on the efficiency of the hash function used.
Hashing Key
is the raw data that has to be hashed in a hash table. The hashing algorithm carries out a function to translate the hash keyinto the hash value. The outcome of feeding the
hash key through the hashing algorithm is what is known as the hash value.
Hash Key = Key Value % Number of Slots in the Table
In hashing, large keys are converted into small keys by using
hash functions.
A hash function is any function that can be used to map a data set of an arbitrary size to a data set of a
fixed size, which falls into the hash table. The values returned by a hash function are called hash values, hash codes, hash sums, or simply hashes.
To achieve a good hashing mechanism, It is important to have a good hash function with the following basic requirements:
Easy to compute: It should be easy to compute and must not become an algorithm in itself.
Uniform distribution: It should provide a uniform distribution across the hash table and should not result in clustering.
b.
Less collisions: Collisions occur when pairs of elements are mapped to the same hash value. These should be avoided.
c.
The key-values pairs stored in a data structure called
hash tables
.
Types of Hash function:
Divison method:
○
The division method is the simplest and easiest method used to generate a hash value. In this hash function, the value of k is divided by M and uses the remainder as obtained.
Formula-
h(K) = k mod M
◊
(where k = key value and M = the size of the hash table)
Example -
k = 1320
M = 11
h (1320) = 1320 mod 11
= 0
Advantages -
This method works well for any value of M
1.
The division approach is extremely quick because it only calls for one operation.
2.
Disadvantages -
This may lead to poor performance as consecutive keys are mapped to consecutive hash values in the hash table
1.
There are situations when choosing the value of M requires particular caution.
2.
Mid square method
○
Folding method
○
Multiplication method
○
Collision:
◊
It is a situation in which the hash function returns the same hash key for more than one record,
it is called as collision. The collision creates a problem because each index in a hash table is
supposed to store only one value.
For E.g.
If using the Division method: Let’s say the array size=10
Then Hash(15) = 15%10 = 5
And Hash(25) = 25%10 = 5
So this hash function will return 5 as the index for both of these keys.
This is called a Collision, where the resultant hashes of 2 or more Keys, wrongly map to the
same place in the hash table.
Types of Hashing:
Open hashing(Closed addressing):
○
◊
Open hashing is used to avoid any hash collisions, using an array of linked lists in order to
resolve the collision. In this hashing technique key-values are stored using
separate chaining
method.
Separate chaining is one of the most commonly used collision resolution techniques. It is
usually implemented using linked lists. In separate chaining, each element of the hash table
is a linked list. To store an element in the hash table you must insert it into a specific linked
list. When two or more elements are hash to the same location, these elements are
represented into a singly-linked list like a chain.
Problems on separate chaining:
Advantages
Separate Chaining is one of the simplest methods to implement and understand.
1.
We can add any number of elements to the chain.
2.
It is frequently used when we don't know about the number of elements and the number of
3.
Quadratic probing:
When the collision occurs, using the Quadratic
Probing technique, to find the empty cell to place this Data at, here the
next slot is computed by adding the successive value of an arbitrary
polynomial in the original hashed index.
Quadratic probing uses a hash function of the form
h(k, i) = (h' (k) + c1i + c2i2) mod m
where h' is an auxiliary hash function, c1and c2are positive
auxiliary constants, and i = 0, 1 … m
-
1. The initial position
probed is T [h'(k)], later positions probed are offset by amounts that
depend in a quadratic manner on the probe number i.
Problems on quadratic probing:
▪
○
Secondary clustering
is the tendency for a collision resolution scheme
such as quadratic probing to create long runs of filled slots away from the
hash position of keys.
If the primary hash index is x, probes go to x+1, x+4, x+9, x+16, x+
25 and so on, this results in Secondary Clustering.
Secondary clustering is less severe in terms of performance hit than
primary clustering, and is an attempt to keep clusters from forming by
using Quadratic Probing. The idea is to probe more widely separated
cells, instead of those adjacent to the primary hash site.
Double hashing:
Double hashing offers us one of the best techniques
for hashing with open addressing. The permutation formed by double
hashing is like a random permutation therefore it almost eliminates the
chances of cluster forming as it uses a secondary hash function as an
offset to deal with collision condition.
○
HASHING
29 December 2022
11:52
DAA Page 1
pf2

Partial preview of the text

Download Hashing functionality and more Lecture notes Computer Architecture and Organization in PDF only on Docsity!

◊ Hashing:

Hashing in the data structure is a technique of mapping a large chunk of data into small tables using a hashing function. It is done for faster access to elements. The efficiency of mapping depends on the efficiency of the hash function used.

Hashing Key is the raw data that has to be hashed in a hash table. The hashing algorithm carries out a function to translate the hash key into the hash value. The outcome of feeding the

hash key through the hashing algorithm is what is known as the hash value. Hash Key = Key Value % Number of Slots in the Table

In hashing, large keys are converted into small keys by using hash functions. A hash function is any function that can be used to map a data set of an arbitrary size to a data set of a

fixed size, which falls into the hash table. The values returned by a hash function are called hash values, hash codes, hash sums, or simply hashes. To achieve a good hashing mechanism, It is important to have a good hash function with the following basic requirements: a. Easy to compute: It should be easy to compute and must not become an algorithm in itself. b. Uniform distribution: It should provide a uniform distribution across the hash table and should not result in clustering. c. Less collisions: Collisions occur when pairs of elements are mapped to the same hash value. These should be avoided.

The key-values pairs stored in a data structure called hash tables.

Types of Hash function:

○ Divison method:

The division method is the simplest and easiest method used to generate a hash value. In this hash function, the value of k is divided by M and uses the remainder as obtained. Formula- h(K) = k mod M

(where k = key value and M = the size of the hash table) Example - k = 1320 M = 11 h (1320) = 1320 mod 11 = 0

Advantages -

  1. This method works well for any value of M
  2. The division approach is extremely quick because it only calls for one operation.

Disadvantages -

  1. This may lead to poor performance as consecutive keys are mapped to consecutive hash values in the hash table
  2. There are situations when choosing the value of M requires particular caution.

○ Mid square method

○ Folding method

○ Multiplication method

◊ Collision:

It is a situation in which the hash function returns the same hash key for more than one record, it is called as collision. The collision creates a problem because each index in a hash table is supposed to store only one value. For E.g. If using the Division method: Let’s say the array size= Then Hash(15) = 15%10 = 5 And Hash(25) = 25%10 = 5 So this hash function will return 5 as the index for both of these keys. This is called a Collision, where the resultant hashes of 2 or more Keys, wrongly map to the same place in the hash table.

Types of Hashing:

○ Open hashing(Closed addressing):

Open hashing is used to avoid any hash collisions, using an array of linked lists in order to

resolve the collision. In this hashing technique key-values are stored using separate chaining

method. Separate chaining is one of the most commonly used collision resolution techniques. It is usually implemented using linked lists. In separate chaining, each element of the hash table is a linked list. To store an element in the hash table you must insert it into a specific linked list. When two or more elements are hash to the same location, these elements are represented into a singly-linked list like a chain.

Problems on separate chaining:

Advantages

  1. Separate Chaining is one of the simplest methods to implement and understand.
  2. We can add any number of elements to the chain.
  3. It is frequently used when we don't know about the number of elements and the number of

Quadratic probing: When the collision occurs, using the Quadratic

Probing technique, to find the empty cell to place this Data at, here the next slot is computed by adding the successive value of an arbitrary polynomial in the original hashed index. Quadratic probing uses a hash function of the form h(k, i) = (h' (k) + c 1 i + c 2 i 2 ) mod m where h' is an auxiliary hash function, c 1 and c 2 are positive

auxiliary constants, and i = 0, 1 … m - 1. The initial position

probed is T [h'(k)], later positions probed are offset by amounts that depend in a quadratic manner on the probe number i. ▪ Problems on quadratic probing:

Secondary clustering is the tendency for a collision resolution scheme

such as quadratic probing to create long runs of filled slots away from the hash position of keys. If the primary hash index is x, probes go to x+1, x+4, x+9, x+16, x+ 25 and so on, this results in Secondary Clustering. Secondary clustering is less severe in terms of performance hit than primary clustering, and is an attempt to keep clusters from forming by using Quadratic Probing. The idea is to probe more widely separated cells, instead of those adjacent to the primary hash site.

Double hashing: Double hashing offers us one of the best techniques

for hashing with open addressing. The permutation formed by double hashing is like a random permutation therefore it almost eliminates the chances of cluster forming as it uses a secondary hash function as an offset to deal with collision condition.

HASHING

29 December 2022 11: DAA Page 1

Advantages

  1. Separate Chaining is one of the simplest methods to implement and understand.
  2. We can add any number of elements to the chain. It is frequently used when we don't know about the number of elements and the number of keys that can be inserted or deleted.

Disadvantages

  1. The keys in the hash table are not evenly distributed.
  2. Some amount of wastage of space occurs.
  3. The complexity of searching becomes O(n) in the worst case when the chain becomes long.

○ Closed hashing(Open addressing):

In open addressing, all entry records are stored in an array. When a new entry has to be inserted, the hash index of the hashed value is computed and then the array is examined (starting with the hashed index). If the slot at the hashed index is unoccupied, then the entry record is inserted in slot at the hashed index else it proceeds in some probe sequence until it finds an unoccupied slot. The procedure used to navigate through entries is known as the probe sequence. In Closed hashing, there are three techniques that are used to resolve the collision:

Linear probing: When the collision occurs, using the Linear Probing technique, the closest

free cell is found by linearly iterating over the cells. Here, the searching is performed sequentially, starting from the position where the collision occurs till the empty cell is not found.

This method uses the hash function h(k, i)= (h'(k) + i) mod m

for i = 0, 1, … m - 1

Problems on linear probing: probing.pdf^ linear

Linear probing is easy to implement, but it suffers from a problem known as primary

clustering. Long runs of occupied slots near the hash position of keys build up, increasing the

average search time. Clusters arise because an empty slot preceded by i full slots gets filled next with probability (i

  • 1)/m. If the primary hash index is x, subsequent probes go to x+1, x+2, x+3 and so on, this results in Primary Clustering. Once the primary cluster forms, the bigger the cluster gets, the faster it grows. And it reduces the performance.

Double hashing: Double hashing offers us one of the best techniques

for hashing with open addressing. The permutation formed by double hashing is like a random permutation therefore it almost eliminates the chances of cluster forming as it uses a secondary hash function as an offset to deal with collision condition. The hash function that is used in double hashing is of the form - h(k, i)= (h 1 (k) + ih 2 (k)) mod m Where both h1 and h2 are auxiliary hash functions. The initial probe goes to position T[h 1 (k)]; successive probe positions are offset from previous positions by the amount h2(k), modulo m.

DAA Page 2