70 likes | 189 Views
Hashing for dummies 1. For quick search, insert and delete of data from a table. 1 figures and concepts from MITOPENCOURSEWARE http://videolectures.net/mit6046jf05_leiserson_lec07/. Hashing function. Collisions. Hash Cases. Worst case All key “hashes” to same slot
E N D
Hashing for dummies1 For quick search, insert and delete of data from a table. 1 figures and concepts from MITOPENCOURSEWARE http://videolectures.net/mit6046jf05_leiserson_lec07/
Hash Cases • Worst case • All key “hashes” to same slot • Built a fancy linked list • Best case • assumption of simple uniform hashing • Each key is equally likely to be hashed to any slot of table T, independent of where other keys are hashed.
Search times • Unsuccessful search • Calculate hash function + Search list of average size (load factor) • Successful • Calculate has function + search of list average size + 1 /2 • Order 1 ~ constant time assuming uniform hashing
Choosing hash function • Characteristics • Distribute keys uniformly into slots • Regularity in key distribution should not affect uniformity • Division method • H(k) = k mod m (m # of slots) • all even #s to hash only hashes into even slots & odd slots aren’t used
Hash function example • Example ASCII # for first letter • Jones (74), Williams (87), Smith (83), Davis (68), Kelly (75), Garcia (71), Lo (76), Moon (77) Table size m = 5 H(74) = 74 mod 5 4 H(87) = 87 mod 5 2 H(83) = 83 mod 5 3 H(68) = 68 mod 5 3 H(75) = 75 mod 5 0 H(71) = 71 mod 5 1 H(76) = 76 mod 5 1 H(77) = 77 mod 5 2 • Kelly • -Garcia - Lo • Williams - Moon • Smith - Davis • Jones 0 1 2 3 4