Thông tin tài liệu
DATA STRUCTURES Hash Tables and Applica5ons Design and Analysis of Algorithms I Consider 𝑛 people with random birthdays (i.e., with each day of the year equally likelyti How large does 𝑛 need to be before there is at least a 50% chance that two people have the same birthday? 50 % 23 57 99 % 184 99.99….% 367 100% Collision: dis6nct RESOLVING COLLISIONS Solu6on # : (separateti chaining SUCH THAT -‐keep linked list in each bucket -‐given a key/object x, perform Insert/Delete/Lookup in the list in A[h(xti] Linked list for x Bucket for x Solu6on #2 : open addressing (only one object per bucketti Use hash -‐Hash func6on now 6ll specifies probe sequence h1(xti,h (keep trying find open 2(xti, func6ons slotti -‐Examples : linear probing (look consecu6velyti, double hashing Nextcore AI Gopal Shangari Insert new object x at Note : in hash table with chaining, Insert is front of list in for Insert/Delete Equal-‐length lists A[h(xti] could be anywhere from m/n to m for m objects Point All objects in : performance depends on the choice of hash func6on! same (analogous situa6on with open addressingti bucket WHAT MAKES A GOOD HASH FUNC6ON? Proper6es of a “Good” Hash func6on Should lead to good performance => i.e., should “spread data out” (gold standard – completely random hashingti Should be easy to store/ very fast to evaluate Nextcore AI Gopal Shangari |u| = 1010 Example : keys = phone numbers (10-‐digitsti choose n = 103 -‐Terrible hash func6on : h(xti = 1st digits of x (i.e., area codeti -‐mediocre hash func6on : h(xti = last digits of x [s6ll vulnerable to pa t erns in last digits ] BAD HASH FUNC6ONS Example : keys = memory loca6ons (will be mul6ples of a power of 2ti -‐Bad hash func6on : h(xti = x mod 1000 (again n = 103ti => All odd buckets guaranteed to be empty Nextcore AI Gopal Shangari QUICKAND-‐DIRTY“comparison HASH FUNC6ONS “hash ‐ code” e.g., subrou6ne to convert strings to integers func6on “ like the mod n func6on How to choose n = # of buckets Choose n to be a prime ( within constant factor of # of objects in tableti Not too close to a power of Not too close to a power of 10 Nextcore AI Gopal Shangari ... = 10 10 Example : keys = phone numbers (10 -‐digitsti choose n = 10 3 -‐Terrible hash func6on : h(xti = 1st digits of x (i.e., area codeti -‐mediocre hash func6on : h(xti = last digits of x [s6ll... erns in last digits ] BAD HASH FUNC6ONS Example : keys = memory loca6ons (will be mul6ples of a power of 2ti -‐Bad hash func6on : h(xti = x mod 10 00 (again n = 10 3ti => All odd buckets guaranteed... depends on the choice of hash func6on! same (analogous situa6on with open addressingti bucket WHAT MAKES A GOOD HASH FUNC6ON? Proper6es of a “Good” Hash func6on Should lead to good performance
Ngày đăng: 17/11/2019, 07:37
Xem thêm: 6 1 hasttables tủ tài liệu training pdf