0% found this document useful (0 votes)
44 views

Dsa U4

This document discusses hash tables and hashing. It defines hashing as mapping data to a hash table using a hash function for fast retrieval. A hash table stores key-value pairs by using a hash function to map keys to indexes in an array. This allows searching in constant time O(1), ideal for applications where searching is critical. The document outlines concepts, terminology like hash functions, buckets, and hash addresses used to store and retrieve data from hash tables.

Uploaded by

Harshada Bavale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Dsa U4

This document discusses hash tables and hashing. It defines hashing as mapping data to a hash table using a hash function for fast retrieval. A hash table stores key-value pairs by using a hash function to map keys to indexes in an array. This allows searching in constant time O(1), ideal for applications where searching is critical. The document outlines concepts, terminology like hash functions, buckets, and hash addresses used to store and retrieve data from hash tables.

Uploaded by

Harshada Bavale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Graph

CHAPTER
1M
Hash Table
Objectives ...
To study Basic Concepts of Hashing
To learn Hash Table and Hash Function
To understond Terminologies in Hoshing
To study Collision Resolution Techniques

0 INTRODUCTION
1 M Hashing is adata structure which is designed to use a special function called the hash
function which is used to map a given value with a particular key for faster access of
elements.
with the
Hashing is the process of mapping large amount of data item to smaller table
help of hashing function.
hash table
The mapping between an item and the slot where that item belongs in the
is called the hash function.
in the
The hash function will take any item in the collection and return an integer
range of slot names, between 0 and m-1.
A hash table is a collection of items which are stored in such a way as to make it easy

to find them later.


Need for Hash Table Data Structure:
location of the item is determined by a
In the linear search and binary search, the
be searched is compared with items
sequence/series of comparisons. The data item to
at certain locations in the list.
item to be searched, the search is successful.
If any item/element matches with the
locate an item depends on the data structure
The number of comparisons required to
search tree, etc. and the search algorithm
like array, linked list, sorted array, binary
used.
stored in sorted order in an array, binary search can be
For example, if the items are
appliedwhich locates an item in Olog n) comparisons.
unsorted array,
On the other hand, if an item is to
be searched in a linked list or an
locates an item in O(n) comparisons.
linear search has to be applied which

4.1
Data Structures and Algorithms Hash Table Data Structures and Algorlthms
Hash Table
However, for some applications, searching is very critical operation and need
they a
0 Key
search algorithm which performs search in constant time, i.e. O(1). Hash value Actual data stored
Although, ideally it is almost impossible to achieve a performance of O(1), but still a
search algorithm can be derived which is independent of and
n can give a
performance very close to O(1). Hash
Key- Hash value
That search algorithm is called hashing. Hashing uses a data structure called hash function
table which is merely an array of fixed size and items in it are inserted
using a Actual data to be store
function called hash function.
Best case timing behavior of searching using hashing =O(1) and Worst case timing
Behavior of searching using hashing = O(n).
A hash table is a data structure in which the location of a data item is determined Fig. 4.1: Pictorial representation of Simple Hashing
directly as a function of the data item itself rather than by a sequence of comparisons.
Under ideal condition, the time required to locate a data item in a hash table is O(1) 4.2 TERMINOLOGY
i.e. it is constant and does nor depend on the number of data items stored. The basic terms used in hashing are explained below
1. Hash Table: Ahash table is a data structure that is used to sttse
keys/value pairs
4.1 CONCEPT OF HASHING Hashing uses a data structure called hash table which is mer:ly an array of fixed
Hashing is the process of indexing and retrieving element (data) in a data structure to size and items in it are inserted using a hash function. It use: a hash function to
compute an index into an array in which an element will be ir serted or searched.
provide a faster way of finding the element using a hash key.
A hash table is a data structure that maps keys to values. a hast. table (hash is
Here, the hash key is a value which provides the index value where the actual data is map)
a data structure that
implements an associative array at iract data type, a
likely to be stored in the data structure.
straeture that can map keys to values. A hash table uses a hash function to
compute an index, also called a hash code, into an array of b.ckets or slots, from
In this data structure, we use a concept called hash table to store data. All the data
which the desired value can be found.
values are inserted into the hash table based on the hash key value.
2. Hash Function: A hash function is a function that maps the ke" to some slot in the
The hash key value is used to map the data with an index in the hash table. And the hash table. A hash function, is a mapping function which maps all the set of search
hash key is generated for every data using a hash function. keys to the address where actual records are placed.
That means every entry in the hash table is based on the hash key value generated 3. Bucket: A hash file stores data in bucket format. Bucket is onsidered a unit of
using the hash function. storage. A bucket typically stores one complete disk block, wha in turn canstore
Hash table is just an array which maps a key (data) into the data structure with the one or more records.

