Download presentation
Presentation is loading. Please wait.
Published byChloe Hansen Modified over 11 years ago
1
Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction String to Integer conversion Shifting method
2
Separate Chaining 0 1 4 25 16 9 81 64 36 49 01234567890123456789
3
Type declaration Struct ListNode { ElementType Element; Position Next; }; typedef Position List; Struct HashTbl { int TableSize; List *TheLists; };
4
Initialization routine HashTable InitializeTable(int TableSize) { HashTable H; int i; H->TheLists = malloc( sizeof( List ) * H->TableSize ); for(i= 0; i Tablesize; i++ ) { H->TheLists[ i ] = malloc( sizeof( struct ListNode ) ); H->TheLists[ i ] -> Next = NULL; } return H; }
5
Find routine 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 ) P = P->Next; return P; }
6
Insert Routine void Insert( ElemetType Key, HashTable H) { Position Pos, NewCell; List L; Pos = Find (Key, H); if ( Pos == NULL) { NewCell = malloc( sizeof( struct ListNode )); L = H ->TheLists[ Hash( Key, H->TableSize ) ]; NewCell -> Next = L ->Next; NewCell -> Element = Key; L ->Next = NewCell; }
7
OPEN ADDRESSING Linear Probing Quadratic Probing Double Hashing
8
Linear Probing Empty TableAfter 89After 18After 49After 58After 69 049 158 269 3 4 5 6 7 818 989
9
049 158 269 3 4 5 6 7 818 989
10
Quadratic Probing Empty TableAfter 89After 18After 49After 58After 69 049 1 258 369 4 5 6 7 818 989
11
049 1 258 369 4 5 6 7 818 989 Key=89 ; K=9 Key=49 ; K=9(Collision) K+1 2 9+1=10 (10 mod 10 =0) So Key=49 ; K=0 Key=69 ; K=9(Collision) K+2 2 9+4=13 (13mod 10 =3) So Key=69 ; K=3
12
Quadratic Probing Type Declaration enum kindOfEntry { Legitimate, Empty, Deleted }; struct HashEntry { ElementType Element; enum KindOfEntry Info; }; typedef struct HashEntry Cell; struct HashTbl { int TableSize; Cell *TheCells; };
13
Routine HashTable InitializeTable ( int TableSize ) { HashTable H; int i; H=malloc ( sizeof ( struct HashTbl ) ); H->TheCells = malloc( sizeof( Cell ) * H->TableSize ); for( i=0 ; i TableSize ; i++) H-> TheCells[ i ].Info = Empty; return H; }
14
Find Routine 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; }
15
Insert Routine void Insert( ElementType Key, HashTable H) { Position Pos; Pos = Find( Key, H ); if( H->TheCells[ Pos ].Info != Legitimate ) { H -> TheCells[ Pos ]. Info = Legitimate; H->TheCells[ Pos ].Element = Key; }
16
Double Hashing Empty TableAfter 89After 18After 49After 58After 69 069 1 2 358 4 5 649 7 818 989
17
Key=89 ; K=9 Key=49 ; K=9(Collision) F(i) = i + hash 2 (X) hash 2 (X) = R - (X mod R) (R-Prime Number(less than the table size) =7-(49 mod 7) =7-0 =7 F(i) = i + hash 2 (X) =9+7 =16 (16 mod 10) = 6 So key =49; k=6 Key =69 ; K=9 (Collision) F(i) = i + hash 2 (X) hash 2 (X) = R - (X mod R) (R-Prime Number(less than the table size) =7-(69 mod 7) =7-6 =1 F(i) = i + hash 2 (X) =9+1 =10 (10 mod 10) = 0 So key =69; k=0
18
Rehashing 6 15 23 24 13 6 23 24 13 15 01234560123456 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 13,15,6,24,23
19
Rehash( HashTable H) { int i, OldSize; Cell *OldCells; H=InitializeTable( 2 * OldSize ); for(i=0; i < OldSize; i++) if(Oldcells[ i ].Info == Legitimate ) Insert ( OldCells[ i ].Element, H); free( Oldcells ); return H; }
20
Extendible Hashing 0001 10 11 (2) 000100 001000 001010 001011 (2) 010100 011000 (2) 100000 101000 101100 101110 (2) 111000 111001
21
010011 100 101 (2) 010100 011000 (3) 100000 100100 (3) 101000 101100 101110 (2) 111000 111001 000001110111 (2) 000100 001000 001010 001011 After insertion of 100100 and directory split
22
010011 100 101 (2) 010100 011000 (3) 100000 100100 (3) 101000 101100 101110 (2) 111000 111001 000001110111 (3) 001000 001010 001011 (3) 000000 000100 After insertion of 000000 and leaf split
23
Disjoint Set ADT Equivalence Relations A relation R is defined on a set S if for every pair of elements a,b E S A equivalence relation is a relation R that satisfies three properties aRa, for all a E S (Reflexive) aRb if and only if bRa (Symmetric) aRb and bRc implies that aRc (Transitive)
24
Dynamic Equivalence problem Equivalence class of an element a E S is the subset of S that contains all the elements that are related to a. The input is initially a collection of N sets, each with one element. Each set has a different element, so S i S j =Φ; this makes the sets disjoint. There are two permissible operations Find(a) – returns the name of the set containing the element a Add(a,b)- check whether the element a and b are in the same equivalence class if they are not in the same class then perform the union operation
25
12876543 1 2 3 4 5 6 7 8 9 9 00 00000 00 void initialize(DisjSet S) { Int I; For(i=Numsets;i>0;i--) S[i]=0; }
26
Union(5,6)union(7,8) 12 8 7 6 543 1 2 3 4 5 6 7 8 9 9 0 0 00 0 5 070
27
Union(5,7) 12 8 7 6 543 1 2 3 4 5 6 7 8 9 9 0 0 00 0 5 570 void setUnion(Disjset S, SetType r1, SetType r2) { S[r2] = r1; }
28
Find 12 8 7 6 543 1 2 3 4 5 6 7 8 9 9 0 0 00 0 5 570 SetType find(ElementType X, Disjset S) { if(S[X] <= 0) return X; else return find(S[X], S) }
29
Smart Union Algorithm Union operations can be performed in the following ways Arbitrary union Union by size Union by height / rank
30
Union(5,6)union(8,9) 1287 6 543 1 2 3 4 5 6 7 8 9 9 0 0 00 0 5 008 Arbitrary union (Second tree is the subtree of first tree)
31
Union(5,8) 12 9 8 6 543 1 2 3 4 5 6 7 8 9 7 0 0 00 0 5 058
32
Union by size This can be performed by making the smaller tree is a subtree of larger 12876543 1 2 3 4 5 6 7 8 9 9
33
Union(5,6)union(7,8) 12 8 7 6 543 1 2 3 4 5 6 7 8 9 9 -2 5 70 S[5]=S[5]+S[6] = -1 + -1 = -2 S[6]=5 S[7]=S[7]+S[8] = -1 + -1 = -2 S[8]=7
34
Union(5,7) 12 8 7 6 543 1 2 3 4 5 6 7 8 9 9 -4 5 570 S[5]=S[5]+S[7] = -2 + -2 = -4 S[7]=5
35
Union(4,5) 12 8 7 6 5 4 3 1 2 3 4 5 6 7 8 9 9 5 -5 5 570 S[5]=S[4]+S[5] = -1 + -4 = -5 S[4]=5
36
Union by height (rank) Shallow tree is a subtree of the deeper tree 12876543 1 2 3 4 5 6 7 8 9 9 00 00000 00 Void setunoin(DisjSet S, SetType R1, SetType R2) { if (S[R2]<S[R1]) S[R1]=R2; else if (S[R1]==S[R2]) { S[R1]=S[R1]-1; S[R2]=R1; } }
37
Union(5,6)union(7,8) 12 8 7 6 543 1 2 3 4 5 6 7 8 9 9 0 0 00 5 70 If (S[5]==S[6]) S[5]=S[5]-1 =0-1 = -1 S[6]= 5 If (S[7]==S[8]) S[7]=S[7]-1 =0-1 = -1 S[8]= 7
38
Union(5,7) 12 8 7 6 543 1 2 3 4 5 6 7 8 9 9 0 0 00 -2 5 570 If (S[5]==S[7]) S[5]=S[5]-1 =-1-1 = -2 S[7]= 5
39
Union(4,5) 12 8 7 6 5 4 3 1 2 3 4 5 6 7 8 9 9 0 0 0 -2 5 570 S[5]<S[4] S[4]=5 5
40
Path Compression Path compression is performed during a find operation i.e. every node on the path from X to the root has its parent changed to the root 1 2 8 7 6 5 4 3
41
Find(7) 1 2 8 7 6 5 4 3 SetType find(ElementType X, DisjSet S) { if(S[X] <= 0) return X; else return S[X]=find(S[X], S) }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.