wk3 3
wk3 3
2
Recap: Hash Functions
2
Recap: Hash Functions
2
Recap: Hash Functions
HT
i (k, v)
H
2
Recap: Hash Functions
HT
• Motivation: Fast storage and retrieval
i (k, v)
H
2
Recap: Hash Functions
HT
• Motivation: Fast storage and retrieval
i (k, v)
H
Sorted Array wit
LL, no dup Hash Table
h Binary Search
fi
A Good Hash Function: Reqs
• Uniform Distribution: Distribution of keys uniformly across the HT
• This minimises collision, improves HT utilisation!
• Collision Resistant: Computationally infeasible to nd
x, y : H(x) = H(y) ∧ x ≠ y
• Deterministic and Fast computation
fi
A Good Hash Function: Reqs
• Uniform Distribution: Distribution of keys uniformly across the HT
• This minimises collision, improves HT utilisation!
• Collision Resistant: Computationally infeasible to nd
x, y : H(x) = H(y) ∧ x ≠ y
• Deterministic and Fast computation
• Using all of the input data: Every part of input a ects the output hash
• ∃i ∈ ℕ : xi ≠ yi ⇒ P(H(x) ≠ H(y)) > 0
ff
fi
A Good Hash Function: Reqs
• Uniform Distribution: Distribution of keys uniformly across the HT
• This minimises collision, improves HT utilisation!
• Collision Resistant: Computationally infeasible to nd
x, y : H(x) = H(y) ∧ x ≠ y
• Deterministic and Fast computation
• Using all of the input data: Every part of input a ects the output hash
• ∃i ∈ ℕ : xi ≠ yi ⇒ P(H(x) ≠ H(y)) > 0
• Dynamic: Dynamic resizing of HT should be possibles
ff
fi
Cryptographic Hashing vs Hashing
Cryptographic Hashing vs Hashing
• Cryptographic hashes require additional properties
Cryptographic Hashing vs Hashing
• Cryptographic hashes require additional properties
• Preimage Resistance (One Way property): Let H(x) = h. Given h, it is
computationally intractable to nd x
fi
Cryptographic Hashing vs Hashing
• Cryptographic hashes require additional properties
• Preimage Resistance (One Way property): Let H(x) = h. Given h, it is
computationally intractable to nd x
• Avalanche e ect: Small change in the input produces signi cant change in the
output
fi
ff
fi
fi
Cryptographic Hashing vs Hashing
• Cryptographic hashes require additional properties
• Preimage Resistance (One Way property): Let H(x) = h. Given h, it is
computationally intractable to nd x
• Avalanche e ect: Small change in the input produces signi cant change in the
output
• Analysis:
fi
Collision Resolution (1): Chaining
• Analysis:
• Assume that time to compute H(k) is Θ(1)
fi
Collision Resolution (1): Chaining
• Analysis:
• Assume that time to compute H(k) is Θ(1)
• Load factor of an HT: α = n/m with m
slots holding n elements
fi
Collision Resolution (1): Chaining
• Algorithm: Insert 3
4
Collision Resolution (2): Linear Probing
Collision Resolution (2): Linear Probing
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (2): Linear Probing
18
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (2): Linear Probing
41 18
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (2): Linear Probing
41 18 22
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (2): Linear Probing
44
41 18 22
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (2): Linear Probing
44
41 18 22
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (2): Linear Probing
31
44
41 18 22
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (2): Linear Probing
31
44
41 18 X 22
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (2): Linear Probing
• Q: What happens to the e ciency of lookup if there are too many marks?
ffi
Collision Resolution (2): Linear Probing
• Q: What happens to the e ciency of lookup if there are too many marks?
• Q: What is the solution to the above problem?
ffi
Collision Resolution (2): Linear Probing
Collision Resolution (2): Linear Probing
fi
Collision Resolution (2): Linear Probing
fi
Collision Resolution (2): Linear Probing
fi
Collision Resolution (3): Double Hashing
• Use two hash functions : H1, H2
• H1(k) : Is the position in the table where we rst check for key k
• H2(k) : Determines the o set when we search for k
ff
fi
Collision Resolution (3): Double Hashing
• Use two hash functions : H1, H2
• H1(k) : Is the position in the table where we rst check for key k
• H2(k) : Determines the o set when we search for k
• In Linear Probing H2(k) is 1
ff
fi
Collision Resolution (3): Double Hashing
• Use two hash functions : H1, H2
• H1(k) : Is the position in the table where we rst check for key k
• H2(k) : Determines the o set when we search for k
• In Linear Probing H2(k) is 1
• Insert Algorithm:
ff
fi
Collision Resolution (3): Double Hashing
• Use two hash functions : H1, H2
• H1(k) : Is the position in the table where we rst check for key k
• H2(k) : Determines the o set when we search for k
• In Linear Probing H2(k) is 1
• Insert Algorithm:
• Distributes keys more uniformly
ff
fi
Collision Resolution (3): Double Hashing
• Use two hash functions : H1, H2
• H1(k) : Is the position in the table where we rst check for key k
• H2(k) : Determines the o set when we search for k
• In Linear Probing H2(k) is 1
• Insert Algorithm:
• Distributes keys more uniformly
ff
fi
Collision Resolution (3): Double Hashing
Collision Resolution (3): Double Hashing
• H1(k) : k mod 13
• H2(k) : 8 - (k mod 8)
• Insert keys: 18, 41, 22, 44, 59, 32, 31
Collision Resolution (3): Double Hashing
• H1(k) : k mod 13
• H2(k) : 8 - (k mod 8)
• Insert keys: 18, 41, 22, 44, 59, 32, 31
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (3): Double Hashing
• H1(k) : k mod 13
• H2(k) : 8 - (k mod 8)
• Insert keys: 18, 41, 22, 44, 59, 32, 31
18
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (3): Double Hashing
• H1(k) : k mod 13
• H2(k) : 8 - (k mod 8)
• Insert keys: 18, 41, 22, 44, 59, 32, 31
41 18
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (3): Double Hashing
• H1(k) : k mod 13
• H2(k) : 8 - (k mod 8)
• Insert keys: 18, 41, 22, 44, 59, 32, 31
41 18 22
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (3): Double Hashing
• H1(k) : k mod 13
• H2(k) : 8 - (k mod 8)
• Insert keys: 18, 41, 22, 44, 59, 32, 31
44
41 18 22
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (3): Double Hashing
• H1(k) : k mod 13
• H2(k) : 8 - (k mod 8)
• Insert keys: 18, 41, 22, 44, 59, 32, 31
44
41 18 22
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (3): Double Hashing
• H1(k) : k mod 13
• H2(k) : 8 - (k mod 8)
• Insert keys: 18, 41, 22, 44, 59, 32, 31
44
41 18 22
0 1 2 3 4 5 6 7 8 9 10 11 12
Collision Resolution (3): Double Hashing
• H1(k) : k mod 13
• H2(k) : 8 - (k mod 8)
• Insert keys: 18, 41, 22, 44, 59, 32, 31
31
44
41 18 22
0 1 2 3 4 5 6 7 8 9 10 11 12
Expected Number of Probes
RECOMMENDATIONS
RECOMMENDATIONS
RECOMMENDATIONS
RECOMMENDATIONS
RECOMMENDATIONS
RECOMMENDATIONS
RECOMMENDATIONS
RECOMMENDATIONS