help of hash function such that insertion, deletion and search operations are 4. Hash Address: A hash function is a function which when giver : key. generates an
address in the table. Hash index is an address of the data block
performed.
5.
Generally, every hash table makes use of a function called hash function to map the Collision: The situation where a newly inserted key
maps to ur already
slot in the hash table is called collision. A colision occurs when wo data
ocupied
data into the hash table. elements
are hashed to the same value and
Hash function is a function which takes a piece of data (i.e. key) as input and produces
try to occupy the same spau: in the hash tabe
In simple words, the situation in which a key hashes to an indt:: which is alreaay
an integer i.e. hash value) as output which maps the data to a particular index in the occupied by another key is called as collision.
hash table. 6. Synonym: It is possible for different keys to hash to the same ::ray location. Ths
Basic concept of hashing and hash table is shown in the Fig. 4.1.
sltuation is called collision and the colliding keys are called syn :r yms.
4.2 4.3
Hash Table
Aigorithms
Hash Table Data Structures and Algorithms-
the ko
8nd values k, ka. kn. in
Structyres
k). The
a
An o' rlow occurs at the time of the bucket for S u c h a fleld is known as primary key (denoted by ey
7, Overflow:. a new pair (key,
element) is full. field are known as keys or key values. location of
function determines the
a particular
elemeddressin; It is performed to ensure that all elements are stored directdy The key through an algorithmic
&Op thehash tal: e, thus it attempts to resolve collisions implementing varlous
hash tal record. the key-to-address
the function basically performs
Into
The algorithmic function i.e. hashing
is to the addresses of records in the file as shown
me probing: it is performed to resolve collisions by placing the data into the transformatión in which key mapped
Linear
in tlhe table. in Fig. 4.2.
next open slot
hing Hashin is a process that uses a hash function to get the key.for the hash Address = Hash Function (Key)
Address
e and transfo n it into an index that will point to different arrays of buckets, Key
which is where the informadon will be stored.
ainingItis at»sinique used for avoiding collisions in hash tables. Fig. 4.2: Hash Function
Collision should be resolved by finding some other location
mision Resolution: this
12

r e
insert
ins
s o
the
l
new sey,
u d o
process of finding another location is called as collision
n .
ADivision Method
In division method, the key k is divided by the number of slots N in the hash table,
and
the remainder obtained after division is used as an index in the hash table. That is, the
PROPERTIES OF GOOD HASH FUNCTION hash function is,
hash function are given below: hk) kmod N
of a go d
The propertdes where, mod is the modulus operator. Different languages have different operators for
1. The hash function is easy to understand and simple to compute. calculating the modulus. In C/C++, %' operator is used for computing the modulus.
Anumberfunction
of colisi should be less while placing the data in the hash table.
>ns
should generate different hash values for the similar string.
For example, consider a hash table with N=101. The hash value of the key value
The hash 132437 can be calculated as follows:
The hash function uniformly" distributes the data across the entire set of possible h(132437) = 132437 mod 101 26
hash values. Note that above has ction works well if the index ranges from 0 to N-1 (like in
The hash funcdion is a perfect hash function when it uses all the input data. A hash C/C++). However, if the index ranges from 1 to N, the function will be,
a unique slot is referred to as a perfect hash hk) = k mod N+1
function that maps each item into
function. This technigque works very well if N is either a prime number not too close to a power
of two. Moreover, since this technique requires only a single division operation, it is
HASH FUNCTONS quite fast.
hash function h is siiaply a mathematical formula that manipulates the key in some For examole, suppose, k= 23, N =
10 then
mto compute the ii.1ex for the key in the hash table.hash table is knówn as h(23) 23 mod 10+1 3+1 4 . The key whose
value is 23 is placed in 4h location.
eprocess of mappii ; keys to appropriate slots in a hashing. Take another example, consider an example of hash table of size 20, and the
the data in the hash table. items are to be stored. Item are in the (key, value) format.hash
following
ash function is a fun ion which is used to put the data from the hash table.
function kis, h{k) =

ence, one can use th: same hash function to retrieve mod 20.
us, hash functdon is .sed to implement the hash table. (1, 20)
ash functionh is si: ply a mathematical formula that maps the key to some slot in , 70)
hash table 42, 80)
is the hash value of key k. If
s, we can say that :he key k hashes to slot h{k), or hk)table from 0 to N-1. A
(4,25)
size of the hash tal e is N, then the index of the hash ranges (12, 44)
h table with N slots is denoted by T[N]. (14, 32)
to a file F containing
shing (also known is hash addressing) is generally applied (17, 11)
Each recor.i contains many fields, out of these one particular fleld may
ecords. (13, 78)
quely identiy the e2ords in the fle. (37, 98)
4.4
4.5
Data Structures and
Algorithms- Data Structures and Algorlthms- Hash Table
Hash Table
Hashing is a
technique to convert a struct DataItem *search(int key)
range of key values into a range of indexes of
array. an

Sr. No. Key Hash l g e t the hash


1.
Array Index int hashIndex = hashCode (key);
1 % 20 1
2. //move in array until an empty
2 % 20 =2 2
42 42 % 20 =2 while(hashArray[hashIndex] I= NULL)

4 % 20 4
12
if(hashArray[hashIndex]->key =
key)
12 % 20 12 return hashArray[hashIndex];
14 14 % 20 = 14 l/go to next cell

17 17 % 20 =17 ++hashIndex;
17
/wrap around the table
8. 13 13 % 20 = 13 13 hashIndex %= SIZE;
9. 37 37 % 20 = 17 17
Basic Operations: return NULL.
Following are the basic primary operations of a hash table:
1. Search:
Searches an element in a hash table. void insert (int key, int data)
2. Insert: inserts an element in a hash table. {
struct DataItem *item= (struct DataItem*) malloc(sizeof(struct DataItem);
3. Delete: Deletesan elementfrom ahash table. item->data = data;
Program 4.1: Program for operations on hashing.
#include <stdio.h> item->key key;
1/get the hash
#include <string.h
int hashIndex hashCode(key);
#include <stdlib.h»
/move in array until an empty or deleted cell
#include <stdbool. h>
-)
#define SIZE 20 while(hashArray[hashIndex] I= NULL && hashArray[hashI 1t ex]->key I

struct DataItem //go to next cel1


++hashIndex;
int data /wrap around the table
int key hashIndex SIZE;

struct DataItem* hashArray[SIZE];


hashArray[hashIndex] item
struct DataItem* dummy Item; struct DataItem*
delete (struct DataItem* item)
struct DataItem* item;
int hashCode(int key) int key item->key;
=

l/get the hash


int hashIndex
return key % SIZE; hashCode(key) ;
/move in array until an empty

4.6 4.7
Hash Table
Data Structures and Algorithms -I
wetuves and Algorithns- Hash Table
insert (14, 32);
- NULL)
hile (hashArr ay[hashIndex] insert(17, 11);
insert (13, 78);
f(hasharray[hashIndex] ->key key) insert(37, 97);
display()
struct ataItem temp hashArray[hashIndex]; item search (37)
1/assign a dummy item at deleted position if(item l- NULL)
hashArr ay[hashIndex] - dummyTItem;

return emp; printf("Element found: %d\n", item->data) ;


}else
l/go to ne.t cell

