7.- Hashing, extendible hashing
7.- Hashing, extendible hashing
Hashing,
extendible hashing
1/22
7. Hashing, extendible
hashing
What is hashing?
When you take any type of data and assign an integer value to it.
It involves less key comparisons and searching can be performed in a constant time
2/22
7. Hashing, extendible
hashing
Hash Function
A hash function is used to map data of arbitrary size to data of a fixed size.
Hash functions are often used in combination with hash tables, a common
data structure used for rapid data lookup.
3/22
7. Hashing, extendible
hashing
Example of hashing
Note that there are two keys assigned for the same
hash value; it is called a “collision”, and it is a hash
conflict.
4/22
7. Hashing, extendible
hashing
Insertion O(1)
Deletion O(1)
5/22
7. Hashing, extendible
hashing
*Associative array: a.k.a. map, symbol table, or dictionary, is an abstract data type composed of a
collection of (key, value) pairs, such that each possible key appears at most once in the collection.
6/22
7. Hashing, extendible
hashing
No hash conflict.
7/22
7. Hashing, extendible
hashing
There are some ways to deal with hash conflicts, mentioned below:
2. Linear probing
3. Quadratic probing
4. Double hashing
8/22
7. Hashing, extendible
hashing
Example:
Before After
9/22
7. Hashing, extendible
hashing
2. Linear probing
When a collision happens because of the insertion of a new key to an already occupied
slot, it searches the table for the closest following free location and inserts the new key
there.
Lookups are performed in the same way, by searching the table sequentially starting at
the position given by the hash function, until finding a cell with a matching key or an
empty cell.
10/22
7. Hashing, extendible
hashing
11/22
7. Hashing, extendible
hashing
3. Quadratic probing
It operates by taking the original hash index and adding successive values of an arbitrary
quadratic polynomial until an open slot is found.
It better avoids the clustering problem that can occur with linear probing, although it is
not immune to it.
12/22
7. Hashing, extendible
hashing
13/22
7. Hashing, extendible
hashing
4. Double hashing
• Double hashing is similar to linear probing and the only difference is the interval between
successive probes. Here, the interval between probes is computed by using 2 Hash
functions.
• The hashed index for an entry record is an index that is computed by one hashing function
and the slot at that index is already occupied. We must traverse in a specific probing
sequence to look for unoccupied slot.
14/22
7. Hashing, extendible
hashing
15/22
7. Hashing, extendible
hashing
What is rehashing?
Operation by which we create another hash table twice as big as the original one (using
a new hash function), because the old table gets too full (load factor lambda > 50%).
Very expensive operation: running time complexity of O(N); since there are N elements
to rehash.
The size of the new table will be the first prime that is twice as large as the old table
size.
An insert fails
17/22
7. Hashing, extendible
hashing
18/22
Example
19
7. Hashing, extendible
hashing
Thank you!
20/22