hashing part one -...

98
Advanced Algorithms – COMS31900 Hashing part one Chaining, true randomness and universal hashing Rapha ¨ el Clifford Slides by Benjamin Sach and Markus Jalsenius

Upload: others

Post on 11-Jul-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Advanced Algorithms – COMS31900

Hashing part one

Chaining, true randomness and universal hashing

Raphael Clifford

Slides by Benjamin Sach and Markus Jalsenius

Page 2: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Dictionaries

Often we want to perform the following three operations:

In a dictionary data structure we store (key, value)-pairs

� add(x, v) Add the the pair (x, v).

� lookup(x) Return v if (x, v) is in dictionary, or NULL otherwise.

� delete(x) Remove pair (x, v) (assuming (x, v) is in dictionary).

such that for any key there is at most one pair (key, value) in the dictionary.

Page 3: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Dictionaries

Often we want to perform the following three operations:

� Linked lists

� Binary search trees

� (2,3,4)-trees

In a dictionary data structure we store (key, value)-pairs

� add(x, v) Add the the pair (x, v).

� lookup(x) Return v if (x, v) is in dictionary, or NULL otherwise.

� delete(x) Remove pair (x, v) (assuming (x, v) is in dictionary).

There are many data structures that will do this job, e.g.:

� Red-black trees

� Skip lists

� van Emde Boas trees (later in this course)

such that for any key there is at most one pair (key, value) in the dictionary.

Page 4: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Dictionaries

Often we want to perform the following three operations:

� Linked lists

� Binary search trees

� (2,3,4)-trees

In a dictionary data structure we store (key, value)-pairs

� add(x, v) Add the the pair (x, v).

� lookup(x) Return v if (x, v) is in dictionary, or NULL otherwise.

� delete(x) Remove pair (x, v) (assuming (x, v) is in dictionary).

There are many data structures that will do this job, e.g.:

� Red-black trees

� Skip lists

� van Emde Boas trees (later in this course)

such that for any key there is at most one pair (key, value) in the dictionary.

these data structures all support extra operations beyond the three above

Page 5: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Dictionaries

Often we want to perform the following three operations:

� Linked lists

� Binary search trees

� (2,3,4)-trees

In a dictionary data structure we store (key, value)-pairs

� add(x, v) Add the the pair (x, v).

� lookup(x) Return v if (x, v) is in dictionary, or NULL otherwise.

� delete(x) Remove pair (x, v) (assuming (x, v) is in dictionary).

There are many data structures that will do this job, e.g.:

� Red-black trees

� Skip lists

� van Emde Boas trees (later in this course)

such that for any key there is at most one pair (key, value) in the dictionary.

these data structures all support extra operations beyond the three above

but none of them take O(1) worst case time for all operations. . .

Page 6: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Dictionaries

Often we want to perform the following three operations:

� Linked lists

� Binary search trees

� (2,3,4)-trees

In a dictionary data structure we store (key, value)-pairs

� add(x, v) Add the the pair (x, v).

� lookup(x) Return v if (x, v) is in dictionary, or NULL otherwise.

� delete(x) Remove pair (x, v) (assuming (x, v) is in dictionary).

There are many data structures that will do this job, e.g.:

� Red-black trees

� Skip lists

� van Emde Boas trees (later in this course)

such that for any key there is at most one pair (key, value) in the dictionary.

these data structures all support extra operations beyond the three above

but none of them take O(1) worst case time for all operations. . .

so maybe there is room for improvement?

Page 7: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Hash tables

Universe U containing u keys.

We want to store n elements from the universe, U in a dictionary.Typically u = |U | is much, much larger than n.

Page 8: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Hash tables

Universe U containing u keys.

We want to store n elements from the universe, U in a dictionary.

Array T of size m.

Typically u = |U | is much, much larger than n.

m

Page 9: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Hash tables

Universe U containing u keys.

We want to store n elements from the universe, U in a dictionary.

Array T of size m.

Typically u = |U | is much, much larger than n.

T is called a hash table.

m

Page 10: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Hash tables

Universe U containing u keys.

We want to store n elements from the universe, U in a dictionary.

