Title: Maps, Dictionaries, Hashing
1Maps, Dictionaries, Hashing
2Outline and Reading
- Map ADT (9.1)
- Dictionary ADT (9.5)
- Hash Tables (9.2)
- Ordered Maps (9.3)
3Map ADT
- The map ADT models a searchable collection of
key-element items - The main operations of a map are searching,
inserting, and deleting items - Multiple items with the same key are not allowed
- Applications
- address book
- mapping host names (e.g., cs16.net) to internet
addresses (e.g., 128.148.34.101)
- Map ADT methods
- find(k) if M has an entry with key k, return an
iterator p referring to this element, else,
return special end iterator. - put(k, v) if M has no entry with key k, then add
entry (k, v) to M, otherwise replace the value of
the entry with v return iterator to the
inserted/modified entry - erase(k) or erase(p) remove from M entry with
key k or iterator p An error occurs if there
is no such element. - size(), isEmpty()
4Map - Direct Address Table
- A direct address table is a map in which
- The keys are in the range 0,1,2,,N-1
- Stored in an array of size N - T0,N-1
- Item with key k stored in Tk
- Performance
- insertItem, find, and removeElement all take O(1)
time - Space - requires space O(N), independent of n,
the number of items stored in the map - The direct address table is not space efficient
unless the range of the keys is close to the
number of elements to be stored in the map, I.e.,
unless n is close to N.
5Dictionary ADT
- The dictionary ADT models a searchable collection
of key-element items - The main difference from a map is that multiple
items with the same key are allowed - Any data structure that supports a dictionary
also supports a map - Applications
- Dictionary that has multiple definitions for the
same word
- Dictionary ADT methods
- find(k) if the dictionary has an entry with key
k, returns an iterator p to an arbitrary element - findAll(k) Return iterators (b,e) s.t. that all
entries with key k are between them - insert(k, v) insert entry (k, v) into D, return
iterator to it - erase(k), erase(p) remove arbitrary entry with
key k or entry referenced by iterator p. Error
occurs if there is no such entry - Begin(), end() return iterator to first or just
beyond last entry of D - size(), isEmpty()
6Map/Dictionary - Log File (unordered sequence
implementation)
- A log file is a dictionary implemented by means
of an unsorted sequence - We store the items of the dictionary in a
sequence (based on a doubly-linked lists or a
circular array), in arbitrary order - Performance
- insert takes O(1) time since we can insert the
new item at the beginning or at the end of the
sequence - find and erase take O(n) time since in the worst
case (item is not found) we traverse the entire
sequence to find the item with the given key - Space - can be O(n), where n is the number of
elements in the dictionary - The log file is effective only for dictionaries
of small size or for dictionaries on which
insertions are the most common operations, while
searches and removals are rarely performed (e.g.,
historical record of logins to a workstation)
7Map/Dictionarie implementations
- n - elements in map/Dictionary
Insert Find Space
Log File O(1) O(n) O(n)
Direct Address Table (map only) O(1) O(1) O(N)
8Hash Tables
- Hashing
- Hash table (an array) of size N, H0,N-1
- Hash function h that maps keys to indices in H
- Issues
- Hash functions - need method to transform key to
an index in H that will have nice properties. - Collisions - some keys will map to the same index
of H (otherwise we have a Direct Address Table).
Several methods to resolve the collisions - Chaining - put elements that hash to same
location in a linked list - Open addressing - if a collision occurs, have a
method to select another location in the table
9Hash Functions and Hash Tables
- A hash function h maps keys of a given type to
integers in a fixed interval 0, N - 1 - Example h(x) x mod Nis a hash function for
integer keys - The integer h(x) is called the hash value of key x
- A hash table for a given key type consists of
- Hash function h
- Array (called table) of size N
- When implementing a dictionary with a hash table,
the goal is to store item (k, o) at index i
h(k)
10Example
- We design a hash table for a dictionary storing
items (SSN, Name), where SSN (social security
number) is a nine-digit positive integer - Our hash table uses an array of size N 10,000
and the hash functionh(x) last four digits of x
11Collisions
- Collisions occur when different elements are
mapped to the same cell - collisions must be resolved
- Chaining (store in list outside the table)
- Open addressing (store in another cell in the
table)
- Example with Modulo Method
- h(k) k mod N
- If N10, then
- h(k)0 for k0,10,20,
- h(k) 1 for k1, 11, 21, etc
-
12Collision Resolution with Chaining
- Collisions occur when different elements are
mapped to the same cell - Chaining let each cell in the table point to a
linked list of elements that map there
- Chaining is simple, but requires additional
memory outside the table
13Exercise chaining
- Assume you have a hash table H with N9 slots
(H0,8) and let the hash function be h(k)k mod
N. - Demonstrate (by picture) the insertion of the
following keys into a hash table with collisions
resolved by chaining. - 5, 28, 19, 15, 20, 33, 12, 17, 10
14Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
0
1
2
3
4
5
6
7
8
9
10
11
12
15Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
18
0
1
2
3
4
5
6
7
8
9
10
11
12
16Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
0
1
2
3
4
5
6
7
8
9
10
11
12
17Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
22
0
1
2
3
4
5
6
7
8
9
10
11
12
18Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
22
0
1
2
3
4
5
6
7
8
9
10
11
12
19Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
22
0
1
2
3
4
5
6
7
8
9
10
11
12
20Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
22
0
1
2
3
4
5
6
7
8
9
10
11
12
21Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
22
0
1
2
3
4
5
6
7
8
9
10
11
12
22Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
22
0
1
2
3
4
5
6
7
8
9
10
11
12
23Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
0
1
2
3
4
5
6
7
8
9
10
11
12
24Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
0
1
2
3
4
5
6
7
8
9
10
11
12
25Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
0
1
2
3
4
5
6
7
8
9
10
11
12
26Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
0
1
2
3
4
5
6
7
8
9
10
11
12
27Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
0
1
2
3
4
5
6
7
8
9
10
11
12
28Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
0
1
2
3
4
5
6
7
8
9
10
11
12
29Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
31
0
1
2
3
4
5
6
7
8
9
10
11
12
30Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
31
0
1
2
3
4
5
6
7
8
9
10
11
12
31Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
31
0
1
2
3
4
5
6
7
8
9
10
11
12
32Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
31
0
1
2
3
4
5
6
7
8
9
10
11
12
33Collision Resolution in Open Addressing - Linear
Probing
- Open addressing the colliding item is placed in
a different cell of the table - Linear probing handles collisions by placing the
colliding item in the next (circularly) available
table cell. So the i-th cell checked is - H(k,i) (h(k)i)mod N
- Each table cell inspected is referred to as a
probe - Colliding items lump together, causing future
collisions to cause a longer sequence of probes
- Example
- h(x) x mod 13
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
41
18
44
59
32
22
31
73
0
1
2
3
4
5
6
7
8
9
10
11
12
34Search with Linear Probing
- Consider a hash table A that uses linear probing
- find(k)
- We start at cell h(k)
- We probe consecutive locations until one of the
following occurs - An item with key k is found, or
- An empty cell is found, or
- N cells have been unsuccessfully probed
Algorithm find(k) i ? h(k) p ? 0 repeat c ?
Ai if c ? return Position(null) else
if c.key () k return Position(c)
else i ? (i 1) mod N p ? p 1 until
p N return Position(null)
35Updates with Linear Probing
- To handle insertions and deletions, we introduce
a special object, called AVAILABLE, which
replaces deleted elements - removeElement(k)
- We search for an item with key k
- If such an item (k, o) is found, we replace it
with the special item AVAILABLE and we return the
position of this item - Else, we return a null position
- insertItem(k, o)
- We throw an exception if the table is full
- We start at cell h(k)
- We probe consecutive cells until one of the
following occurs - A cell i is found that is either empty or stores
AVAILABLE, or - N cells have been unsuccessfully probed
- We store item (k, o) in cell i
36Exercise Linear Probing
- Assume you have a hash table H with N11 slots
(H0,10) and let the hash function be h(k)k mod
N. - Demonstrate (by picture) the insertion of the
following keys into a hash table with collisions
resolved by linear probing. - 10, 22, 31, 4, 15, 28, 17, 88, 59
37Open Addressing Double Hashing
- Common choice of compression map for the
secondary hash function d2(k) q - (k mod q) - where
- q lt N
- q is a prime
- The possible values for d2(k) are 1, 2, , q
- Double hashing uses a secondary hash function
d(k) and handles collisions by placing an item
in the first available cell of the seriesh(k,i)
(h(k) id(k)) mod N for i 0, 1, , N - 1 - The secondary hash function d(k) cannot have zero
values - The table size N must be a prime to allow probing
of all the cells
38Example of Double Hashing
- Consider a hash table storing integer keys that
handles collision with double hashing - N 13
- h(k) k mod 13
- d(k) 7 - (k mod 7)
- Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
this order
0
1
2
3
4
5
6
7
8
9
10
11
12
31
41
18
32
59
73
22
44
0
1
2
3
4
5
6
7
8
9
10
11
12
39Exercise Double Hashing
- Assume you have a hash table H with N11 slots
(H0,10) and let the hash functions for double
hashing be - h(k,i)(h(k) ih2(k))mod N
- h(k)k mod N
- h2(k)1 (k mod (N-1))
- Demonstrate (by picture) the insertion of the
following keys into H - 10, 22, 31, 4, 15, 28, 17, 88, 59
40Hash Functions
- A hash function is usually specified as the
composition of two functions - Hash code map h1 keys ? integers
- Compression map h2 integers ? 0, N - 1
- The hash code map is applied first, and the
compression map is applied next on the result,
i.e., h(x) h2(h1(x)) - The goal of the hash function is to disperse
the keys in an apparently random way
41Hash Code Maps
- Memory address
- We reinterpret the memory address of the key
object as an integer - Good in general, except for numeric and string
keys - Integer cast
- We reinterpret the bits of the key as an integer
- Suitable for keys of length less than or equal to
the number of bits of the integer type (e.g.,
char, short, int and float on many machines)
- Component sum
- We partition the bits of the key into components
of fixed length (e.g., 16 or 32 bits) and we sum
the components (ignoring overflows) - Suitable for numeric keys of fixed length greater
than or equal to the number of bits of the
integer type (e.g., long and double on many
machines)
42Hash Code Maps (cont.)
- Polynomial accumulation
- We partition the bits of the key into a sequence
of components of fixed length (e.g., 8, 16 or 32
bits) a0 a1 an-1 - We evaluate the polynomial
- p(z) a0 a1 z a2 z2 an-1zn-1
- at a fixed value z, ignoring overflows
- Especially suitable for strings (e.g., the choice
z 33 gives at most 6 collisions on a set of
50,000 English words)
- Polynomial p(z) can be evaluated in O(n) time
using Horners rule - The following polynomials are successively
computed, each from the previous one in O(1) time - p0(z) an-1
- pi (z) an-i-1 zpi-1(z) (i 1, 2, , n
-1) - We have p(z) pn-1(z)
43Compression Maps
- Division
- h2 (y) y mod N
- The size N of the hash table is usually chosen to
be a prime - The reason has to do with number theory and is
beyond the scope of this course
- Multiply, Add and Divide (MAD)
- h2 (y) (ay b) mod N
- a and b are nonnegative integers such that a
mod N ? 0 - Otherwise, every integer would map to the same
value b
44Performance of Hashing
- In the worst case, searches, insertions and
removals on a hash table take O(n) time - The worst case occurs when all the keys inserted
into the dictionary collide - The load factor a n/N affects the performance
of a hash table - Assuming that the hash values are like random
numbers, it can be shown that the expected number
of probes for an insertion with open addressing
is 1 / (1 - a)
- The expected running time of all the dictionary
ADT operations in a hash table is O(1) - In practice, hashing is very fast provided the
load factor is not close to 100 - Applications of hash tables
- small databases
- compilers
- browser caches
45Uniform Hashing Assumption
- The probe sequence of a key k is the sequence of
slots that will be probed when looking for k - In open addressing, the probe sequence is h(k,0),
h(k,1), h(k,2), h(k,3), - Uniform Hashing Assumption Each key is equally
likely to have any one of the N! permutations of
0,1, 2, , N-1 as is probe sequence - Note Linear probing and double hashing are far
from achieving Uniform Hashing - Linear probing N distinct probe sequences
- Double Hashing N2 distinct probe sequences
46Performance of Uniform Hashing
- Theorem Assuming uniform hashing and an
open-address hash table with load factor a n/N
lt 1, the expected number of probes in an
unsuccessful search is at most 1/(1-a). - Exercise compute the expected number of probes
in an unsuccessful search in an open address hash
table with a ½ , a3/4, and a 99/100.
47Maps/Dictionaries
- n elements in map/dictionary,
- Npossible keys (it could be Ngtgtn) or size of
hash table
Insert Find Space
Log File O(1) O(n) O(n)
Direct Address Table (map only) O(1) O(1) O(N)
Hashing (chaining) O(1) O(n/N) O(nN)
Hashing (open addressing) O(1/(1-n/N)) O(1/(1-n/N)) O(N)
48Ordered Map
- In an ordered Map, we wish to perform the usual
map operations, but also maintain an order
relation for the keys in the dictionary. - Naturally supports
- Look-Up Tables - store dictionary in a vector by
non-decreasing order of the keys - Binary Search
- Ordered Dictionary ADT
- In addition to the generic dictionary ADT, the
ordered dictionary ADT supports the following
functions - closestBefore(k) return the position of an item
with the largest key less than or equal to k - closestAfter(k) return the position of an item
with the smallest key greater than or equal to k
49Lookup Table
- A lookup table is a dictionary implemented by
means of a sorted sequence - We store the items of the dictionary in an
array-based sequence, sorted by key - We use an external comparator for the keys
- Performance
- find takes O(log n) time, using binary search
- insertItem takes O(n) time since in the worst
case we have to shift n/2 items to make room for
the new item - removeElement take O(n) time since in the worst
case we have to shift n/2 items to compact the
items after the removal - The lookup table is effective only for
dictionaries of small size or for dictionaries on
which searches are the most common operations,
while insertions and removals are rarely
performed (e.g., credit card authorizations)
50Example of Ordered Map Binary Search
- Binary search performs operation find(k) on a
dictionary implemented by means of an array-based
sequence, sorted by key - similar to the high-low game
- at each step, the number of candidate items is
halved - terminates after a logarithmic number of steps
- Example find(7)
1
3
4
5
7
8
9
11
14
16
18
19
0
m
l
h
1
3
4
5
7
8
9
11
14
16
18
19
0
m
l
h
1
3
4
5
7
8
9
11
14
16
18
19
0
m
h
l
1
3
4
5
7
8
9
11
14
16
18
19
0
lm h
51(No Transcript)
52Universal Hashing
- A family of hash functions is universal if, for
any 0lti,jltM-1, - Pr(h(j)h(i)) lt 1/N.
- Choose p as a prime between M and 2M.
- Randomly select 0ltaltp and 0ltbltp, and define
h(k)(akb mod p) mod N
- Theorem The set of all functions, h, as defined
here, is universal.
53Proof of Universality (Part 1)
- Let f(k) akb mod p
- Let g(k) k mod N
- So h(k) g(f(k)).
- f causes no collisions
- Let f(k) f(j).
- Suppose kltj. Then
- So a(j-k) is a multiple of p
- But both are less than p
- So a(j-k) 0. I.e., jk. (contradiction)
- Thus, f causes no collisions.
54Proof of Universality (Part 2)
- If f causes no collisions, only g can make h
cause collisions. - Fix a number x. Of the p integers yf(k),
different from x, the number such that g(y)g(x)
is at most - Since there are p choices for x, the number of
hs that will cause a collision between j and k
is at most - There are p(p-1) functions h. So probability of
collision is at most -
- Therefore, the set of possible h functions is
universal.