Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random...
-
Upload
chloe-hansen -
Category
Documents
-
view
214 -
download
1
Transcript of Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random...
![Page 1: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/1.jpg)
HashKey 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
![Page 2: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/2.jpg)
Separate Chaining
0
1
4
25
16
9
81
64
36
49
0
1
2
3
4
5
6
7
8
9
![Page 3: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/3.jpg)
• Type declarationStruct ListNode
{
ElementType Element;
Position Next;
};
typedef Position List;
Struct HashTbl
{
int TableSize;
List *TheLists;
};
![Page 4: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/4.jpg)
• Initialization routineHashTable
InitializeTable(int TableSize)
{
HashTable H;
int i;
H->TheLists = malloc( sizeof( List ) * H->TableSize );
for(i= 0; i < H -> Tablesize; i++ )
{
H->TheLists[ i ] = malloc( sizeof( struct ListNode ) );
H->TheLists[ i ] -> Next = NULL;
}
return H;
}
![Page 5: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/5.jpg)
• Find routineFind (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;
}
![Page 6: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/6.jpg)
• Insert RoutinevoidInsert( 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;
}}
![Page 7: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/7.jpg)
OPEN ADDRESSING
• Linear Probing
• Quadratic Probing
• Double Hashing
![Page 8: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/8.jpg)
Linear Probing
Empty Table After 89 After 18 After 49 After 58 After 69
0 49 49 49
1 58 58
2 69
3
4
5
6
7
8 18 18 18 18
9 89 89 89 89 89
![Page 9: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/9.jpg)
0 49
1 58
2 69
3
4
5
6
7
8 18
9 89
![Page 10: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/10.jpg)
Quadratic Probing
Empty Table After 89 After 18 After 49 After 58 After 69
0 49 49 49
1
2 58 58
3 69
4
5
6
7
8 18 18 18 18
9 89 89 89 89 89
![Page 11: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/11.jpg)
0 49
1
2 58
3 69
4
5
6
7
8 18
9 89
Key=89 ; K=9
Key=49 ; K=9(Collision)
K+12
9+1=10
(10 mod 10 =0)
So Key=49 ; K=0
Key=69 ; K=9(Collision)
K+22
9+4=13
(13mod 10 =3)
So Key=69 ; K=3
![Page 12: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/12.jpg)
Quadratic Probing
• Type Declarationenum kindOfEntry { Legitimate, Empty, Deleted };struct HashEntry{
ElementType Element;enum KindOfEntry Info;
};typedef struct HashEntry Cell;
struct HashTbl{
int TableSize;Cell *TheCells;
};
![Page 13: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/13.jpg)
• RoutineHashTable
InitializeTable ( int TableSize )
{
HashTable H;
int i;
H=malloc ( sizeof ( struct HashTbl ) );
H->TheCells = malloc( sizeof( Cell ) * H->TableSize );
for( i=0 ; i < H -> TableSize ; i++)
H-> TheCells[ i ].Info = Empty;
return H;
}
![Page 14: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/14.jpg)
• Find RoutinePosition
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;
}
![Page 15: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/15.jpg)
• Insert Routinevoid
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;
}
}
![Page 16: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/16.jpg)
Double Hashing
Empty Table After 89 After 18 After 49 After 58 After 69
0 69
1
2
3 58 58
4
5
6 49 49 49
7
8 18 18 18 18
9 89 89 89 89 89
![Page 17: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/17.jpg)
Key=89 ; K=9
Key=49 ; K=9(Collision)
F(i) = i + hash2(X)
hash2(X) = R - (X mod R)
(R-Prime Number(less than the table size)
=7-(49 mod 7)
=7-0
=7
F(i) = i + hash2(X) =9+7 =16
(16 mod 10) = 6
So key =49; k=6
Key =69 ; K=9 (Collision)
F(i) = i + hash2(X)
hash2(X) = R - (X mod R)
(R-Prime Number(less than the table size)
=7-(69 mod 7)
=7-6
=1
F(i) = i + hash2(X) =9+1 =10
(10 mod 10) = 0
So key =69; k=0
![Page 18: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/18.jpg)
Rehashing
6
15
23
24
13
6
23
24
13
15
0
1
2
3
4
5
6
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
13,15,6,24,23
![Page 19: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/19.jpg)
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;
}
![Page 20: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/20.jpg)
Extendible Hashing
00 01 10 11
(2)
000100
001000
001010
001011
(2)
010100
011000
(2)
100000
101000
101100
101110
(2)
111000
111001
![Page 21: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/21.jpg)
010 011 100 101
(2)
010100
011000
(3)
100000
100100
(3)
101000
101100
101110
(2)
111000
111001
000 001 110 111
(2)
000100
001000
001010
001011
After insertion of 100100 and directory split
![Page 22: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/22.jpg)
010 011 100 101
(2)
010100
011000
(3)
100000
100100
(3)
101000
101100
101110
(2)
111000
111001
000 001 110 111
(3)
001000
001010
001011
(3)
000000
000100
After insertion of 000000 and leaf split
![Page 23: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/23.jpg)
Disjoint Set ADT
• Equivalence RelationsA 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)
![Page 24: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/24.jpg)
• 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 Si ∩ Sj=Φ; 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
![Page 25: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/25.jpg)
1 2 876543
1 2 3 4 5 6 7 8 9
9
0 0 0 0 0 0 0 0 0
void initialize(DisjSet S)
{
Int I;
For(i=Numsets;i>0;i--)
S[i]=0;
}
![Page 26: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/26.jpg)
Union(5,6) union(7,8)
1 2
8
7
6
543
1 2 3 4 5 6 7 8 9
9
0 0 0 0 0 5 0 7 0
![Page 27: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/27.jpg)
Union(5,7)
1 2
8
7
6
543
1 2 3 4 5 6 7 8 9
9
0 0 0 0 0 5 5 7 0
void
setUnion(Disjset S, SetType r1 , SetType r2)
{
S[r2] = r1;
}
![Page 28: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/28.jpg)
Find
1 2
8
7
6
543
1 2 3 4 5 6 7 8 9
9
0 0 0 0 0 5 5 7 0
SetType find(ElementType X, Disjset S)
{
if(S[X] <= 0) return X;
else return find(S[X] , S)
}
![Page 29: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/29.jpg)
Smart Union Algorithm
Union operations can be performed in the following ways
• Arbitrary union• Union by size• Union by height / rank
![Page 30: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/30.jpg)
Union(5,6) union(8,9)
1 2 87
6
543
1 2 3 4 5 6 7 8 9
9
0 0 0 0 0 5 0 0 8
Arbitrary union(Second tree is the subtree of first tree)
![Page 31: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/31.jpg)
Union(5,8)
1 2
9
8
6
543
1 2 3 4 5 6 7 8 9
7
0 0 0 0 0 5 0 5 8
![Page 32: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/32.jpg)
Union by sizeThis can be performed by making the smaller tree is a subtree of larger
1 2 876543
1 2 3 4 5 6 7 8 9
9
-1 -1 -1 -1 -1 -1 -1 -1 -1
![Page 33: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/33.jpg)
Union(5,6) union(7,8)
1 2
8
7
6
543
1 2 3 4 5 6 7 8 9
9
-1 -1 -1 -1 -2 5 -2 7 0
S[5]=S[5]+S[6]
= -1 + -1
= -2
S[6]=5
S[7]=S[7]+S[8]
= -1 + -1
= -2
S[8]=7
![Page 34: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/34.jpg)
Union(5,7)
1 2
8
7
6
543
1 2 3 4 5 6 7 8 9
9
-1 -1 -1 -1 -4 5 5 7 0
S[5]=S[5]+S[7]
= -2 + -2
= -4
S[7]=5
![Page 35: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/35.jpg)
Union(4,5)
1 2
8
7
6
5
4
3
1 2 3 4 5 6 7 8 9
9
-1 -1 -1 5 -5 5 5 7 0
S[5]=S[4]+S[5]
= -1 + -4
= -5
S[4]=5
![Page 36: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/36.jpg)
Union by height (rank) Shallow tree is a subtree of the deeper tree
1 2 876543
1 2 3 4 5 6 7 8 9
9
0 0 0 0 0 0 0 0 0
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; }
}
![Page 37: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/37.jpg)
Union(5,6) union(7,8)
1 2
8
7
6
543
1 2 3 4 5 6 7 8 9
9
0 0 0 0 -1 5 -1 7 0
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
![Page 38: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/38.jpg)
Union(5,7)
1 2
8
7
6
543
1 2 3 4 5 6 7 8 9
9
0 0 0 0 -2 5 5 7 0
If (S[5]==S[7])
S[5]=S[5]-1
=-1-1 = -2
S[7]= 5
![Page 39: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/39.jpg)
Union(4,5)
1 2
8
7
6
5
4
3
1 2 3 4 5 6 7 8 9
9
0 0 0 -2 5 5 7 0
S[5]<S[4]
S[4]=5
5
![Page 40: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/40.jpg)
Path CompressionPath 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
![Page 41: Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.](https://reader035.fdocuments.us/reader035/viewer/2022062618/55142e705503466d1a8b4ed0/html5/thumbnails/41.jpg)
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)
}