You are on page 1of 6

Unit -3:

Hash Table:

 Hash table is a data structure that stores some information, and the information has
basically two main components, i.e., key(which is required to search the value) and
value.It maps the keys to values.
 For example , A phone book having the numbers as well as the Names of the
concerned numbers.

hashes(values)
 Data structures that uses a special function known as a hash function.
 Now think about where to store the keys and the hashes or values.
 Can we store it in Arrays or Array List?

 But while storing, if one is missed the mismatch will happen.


 So instead of using array list we are gonna use map interface.
 Syntax in java for using Map Interface:
 Map<String,String> Phonebook= new HashMap<>();
Phonebook.put(“John Smith”,”02”);
Phonebook.put(“Lisa Smith”,”01”);
Phonebook.put(“Sam Deo”,”04”);
Phonebook.put(“Sandra Dee”,”02”);
 <String,String > is for key which is of type string and value can be of type
int or string.
Map Types

Hash map Linked Hash Map


Hash Table Tree Hash Map

 For example, suppose the key value is John Smith and the value is the
number, so when we pass the key value in the hash function shown as below:

Hash(key)= index;

Hash(John Smith) = 02;

 The main goal of has function is to calculate and return the index of corresponding
data

What is the pros of a hash table as a data structure?

 faster access of data

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. For example here, both John Smith and Sandra
Dee is having the same value as 02.

Collision:

If several elements are competing for the same bucket in the hash table

There are three ways of calculating the hash function:

o Division method
o Folding method
o Mid square method

In the division method, the hash function can be defined as:


h(ki) = ki % m;

where m is the size of the hash table.

For example, if the key value is 6 and the size of the hash table is 10. When we apply
the hash function to key 6 then the index would be:

h(6) = 6%10 = 6

The index is 6 at which the value is stored.

Hashing
 Hashing is one of the searching techniques that uses a constant time.

 The time complexity in hashing is O(1).

 Till now, we read the two techniques for searching, i.e., linear


search and binary search.

 The worst time complexity in linear search is O(n), and O(logn) in binary
search. In both the searching techniques, the searching depends upon the
number of elements but we want the technique that takes a constant time.

 So, hashing technique came that provides a constant time.

 In Hashing technique, the hash table and hash function are used. Using the
hash function, we can calculate the address at which the value can be stored.

The main idea behind the hashing is to create the (key/value) pairs. If the key is
given, then the algorithm computes the index at which the value would be stored. It
can be written as:

Index = hash(key)

Types of Hashing:

https://youtu.be/zeMa9sg-VJM

Watch the vdo and most Importantly study how to solve using Linear Probing.

Questions:

1. Consider the given set of key values {7,3,9,2,6,2,12,13}. Given hash function is h(k)=
2K+3. How will keys get store in hash table. Use Linear Probing for handling
collision.
2. Given the following input (4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199) and
the hash function x mod 10, which of the following statements are true?

i. 9679, 1989, 4199 hash to the same value 


ii. 1471, 6171 hash to the same value 
iii. All elements hash to the same value 
iv. Each element hashes to a different value 
(A) i only 
(B) ii only 
(C) i and ii only 
(D) iii or iv 
Solutions: Using given hash function h(x) = x mod 10 
 h(9679) = 9679%10 = 9
h(1989) = 1989%10 = 9
h(4199) = 4199%10 = 9
h(1471) = 1471%10 = 1
h(6171) = 6171%10 = 1
As we can see, 9679, 1989 and 4199 hash to same value 9. Also, 1471 and 6171 hash
to same value 1. Therefore, statement (i) and (ii) are correct which match with option
(C). 
3. Which one is the correct formula for linear Probing.
a. Location mod size of hash table   
b. (Location+i)mod size of hash table
c.(Location^2+i)mod size of hash table
d.None

Answer: B

1. During finding the correct location for saving key value pair, how many times the key
is hashed?
a.1
b.2
c.3
d.unlimited till bucket is found
Answer: B

1. Hash Map implements collection interface?


a.True
b.False
Answer: A

1. Which of the following technique stores data in a separate entity in case of a


collision?
a.Open addressing
b.Chaining using doubly linked list
c.Double hashing
d.Linear probing
Answer: A

4. What is the formula used in quadratic probing?


a) Hash key = key mod table size
b) Hash key=(hash(x)+F(i)) mod table size
c) Hash key=(hash(x)+F(i )) mod table size
2

d) H(x) = x mod 17
Answer:C
5. If the key value 5 got 6 location in has table, another key value 8 is also showing 6
th th

location as this is the case of collision, upon applying linear probing where should this
8 will get store?
a.8 will get store to next subsequent vacant position
b.8 will store to 7 location.
th

c.8 will store 6 location only.


th

d.None of these.
Answer:A
6. What should be the output of Symmetric Pair Problem for the given list:-{{2,3},{4,5}
{7,2}{3,2}{5,4}}

a.{2,3}{4,5}
b.{2,3}
c.{4,5}
d.{2,3},{7,2}
Answer:A

Algorithm to find symmetric pairs in an array


using hashing

1. From all the array pairs, the first element is used as the key and the second
element is used as the value.
2. Traverse all the pairs one by one.
3. For every pair, check if its second element is found in the hash table.
4. If yes, then compare the first element with the value of the matched entry of
the hash table.
5. If the value and the first element match, it is displayed as a symmetric pair.
6. Else, insert the first element as the key and second element as value and
repeat the same.

Input: arr [6] [2] = {{1, 2}, {3, 4}, {5, 6}, {2, 1}, {4, 3},{10,11}}

Output:{1,2} and {2,1} are symmetric and {3,4} abd {4,3} are
symmetric

Time complexity: O(n)

You might also like