§3 separate chaining

13
§3 Separate Chain ing ---- keep a list of all keys that hash to the same value struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef struct HashTbl *HashTable; struct ListNode { ElementType Element; Position Next; }; typedef Position List; /* List *TheList will be an array of lists, allocated late /* The lists use headers (for simplicity), */ /* though this wastes space */ struct HashTbl { int TableSize; List *TheLists; }; 1/13

Upload: tashya-combs

Post on 31-Dec-2015

21 views

Category:

Documents


0 download

DESCRIPTION

§3 Separate Chaining. ---- keep a list of all keys that hash to the same value. struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef struct HashTbl *HashTable; struct ListNode { ElementType Element; Position Next; }; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: §3  Separate Chaining

§3 Separate Chaining---- keep a list of all keys that hash to the same value

struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef struct HashTbl *HashTable;

struct ListNode { ElementType Element; Position Next;

}; typedef Position List;

/* List *TheList will be an array of lists, allocated later */ /* The lists use headers (for simplicity), */ /* though this wastes space */ struct HashTbl {

int TableSize; List *TheLists;

};

1/13

Page 2: §3  Separate Chaining

HashTable InitializeTable( int TableSize ) { HashTable H; int i; if ( TableSize < MinTableSize ) {

Error( "Table size too small" ); return NULL; } H = malloc( sizeof( struct HashTbl ) ); /* Allocate table */ if ( H == NULL ) FatalError( "Out of space!!!" ); H->TableSize = NextPrime( TableSize ); /* Better be prime */ H->TheLists = malloc( sizeof( List ) * H->TableSize ); /*Array of lists*/ if ( H->TheLists == NULL ) FatalError( "Out of space!!!" ); for( i = 0; i < H->TableSize; i++ ) { /* Allocate list headers */

H->TheLists[ i ] = malloc( sizeof( struct ListNode ) ); /* Slow! */if ( H->TheLists[ i ] == NULL ) FatalError( "Out of space!!!" ); else H->TheLists[ i ]->Next = NULL;

} return H; }

§3 Separate Chaining Create an empty table

HTheLists

TableSize

……

……

2/13

Page 3: §3  Separate Chaining

§3 Separate Chaining

Find a key from a hash table

Position Find ( ElementType Key, HashTable H ) { Position P; List L;

L = H->TheLists[ Hash( Key, H->TableSize ) ];

P = L->Next; while( P != NULL && P->Element != Key ) /* Probably need strcmp */

P = P->Next; return P; }

Your hash function

Identical to the code to perform a Find for general lists -- List ADT

3/13

Page 4: §3  Separate Chaining

§3 Separate Chaining Insert a key into a hash table

void Insert ( ElementType Key, HashTable H ) { Position Pos, NewCell; List L; Pos = Find( Key, H ); if ( Pos == NULL ) { /* Key is not found, then insert */

NewCell = malloc( sizeof( struct ListNode ) ); if ( NewCell == NULL ) FatalError( "Out of space!!!" ); else { L = H->TheLists[ Hash( Key, H->TableSize ) ]; NewCell->Next = L->Next; NewCell->Element = Key; /* Probably need strcpy! */ L->Next = NewCell; }

} }

Again?!

Tip: Make the TableSize about as large as the number of keys expected (i.e. to make the loading density factor 1).

4/13

Page 5: §3  Separate Chaining

§4 Open Addressing---- find another empty cell to solve collision (avoiding pointers)

Algorithm: insert key into an array of hash table{ index = hash(key); initialize i = 0 ------ the counter of probing; while ( collision at index ) {

index = ( hash(key) + f(i) ) % TableSize;if ( table is full ) break;else i ++;

} if ( table is full )

ERROR (“No space left”); else

insert key at index;}

Collision resolving function.f(0) = 0.

Tip: Generally < 0.5.

5/13

Page 6: §3  Separate Chaining

§4 Open Addressing

1. Linear Probing f ( i ) = i ; /* a linear function */f ( i ) = i ; /* a linear function */

〖 Example〗 Mapping n = 11 C library functions into a hash table ht[ ] with b = 26 buckets and s = 1.

acos atoi char define exp ceil cos float atol floor ctime

bucket x012345678910

… …25

search timeacos 1atoi 2char 1

define 1exp 1ceil 4cos 5

float 3atol 9floor 5ctime 9

Loading density = 11 / 26 = 0.42

Average search time = 41 / 11 = 3.73

Although p is small,the worst case can be

LARGE.

Analysis of the linear probing show that the expected number of probes

