Docsity

Various hashing techniques, Lecture notes of Data Structures and Algorithms

Toggle sidebar
bg1
16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]
๎˜๎˜‚๎˜ƒ๎˜„๎˜…๎˜„
๎˜๎˜‚๎˜ƒ๎˜„๎˜…๎˜†๎˜‡๎˜ˆ๎˜ƒ๎˜‰๎˜…๎˜‚๎˜Š๎˜ƒ๎˜…๎˜Š๎˜‹๎˜…๎˜Œ๎˜๎˜‡๎˜ƒ๎˜Ž๎˜„๎˜๎˜Š๎˜๎˜Š๎˜‘๎˜„๎˜…๎˜’๎˜„๎˜“๎˜Œ๎˜Œ๎˜‰๎˜‚๎˜’
๎˜†๎˜‡๎˜ˆ๎˜‰๎˜Š๎˜‹๎˜Œ๎˜๎˜๎˜‚๎˜ƒ๎˜„๎˜…๎˜†๎˜‡๎˜ˆ๎˜‰๎˜Š๎˜‹๎˜ƒ๎˜‰
๎˜”๎˜…๎˜’๎˜Š๎˜‚
๎˜๎˜„๎˜‚๎˜‚๎˜†๎˜ƒ๎˜‚
๎˜๎˜‚๎˜ƒ๎˜„๎˜…๎˜†๎˜‡๎˜‚๎˜ˆ๎˜‰๎˜Š๎˜ˆ๎˜‹๎˜Œ๎˜†๎˜๎˜Ž๎˜‹๎˜๎˜๎˜‘๎˜ˆ๎˜‰๎˜Š
๎˜’ ๎˜“๎˜”๎˜Š๎˜…๎˜†๎˜ƒ๎˜„๎˜†๎˜• ๎˜“ ๎˜‚๎˜ƒ๎˜–๎˜• ๎˜‰๎˜Š๎˜‚๎˜ƒ๎˜—๎˜†๎˜˜ ๎˜™๎˜š
16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]
๎˜†๎˜‡๎˜ˆ๎˜‰๎˜Š๎˜‹๎˜Œ๎˜Ž๎˜‡๎˜‹๎˜๎˜Ž๎˜†๎˜‡๎˜ˆ๎˜‰๎˜Ž๎˜๎˜‡๎˜‘๎˜’๎˜“๎˜ˆ
๎˜Represent a table of names
๎˜Set aside an array big enough to
contain one element for each
possible string of letters
๎˜Convert from names to integers
๎˜Tells where personโ€™s phone
number is immediately
๎˜Dictionary operations
๎˜Insert / delete /search ๎˜๎˜‚๎˜ƒ๎˜„๎˜‚๎˜…๎˜†๎˜‡๎˜ˆ๎˜‡๎˜‰๎˜„๎˜Š๎˜‹๎˜‰๎˜ˆ๎˜Œ๎˜๎˜‹๎˜Ž
๎˜๎˜‚๎˜ƒ๎˜„๎˜‚๎˜…๎˜†๎˜‡๎˜„๎˜๎˜ˆ๎˜๎˜Œ๎˜๎˜ˆ๎˜‹๎˜Œ๎˜‘๎˜๎˜Ž
๎˜๎˜‚๎˜ƒ๎˜„๎˜‚๎˜…๎˜†๎˜‡๎˜ˆ๎˜‡๎˜’๎˜Œ๎˜“๎˜“๎˜Ž
๎˜๎˜‚๎˜ƒ๎˜„๎˜‚๎˜…๎˜†๎˜‡๎˜ˆ๎˜‡๎˜”๎˜ˆ๎˜‹๎˜‹๎˜„๎˜‰๎˜๎˜Ž
๎˜๎˜‚๎˜ƒ๎˜„๎˜‚๎˜…๎˜†๎˜‡๎˜ˆ๎˜‡๎˜Š๎˜๎˜ˆ๎˜“๎˜“๎˜‡๎˜‚๎˜‰๎˜ˆ๎˜‚๎˜…๎˜Ž
๎˜๎˜‚๎˜ƒ๎˜„๎˜‚๎˜…๎˜†๎˜‡๎˜๎˜‘๎˜•๎˜„๎˜‡๎˜Œ๎˜๎˜‡๎˜‚๎˜ƒ๎˜„๎˜Š๎˜Š๎˜Ž
๎˜–๎˜ˆ๎˜‹๎˜ƒ๎˜„๎˜‰๎˜Œ๎˜๎˜„
๎˜—๎˜‹๎˜„๎˜˜๎˜ˆ๎˜๎˜‘
๎˜™๎˜š๎˜“๎˜Œ๎˜„
๎˜›๎˜“๎˜ˆ๎˜
๎˜œ๎˜„๎˜๎˜ˆ๎˜
๎˜ž๎˜Œ๎˜‚๎˜ƒ๎˜ˆ๎˜‰๎˜Ÿ
๎˜™๎˜ˆ๎˜‚๎˜“ ๎˜
16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]
๎˜†๎˜‡๎˜ˆ๎˜‰๎˜Š๎˜‹๎˜Œ๎˜Ž๎˜‡๎˜‹๎˜๎˜Ž๎˜†๎˜‡๎˜ˆ๎˜‰๎˜Ž๎˜๎˜‡๎˜‘๎˜’๎˜“๎˜ˆ
๎˜Dictionary operations
๎˜Insert / delete /search
1. Sequential search through nrecords
2. n records are specially ordered or stored in a tree
3. Certain information from each record is used to
generate a memory address
๎˜๎˜‚๎˜ƒ๎˜„
๎˜๎˜‚๎˜…๎˜†๎˜ƒ๎˜„
๎˜๎˜‚๎˜‡๎˜„
โ€œhashingโ€
16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]
๎˜๎˜”๎˜๎˜‡๎˜•
๎˜Direct-access table
๎˜Hash table
๎˜Hash function
๎˜Collision resolution
๎˜Chaining
๎˜Open addressing
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Various hashing techniques and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^

