






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







16.070 โ March 31/2003 โ Prof. I. K. Lundqvist โ [email protected]
^ ^
16.070 โ March 31/2003 โ Prof. I. K. Lundqvist โ [email protected]
^ Direct addressing
is a simple technique that works well when the^ universe
U^ of keys is reasonably small ^ Assume we have:^ ^ Application needs a dynamic set^ ^ All elements of dynamic set have keys,
from Universe
U^ = {0, 1, โฆ, m-1} of keys
, associated with them ^ m is^ not too large ^ No two elements have the same key Direct-address tables ^ Implement a
dynamic set
as an array (direct-address table), T[0 .. m-1] Each^ slot^ corresponds to a key in
U ^ Slot^ k^ points to an element in dynamic set with key
k
^ If dynamic set contains no element with key
k^ then T[k] = NIL
^ If universe
U^ is large, storing a table of size |
U | is
impractical If the set of actually stored keys
k^ is small relative to
^ When set
k^ of keys stored in dictionary is much smallerthan the universe^ U of all keys, a hash table requires much less space than a direct-address table Storage requirements are reduced to
ฮ(|k|) instead of
ฮ(|U|)
^ Slot^ j^ contains a pointer to the
head^ of the list of all stored elements that hash to
j. ^ If no element hashes to
j , then^ j^ contains NIL
!^ ^ &
^
#' "
()
^
^ *
^ #+^
-,
^ .^ /
-- %^ ^ #$
^ ^
^ Insert^ ^ chained_hash_insert (T, x)
insert x at head of list T[h(key[x])]worst-case runtime^ O (1) ^ Delete^ ^ chained_hash_delete (T, x)
delete x from list T[h(key[x])]worst-case runtime^ O (1)
if lists are doubly-linked ^ Search^ ^ chained_hash_search (T, k)
search for element with key k in list T[h(k)]worst-case runtime^ O (1) If the number of hash table slots n is at least proportionalto the number of elements in the table m or n =
O (m)
^ So that^ ฮฑ
= n/m = O(m)/m = O(1)
ฮฑ: is the ratio of the number of stored elements n divided by the number of slots m in hashtable T or^
ฮฑ^ = n/m ^ Simple uniform hashing
: is when^ any given element is equally likely to hash into any of the m slots
independently of where any other element has hashedto
(terrible!)^ ฮ(n)
^ Which is no better than if using one linked list for allelements,
plus^ the time it takes to compute the hashfunction ^ Hash tables are
not^ used for their worst-case performance
h^ distributes the set
of keys to be stored among the
m^ slots, on the average
^ Assume
simple uniform hashing ^ Assume the hash value
h(k)^ can be computed in
O (1) time
^ Must examine the number of elements in the list T[h(k)]that are checked to see if their keys are equal to k.^ ^ Two casesโ The search is unsuccessful. No element in the table has key kโ The search successfully finds an element with key k.
^ Theorem
: In a hash table in which collisions are resolvedby chaining, an unsuccessful search takes expected time ฮ(1+ฮฑ), under the assumption of simple uniform hashing. ^ Proof : Under the assumption of simple uniform hashing,any key k not already stored in the table is equally likely tohash to any of the m slots. The expected time to searchunsuccessfully for a key k is the expected time to search tothe end of list T[h(k)], which has expected length =
ฮฑ.
Thus, the expected number of elements examined in anunsuccessful search is
ฮฑ, and the total time required (including the time for computing h(k)) is
ฮ(1+ฮฑ).
^ Theorem
: In a hash table in which collisions are resolvedby chaining, a successful search takes time
ฮ(1+ฮฑ), on the
average, under the assumption of simple uniform hashing. โดIf the number of hash-table slots is at least proportionalto the number of elements in the table, we have n= O(m)and, consequently,
ฮฑ=n/m=O(m)/m=O(1). Thus, searching takes constant time on average. Since insertion takes O(1)worst-case time and deletion takes O(1) worst-case timewhen the lists are doubly linked,
all dictionary operations
can be supported in O(1) time on average
16.070 โ March 31/2003 โ Prof. I. K. Lundqvist โ [email protected]
^ Suppose that the word size of themachine is
w^ bits and that
k^ fits into a single word. We restrict
A^ to be a fraction of the form s/
w, where^ s
is an
integer in the range 0 <
w s < 2. '
^ First multiply
k^ by the w -bit integer
w s =A2. The result is a 2
w -bit wvalue r 21 +r, where r^0
1 is the high-order wordof the product and r
is 0 the low-order word ofthe product. Thedesired^ p -bit hashvalues consists of the
p most significant bits ofr^.^0
16.070 โ March 31/2003 โ Prof. I. K. Lundqvist โ [email protected]
^ Let^ H^ be a finite collection of hash functions so that^ ^ For every h
โ H , we have h: U
^ {0, 1, โฆ, m-1}
^ This collection
H^ is universal ^ If for each pair of distinct keys x,y
โU, the number of hash functions hโ
H^ where h(x)=h(y) is |
H |/m
^ We interpret this to mean that:^ ^ Given hash function h
โ H^ chosen randomly ^ The probability of a collision between x and y when x
โ y
is 1/m This is exactly the probability of a collision of h(x) andh(y) are randomly chosen from {0, 1, โฆ, m-1}
^ {0, 1, โฆ, m-1}
^ Four techniques for computing probe sequences for openaddressing1.^ Sequential probing: h, h+1, h+2, h+3, โฆ2.^ Linear probing : h, h+k, h+2k, h+3k, โฆ3.^ Quadratic probing: h, h+
2 2 , h+2^ , h+^ 2 , โฆ
4.^ Double hashing: h(k,i) = (h
(k) + ih^ (k)) mod m 12 , where h^ and h^1
are auxiliary hash functions. 2 ^ All generate <h(k,0), h(k, 1), โฆ, h(k, m-1)> as a permutation of,0, 1, โฆ, m-1> ^ None can generate more than m2 different probe sequences asuniform hashing requires m! different probe sequences(permutations) ^ Double hashing has the greatest number and may give the bestresults
^ Theorem
: given an open-address hash table with load factor
ฮฑ^ = n/m < 1, the
expected number of probes in an unsuccessful search is at most
(^1) 1-^ ฮฑ assuming uniform hashing Corollary : Inserting an element into an open-address hash table with load factor ฮฑ^ requires at most
(^1) 1-^ ฮฑ probes on average, assuming uniform hashing Theorem : Given an open-address hash table with load factor
ฮฑ^ < 1, the expected
number of probes in a successful search is at most
1 1
1 ฮฑ^ 1-^ ฮฑ^
ฮฑ
assuming uniform hashing and assuming that each key in the table is equallylikely to be searched for. If the hash table is half full, then the expected number of probes is less than3.38629. If it is ninety percent full, we have less than 3.66954 probes.
ln