Array T of size m.

A hash function h : U → [m] maps a key to a position in T .We write [m] to denote the set {0, . . . ,m− 1}.

Typically u = |U | is much, much larger than n.

T is called a hash table.

m

Page 11: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Hash tables

Universe U containing u keys.

We want to store n elements from the universe, U in a dictionary.

Array T of size m.

A hash function h : U → [m] maps a key to a position in T .

x

h(x) (x, vx)

We write [m] to denote the set {0, . . . ,m− 1}.

Typically u = |U | is much, much larger than n.

T is called a hash table.

m

Page 12: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Hash tables

Universe U containing u keys.

We want to store n elements from the universe, U in a dictionary.

Array T of size m.

A hash function h : U → [m] maps a key to a position in T .

x

h(x)

We want to avoid collisions, i.e. h(x) = h(y) for x 6= y.

(x, vx)

We write [m] to denote the set {0, . . . ,m− 1}.

Typically u = |U | is much, much larger than n.

T is called a hash table.

m

Page 13: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Hash tables

Universe U containing u keys.

We want to store n elements from the universe, U in a dictionary.

Array T of size m.

A hash function h : U → [m] maps a key to a position in T .

x

h(x)

We want to avoid collisions, i.e. h(x) = h(y) for x 6= y.

y

w

z (x, vx) (y, vy) (z, vz) (w, vw)

� Collisions can be resolved

with chaining, i.e. linked list.

We write [m] to denote the set {0, . . . ,m− 1}.

Typically u = |U | is much, much larger than n.

T is called a hash table.

m

Page 14: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Time complexity

We cannot avoid collisions entirely since u� m;

Operation Worst case time Comment

Simply add item to the list link if

necessary.

We might have to search through the

whole list containing x.

Only O(1) to perform the actual

delete. . . but you have to find x first

add(x, v) O(1)

O(length of chain containing x)lookup(x)

delete(x)

some keys from the universe are bound to be mapped to the same position.

O(length of chain containing x)

By building a hash table with chaining, we get the following time complexities:

(remember u is the size of the universe and m is the size of the table)

Page 15: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Time complexity

We cannot avoid collisions entirely since u� m;

Operation Worst case time Comment

Simply add item to the list link if

necessary.

We might have to search through the

whole list containing x.

Only O(1) to perform the actual

delete. . . but you have to find x first

add(x, v) O(1)

O(length of chain containing x)lookup(x)

delete(x)

some keys from the universe are bound to be mapped to the same position.

So how long are these chains?

O(length of chain containing x)

By building a hash table with chaining, we get the following time complexities:

(remember u is the size of the universe and m is the size of the table)

Page 16: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 17: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 18: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

Let x, y be two distinct keys from U .

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 19: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 20: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 21: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).

we have that, Pr(h(x) = h(y)

)=

1

m

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 22: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).

we have that, Pr(h(x) = h(y)

)=

1

m

this is because h(x) and h(y) are chosen uniformly and independently from [m].

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 23: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).

we have that, Pr(h(x) = h(y)

)=

1

m

this is because h(x) and h(y) are chosen uniformly and independently from [m].

Therefore, E(Ix,y) = Pr(Ix,y = 1) = Pr(h(x) = h(y)

)= 1

m .

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 24: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).

we have that, Pr(h(x) = h(y)

)=

1

m

this is because h(x) and h(y) are chosen uniformly and independently from [m].

Therefore, E(Ix,y) = Pr(Ix,y = 1) = Pr(h(x) = h(y)

)= 1

m .

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

We have that, E(Ix,y) = 1m .

Page 25: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

We have that, E(Ix,y) = 1m .

Page 26: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

We have that, E(Ix,y) = 1m .

Page 27: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

We have that, E(Ix,y) = 1m .

Page 28: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).We have that, E(Ix,y) = 1

m .

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 29: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).We have that, E(Ix,y) = 1

m .

Let Nx be the number of keys stored in T that are hashed to h(x)

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 30: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).We have that, E(Ix,y) = 1

m .

Let Nx be the number of keys stored in T that are hashed to h(x)

so, in the worst case it takes Nx time to look up x in T .

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 31: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

