50 likes | 307 Views
Hashing. Basic Technique Open Hashing Closed Hashing Restructuring Hash Tables. Hashing. Basic Technique Hash Function – h(x) : X (0, 1, ... , b-1) Given an object x, calculate an “equivalence class” h1(x) = x mod b h2(x) = floor(x / 10) mod b
E N D
Hashing Basic Technique Open Hashing Closed Hashing Restructuring Hash Tables CS 303 – Hashing Lecture 11
Hashing • Basic Technique • Hash Function – h(x) : X (0, 1, ... , b-1) • Given an object x, calculate an “equivalence class” • h1(x) = x mod b • h2(x) = floor(x / 10) mod b • h3(s) = ( ord(ci) ) mod b • Desiderata • h should “hash” x so that: • |B0| = |B1| = .... = |Bb-1| • x’s should be “randomly” (but repeatably!) dispersed (fast) • May need to use domain specific information about the distribution of values for x CS 303 – Hashing Lecture 11
Open Hashing Set of Sets • Member • calculate h(x) • scan list B(h(x)) • Insert • calculate h(x) • add to B(h(x)) • Delete • calculate h(x) • remove from B(h(x)) • Ideally, the lists B(i) are short and uniform in length • O(N/B) [NOT O(1)!] ... 1 2 ... ... b-1 CS 303 – Hashing Lecture 11
Closed Hashing • No linked lists – just the table! • Note 0-based addressing • Search • calculate h(x) • inspect B(h(x)) • if FULL and B(h).v = x • then FOUND • else ReHash with hi(x) • Strategies • LINEAR – hi+1(x) = hi(x) + 1 mod b • RANDOM – new hi(x) every time? 0 e is one of: EMPTY DELETED FULL i b-1 CS 303 – Hashing Lecture 11
Restructuring Hash Tables Insertion 1/(1-a) • For Closed Hashing • Slow growth until a = 0.8, then explosive growth • If a 0.9 (closed) or N/B 2 (open) it pays to • 1) create a new table with B’ = 2B • 2) re-insert all elements into the new table Deletion -(1/a)log(1-a) a 0.8 CS 303 – Hashing Lecture 11