hashInde: printf("Element not found\n");


Iwrap anond the table

hashIndex SIZE; delete(item);


item search (37);
neturn NJLL;
if(item l= NULL)

vaid cisplay0 printf("Element found: %d\n", item->data);


else

for( e; i<sIZE; i ) printf("Element not foundn"):

if(hashArray[4] !- NULL)
printf(" (xe,kd) ", hashArray[i] ->key, hashArray[i]->data) ; Output:
else $gcc -o hashpgm *.c
printf( "); $hashpgm
(1,20) (2,70)) (42,80) (4,25) (12,44)
printf("\n"); (13,78) (14,32) o (17,11) (37,97)
Element found: 97
t mein() Element not found

malloc(sizeof(struct
4:2 Mid Square Method
amylter (sti
dumy
uct DataItem") DataItem) :
In this method, we square the value of a key and take the number of
Iten->date =
-1;
form an address, from the middle digits required to
dunmyItem->key-1; position of squared value.
insert(1, 20); Suppose a key value is 16, then its square is 256. Now if we want address
then we select the address as 56 (d.e., two of two digits,
insert (2, 7e); Mid square method operates in
digits starting from middle of 256).
insert (42, 8e); following two steps:
insert (4, 25); 1. First, the
square of the key k (that is, k) is calculated and
Ansert(12, 44); 2. Then some of the
digits from left and right ends of k'
4.9
are
removed.
4.8
Data Structures and Algorithms I
Hash Table
The number obtained after removing the
Data Structuresand Algorlthms -T Hash Table
digits is used as the hash value. Note that the If the element to be inserted is mapped to the same location, nere an element is
digits at thee same position of k' must be used for all keys.
Thus, the hash function is given below,
already inserted then we have a collision and it must be resolved.
There are several strategies for collision resolution. The most comr only, used are:
hk) = s
1. Separate Chaining: Used with open hashing.
where, s is obtained by deleting digits from both sides of . 2. Open Addressing: Used with closed hashing
For example, consider a hash table with N
132437 can be calculated as follows:
1000. The hash value of the key value
45 Open Addressing
1. The square of the key vaue is calculated, which is, 17539558969. Open addressing or closed hashing is a method of collision resoltion in hash tables
The hash value is obtained by taking 5", 6h and 7h digits counting from right With this method a hash collision is resolved by probing, er
searching through
alternate locations in the array (the probe sequence) untl either 1e
which is, 955. target record is
found.
4.4.3 Folding Method In open addressing separate data structure is used because all i e key values are
stored in the hash table itself. Since, each slot in the hash table co rains the
The folding method also operates in two steps. In the first step, the key value k is key value
rather than the address value, a bigger hash table is
divided into number of parts, k,k, k , k , where each parthas the same number of to separate chaining.
required in tis case as compared
digits except the last part, which can have lesser digits.
Some value is usedto indicate an
In the second step, these parts are added together and the hash value is obtained by empty slot. For example, if it is known that all the
keys are positive values, then -1 can be used to represent a free or
ignoring the last carry, if any. For example, if the hash table has 1000 slots, each part tmpty slot
To insert a key value, first the slot in the hash
will have three digits, and the sum of these parts after ignoring the last carry will also table to which the cey value hashes is
determined using any hash function. If the slot is
be three-digit number in the range of 0 to 999. free, the key vz 1e is inserted into
that slot.
For example, the hash
if table has
100 slots, then each group will have two digits, and In case the slot is
already occupied, then the subsequent slots starting from the
the sum of
the
groups after ignoring the lastcarry will also be a 2-digit number
between 0 and 99. The hash value for the key value 132437 is computed as follows: occupied slot, are examined
systematically in the forward directon, until an empty
slot is found. If no empty slot is
1. The key value is first broken down into a group of 2-digit numbers from the left found, then overflow condition occurs.
In case of
most digits. Therefore, the groups are 13, 24 and 37. searching of a key value also, first the slot in the hash table to which the
key value hashes is determined using any hash
2. These groups are then added like 13 + 24 3 7 74. The sum 74 is now úsed the function. Then the l:ay value stored Im
that slot is compared with the
hash value for the key value 132437. key value tobe searched.
If they match,the search
operation is
examined systematically in the forward successful; otherwise alternative slots ae
Similarly, the hash value of another key value, say 6217569, can be calculated as
follows: direction to find the sct containing tne
desired key value.
1. The key value is first broken down into a group of 2-digit numbers from the left If such slot is found, then the search is unsuccss sful.
no
The process of
most digits. Therefore, the groups are 62, 17, 56 and 9. examining the slots in' the hash table to find the loce on of a
is known as
probing. The linear probing, key vau
2. These groups are then added like 62+ 17+ 56+9- 144. The sum 44 after ignoring quadratic probing and d:* tble
the last carry 1 is now used as the hash value for the key value 6217569.
are used in
open addressing method. hashing u
4.5 COLLISION RESOLUTION TECHNIQUES 4.5.1.1 Linear Probing
Linear probing is
Collision resolution is the main problem in hashing. The situation in which a key
a
technique for resolving collisions in hash tables, rdata
hashes to an index which is already occupied by another key is called collision. maintaining a collection of key-value struc
given key. pairs and looking up the valu: issociatea
Collision should be resolved by finding some other location to insert the new key. This
In linear
process of finding anotherlocation is called collision resolution. probing whenever there is a collision,
4.10 Wraparound) for searching the hash-table cells are searched sequenuauy (with
free location.
4.11
- -

N
| | |
Data
Structures and Algorithms -

Hash Table
4.5.1.2 Quadratic Probing Data Structuresand Algorithms- Hash Table
A simple solution to this problem is rehashing, in which all the <3ys in the original
Quadratic probing is a collision
resolving
operates by taking the original hash technique
in open addressing hash table. It hash table are rehashed to a new hash table of larger size.
andindex adding successive values of an The default size of the new hash table is twice as that of the origiral hash table. Onos
arbitrary quadratic polynomial until an open slot is found. the new hash table is created, a new hash value is computed. ior each key in the
One way of
reducing "primary clustering" is to
quadratic probing to resolve
use
original hash table and the keys are inserted into the new hash tatle.
collision. In quadratic probing, we try to resolve the collision
of the index of a hash
table by quadratically increasing the search index
free location.
After this, the memory allocated to the original hash table is freed. The performance
the hash table improves significantly after rehashing.
o
Let suppose, if k is the index retrieved from the hash function. If the
us

already filled then we will look for (k+1) %M, then (k+2) 6M and so on. When kth index is
a free slot, we will insert the
we get Transfering
object into that free slot. the contents
It does not ensure that all cells in the table will be
examined to find an empty cel.
Thus, it may be possible that key will not be inserted even if
there is an empty cell in
the table.
Double Hashing:
Double hashing is a collision resolving
technique in open addressing hash table.
Double hashing uses the idea of using a second hash function to when a key collision
occurs.

This method requires two hashing functions for


f1(key) and f2 key). Problem of Old table
clustering can easily be handled through double hashing.
Function f1 (key) is known as primary
hash function. In case the address obtained by
f1 (key) is already occupied by a key, the function f2
(key) is evaluated.
The second function f2 (key) is used to compute the increment to be added to the 22
address obtained by the first hash function f1 (key) in case of collision.
New table
The search for empty location is made Fig. 4:3: Rehashing
an
successively at the addresses
f1 (key) + f2 (key), f1 (key) + 2f2 (key), fi (key) + 3f2(key), .
.5.2 Chaining
4:513 Rehashing In hashing the chaining is one collision resolution technique. Chai ing is a possible
way to resolve collisions. Each slot of the array contains a link to : singly-linked list
As the suggests, rehashing hashing again. Rehashing is
name means a technique in
containing key-value pairs with the same hash.
which the table is resized, i.e. the size of table is doubled by creating a new table.
Chaining allows storing the key elements with the same hash valu: into linked list as
Several deletion operations are intermixed with insertion operations while shown in Fig. 4.4.
performing operations on hash table. Eventually, a situation arises when the hash
Thus, cach slots in the hash table contains a pointer to the head of: e linked list
table becomes almost full.
the elements that hashes to the value h.
of al
A t this time, it might happen that the insert, delete, and search operations on the hash All collisions are chained in the lists attached to the
table take too much time. The insert operation even fails in spite of performing open appropriate sitt. This allows an
unlimited number of collisions to be handled and does not
addressing with quadratic probing for collision resolution. This condition/situation of how many elements are contained in
require i prior knowledge
the collection.
indicates that the current space allocated to the hash table is not sufficient to The tradeoff is the same with linked lists versus array ii
as f
accommodate all the keys. collections: linked list overhead lementaaons
4.14
in space and to a lesser extent, in tir
4.15
Algorithia3-u Hash
Table Hash Table
DataStructuresand Data Structures and Algorithms I

ofu
He d
gcl

122803 2 qur
Hash Ad Iess hk4
Key%3 :7 +1
151354 Uday Lodia 122803 clq
3
Key. Value Next
ecd

Ashish Gang 151354 dim


Key Value Next
Fig. 4.4: Chaining through Linked List 6 aty

Coalesced haining hv
Coalesced hashing a.o called coalesced chaining is a technique of collision resolution
ma hash table that it rms a
hybrid of separate chaining and open addressing.
Tses the concept :f open addressing (linear probing) to find first empty place for
liding element fium the bottom of the hash table and the concept of separate gsu
chaining to link the liding elements to each other through pointers.
Sequence"i "aty:" "gur," "dim" "ofu," "gcl" "rhv," "ela" "ecd," "gsu" of Fig. 4.5
generateP three character long strings, the following table would be Coalesced hashing technique is effective, efficient, and
nerated with a tati2 of size 10. very easy to implement.
Separate Chaining
.In separate chaining, hash table is an array
points the first element of the linked list of
to pointers and each element of array
of all the records that
location. hashes to that
Cil Open hashing is a collision avoidance method which
uses
resolve the collision. It is also known as the array of linked list to
is considered as a chain). separate chaining method (each linked list
I n separate chaining collision resolution
"gsu" technique, a linked ist of all the
that hash to the same hash value is
maintained. Each node of the linked list key values
key value and the pointer to the next node. contains a
Each indexi (0 « I < N) in the hash table
"ofu" gel" "ecd" contains the address
linked list containing all the keys that hash to the of the first node of the
index i.
If there is no key value that hashes to
the index i,
4S dhows a Therefore, in this method, a slot in the hash table the slot contains NULL value.
does not contain the
liaon bucketsinareexaraple of coalesced hashing ezample (for purpose of this example, values; rather it contains the address of the first
node of the linked list
actual key
i located increasingorder, starung with bucket 0). elements that hash to this slot. containing the
4.16 4.17
Hash Table Data Structuresand Algorithms
Data Struchures and Algorithms D Hash Table
mapped
elements to the same
In separate chaining technique,separate list of all
a

is based on collision avoidance.


value is maintained. Separate chaining
should be avoided. Additional memory
rmemory space is ight, separate chaining
address of linked elements.
space for links is wasted storing
in
even distribution of elements among
buckets;
Hashingfunction should ensure
hash table will deteriorate.
otherwise the timing behavior of most operations
on

chaining hash table.


Fig. 4.6 shows a separate List of elements
Fig. 4.7
H HD
Example 2: Consider the key values 20, 32, 41, 66, 72, 80, 105, 77,:6, 53 that need to be
hashed using the simple hash function h{k) =
k mod 10. The keys 20 :nd 80 hash to index
0, key 41 hashes to index 1, keys 32 and 72 hashes to index 2, key 53:s hashed to index 3,
key 105 is hashed to index 5, keys 66 and 56 are hashed to index 6 an: înally the key 77 is
hashed to index 7. The collision is handled using the separate chai ng (also known as
synonyms chaining)techniqueas shown in Fig. 4.8.
NULL

HLHe} 41 NULL
Fig. 4.6: Aseparate ChainingHash Table -2 NULL

