Hash Table: Aad Cse Srm-Ap 1

You might also like

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

HASH TABLE

AAD CSE SRM-AP 1


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.

AAD CSE SRM-AP 2


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.

AAD CSE SRM-AP 3


Hashing
Assume we have a set of integers 54, 26, 93, 17, 77,
31. Our first hash function required to be as
"remainder method" simply takes the item and divide
it by table size, returning remainder as its hash value
i.e.
ITEM HASH VALUE
 h item = item % (size of table)   
54 10
Let us say the size of table = 11, then  
26 4
54 % 11 = 10  
93 5
 26 % 11 = 4    17 6
 93 % 11 = 5   77 0
17 % 11 = 6  31 9
  77 % 11 = 0   
 31 % 11 = 9   AAD CSE SRM-AP 4
Hashing
Collision: According to the Hash Function, two or
more item would need in the same slot. This is said
to be called as Collision.
Now taking one more element 44 when we apply the
hash function on 44, we get (44 % 11 = 0), But 0
hash value already has an element 77. This Problem
is called as Collision.

AAD CSE SRM-AP 5


Methods of Hashing
There are two main methods used to
implement hashing:
 Hashing with Chaining
 Hashing with open addressing

AAD CSE SRM-AP 7


Methods of Resolution
 Chaining (Open Hashing):
0
» Store all elements that hash to the k1 k4

same slot in a linked list. k5 k2 k6

» Store a pointer to the head of the k7


k8
k3

linked list in the hash table slot. m–1

 Open Addressing (Linear Probing):


» All elements stored in hash table
itself.
» When collisions occur, use a
systematic (consistent) procedure to
store elements in free slots of the
table.
AAD CSE SRM-AP 8
Collision Resolution by
Chaining
0
U
(universe of keys) h(k1)=h(k4)
X
k1
k4
K
(actual k2 k6 X
k5 h(k2)=h(k5)=h(k6)
keys)
k8 k7
k3
X h(k3)=h(k7)
h(k8)
m–1
AAD CSE SRM-AP 9
Collision Resolution by
Chaining
0
U
(universe of keys) k1 k4

k1
k4
K
(actual k2 k6
k5 k5 k2 k6
keys)
k8 k7
k3
k7 k3

k8
m–1
AAD CSE SRM-AP 10
Hashing with Chaining
Dictionary Operations:
Chained-Hash-Insert (T, x)
 Insert x at the head of list T[h(key[x])].

 Worst-case complexity – O(1).

Chained-Hash-Delete (T, x)
 Delete x from the list T[h(key[x])].

 Worst-case complexity – proportional to length of list with

singly-linked lists. O(1) with doubly-linked lists.


Chained-Hash-Search (T, k)
 Search an element with key k in list T[h(k)].

 Worst-case complexity – proportional to length of list.

AAD CSE SRM-AP 11


Closed Hashing I: Linear
Probing
Main Idea: When collision occurs, scan
down the array one cell at a time looking
for an empty cell
 hi(X) = (Hash(X) + i) mod TableSize (i = 0, 1,
2, …)
 Compute hash value and increment it until a free
cell is found

12
Linear Probing Example
insert(14) insert(8) insert(21) insert(2)
14%7 = 0 8%7 = 1 21%7 =0 2%7 = 2
0 0 0 0
14 14 14 14
1 1 1 1
8 8 8
2 2 2 2
21 12
3 3 3 3
2
4 4 4 4

5 5 5 5

6 6 6 6

1 1 3 2
probes:
13
Drawbacks of Linear Probing
Works until array is full, but as number of items N
approaches TableSize (  1), access time approaches
O(N)
Very prone to cluster formation (as in our example)
 If a key hashes anywhere into a cluster, finding a free
cell involves going through the entire cluster – and
making it grow!
 Primary clustering – clusters grow when keys hash to
values close to each other
Can have cases where table is empty except for a few
clusters
 Does not satisfy good hash function criterion of
distributing keys uniformly

14
Basic Operations
Search − Searches an element in a
hash table.
Insert − inserts an element in a hash
table.
delete − Deletes an element from a
hash table.

AAD CSE SRM-AP 15


Insertion
HASH-INSERT (T, k)
1. i ← 0
2. repeat j ← h (k, i)
3. if T [j] = NIL then
4. T [j] ← k
5. return j
6. else ← i= i +1
7. until i=m
8. error "hash table overflow"
AAD CSE SRM-AP 16
Search
HASH-SEARCH.T (k)
1. i ← 0
2. repeat j ← h (k, i)
3. if T [j] =j then
4. return j
5. i ← i+1
6. until T [j] = NIL or i=m
7. return NIL
AAD CSE SRM-AP 17

You might also like