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

Hashing

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

Hashing

Hashing Notes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Hashing

General Idea
• Facilitates search ideally in O(1) time
• The ideal hash table structure is merely an array of some fixed size,
containing the items.
• A stored item needs to have a data member, called key, that will be used in
computing the index value for the item.
• Key could be an integer, a string, etc
▪ e.g. a name or Id that is a part of a large employee structure
• The size of the array is TableSize.
• The items that are stored in the hash table are indexed by values from 0 to
TableSize – 1.
• Each key is mapped into some number in the range 0 to TableSize – 1.
• The mapping is called a hash function.
Example Hash
Table
0
1
Items
2
john 25000
3 john 25000
Hash
phil 31250 key 4 phil 31250
Functio
dave 27500 n 5
mary 28200 6 dave 27500
7 mary 28200
key 8
9
Hash Function
• Function H from set of K keys to set of L memory locations
▪ H:K→L
• The hash function:
▪ must be simple to compute.
▪ must distribute the keys evenly among the cells.
• If we know which keys will occur in advance we can write
perfect hash functions, but we don’t
• Problems:
▪ Keys may not be numeric.
▪ Number of possible keys is much larger than the space available in
table.
• Different keys may map into same location
▪ Hash function is not one-to-one => collision.
▪ If there are too many collisions, the performance of the hash table will
Some popular hash functions

• Division Method
• Midsquare Mthod
• Folding Method
Division Method

• h(k) = k mod M
• Generally, it is best to choose M to be a prime
number because making M a prime increases
the likelihood that the keys are mapped with a
uniformity in the output range of values.
Midsquare Method
• Step 1: Square the value of the key. That is, find k2
• Step 2: Extract the middle r bits of the result obtained
in Step 1 where r is the size of the address of location
Example: Calculate the hash value for keys 1234 and 5642 using the mid
square method. The hash table has 100 memory locations.

Note the hash table has 100 memory locations whose indices vary from
0-99. this means, only two digits are needed to map the key to a location in
the hash table, so r = 2.

When k = 1234, k2 = 1522756, h (k) = 27


When k = 5642, k2 = 31832164, h (k) = 21

Observe that 3rd and 4th digits starting from the right are chosen.
Folding Method
• The folding method works in two steps.
• Step 1: Divide the key value into a number of parts. That is divide k into
parts, k1, k2, …, kn, where each part has the same number of digits except
the last part which may have lesser digits than the other parts.
• Step 2: Add the individual parts. That is obtain the sum of k1 + k2 + .. +
kn. Hash value is produced by ignoring the last carry, if any.
• Note that the number of digits in each part of the key will vary depending
upon the size of the hash table. For example, if the hash table has a size of
1000. Then it means there are 1000 locations in the hash table. To address
these 1000 locations, we will need at least three digits, therefore, each part
of the key must have three digits except the last part which may have lesser
digits.
Collision Resolution

• Collision occurs when the hash function maps


two different keys to same location
• Methods for handling collision:
• Open addressing
▪ Linear Probing
▪ Quadratic Probing
▪ Double Hashing
• Chaining
Collision Resolution with Open Addressing
• In an open addressing hashing system, all the data go
inside the table.
– Thus, a bigger table is needed.
• Generally the load factor should be below 0.5.
– If a collision occurs, alternative cells are tried until an empty cell is
found.
• More formally:
– Cells h0(x), h1(x), h2(x), …are tried in succession where hi(x) =
(hash(x) + f(i)) mod TableSize, with f(0) = 0.
– The function f is the collision resolution strategy.
• There are three common collision resolution strategies:
– Linear Probing
– Quadratic probing
– Double hashing
Linear Probing

• In linear probing, collisions are resolved by


sequentially scanning an array (with
wraparound) until an empty cell is found.
▪ i.e. f is a linear function of i, typically f(i)= i.
• Example:
▪ Insert items with keys: 89, 18, 49, 58, 9 into an
empty hash table.
▪ Table size is 10.
▪ Hash function is hash(x) = x mod 10.
o f(i) = i;
Linear probing
hash table after
each insertion
Find and Delete

• The find algorithm follows the same probe


sequence as the insert algorithm.
▪ A find for 58 would involve 4 probes.
▪ A find for 19 would involve 5 probes.
• We must use lazy deletion (i.e. marking items
as deleted)
▪ Standard deletion (i.e. physically removing the
item) cannot be performed.
▪ e.g. remove 89 from hash table
Clustering Problem

• As long as table is big enough, a free cell can


always be found, but the time to do so can get
quite large.
• Worse, even if the table is relatively empty,
blocks of occupied cells start forming.
• This effect is known as primary clustering.
• Any key that hashes into the cluster will
require several attempts to resolve the
collision, and then it will add to the cluster.
Quadratic Probing

• Quadratic Probing eliminates primary clustering


problem of linear probing.
• Collision function is quadratic.
▪ The popular choice is f(i) = i2.
• If the hash function evaluates to h and a search in cell
h is inconclusive, we try cells h + 12, h+22, … h + i2.
▪ i.e. It examines cells 1,4,9 and so on away from the original
probe.
• Remember that subsequent probe points are a
quadratic number of positions from the original probe
point.
A quadratic probing
hash table after
each insertion (note
that the table size
was poorly chosen
because it is not a
prime number).
Double Hashing

• A second hash function is used to drive the collision


resolution.
▪ f(i) = i * hash2(x)
• We apply a second hash function to x and probe at a
distance hash2(x), 2*hash2(x), … and so on.
• The function hash2(x) must never evaluate to zero.
▪ e.g. Let hash2(x) = x mod 9 and try to insert 99 in the
previous example.
Chaining
• The idea is to keep a list of all elements that hash
to the same value.
– The array elements are pointers to the first nodes of the
lists.
– A new item is inserted to the front of the list.
• Advantages:
– Better space utilization for large items.
– Simple collision handling: searching linked list.
– Overflow: we can store more items than the hash table
size.
– Deletion is quick and easy: deletion from the linked list.
Example
Keys: 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
hash(key) = key % 10.
0 0
1 81 1
2

4 64 4
5 25
6 36 16
7

9 49 9
Operations
• Initialization: all entries are set to NULL
• Find:
– locate the cell using hash function.
– sequential search on the linked list in that cell.
• Insertion:
– Locate the cell using hash function.
– (If the item does not exist) insert it as the first item in
the list.
• Deletion:
– Locate the cell using hash function.
– Delete the item from the linked list.

You might also like