Observe that Nx =∑y∈T

Ix,y

.

iff means if and only if.Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).We have that, E(Ix,y) = 1

m .

Let Nx be the number of keys stored in T that are hashed to h(x)

so, in the worst case it takes Nx time to look up x in T .

the keys in T

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 32: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomnessTHEOREM

Consider any n fixed inputs to the hash table (which has size m),

PROOF

Observe that Nx =∑y∈T

Ix,y

.

iff means if and only if.

linearity of expectation.

Let x, y be two distinct keys from U .

Let indicator r.v. Ix,y be 1 iff h(x) = h(y).We have that, E(Ix,y) = 1

m .

Let Nx be the number of keys stored in T that are hashed to h(x)

so, in the worst case it takes Nx time to look up x in T .

Finally, we have that E(Nx) = E

∑y∈T

Ix,y

=∑y∈T

E(Ix,y) = n·1

m=

n

m

the keys in T

i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1 + nm ), or simply O(1) if m > n.

Pick h uniformly at random from the set of all functions U → [m].

Page 33: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

Page 34: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

this is a number in [m], so requires≈ log2 m bits.

Page 35: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

Page 36: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

Page 37: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

Why not pick the hash function as we go?

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

Page 38: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

Why not pick the hash function as we go?

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

Couldn’t we generate h(x) when we first see x?

Page 39: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

Why not pick the hash function as we go?

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

Couldn’t we generate h(x) when we first see x?

Wouldn’t we only use n log2 m bits? (one per key we actually store)

Page 40: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

Why not pick the hash function as we go?

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

Couldn’t we generate h(x) when we first see x?

Wouldn’t we only use n log2 m bits? (one per key we actually store)

The problem with this approach is recalling h(x) the next time we see x

Page 41: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

Why not pick the hash function as we go?

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

Couldn’t we generate h(x) when we first see x?

Wouldn’t we only use n log2 m bits? (one per key we actually store)

The problem with this approach is recalling h(x) the next time we see x

Essentially we’d need to build a dictionary to solve the dictionary problem!

Page 42: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

Why not pick the hash function as we go?

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

Couldn’t we generate h(x) when we first see x?

Wouldn’t we only use n log2 m bits? (one per key we actually store)

The problem with this approach is recalling h(x) the next time we see x

Essentially we’d need to build a dictionary to solve the dictionary problem!

This has become rather cyclic... let’s try something else!

Page 43: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

Page 44: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

Instead, we define a set, or family of hash functions: H = {h1, h2, . . . }.

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

Page 45: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

Instead, we define a set, or family of hash functions: H = {h1, h2, . . . }.

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

As part of initialising the hash table,

we choose the hash function h from H randomly.

Page 46: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Specifying the hash function

Problem: how do we specify an arbitrary (e.g. a truly random) hash function?

For each key in U we need to specify an arbitrary position in T ,

Instead, we define a set, or family of hash functions: H = {h1, h2, . . . }.

How should we specify the hash functions in H and how do we pick one at random?

this is a number in [m], so requires≈ log2 m bits.

So in total we need≈ u log2 m bits, which is a ridiculous amount of space!

(in particular, it’s much bigger than the table :s)

As part of initialising the hash table,

we choose the hash function h from H randomly.

Page 47: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Weakly universal hashing

� A set H of hash functions is weakly universal if for any two distinct keys x, y ∈ U ,

Pr(h(x) = h(y)

)6

1

m

where h is chosen uniformly at random from H .

Page 48: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Weakly universal hashing

� A set H of hash functions is weakly universal if for any two distinct keys x, y ∈ U ,

Pr(h(x) = h(y)

)6

1

m

where h is chosen uniformly at random from H .

OBSERVE

The randomness here comes from the fact that h is picked randomly.

Page 49: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Weakly universal hashing

� A set H of hash functions is weakly universal if for any two distinct keys x, y ∈ U ,

Pr(h(x) = h(y)

)6

1

m

where h is chosen uniformly at random from H .

OBSERVE

The randomness here comes from the fact that h is picked randomly.

THEOREM

Consider any n fixed inputs to the hash table (which has size m),i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1) if m > n.

