Lecture 12
Lecture 12
2
Searching
3
Hashing
• Method for storing and retrieving data in O(1) time
• Also called mapping technique
• Because we try to map larger values to smaller
values by using the concept of hashing
• Search key
• Hash table
• Hash function(k mod n)(for insertion, deletion,
searching)
• K mode n -> hash table is from 0 to n-1
4
Collision in Hashing
5
Chaining in Hashing
• Chaining (open hashing)
• Chain is nothing but a linked list
6
Direct Addressing (cont’d)
7
Hash Tables
• Is a data structure used to store info
• When K is much smaller than U, a hash table
requires much less space than a direct-address
table
– Can reduce storage requirements to |K|
– Can still get O(1) search time, but on the average
case, not the worst case
8
Hash Tables
Idea:
– Use a function h to compute the slot for each key
– Store the element in slot h(k)
9
Example: HASH TABLES
0
U
(universe of keys) h(k1)
h(k4)
K k1
h(k2) = h(k5)
(actual k4 k2
keys)
k5 k3 h(k3)
m-1
10
Revisit Example 2
11
Do you see any problems
with this approach?
0
U
(universe of keys) h(k1)
h(k4)
K k1
h(k2) = h(k5)
(actual k4 k2
keys) Collisions!
k5 k3 h(k3)
m-1
12
Collisions
• Two or more keys hash to the same slot!!
• For a given set K of keys
– If |K| ≤ m, collisions may or may not happen,
depending on the hash function
– If |K| > m, collisions will definitely happen (i.e., there
must be at least two keys that have the same hash
value)
• Avoiding collisions completely is hard, even with
a good hash function
13
Handling Collisions Using Chaining
• Idea:
– Put all elements that hash to the same slot into a
linked list
15
Insertion in Hash Tables
Alg.: CHAINED-HASH-INSERT(T, x)
insert x at the head of list T[h(key[x])]
17
Searching in Hash Tables
Alg.: CHAINED-HASH-SEARCH(T, k)
18
Analysis of Hashing with Chaining:
Worst Case
• How long does it take to T
0
search for an element with a
given key?
• Worst case:
– All n keys hash to the same slot
– Worst-case time to search is
chain
(n), plus time to compute the
hash function m-1
19