Hash Key to address transformation Division remainder method Hash(key)= key mod tablesize Random number generation Folding method Digit or Character extraction.

Slides:



Advertisements
Similar presentations
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables III.
Advertisements

Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
0 - 0.
2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt Time Money AdditionSubtraction.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
Properties of numbers EVEN and ODD numbers
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
Learning to show the remainder
Binary Relations Binary Relations on Real Numbers.
Linked Lists.
LIST PROCESSING.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
© 2004 Goodrich, Tamassia Hash Tables
1 Hash Tables Saurav Karmakar. 2 Motivation What are the dictionary operations? What are the dictionary operations? (1) Insert (1) Insert (2) Delete (2)
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
CSCI 2720 Hashing   Spring 2005.
The ADT Hash Table What is a table?
Hash Table.
Briana B. Morrison Adapted from William Collins
© 2004 Goodrich, Tamassia Hash Tables
Preliminaries Advantages –Hash tables can insert(), remove(), and find() with complexity close to O(1). –Relatively easy to program Disadvantages –There.
Dictionaries and Hash Tables
1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Analysis of Algorithms CS 477/677
Hash Tables.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
Dictionaries 4/1/2017 2:36 PM Hash Tables  …
Hashing.
Hash Tables Dr. Li Jiang School of Computer Science,
Quadratic Inequalities
Backup Slides. An Example of Hash Function Implementation struct MyStruct { string str; string item; };
Overview Hash Table Hash Function Hash table ADT operations
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
Addition 1’s to 20.
25 seconds left…...
Test B, 100 Subtraction Facts
We will resume in: 25 Minutes.
Equivalence Relations
§4 Open Addressing 2. Quadratic Probing f ( i ) = i 2 ; /* a quadratic function */ 【 Theorem 】 If quadratic probing is used, and the table size is prime,
Equivalence Relations
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
CHAPTER 7 HASHING What is hashing for? For searching But we already have binary search in O( ln n ) time after sorting. And we already have algorithms.
Hashing Techniques.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
1 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union.
Aree Teeraparbseree, Ph.D
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
Hashing. Hashing as a Data Structure Performs operations in O(c) –Insert –Delete –Find Is not suitable for –FindMin –FindMax –Sort or output as sorted.
§3 Separate Chaining ---- keep a list of all keys that hash to the same value struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
Robbie CSCI2100A Data Structures Tutorial
Hash Tables - Motivation
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 Equivalence relations Binary relations: –Let S1 and S2 be two sets, and R be a (binary relation) from S1 to S2 –Not every x in S1 and y in S2 have such.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
Fundamental Structures of Computer Science II
Hashing Problem: store and retrieving an item using its key (for example, ID number, name) Linked List takes O(N) time Binary Search Tree take O(logN)
Hash In-Class Quiz.
Collision Resolution Neil Tang 02/18/2010
Collision Resolution Neil Tang 02/21/2008
Hashing.
Presentation transcript:

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