^

^

^  ^ 

^ ^

      

 

^ ^ 16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Represent a table of names^ ^ Set aside an array big enough tocontain one element for eachpossible string of letters^ ^ Convert from names to integers^ ^ Tells where personโ€™s phonenumber is immediately ^ Dictionary operations^ ^ Insert / delete /search

^ 
^ ^  
^ ^ 
^ 
 ^
^ ^     

^   ^ 16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Dictionary operations^ ^ Insert / delete /search1.^ Sequential search through

n^ records

2.^ n^ records are specially ordered or stored in a tree3.^ Certain information from each record is used to^ generate a memory address

โ€œhashingโ€

16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Direct-access table ^ Hash table ^ Hash function ^ Collision resolution^ ^ Chaining^ ^ Open addressing

  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

  16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

   

       

  16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Dictionary operations^ ^ Insert^ ^ direct_access_insert (T, x)

T[key[x]] := x

O (1)

^ Delete^ ^ direct_access_ delete (T, x)

T[key[x]] := NIL

O (1)

^ Search^ ^ direct_access_ search (T, k)

return^ T[k]

O (1)

  16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ The^ problem

with^ direct-addressing

is:

^ 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

U ,

then most of the space allocated for T is wasted  The^ advantages

of^ hash table

is:

^ 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|)

!    16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ In^ chaining

we put all the elements that hash to

the same slot in a

linked list

^ 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

!  16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]



     !^  ^   &    

   ^

#' " 

 ()

^  

 

^ *  

^ #+^ 

-,

^ .^ /

-- %^ ^  #$

  

^   ^ 



   16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ 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)

   ^ 16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Some definitions:^ ^ Load factor

ฮฑ: 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

    16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Worst-case behaviour:^ ^ All n keys hash to the same slot, this creates a list oflength n^ ^ The worst-case time is therefore

(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

   ^ 16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Average-case behaviour^ ^ Depends on how well the hash function

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.

   ^ 16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ 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+ฮฑ).

 ^ 16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ 

^ 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]

^ Suppose we have

k =123456,

p =14,^ m

and^ w =32.  Choose^ A

to be the fraction of the form s/

32 that is

closest to (

โˆš5 โ€“ 1)/2 so that

A^ = 2654435769/

^ Then^ ks

^ and so r

=76300 and r 1

^ The 14 most significant bits of r

yields the value 0

h(k) =.

'^ # 16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ The worst case scenario is when n keys all hash tothe same slot. This requires a

ฮ˜(n) retrieval time.

Any^ fixed

hash function is

vulnerable

to the

possibility of the worst case. The only effectivecounter measure is t choose the hash functionrandomly in a way that is independent of the keysthat are actually going to be stored. This method,known as

universal hashing

yields good

performance on average.

'^ # 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}

!   16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Two approaches^ ^ Separate chaining^ ^ m much smaller than n^ ^ ~n/m keys per table position^ ^ Put keys that collide in a list^ ^ Need to search lists^ ^ Open addressing (linear probing, double hashing)^ ^ m much larger than n^ ^ Plenty of empty table slots^ ^ When a new key collides, find an empty slot^ ^ Complex collision patterns

(  16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ To perform insertion using open addressing we^ probe

the hash table to find an empty slot inwhich to put the key. Instead of being fixed in theorder 0, 1, โ€ฆ, m-1 (requiring

ฮ˜(n) time), the

sequence of positions is probed depending uponthe key being inserted.

(  16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Advantages:^ ^ Do not use pointers, which speed up addressing schemes,frees up space^ ^ Faster retrieval times^ ^ Reduces the number of collisions^ ^ May store a larger table with more slots for the same memory^ ^ Compute the sequence of slots to be examined

(  16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Extend the hash function to also include the probenumber (starting from 0) as a second input.^ ^ h:^ U

  • {0, 1, โ€ฆ, m-1}

^ {0, 1, โ€ฆ, m-1}

^ For open addressing, we require that for every keyk, the^

probe sequence <h(k, 0), h(k, 1), โ€ฆ, h(k, m-1)>be a permutation of <0, 1, โ€ฆ, m-1>, so that everyhash-table position is eventually considered as aslot for a new key as the table fills up.

(  16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ Assume:

uniform hashing

instead of

simple uniform

hashing. ^ The hash function in uniform hashing produces a hashsequence ^ Each key is equally likely to have any of m! permutations of{0, 1, โ€ฆ, m-1} as its probe sequence ^ Deletion is difficult and a modification to hash_search isnecessary to continue to search if a slot is marked deletedinstead of NIL. ^ Chaining may be needed

(  16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ 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

*   ^ 16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ We have a hash table of size 13 withh1(k) = k mod 13 andh2(k) = 1 + (k mod 11)since 14

โ‰ก^ 1 (mod 13) and 14

โ‰ก^ 1 (mod 11),

the key 14 is inserted into empty slot 9, afterslots 1 and 5 are examined and found to beoccupied.

^79  ^69 ^9872 ^14  ^50 

  ( ^ 16.070 โ€” March 31/2003 โ€” Prof. I. K. Lundqvist โ€” [email protected]

^ 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