CSE 326 Hashing Richard Anderson (instead of Martin Tompa)
Chaining review kH(k) A B C D E F G H I J K H(k) = k mod 17
Open address hashing Store all elements in table If a cell is occupied, try another cell. Linear probing, try cells H(k), H(k) + 1 mod m, H(k) + 2 mod m,..
Open Address Hashing kH(k) A532 B417 C916 D757 E13 F66 G437 H6716 I883 J362 K H(k) = k mod A 3I 4J 5 6C 7B 8D 9F 10K E H
Open address hashing Lookup (K) { p = H(K); loop { if (A[p] is empty) return false; if (A[p] == K) return true; p = (p + 1) mod m; }
Open address hashing issues Issues: Clumping Cost per operation Deletion
Double hashing Use separate hash functions for the first probe and the collision resolution H 1 (k), H 1 (k) + H 2 (k) mod m, H 1 (k) + 2H 2 (k) mod m, H 1 (k) + 3H 2 (k) mod m,...
Double hashing example monthdayH 1 (k) A B C D E F G H I J K H 1 (k) = day mod 17 H 2 (k) = month
Double hashing vs. Single hashing Load factor cost per operation Single hashing Double hashing
Trade offs between chaining and open addressing Chaining Open Addressing
Hash Functions Function Efficient Uniform mapping to range Avoids systematic collisions
Hashing strings String Suppose
Fact: So: