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
Separate Chaining
Type declaration Struct ListNode { ElementType Element; Position Next; }; typedef Position List; Struct HashTbl { int TableSize; List *TheLists; };
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; }
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; }
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; }
OPEN ADDRESSING Linear Probing Quadratic Probing Double Hashing
Linear Probing Empty TableAfter 89After 18After 49After 58After
Quadratic Probing Empty TableAfter 89After 18After 49After 58After
Key=89 ; K=9 Key=49 ; K=9(Collision) K =10 (10 mod 10 =0) So Key=49 ; K=0 Key=69 ; K=9(Collision) K =13 (13mod 10 =3) So Key=69 ; K=3
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; };
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; }
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; }
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; }
Double Hashing Empty TableAfter 89After 18After 49After 58After
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
Rehashing ,15,6,24,23
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; }
Extendible Hashing (2) (2) (2) (2)
(2) (3) (3) (2) (2) After insertion of and directory split
(2) (3) (3) (2) (3) (3) After insertion of and leaf split
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)
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
void initialize(DisjSet S) { Int I; For(i=Numsets;i>0;i--) S[i]=0; }
Union(5,6)union(7,8)
Union(5,7) void setUnion(Disjset S, SetType r1, SetType r2) { S[r2] = r1; }
Find SetType find(ElementType X, Disjset S) { if(S[X] <= 0) return X; else return find(S[X], S) }
Smart Union Algorithm Union operations can be performed in the following ways Arbitrary union Union by size Union by height / rank
Union(5,6)union(8,9) Arbitrary union (Second tree is the subtree of first tree)
Union(5,8)
Union by size This can be performed by making the smaller tree is a subtree of larger
Union(5,6)union(7,8) S[5]=S[5]+S[6] = = -2 S[6]=5 S[7]=S[7]+S[8] = = -2 S[8]=7
Union(5,7) S[5]=S[5]+S[7] = = -4 S[7]=5
Union(4,5) S[5]=S[4]+S[5] = = -5 S[4]=5
Union by height (rank) Shallow tree is a subtree of the deeper tree 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; } }
Union(5,6)union(7,8) 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
Union(5,7) If (S[5]==S[7]) S[5]=S[5]-1 =-1-1 = -2 S[7]= 5
Union(4,5) S[5]<S[4] S[4]=5 5
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
Find(7) SetType find(ElementType X, DisjSet S) { if(S[X] <= 0) return X; else return S[X]=find(S[X], S) }