Pick h uniformly at random from a weakly universal set H of hash functions.

Page 50: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Weakly universal hashing

� A set H of hash functions is weakly universal if for any two distinct keys x, y ∈ U ,

Pr(h(x) = h(y)

)6

1

m

where h is chosen uniformly at random from H .

PROOF

The proof we used for true randomness works here too (which is nice)

OBSERVE

The randomness here comes from the fact that h is picked randomly.

THEOREM

Consider any n fixed inputs to the hash table (which has size m),i.e. any sequence of n add/lookup/delete operations.

The expected run-time per operation is O(1) if m > n.

Pick h uniformly at random from a weakly universal set H of hash functions.

Page 51: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Constructing a weakly universal family of hash functions

� Suppose U = [u], i.e. the keys in the universe are integers 0 to u−1.

� Let p be any prime bigger than u.

� For a, b ∈ [p], let

ha,b(x) = ((ax+ b) mod p) mod m,

Hp,m = {ha,b | a ∈ {1, . . . , p− 1}, b ∈ {0, . . . , p− 1}}.

Page 52: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Constructing a weakly universal family of hash functions

� Suppose U = [u], i.e. the keys in the universe are integers 0 to u−1.

� Let p be any prime bigger than u.

� For a, b ∈ [p], let

ha,b(x) = ((ax+ b) mod p) mod m,

Hp,m = {ha,b | a ∈ {1, . . . , p− 1}, b ∈ {0, . . . , p− 1}}.

THEOREM

Hp,m is a weakly universal set of hash functions.

Page 53: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Constructing a weakly universal family of hash functions

� Suppose U = [u], i.e. the keys in the universe are integers 0 to u−1.

� Let p be any prime bigger than u.

� For a, b ∈ [p], let

ha,b(x) = ((ax+ b) mod p) mod m,

Hp,m = {ha,b | a ∈ {1, . . . , p− 1}, b ∈ {0, . . . , p− 1}}.

THEOREM

Hp,m is a weakly universal set of hash functions.

PROOF

See CLRS, Theorem 11.5, (page 267 in 3rd edition).

Page 54: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Constructing a weakly universal family of hash functions

� Suppose U = [u], i.e. the keys in the universe are integers 0 to u−1.

� Let p be any prime bigger than u.

� For a, b ∈ [p], let

ha,b(x) = ((ax+ b) mod p) mod m,

Hp,m = {ha,b | a ∈ {1, . . . , p− 1}, b ∈ {0, . . . , p− 1}}.

THEOREM

Hp,m is a weakly universal set of hash functions.

PROOF

See CLRS, Theorem 11.5, (page 267 in 3rd edition).

OBSERVE

� ax+ b is a linear transformation which “spreads the keys” over p values when

taken modulo p. This does not cause any collisions.

� Only when taken modulo m do we get collisions.

Page 55: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomness vs. weakly universal hashing

the expected lookup time in the hash table is O(1).we have seen that when m > n,

For both,

true randomness

(h is picked uniformly from the set of all possible hash functions)

and weakly universal hashing

(h is picked uniformly from a weakly universal set of hash functions)

Page 56: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomness vs. weakly universal hashing

Since constructing a weakly universal set of hash functions seems much easier

the expected lookup time in the hash table is O(1).we have seen that when m > n,

than obtaining true randomness, this is all good news!

For both,

true randomness

(h is picked uniformly from the set of all possible hash functions)

and weakly universal hashing

(h is picked uniformly from a weakly universal set of hash functions)

Page 57: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomness vs. weakly universal hashing

Since constructing a weakly universal set of hash functions seems much easier

isn’t it?

the expected lookup time in the hash table is O(1).we have seen that when m > n,

than obtaining true randomness, this is all good news!

For both,

true randomness

(h is picked uniformly from the set of all possible hash functions)

and weakly universal hashing

(h is picked uniformly from a weakly universal set of hash functions)

Page 58: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomness vs. weakly universal hashing

Since constructing a weakly universal set of hash functions seems much easier

What about the length of the longest chain? (the longest linked list)

isn’t it?

