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

Lecture 3.Pptx 3

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 views24 pages

Lecture 3.Pptx 3

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/ 24

NEHRU ARTS AND SCIENCE COLLEGE

COIMBATORE -641 105

DEPARTMENT OF INFORMATION TECHNOLOGY


Course: Data Structures

Facilitator : Dr.S.Saraswathi
HASH TABLE
• 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
from.
• A Hash Function is a function that converts a given numeric or
alphanumeric key to a small practical integer value.

• The mapped integer value is used as an index in the hash table.

• In simple terms, a hash function maps a significant number or


string to a small integer that can be used as the index in the
hash table.
Hashing
• Hashing is a technique to convert a range of key values into a range of
indexes of an array.

• We're going to use modulo operator to get a range of key values.

• Consider an example of hash table of size 20, and the following items are to
be stored. Item are in the (key,value) format.

• The Hash table data structure stores elements in key-value pairs


where
• Key- unique integer that is used for indexing the values
• Value - data that are associated with keys.
(1,20)

(2,70)

(42,80)

(4,25)

(12,44)

(14,32)

(17,11)

(13,78)

Hash(key)= index; (37,98)


Basic Operations

Following are the basic primary operations of a hash table.

• Search − Searches an element in a hash table.


• Insert − inserts an element in a hash table.
• delete − Deletes an element from a hash table.
DataItem

• Define a data item having some data and key, based on which the
search is to be conducted in a hash table.

• struct DataItem
{
int data;
int key;
};
Hash Method

• Define a hashing method to compute the hash code of the key of the
data item.

int hashCode(int key)


{
return key % SIZE;
}
Hash Collision / Overflow Hashing
• When the hash function generates the same index for multiple
keys, there will be a conflict (what value to be stored in that
index). This is called a hash collision.

Resolve the hash collision using one of the following techniques.

• Collision resolution by chaining

• Open Addressing: Linear/Quadratic Probing and Double


Hashing
1. Collision resolution by chaining
In chaining, if a hash function produces the same index for multiple elements,
these elements are stored in the same index by using a doubly-linked list.
2. Open Addressing

• Unlike chaining, open addressing doesn't store multiple


elements into the same slot. Here, each slot is either filled with
a single key or left

• i. Linear Probing
• ii. Quadratic Probing
• iii. Double hashing
Linear Probing
• 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.


• Consider the above example for the linear probing:
• A = 3, 2, 9, 6, 11, 13, 7, 12 where m = 10, and h(k) = 2k+3
Quadratic Probing

• It works similar to linear probing but the spacing between the


slots is increased (greater than one) by using the following
relation.

• index = index % hashTableSize


index = (index + 12) % hashTableSize
index = (index + 22) % hashTableSize
index = (index + 32) % hashTableSize
Double hashing
• if a collision occurs after applying a hash function h(k), then another hash
function is calculated for finding the next slot.
• The interval between probes is fixed for each record but the hash is
computed again by double hashing.

• h1(k) = k mod 11 (first hash function)


• h2(k) = 8 - (k mod 8) (second hash function)
• first, we will create a hash table of size 11.
Hash Functions(3)

Division Method
If k is a key and m is the size of the hash table, the hash
function h() is calculated as:

h(k) = k mod m

if m = 22, k = 17, then h(k) = 17 mod 22 = 10001 mod 100 = 01


if m = 23, k = 17, then h(k) = 17 mod 22 = 10001 mod 100 = 001
if m = 24, k = 17, then h(k) = 17 mod 22 = 10001 mod 100 = 0001
Multiplication Method

• h(k) = ⌊m(kA mod 1)⌋

• where,

1. A constant is chosen which is between 0 and 1, say A.


2. The key k is multiplied by A.
3. The fractional part of kA is extracted.
4. The result of Step 3 is multiplied by the size of the hash table ( m).
Universal Hashing - Mid-Square Method

• In Universal hashing, the hash function is chosen at random


independent of keys.

1. The value of the key is squared. That is, k^2 is found.


2. The middle r digits of the result are extracted.
3. The result r is the hash obtained.
Applications of Hash Table

• Hash tables are implemented where

• constant time lookup and insertion is required


• cryptographic applications
• indexing data is required
Drawback of Hash function
• A Hash function assigns each value with a unique key.

• Sometimes hash table uses an imperfect hash function that

causes a collision because the hash function generates the

same key of two different values.


Advantages of Hashing
• The main advantage of hash tables over other data structures
is speed .

• The access time of an element is on average O(1), therefore lookup


could be performed very fast.

• Hash tables are particularly efficient when the maximum number


of entries can be predicted in advance.
Thank you

You might also like