Example 1: The integers given below are to be inserted in a hash table with 5 locations 53 NULL
using chaining to resolve collisions. Construct hash table and use simplest hash function.
1,2,3,4, 5, 10, 21, 22, 33, 34, 15, 32, 31, 48, 49, 50, 4 NULL
Solution: An element can be mapped to a location in the hash table using the mapping
-NULL
function hk) =k% 10.
Hash Table Location Mapped Elements
0 5, 10, 15, 50 77 NULL
1,21, 31 8 NULL

2, 22, 32
9 NULL

3,33,48
4, 34, 49 Fig. 4.8: Collision Resoltuion bySeparate Chaining
4.18 4.19
Hash Table
Data Structures and Algorithms
Algorithr Hash Table has the following properties:
tata Structuresand 4. A good hash function ad nust not
Comparison betweenparate aining and Open Addressing: should be easy to compute become an
(a) Easy to compute: It
algorithm in itself.
Sepa te Chaining Open Addressing should provide a uniform distribution across the
(b) Uniform distribution: It

Chaining is sim:.er to implement. Open addressing requires more hash table and should not result
in clustering
pairs of elements are mapped to the
computation. (c) Less collisions: Collisions occur when
avoided.
n chaining, ha:n table never fills up, In open addressing, table may become same hash value. These should be
we can always idd more elements to full. (d) All of these
chain. deletion and search operations very
5. Which tables are used to perform insertion,
Chaining is les: sensitive to the hash | Open addressing requires extra care
quickly in a data structure?
function or load actors. for toavoid clustering and load factor. (a) Root (b) Hash
Chaining is mi dy used when it is Open addressing is used when the
(d) Root
unknown hov many and how frequency and number of keys is (c)Routing
records?
freguenty keys may be inserted or | known. 6. Which is in a hash file is unit of storage that can hold one or more

