13
BABA GHULAM SHAH BADSHAH UNIVERSITY (RAJOURI) Department of computer sciences Name:- Shahid Mohammad Roll-No:- 07-MCA-14 Course Title:-Data and File Structure Using C Presentation Topic:-Hashing Submitted To:- Mr. Muzaffar Ahmad Dar(MD)

Data structure by MD Shahid

Embed Size (px)

Citation preview

Page 1: Data structure by MD Shahid

BABA GHULAM SHAH

BADSHAH UNIVERSITY (RAJOURI)

Department of computer sciences

Name:- Shahid Mohammad Roll-No:- 07-MCA-14 Course Title:-Data and File Structure Using C Presentation Topic:-Hashing Submitted To:- Mr. Muzaffar Ahmad Dar(MD)

Page 2: Data structure by MD Shahid

2

Terminology

Hashing: Hashing is the process of mapping a key value to a position in a table.

Hash Function: A hash function maps key values to positions.

Hash Table: A hash table is an array that holds the records.

Searching in a hash table can be done in O(1) regardless of the hash table size.

Introduction to

hashing

Page 3: Data structure by MD Shahid

3

Introduction to Hashing

Page 4: Data structure by MD Shahid

4

Hashing is the process of chopping up the key and mixing it up in various ways in order to obtain an index which will be uniformly distributed over the range of indices - hence the ‘hashing’.

The various techniques of hashing are as under:

Truncation Midsquare Folding Modular Arithmetic

Hash Functions

Page 5: Data structure by MD Shahid

1.TRUNCATION OR (EXTRACTION):- This is easiest for computing address from a key. Here we take only a part of the key as a address.

2. MIDSQUARE METHOD:- In this method the key is squared and some digits or bits from the middle of this square are taken as address.Generally the selection of the digits on the size of the table.

3. FOLDING METHOD:- In this method the key is broken into different parts where the length of each part is same as that of the required address, except possibly the last part. Then these parts are added.

Page 6: Data structure by MD Shahid

4.DIVISION METHOD (Modulo-Division):- In this method the key is divided by the table size and the reminder is taken as address for the hash table. This operation is provided by % operator. This method ensures that we will get the address in the limited range of the table. If the size of table is m, then we will get the addresses in the range {0,1,2,3,………………………m-1}

H(k)=k mod m

CONT……,

Page 7: Data structure by MD Shahid

7

Collision

It is obvious that no matter what function is used, the possibility exists that the use of the function will produce an index which is a duplicate of an index which already exists. This is a Collision.

Collision resolution strategy:

Open addressing: store the key/entry in a different position .

Separate Chaining: chain together several keys/entries in each position.

Collision

Resolution

Page 8: Data structure by MD Shahid

OPEN ADDRESS (CLOSED

HASHING):-In open addressing, the key which caused the collision is placed inside the hash table

itself but at a location other than its hash

address. Initially a key value is mapped to a particular address in the hash table. If that

address is already occupied then we will try to insert the key at

some other empty location inside the

table.

Page 9: Data structure by MD Shahid

Linear probing.

Quadratic probing.

Double hashing

THREE METHODS TO SEARCH AN

EMPTY LOCATION INSIDE THE TABLE

Page 10: Data structure by MD Shahid

SEPARATE CHAINING(

OPEN HASHING):-

In this method, linked lists are maintained for elements that have same hash address. Here the hash table does not contain the actual keys and records but it is just an array of pointers, where each pointers point to a linked list.

Page 11: Data structure by MD Shahid

Probing

If the table position given by the hashed key is already occupied, increase the position by some amount, until an empty position is found.

Load factor

Page 12: Data structure by MD Shahid

Applications of Hashing

Compilers use hash tables to keep track of declared variables

A hash table can be used for on-line spelling checkers — if misspelling detection (rather than correction) is important, an

entire dictionary can be hashed and words checked in constant time

Game playing programs use hash tables to store seen positions, thereby saving computation time if the position is encountered again

Hash functions can be used to quickly check for inequality — if two elements hash to different values they must be different

Storing sparse data

Page 13: Data structure by MD Shahid

THANXX A LOT HAVE A PLEASENT DAY AHEAD