This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Storage/Retrieval: Unordered Keys Let U be the universe of possible keys. We can define an array indexed by elements in U and store data using keys as indices. Get , Put , and Remove can be implemented to run in (1) time. The constructor can also run in (1) time using virtual initialization . Can use a lot of space. 1 Hashing Though we can use order to help us find keys, disorder can actually work better. We define a hash function h : U N such that h ( k ) < m for every k U . h ( k ) gives an index into a table T [0 ..m 1] , where we store the data item. 2 Collisions Defn: A collision occurs when two keys in the data set are mapped to the same table location. Suppose n keys are assigned to m locations independently with uniform probability. We define the probability space of ntuples of natural numbers less than m , each having probability m n . Let coll ( ( i 1 , . . . , i n ) ) be the number of ordered pairs ( i j , i k ) such that i j = i k and j < k . 3 coll ( ( i 1 , . . . , i n ) ) = n 1 summationdisplay j =1 n summationdisplay k = j +1 I ( i j = i k ) E [ coll ] = E n 1 summationdisplay j =1 n summationdisplay k = j +1 I ( i j = i k ) = n 1 summationdisplay j =1 n summationdisplay k = j +1 E [ I ( i j = i k )] = n 1 summationdisplay j =1 n summationdisplay k = j +1 P ( i j = i k ) . 4 E [ coll ] = n 1 summationdisplay j =1 n summationdisplay k = j +1 1 /m = 1 m n 1 summationdisplay j =1 ( n j ) = 1 m n 1 summationdisplay j =1 j = n ( n 1) 2 m . 5 Handling Collisions We store all items that map to the same location in a single ConsList . To find a key: Apply the hash function to obtain the table location. Search the ConsList at that location. 6 The Worst Case Theorem 7.1 Let T be a hash table with m locations, and suppose the universe U of possible keys contains more than m ( n 1) elements. Then for any function h mapping U to natural numbers less than m , there is some natural number i < m such that h maps at least...
View
Full
Document
 Spring '09
 Howell

Click to edit the document details