(b) Hash values


deleted (a) Bucket
Cache performarce of chaining is not Open addressing provides better cache (c) Token (d) None of these
good as keys a stored using linked performance as everything is stored in hashed to the same value and try to
7 A _occurs when two data elements are
ist the same table.
occupy the same space in the hash table.
iWastage of spac, (some parts of hash In open addressing, a slot can
be used (a) Bucket b) Underflow
able in chaining are never used). even if an input does not mapto it. (c) Collision (d) Token
Chaining uses extra space for links. No links in open addressing.
8. In which method all the key values are stored in the hash table itself?

RACTICEQUESTI NS (a) Open addressing b) Chaining


(d) None of these
IMuitiple Choice Qu stions: (c)Separatechaining
9. Which is a technique in all the keys in the original hash table are rehashed to a
1 Which data strtrture is designed to use a special function called the hash
new hash table of larger size?
function?
(a) Chaining (b) Hashing
(a) Hashingg b) Stacking
(c) Rehashing (d) None of these
(o Queueing (d) None of these
10. In which hashing method, successive slots are searched using another hash
Which is a data st icture that represents data in the form of key-value paírs? function?
a) Hash functio b) Hash table b) Quadratic probing
i (a) Linear probing
(cy Hashing (d) None of these (c) Double (d) All of these

Which is any fun ion that can be used to


map a data set of an arbitrary size to a 11. Name which methods are used by open addressing for hashing?
4ata set of a fxed (b) Quadratic probing
:ize, which falls into the hash tabie? (a) Linear probing
(a) Hash funcdo b) Hash table (c) Double hashing (d) All of these
(c Hash values 4.21
(d) None of these
4.20
Data Structures and Hash Tab
Algorithms -I Hash Table Data Structures and Algorlthms-
12.
Hashing is implementing Q.II State True or False: table is known
using 1. The process of mapping kays
to appropriate slots in a
' ash a

(a Hash table b) Hash function


hashing. and then ttract some portion of
(c)Both (a) and (b) first square the item,
(d) None of these 2. In mid-square method we
. Hash table is a data stracture that represents data in the form of, the resulting digits. division.
3. The hash table is depending upon
the remainder of
(a) value-key pairs (b) key-value pairs hash tablt itself.
all elements are stored in the
4. In open addressing,
) key-key pairs (d) None of these of hasli able point to a linked
The idea of separate chaining is to make each cell
Answers function vaue.
list of records that have sane hash
collisions.
1. (d) 2. (c) 3. (b) 4. (a) 5. (c) 6. (d) 7.(c) 6. open addressing is a method for handling
for next slot.
. (a) 9. (c) 10. b) 11.(d 12.(a) 13.b) 7. In linear probing, we linearly probe
Double hashing uses the idea of applying
a second hash nction to key when a