searches successfulfor )1(

searches ulunsuccessf and insertionsfor )1(

11

21

)1(1

21

2

p

= 1.36

Cause primary clustering: any key that hashes into the cluster will add to the cluster after several attempts to resolve the collision.

6/13

Page 7: §3  Separate Chaining

§4 Open Addressing

2. Quadratic Probing f ( i ) = i 2 ; /* a quadratic function */f ( i ) = i 2 ; /* a quadratic function */

【 Theorem 】 If quadratic probing is used, and the table size is prime, then a new element can always be inserted if the table is at least half empty.

Proof: Just prove that the first TableSize/2 alternative locations are all distinct. That is, for any 0 < i j TableSize/2, we have

( h(x) + i 2 ) % TableSize ( h(x) + j 2 ) % TableSize

Suppose: h(x) + i 2 = h(x) + j 2 ( mod TableSize )

then: i 2 = j 2 ( mod TableSize )

( i + j ) ( i j ) = 0 ( mod TableSize )

TableSize is prime either ( i + j ) or ( i j ) is divisible by TableSize

Contradiction !

For any x, it has TableSize/2 distinct locations into which it can go. If at most TableSize/2 positions are taken, then an empty spot can always be found.

7/13

Page 8: §3  Separate Chaining

§4 Open Addressing

Note: If the table size is a prime of the form 4k + 3, then the quadratic probing f(i) = i 2 can probe the entire table.

Note: If the table size is a prime of the form 4k + 3, then the quadratic probing f(i) = i 2 can probe the entire table.

Read Figures 7.15 - 7.16 for detailed representations and implementations of initialization.

Position Find ( ElementType Key, HashTable H ) { Position CurrentPos; int CollisionNum; CollisionNum = 0; CurrentPos = Hash( Key, H->TableSize ); while( H->TheCells[ CurrentPos ].Info != Empty &&

H->TheCells[ CurrentPos ].Element != Key ) { CurrentPos += 2 * ++CollisionNum 1; if ( CurrentPos >= H->TableSize ) CurrentPos = H->TableSize;

} return CurrentPos; }

What if these two conditions are switched?

f(i)=f(i1)+2i1where 2* is really

a bit shift

Faster than modWhat is

returned?

8/13

Page 9: §3  Separate Chaining

§4 Open Addressing

void Insert ( ElementType Key, HashTable H ) { Position Pos; Pos = Find( Key, H ); if ( H->TheCells[ Pos ].Info != Legitimate ) { /* OK to insert here */

H->TheCells[ Pos ].Info = Legitimate; H->TheCells[ Pos ].Element = Key; /* Probably need strcpy */

} }

Question: How to delete a key?

Note: Insertion will be seriously slowed down if there are too many deletions intermixed with insertions.

Although primary clustering is solved, secondary clustering occurs – that is, keys that hash to the same position will probe the same alternative cells.

Note: Insertion will be seriously slowed down if there are too many deletions intermixed with insertions.

Although primary clustering is solved, secondary clustering occurs – that is, keys that hash to the same position will probe the same alternative cells.

9/13

Page 10: §3  Separate Chaining

§4 Open Addressing3. Double Hashing

f ( i ) = i * hash2( x ); /* hash2( x ) is the 2nd hash function */f ( i ) = i * hash2( x ); /* hash2( x ) is the 2nd hash function */

make sure that all cells can be probed. hash2( x ) 0 ;

Tip: hash2( x ) = R – ( x % R ) with R a prime smaller than TableSize, will work well.

Note: If double hashing is correctly implemented, simulations imply that the expected number of probes is almost the same as for a random collision resolution strategy.

Quadratic probing does not require the use of a second hash function and is thus likely to be simpler and faster in practice.

Note: If double hashing is correctly implemented, simulations imply that the expected number of probes is almost the same as for a random collision resolution strategy.

Quadratic probing does not require the use of a second hash function and is thus likely to be simpler and faster in practice.

10/13

Page 11: §3  Separate Chaining

§5 Rehashing

Oh come on! Haven’t we had enough hashing methods?

Why do we need rehashing ?

Because I enjoy giving you headaches …

Just kidding Say which probing method

do you like?

Practically speaking I would prefer to use quadratic hashing…

What, anything wrong with it?

What will happen if the table is more than half full?

Uhhhh… insertion might fail Then what can we do?

Build another table that is about twice as big;

Scan down the entire original hash table for non-deleted elements;

Use a new function to hash those elements into the new table.

If there are N keys in the table, then T (N) = O(N)

Question: When to rehash?

Answer: As soon as the table is half full When an insertion fails When the table reaches a certain load factor

11/13

Page 12: §3  Separate Chaining

§5 Rehashing

Note: Usually there should have been N/2 insertions before rehash, so O(N) rehash only adds a constant cost to each insertion.

However, in an interactive system, the unfortunate user whose insertion caused a rehash could see a slowdown.

Note: Usually there should have been N/2 insertions before rehash, so O(N) rehash only adds a constant cost to each insertion.

However, in an interactive system, the unfortunate user whose insertion caused a rehash could see a slowdown.

Read Figures 7.23 for detailed implementation of rehashing.

Home work:p.259 7.1 & 7.2

A test case for all kindsof hashing

12/13

Page 13: §3  Separate Chaining

Laboratory Project 3Jumping the Queue

Due: Thursday, November 2nd, 2006 at 10:00pm

Don’t forget to signyou names

and duties at the end of your report.

Detailed requirements can be downloaded from http://10.71.45.99/list.asp?boardid=47

Courseware Download

13/13