the expected lookup time in the hash table is O(1).we have seen that when m > n,

than obtaining true randomness, this is all good news!

For both,

true randomness

(h is picked uniformly from the set of all possible hash functions)

and weakly universal hashing

(h is picked uniformly from a weakly universal set of hash functions)

Page 59: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

True randomness vs. weakly universal hashing

Since constructing a weakly universal set of hash functions seems much easier

What about the length of the longest chain? (the longest linked list)

isn’t it?

If it is very long, some lookups could take a very long time. . .

the expected lookup time in the hash table is O(1).we have seen that when m > n,

than obtaining true randomness, this is all good news!

For both,

true randomness

(h is picked uniformly from the set of all possible hash functions)

and weakly universal hashing

(h is picked uniformly from a weakly universal set of hash functions)

Page 60: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

If h is selected uniformly at random from all functions U → [m] then,

Pr (any chain has length > 3 logm ) 61

m.

LEMMA

over m fixed inputs,

Page 61: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

If h is selected uniformly at random from all functions U → [m] then,

Pr (any chain has length > 3 logm ) 61

m.

LEMMA

OBSERVE

In this lemma we insert m keys, i.e. n = m.

over m fixed inputs,

Page 62: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

If h is selected uniformly at random from all functions U → [m] then,

Pr (any chain has length > 3 logm ) 61

m.

LEMMA

OBSERVE

In this lemma we insert m keys, i.e. n = m.

over m fixed inputs,

Page 63: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

If h is selected uniformly at random from all functions U → [m] then,

Pr (any chain has length > 3 logm ) 61

m.

LEMMA

OBSERVE

In this lemma we insert m keys, i.e. n = m.

PROOF

The problem is equivalent to showing that if we randomly throw m balls into m bins, the

probability of having a bin with at least 3 logm balls is at most 1m .

· · ·

over m fixed inputs,

Page 64: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Page 65: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

� Choose any k of the m balls (we’ll pick k in a bit)

Page 66: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

� Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

Page 67: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

Page 68: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

Page 69: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

Let V1, . . . , Vq be q events. Then

THEOREM

Pr( q⋃i=1

Vi

)6

q∑i=1

Pr(Vi).

Page 70: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

Page 71: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

(mk

)=

m!

k!(m− k)!

Page 72: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

(mk

)=

m!

k!(m− k)!=

m · (m− 1) · (m− 2) · . . . (m− k + 1) · (m− k)!

k!(m− k)!

Page 73: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

(mk

)=

m!

k!(m− k)!=

m · (m− 1) · (m− 2) · . . . (m− k + 1) · (m− k)!

k!(m− k)!

Page 74: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

6m · (m) · (m) · . . . (m)

k!

(mk

)=

m!

k!(m− k)!=

m · (m− 1) · (m− 2) · . . . (m− k + 1) · (m− k)!

k!(m− k)!

Page 75: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

6m · (m) · (m) · . . . (m)

k!

(mk

)=

m!

k!(m− k)!=

m · (m− 1) · (m− 2) · . . . (m− k + 1) · (m− k)!

k!(m− k)!

Page 76: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

6m · (m) · (m) · . . . (m)

k!

k

(mk

)=

m!

k!(m− k)!=

m · (m− 1) · (m− 2) · . . . (m− k + 1) · (m− k)!

k!(m− k)!

Page 77: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

6m · (m) · (m) · . . . (m)

k!6

mk

k!

k

(mk

)=

m!

k!(m− k)!=

m · (m− 1) · (m− 2) · . . . (m− k + 1) · (m− k)!

k!(m− k)!

Page 78: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

By using the union bound again, we have that

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

Page 79: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

Pr(at least one bin receives at least k balls) 6 m · Pr(X1>k) 6m

k!.

By using the union bound again, we have that

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

Page 80: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

� Now we set k = 3 logm and observe thatm

k!6

1

mfor m > 2,

and we are done.

Pr(at least one bin receives at least k balls) 6 m · Pr(X1>k) 6m

k!.

By using the union bound again, we have that

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

Page 81: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

PROOF

continued. . .

� Let X1 be the number of balls in the first bin.

