Open addressing
Another approach to collisions
- no chaining; instead all items stored in a table
- one item per slot ⇒ m ≥ n
- hash function specifies the order of slots to probe (try) for a key (for insert/search/delete), not just one slot; in math. notation:
We want to design a function h, with the property that for all k ∈ U:
h : U × {0, 1,...,m − 1} → {0, 1,...,m − 1} , U = universal of keys, {} = trial count, {} = slot in table
<h(k, 0), h(k, 1), . . . , h(k, m − 1)> is a permutation of 0, 1, . . . , m − 1
Insert(K, V): Keep probing until an empty slot is found; Insert item when found.
Search(K): As long as the slots encountered are occupied by keys != k, keep probing until you either encounter k or find an empty slot
Delete(K):
- can’t just find item and remove it from its slot (i.e. set T[h(k, i)] = None)
- replace item with special flag: “DeleteMe”, which Insert treats as None but Search doesn’t
Probing strategies
Linear Probing
h(k, i) = (h'(k) + i) mod m where h'(k) is ordinary hash function
- clustering - cluster: consecutive group of occupied slots as clusters become longer, it gets more likely to grow further
- can be shown that for 0.01 < α < 0.99 say, clusters of size Θ(log n).
Double Hashing
h(k, i) =(h1(k) +i·h2(k)) mod m where h1(k) and h2(k) are two ordinary hash functions.
- actually hit all slots (permutation) if h2(k) is relatively prime to m for all k
- e.g. m = 2r , make h2(k) always odd
Uniform Hashing Assumption
Each key is equally likely to have any one of the m! permutations as its probe sequence
- not really true
- but double hashing can come close
Analysis
Suppose we have used open addressing to insert n items into a table of size m. Under the uniform hashing assumption, the next operation has expected cost of 1 / (1 - α), where α = n/m (<1)
Every trial, success with probability at least p. Expected Number of trials for success? 1/p = 1/(1-α)
Cryptographic Hashing
A cryptographic hash function is a deterministic procedure that takes an arbitrary block of data and returns a fixed-size bit string, the (cryptographic) hash value, such that an accidental or intentional change to the data will change the hash value. The data to be encoded is often called the message, and the hash value is sometimes called the message digest or simply digest.
The ideal cryptographic hash function has the properties listed below. d is the number of bits in the output of the hash function. You can think of m as being 2^d. d is typically 160 or more. These hash functions can be used to index hash tables, but they are typically used in computer security applications.
Desired Properties
- One-Way(OW): given h(x) = Q, it is very hard to find x satisfy h(x) = Q
- Collision-resistance (CR): Infeasible to find x, x', s.t. x = x' and h(x) = h(x'). This is a collision, two input values have the same hash.
- Target collision-resistance (TCR): Infeasible given x to find x' = x s.t. h(x) = h(x').