Download presentation
Presentation is loading. Please wait.
Published byBryan Norris Modified over 9 years ago
1
LECTURE 35: COLLISIONS CSC 212 – Data Structures
2
Map Performance Want fast implementation 911 Operators immediately need addresses Performance of Google measured in TB/s O(log n) time too slow to achieve these times Would love to use arrays Converts key to int with hash functions Once hashed, put, remove & get in O(1) time But is this even possible?
3
Hash Table Array locations either: null Reference to Entry Marker value * May not be contiguous Better when spread out Hash key to index Begin methods with hash Entry at that index Hash Table Entry s 0000 0001 0256120001“Jay Doe” 0002 9811010002“Bob Doe” 0003 0004 4512290004“Jill Roe” … … 9997 9998 2007519998“Rhi Smith” 9999
4
Ideal World key hashed to unique index Hash and done, Entry is there And then… You wake up
5
Bad Hash Perfect hash does not exist Cannot know all keys beforehand Cluster around a few indices Or find all keys hashed to same index Handling bad hash is a necessary Given Entry must first check key Store multiple Entry s with same hash (Shot of adrenaline restarts heart)
6
Collisions
7
Linear Probing Algorithm used in musical chairs At index where key hashed examine Entry Circle through array until empty index found Algorithm is very simple But creates clusters of Entry s
8
Linear Probe Example h(x) = x mod 13 Now add: 44 h(44) = 5 20 h(20) = 7 22 h(22) = 9 31 h(31) = 5 15 18442032 2231 76 012345678 910 1112
9
Probing Reaction Oh, **** Adding to hash table still O(n)
10
Quadratic Probe Avoids primary clustering problems But does create secondary clustering No one cares about secondary clustering Linear and quadratic probe equally complex Where key is hashed, k, examine Entry Check (k + j 2 ) % length: k +1, k +4, k +9, k +16, … Continue probing until unused array slot found Guaranteed to work when: Table size is prime number Under 50% full
11
Quadratic Probe Example 3115 18442032 22 76 012345678 910 1112 h(x) = x mod 13 Now add: 44 h(44) = 5 20 h(20) = 7 22 h(22) = 9 31 h(31) = 5
12
Quadratic Probing Reaction Darn it to heck. Adding to hash table still O(n)
13
Solve bad hash with even more hash Use 2 nd hash function very different from first 2 nd hash function not allowed to return zero Re-hash key using 2 nd function after the collision Check index equal to sum of two hash function Re-add 2 nd hash for more probes Guaranteed to work when Table size is prime number Double Hashing
14
Double Hash Example 3115 18442032 22 76 012345678 910 1112 h(x) = x mod 13 h 2 (x) = 5 – (x mod 5) Now add: 44 h(44) = 5 20 h(20) = 7 22 h(22) = 9 31 h(31) = 5
15
Double Probing Reaction Sweet! Double hashing keeps put O(n)
16
Probing and Searching Search index where key hashed If cannot place Entry at index The array must keep being probed Stop only at usable index May need to probe every index! Searching takes O(n) even with hash May need to reallocate & rehash table Worst case O(n) put even with perfect hash
17
Linear Probe Example What happens when we remove an Entry ? Set index to null in most structures Consider if we call remove(44) get(31) called, what would happen? First check index it is hashed Checks first probe indexed… & stops at null 15 18442032 2231 76 012345678 910 1112 Even Better! I buggy code!
18
* Marker Value Explained Mark cleared indices in hash table Since collision could have happened, continue search Index can be used to store new Entry Ways to show that array index is clear Entry with null key could be used Make key which is never used Use static final field of type Entry
19
Hash tables can require O(n) complexity Provide O(1) time if you are really good Ultimately depends on hash function used Choose wisely and be rich Why Use Hash Table & Probes?
20
Finish week #12 assignment Due at usual time, whatever that may be Work on programming project #4 (due 12/1) If you want to take the 2 nd chance midterm Study before test on Thursday from 9:30 – 10:20 Read sections 9.2.1 – 9.2.4 of the book Start discussion of what hash is How we implement hash tables What makes hash & hashing good Before Next Lecture…
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.