Pr(X1 > k) 6(mk

1

mk6

1

k!.

� Now we set k = 3 logm and observe thatm

k!6

1

mfor m > 2,

and we are done.

Pr(at least one bin receives at least k balls) 6 m · Pr(X1>k) 6m

k!.

By using the union bound again, we have that

Number of subsets of size k.

Choose any k of the m balls (we’ll pick k in a bit)

the probability that all of these k balls go into the first bin is 1mk .

So, the union bound gives us

Why is mk! 6 1

m? (when k = 3 logm)

k! = k × (k − 1)× (k − 2) . . .× 2× 1

k terms

k! > 2× 2 × 2 . . .× 2× 1 = 2k−1

Let k = 3 logm . . .

k! > 2(3 logm−1) > 22 logm = (2logm)2 = m2

so mk! 6 m

m2 = 1m

Page 82: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – true randomness

If h is selected uniformly at random from all functions U → [m] then,

Pr (any chain has length > 3 logm ) 61

m.

LEMMA

OBSERVE

In this lemma we insert m keys, i.e. n = m.

PROOF

The problem is equivalent to showing that if we randomly throw m balls into m bins, the

probability of having a bin with at least 3 logm balls is at most 1m .

· · ·

over m fixed inputs,

Page 83: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

The conclusion from previous slides is that with true randomness,the longest chain is very short (at most 3 logm) with high probability.

Page 84: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

The conclusion from previous slides is that with true randomness,

If h is picked uniformly at random from a weakly universal set of hash functions then,

over m fixed inputs,

Pr(

any chain has length > 1 +√2m

)6

1

2.

LEMMA

the longest chain is very short (at most 3 logm) with high probability.

Page 85: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

The conclusion from previous slides is that with true randomness,

If h is picked uniformly at random from a weakly universal set of hash functions then,

over m fixed inputs,

Pr(

any chain has length > 1 +√2m

)6

1

2.

LEMMA

OBSERVE

This rubbish upper bound of 12 does not necessarily rule out the possibility that the

tightest upper bound is indeed very small. However, the upper bound of 12 is in fact tight!

the longest chain is very short (at most 3 logm) with high probability.

Page 86: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

Page 87: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

Page 88: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

� Using linearity of expectation and E(Ix,y) = 1m (h is weakly universal),

E(C) = E( ∑

x,y∈T, x<y

Ix,y)

=∑

x,y∈T, x<y

E(Ix,y) =(m2

)·1

m6

m

2.

Page 89: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

� Using linearity of expectation and E(Ix,y) = 1m (h is weakly universal),

E(C) = E( ∑

x,y∈T, x<y

Ix,y)

=∑

x,y∈T, x<y

E(Ix,y) =(m2

)·1

m6

m

2.

� by Markov’s inequality, Pr(C > m) 6 E(C)m 6 1

2 .

Page 90: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

� Using linearity of expectation and E(Ix,y) = 1m (h is weakly universal),

E(C) = E( ∑

x,y∈T, x<y

Ix,y)

=∑

x,y∈T, x<y

E(Ix,y) =(m2

)·1

m6

m

2.

� by Markov’s inequality, Pr(C > m) 6 E(C)m 6 1

2 .

� Let r.v. L be the length of the longest chain. Then C >(L2

).

Page 91: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

� Using linearity of expectation and E(Ix,y) = 1m (h is weakly universal),

E(C) = E( ∑

x,y∈T, x<y

Ix,y)

=∑

x,y∈T, x<y

E(Ix,y) =(m2

)·1

m6

m

2.

� by Markov’s inequality, Pr(C > m) 6 E(C)m 6 1

2 .

� Let r.v. L be the length of the longest chain. Then C >(L2

).

This is because a chain of length L causes(L2

)collisions!

Page 92: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

� Using linearity of expectation and E(Ix,y) = 1m (h is weakly universal),

E(C) = E( ∑

x,y∈T, x<y

Ix,y)

=∑

x,y∈T, x<y

E(Ix,y) =(m2

)·1

m6

m

2.

� by Markov’s inequality, Pr(C > m) 6 E(C)m 6 1

2 .

� Let r.v. L be the length of the longest chain. Then C >(L2

).

Page 93: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

� Using linearity of expectation and E(Ix,y) = 1m (h is weakly universal),

E(C) = E( ∑

x,y∈T, x<y

Ix,y)

=∑

x,y∈T, x<y

E(Ix,y) =(m2

)·1

m6

m

2.

� by Markov’s inequality, Pr(C > m) 6 E(C)m 6 1

2 .

� Let r.v. L be the length of the longest chain. Then C >(L2

).

� Now, Pr

((L−1)2

2 > m

)6 Pr

((L2

)> m

)6 Pr (C > m) 6 1

2 .

Page 94: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

� Using linearity of expectation and E(Ix,y) = 1m (h is weakly universal),

E(C) = E( ∑

x,y∈T, x<y

Ix,y)

=∑

x,y∈T, x<y

E(Ix,y) =(m2

)·1

m6

m

2.

� by Markov’s inequality, Pr(C > m) 6 E(C)m 6 1

2 .

� Let r.v. L be the length of the longest chain. Then C >(L2

).

� Now, Pr

((L−1)2

2 > m

)6 Pr

((L2

)> m

)6 Pr (C > m) 6 1

2 .

this is because(L2

)=

L!

2!(L− 2)!=

L · (L− 1)

2>

(L− 1)2

2

Page 95: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

� Using linearity of expectation and E(Ix,y) = 1m (h is weakly universal),

E(C) = E( ∑

x,y∈T, x<y

Ix,y)

=∑

x,y∈T, x<y

E(Ix,y) =(m2

)·1

m6

m

2.

� by Markov’s inequality, Pr(C > m) 6 E(C)m 6 1

2 .

� Let r.v. L be the length of the longest chain. Then C >(L2

).

� Now, Pr

((L−1)2

2 > m

)6 Pr

((L2

)> m

)6 Pr (C > m) 6 1

2 .

Page 96: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

� Using linearity of expectation and E(Ix,y) = 1m (h is weakly universal),

E(C) = E( ∑

x,y∈T, x<y

Ix,y)

=∑

x,y∈T, x<y

E(Ix,y) =(m2

)·1

m6

m

2.

� by Markov’s inequality, Pr(C > m) 6 E(C)m 6 1

2 .

� Let r.v. L be the length of the longest chain. Then C >(L2

).

� Now, Pr

((L−1)2

2 > m

)6 Pr

((L2

)> m

)6 Pr (C > m) 6 1

2 .

Page 97: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Longest chain – weakly universal hashing

PROOF

� For any two keys x, y, let indicator r.v. Ix,y be 1 iff h(x) = h(y).

� Let r.v. C be the total number of collisions: C =∑

x,y∈T, x<y Ix,y .

� Using linearity of expectation and E(Ix,y) = 1m (h is weakly universal),

E(C) = E( ∑

x,y∈T, x<y

Ix,y)

=∑

x,y∈T, x<y

E(Ix,y) =(m2

)·1

m6

m

2.

� by Markov’s inequality, Pr(C > m) 6 E(C)m 6 1

2 .

� Let r.v. L be the length of the longest chain. Then C >(L2

).

� Now, Pr

((L−1)2

2 > m

)6 Pr

((L2

)> m

)6 Pr (C > m) 6 1

2 .

By rearranging, we have that Pr(L > 1 +

√2m)6 1

2 , and we are done.

Page 98: Hashing part one - people.cs.bris.ac.ukpeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/universal-hash… · Hashing part one Chaining, true randomness and universal hashing

Conclusions

the expected lookup time in a hash table with chaining is O(1).we have seen that when m > n,

For both,

true randomness (h is picked uniformly from the set of all possible hash functions)

and weakly universal hashing

(h is picked uniformly from a weakly universal set of hash functions)

If h is selected uniformly at random from all functions U → [m] then,

Pr (any chain has length > 3 logm ) 61

m.

LEMMA

LEMMA

(both Lemmas hold for m any fixed inputs)

If h is picked uniformly at random from a weakly universal set of hash functions,

Pr(

any chain has length > 1 +√2m

)6

1

2.