Previous Lecture Revision Previous Lecture Revision Hashing Searching : –The Main purpose of computer is to store & retrieve –Locating for a record is the most time consuming action Methods: –Linear Search ( Search for a target element by element) » Asymptotic notation is O(n) –Binary Search ( Break the Group into two and search in one half) » Asymptotic notation is O(log 2 n) –Hashing ( Direct access )-Used in DBMS / File Systems » Asymptotic notation is O(1)
Hashing Techniques What is Hashing? : from the Key (INPUT) itself the index where it should be stored is derived. »Advantage :While Reading Back it can be READ IMMEDIATELY Techniques: – Identity - Key itself becomes a index » Dis : Memory Should be LIMITLESS – Truncation - The Last digit is truncated and used as index » EG : Key So the index is 6 – Folding - Addition/Multiplication/Division is done on the key to obtain the index » EG : so index is 2
Hashing Techniques Modular Arithmetic : Use Some Arithmetic calculation on the Key to obtain the index –KEY % size EG : 123 % 10 – CAN WE USE 123 / 10 ? What is a Collision ? If the hashing Function gives out SAME INDEX for TWO keys then it is called a collision »EG : 123 % 10 index will be 3 » 223 % 10 index will be again 3 We cannot store two values in the same location
RESOLVING COLLISIONS
How to resolve collisions Three Methods are available, 1. Resolving collisions by REPLACEMENT 2. Resolving collisions by OPEN ADDRESSING »Linear probing »Quadratic probing 3. Resolving collisions by CHAINING
Resolving collisions by replacement Working : we simply replace the old KEY with the new KEY when there is a collision » The Old Key is simply Lost » Or » It is combined with the new Key. » EG : index = key % 10 When it is used ? –Very Rarely used –Used only when the data are sets. Using UNION Operation old data is combined with the new data
We resolve the collision by putting the new Key in some other empty location in the table. –Two methods are used to locate the empty location 1. Linear Probing 2. Quadratic Probing Linear Probing : –Start at the point where the collision occurred and do a sequential search through the table for an empty location. Improvement : –Circular Probing : After reaching the end start probing from the first Resolving collisions by Open addressing Example
Example - Linear Probing Keys = 6 Table Size = 7 Function = key mod 7 key Index SOLUTION Index
Quadratic Probing: If there is a collision at the address ‘h’, this method probes the table at locations h+I 2 ( % hashsize )for I = 1,2,… Dis: It does not probe all locations in the table. Keys = 6 Table Size = 7 Function = key mod 7 key Index index = h + I 2 % 7 I = 1, 2, 3 …… Resolving collisions by Open addressing
example Void main ( ) { int table[MAX],index,I target; for(I=1;I<=MAX;I++) table[I-1]=10*I; cin>>target; index = HASH(target); if (index!=1) {if table[index] == target) cout<<“Found at”<<index; else cout <<“Target Not found”;} # define MAX 20 int HASH(int key) { int index; index = key/10-1; if (index<MAX) return index; else return -1; }
Implemented using Linked List Whenever a collision occurs a new node is created and the new value is stored and linked to the old value. Resolving collisions by Chaining key Index
Exercise Using modulo-division method and linear probing store the below in an array of 19 elements , , ,140145, , , , , Index is : % 19 = 1, % 19 =10, % 19 =14, % 19 =1 ( 2), % 19 =9, % 19 =18, % 19 =10 ( 11), % 19 =12, % 19 =