Q. I Fill in the Blanks: 8.


collision occurs.
values.
1. _is the process ofmapping large amount of data item to a hash table with the 9. Ahash function is a datà structure that maps keys to
help of hash function. the original hain index and adding
10. Quadratic probing operates by taking
unt. an open slot is found
2. The situation where a newly inserted key maps to an already occupied slot in the successive values of an arbitrary quadratic polynomial
whei. there is a fixed sized
hash table is called 11. Coalesced hashing is a collision avoidance technique
3. In rehashing the default size ofthe new table is as that ofthe original hash data.
Answers
table.
4. The process of examining the slots in the hash table to find the location of a key 1. (T 2. (T)3.(F) 4.(D|5.T)6.(T)|7.)8.(T) E)|10.(T) 11.T
value is known as. Q. IV Answer the following Questions:
A function is simply a mathematical formula that manipulates the key in (A) Short Answer Questions:
some form to compute the index for this kay in the hash table. 1. What is hashing?
2. What is hash function?
6. Hash is an array of fixed size and items in it are inserted using a hash
3. Define bucket.
function.
4. List steps for mid square method.
7. n method the key is divided by number of slots in the hash table and the
5. What is collision?
remainder obtained after division is used as an index in the hash table.
6. List techniques for collision resolution.
8. In hash table is an array of pointers and each element/item of array points 7. Define rehashing?
to the first element of the linked ist of all the records that hashes to that location.
8. What is linear probing?
9. allows storing the key elements with the same hash vaue into linked list. 9. Define quadratic
10. hashing is a combination of both Separate chaining and Open addressing.
probing
10. Define separate chaining?
Answers (B) Long Answer Questions:
1.Hashing 2. collision 3. twice 4. probing 5. hash 1. Define hashing. State need for hashing. Also list advantages hashing.
6. table 7. division 8. Separate 9. Chaining 10. Coalesced 2. What is chaining? Explain with diagram.
chaining 3. With the help of example describe separate chaining?
4.22 4.23
Date Structures and Algor *hme
Describe qua: ratic probing with
5. With the hely of diagram describe
hat is hash 1able?
Explain
example Haeh Tabte

7. Write a shor 1ote


on:
in
detal ncept of hashing
8. Compare sep irate Linear ppr
9 What is reha:ning?chainingaand open
10. Differentiate etween
Describe enaddressine
addresing?
with dlagrammatically
With the hel f hashingan
diagram describe hash
ed
11.
12. Describe coal isced chaining
rehashing
function
with example.

en a

ding
und.
sized

11.(T)

You might also like