Prev:
W10 , Next:
W12
Links:
Zoom ,
TopHat (010339, or
Form ), Calculator:
Eval
Slide:
Go All Prev Next
# Hashing
📗 (key, value) pairs.
📗 Goal: efficient insert, look up, and delete pairs.
➩ Linked lists: \(O\left(N\right)\).
➩ Search trees: \(O\left(\log\left(N\right)\right)\).
# Perfect Hash Function
📗 Hash functions: key (large range) -> location (smaller range).
📗 Example:
➩ Want to store 100 student records.
➩ Use an array of size 100.
➩ Use students IDs as keys: 11000, 11001, 11002, ..., 11099.
➩ Hash function: index = key - 11000?
📗 A perfect function maps every key to a unique hash index.
# Operations
📗 K: key type, V: value type, field: V[] array.
📗 void insert(K key, V value): array[hash(key)] = value;
📗 V lookup(K key): return array[hash(key)];
📗 void remove(K key): array[hash(key)] = null;
# UW Student IDs Example
📗 10 digit numbers: 9021453190, 9033879101, 9024357190
📗 Is there a perfect hash function? Properties of a Good Hash Function:
➩ Must be deterministic.
➩ Should achieve uniform distribution across output range.
➩ Should minimize collisions.
➩ Should be fast and easy to compute.
# Java API for Hash Functions
📗 int hashCode() method:
➩ Instance method of Object type.
➩ Java has implementations for built-in data types (String, Double, etc).
➩ Can override it for data types.
📗 Steps for using int hashCode():
➩ Call hashCode() on key object.
➩ Convert result (in range of integer) to valid hash index (abs, modulo): Math.abs(key.hasCode()) % array.length.
# Hash Table Properties
📗 Hash table: array that contains (key, value) pairs.
📗 Table size: current capacity (array length).
📗 Load factor (LF): number of key, value pairs in table divided by table size.
📗 Experiment repeated 10 times per table size:
➩ 100 random integer keys.
➩ "Hash function": abs(key) % size.
# Collision Probability
📗 How often do collisions occur for different table sizes?
# keys
table size
load factor
# of collisions
100
10000
0.01
0 or 1
100
1000
0.1
3 to 7
100
100
1
35 to 47
100
10
10
90
ID:
Confirm
# When to Resize
📗 When the table is "full": load factor > load factor threshold.
📗 Who defines the threshold: user of data structure with good default 0.7 to 0.8.
📗 Resizing complexity:
➩ Resizing: \(O\left(1\right)\)
➩ Rehashing: \(O\left(N\right)\), where N is the number of keys in the old hash table.
➩ Amortized over many inserts: \(O\left(1\right)\).
📗 Example:
➩ Hash function: key % size
➩ LF threshold: 0.7
# Collision Handling: Open Addressing
📗 Each element of hash table stores at most one key, value pair.
📗 If there is a collision, look for next "open address".
📗 Linear probing:
➩ Probe sequence: \(H_{K}, H_{K} + 1, H_{K} + 2, H_{K} + 3, ...\), \(H_{K}\) = hash index.
📗 Quadratic probing:
➩ Add polynomials of order 2: \(H_{K} + c_{0} i + c_{1} i^{2}\) (usually \(c_{0} = 0\) and \(c_{1} = 1\)), \(i\) = step number.
➩ Probe sequence: \(H_{K}, H_{K} + 1^{2}, H_{K} + 2^{2}, H_{K} + 3^{2}, ...\).
📗 Double Hashing: second hash function for step size (s) s = hash2(key).
➩ Probing sequence: \(H_{K}, H_{K} + s, H_{K} + 2 s, H_{K} + 3 s, ...\).
# Collision Handling: Chaining
📗 Each element of the hash table is a "chain" that can hold multiple (key, value) pairs.
# Complexities
📗 Insert (put): worst case: \(O\left(N\right)\), average case: \(O\left(1\right)\), best case: \(O\left(1\right)\).
📗 Look up (get): worst case: \(O\left(N\right)\), average case: \(O\left(1\right)\), best case: \(O\left(1\right)\).
📗 Remove: worst case: \(O\left(N\right)\), average case: \(O\left(1\right)\), best case: \(O\left(1\right)\).
# Readings
📗 Hashing and Hash Tables:
Link .
test col,prb,chn l
📗 Notes and code adapted from the course taught by Professor Florian Heimerl and Ashley Samuelson
.
📗 Please use Ctrl+F5 or Shift+F5 or Shift+Command+R or Incognito mode or Private Browsing to refresh the cached JavaScript.
📗 You can expand all the examples and demos: Expand , or print the notes: Print .
📗 If there is an issue with TopHat during the lectures, please submit your answers on paper (include your Wisc ID and answers) or this Google
Form at the end of the lecture.
Prev:
W10 , Next:
W12
Last Updated: November 25, 2025 at 1:46 AM