Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Data Structures and Algorithms - EE 390

Chapter 2
Hash Tables

Dr. Anirban Dasgupta


Assistant Professor

Department of Electronics And Electrical Engineering, IIT Guwahati


Hash Table Data Structures

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.

Hash Table uses an array as a storage medium and uses hashing technique to generate an index
where an element is to be inserted or is to be located from.

Known as ‘Maps’ in C++, ‘Hashmaps’ in Java, ‘Dictionary’ in Python

Department of Electronics And Electrical Engineering, IIT Guwahati


Hash Table - Example
Key Value Two arrays

Rohit 9564523120 Rohit 0 9564523120

Kishore 9856475421 Kishore 1 9856475421

Sonakshi 9656523652 Sonakshi 2 9656523652

Manish 8965785643 Manish 3 8965785643

Sonali 8695696324 Sonali 4 8695696324

Bucket Array
Department of Electronics And Electrical Engineering, IIT Guwahati
Hashing Algorithms
• Hashing is a technique to convert a range of key values into a range of indices of an array
• This array is called the bucket array
• Keys must be unique

Bucket Array

Department of Electronics And Electrical Engineering, IIT Guwahati


Hashing Terminology

Search Key Key attribute of the key-value pairs

Hash Function Mapping of key to index

Hash Code Result of applying a hash function to a specific key

Compression Map Mapping hash code within range [0,N-1]

Department of Electronics And Electrical Engineering, IIT Guwahati


Hashing Algorithms
Algorithm 1: k mod 10
Search keys: {24,52,91,67,48,83}
1 91
24 mod 10 = 4
52 mod 10 = 2 2 52

91 mod 10 = 1 3 83

67 mod 10 = 7 4 24

48 mod 10 = 8 7 67

83 mod 10 = 3 8 83

Department of Electronics And Electrical Engineering, IIT Guwahati


Hashing Algorithms
Algorithm 2: k mod n
Search keys: {24,52,91,68,45,83} =6
0 24
24 mod 6 = 0
52 mod 6 = 4 1 91

91 mod 6 = 1 2 68

68 mod 6 = 2 3 45

45 mod 6 = 3 4 52

83 mod 6 = 5 5 83

Department of Electronics And Electrical Engineering, IIT Guwahati


Hashing Algorithms
Algorithm 3: Mid square method
Search keys: {24,15,12,17,20,13}

242=576→7 0 20

152=225→2 2 15

122=144→4 4 12

172=289→8 6 13
202=400→0 7 24
132=169→6 8 17

Department of Electronics And Electrical Engineering, IIT Guwahati


Hashing Algorithms
Algorithm 4: Folding Hashing method
Search key: 123456

1.Divide into Chunks:


If we choose a chunk size of 2, we get the chunks: 12, 34, and 56.
2.Sum the Chunks:
Summing up the chunks: 12 + 34 + 56 = 102.
3.Take Remainder:
If we take the remainder when divided by the table size (10), we get a hash
code of 2.

Department of Electronics And Electrical Engineering, IIT Guwahati


Collision
Duplicate index when hashing is performed

Search keys: {27,19,32,44,23,20} Collision Resolution Techniques


Hashing Algorithm: k mod 6
27 3 0
Open hashing Closed hashing
19 1 1 19
Linear probing
32 2 2 32 44
44 2 3 27 Quadratic probing
23 5 4 Closed hashing is also
20 2 5
called open addressing Double hashing
Department of Electronics And Electrical Engineering, IIT Guwahati
Open Hashing
Open hashing is a collision avoidence method which uses a linked list
to resolve the collision

It is also known as the chaining method


27 3 0
19 1 1 19
32 2 2 32 44 20 Linked list

44 2 3 27
23 5 4
20 2 5 23
Department of Electronics And Electrical Engineering, IIT Guwahati
Linear Probing
searches for the next
available slot by probing
sequentially until an empty
slot is found

Department of Electronics And Electrical Engineering, IIT Guwahati


Linear Probing
Advantages:
• No extra space required, unlike open hashing
• Requires minimal additional memory for tracking occupied slots

Disadvantages:
• Worst case search O(n)
• Linear probing may lead to clustering, where consecutive slots
are filled.

Department of Electronics And Electrical Engineering, IIT Guwahati


Primary Clustering: Problem with
Linear Probing

Department of Electronics And Electrical Engineering, IIT Guwahati


Quadratic Probing

“key mod 7” and sequence of keys as 50, 700, 76, 85, 92, 73, 101

Quadratic probing is a scheme where we


look for i2 slot in ith iteration if the given
hash value x collides in the hash table.

Department of Electronics And Electrical Engineering, IIT Guwahati


Double Hashing

Double hashing uses the idea of applying a second hash function to key
when a collision occurs

The advantage of double hashing is that it is one of the best form of probing,
producing a uniform distribution of records throughout a hash table.

𝑅 ( 𝑘 ,𝑖 )=( 𝑅1 ( 𝑘 ) + 𝑖 𝑅 2 ( 𝑘 ) ) mod 𝑛
Table size
Prime number<n

Department of Electronics And Electrical Engineering, IIT Guwahati


Basic Operations
Size • Returns number of entries, i.e., either keys or values

Keys • Returns the keys as an array

Values • Returns the values as an array

Search • searches an element in a hash table

Insert • inserts an element in a hash table by a key

Modify • changes the value for a given key

Delete • deletes an element from a hash table

Department of Electronics And Electrical Engineering, IIT Guwahati


Search Operation
Whenever an element is to be searched, compute the hash code of the key
passed

Locate the element using that hash code as index in the array

Use collision handling to get the element ahead if the element is not found
at the computed hash code
Department of Electronics And Electrical Engineering, IIT Guwahati
Insert Operation
Whenever an element is to be inserted

Compute the hash code of the key passed

Locate the index using that hash code as an index in the array.

Use collision handling for empty location, if an element is found at the computed hash code.

Department of Electronics And Electrical Engineering, IIT Guwahati


Delete Operation
Specify the key of the element to be deleted

Compute the hash code of the key passed

Locate the index using that hash code as an index in the array.

Use linear probing to get the element ahead if an element is not found at the computed hash code.

Department of Electronics And Electrical Engineering, IIT Guwahati


References
• Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 3rd
edition, The MIT Press, McGraw-Hill, 2001.
• C. Cherniavsky and J. A. Storer, An Introduction to Data Structures and
Algorithms, 2nd edition. Birkhauser Boston, 2001.

Department of Electronics And Electrical Engineering, IIT Guwahati

You might also like