Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random...

Preview:

Citation preview

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

Separate Chaining

0

1

4

25

16

9

81

64

36

49

0

1

2

3

4

5

6

7

8

9

• Type declarationStruct ListNode

{

ElementType Element;

Position Next;

};

typedef Position List;

Struct HashTbl

{

int TableSize;

List *TheLists;

};

• 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;

}

• 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;

}

• 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;

}}

OPEN ADDRESSING

• Linear Probing

• Quadratic Probing

• Double Hashing

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

0 49

1 58

2 69

3

4

5

6

7

8 18

9 89

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

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

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;

};

• 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;

}

• 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;

}

• 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;

}

}

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

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

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

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

00 01 10 11

(2)

000100

001000

001010

001011

(2)

010100

011000

(2)

100000

101000

101100

101110

(2)

111000

111001

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

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

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)

• 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

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;

}

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

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;

}

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)

}

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)

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)

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

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

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

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

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

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; }

}

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

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

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

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

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)

}

Recommended