Hash Concepts
Hash Concepts
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?
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;
The main goal of has function is to calculate and return the index of corresponding
data
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
o Division method
o Folding method
o Mid square method
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
Hashing
Hashing is one of the searching techniques that uses a constant time.
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.
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?
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
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
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
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)