SlideShare une entreprise Scribd logo
1  sur  33
WELCOME TO OUR PRESENTATION
PRESENTED BY
MD PIAS KHAN
&
MAHDEE TAHMID
INSTRUCTED BY
TANVIR AHAMMED
LECTURER,
JAGANNATH UNIVERSITY
1
HASH FUNCTION
Presented by
Md. Pias Khan
ID:B-160305005
Instructed by
Md. Tanvir Alam
Lecturer, JnU
2
Contents
Introduction
History
Application
Problem Concept
Algorithm
Implementation
Complexity Analysis
Reference
3
Introduciton
A hash function is any function
is that can be used to map data
of arbitrary size to a data of
fixed size. The values returned
by them are called hash values.
4
History
◦ The idea of hashing arose independently in different places.
◦ In January 1953, Hans Peter Luhn wrote an internal IBM memorandum
that used hashing with chaining.
◦ Open addressing with linear probing (relatively prime stepping) is
credited to Gene Amdahl, but Ershov (in Russia) had the same idea.
5
Applications
• Hash table.
• In Database.
• Rabin-Karp Algorithm.
• Cryptography.
• Time efficient search.
6
First we shall look at hashing.
• Hashing is used to reduce the
search time for an element in an
array or group of data.
• For this purpose, we map the
elements into the indices of the
array as same as their hash key.
7
First we shall look at hashing.
In this way, we actually can find an element directly
i.e. at O(1) constant time. And this is done by
mapping, which is the credit of hash functions.
We call each element a key and denote the array
as hash table.
8
HASH FUNCTION
Must return a valid table location
Easy to implement.
Should be 1-to 1 mapping (avoid collision).
If key1!=key2 then hash (key1)!=hash(key2)
9
HASH FUNCTION
A collision occurs when two distinct keys hash to the
same location in the array.
Should distributed the keys evenly.
Any key value k is equally likely to hash to any of the
m array locations.
10
STANDARD HASH FUNCTION
Hash value =key(mod) tablesize
Example:
4112041: 12041 mod 1000 = 41
4163490: 63490 mod 1000= 490
Tablesize should be a prime number for even
distribution
11
Here , key(mod) tablesize
2
3
4
5
13
7
8
10
Key Ind
ex
10 0
1
2 2
3 3
4 4
5 5
6
7 7
8 8
9
12
COLLISION
When an element is inserted, if it hashes to the
same value as an already inserted elements, then
we have a collision.
13
COLLISION RESOLVING
TECHNIQUES:
Separate Chaining
Open Addressing
Linear Probing
Quadratic Probing
Double Hashing
14
SEPARATE CHAINING
Usually implemented using linked lists.
To store an element in the hash table you
must insert it into a specific linked list. If there
is any collision (i.e. two different elements have
same hash value) then store both the elements
in the same linked list.
15
Let us consider a simple hash
function as “key mod 7” and
sequence of keys as 50, 700, 76,
85, 92, 73, 101.Then,
• 50 mod 7=1
• 700 mod 7=0
• 85mod 7=1
• 92 mod 7=1
• 73mod 7=3
• 101 mod 7=3
16
LINEAR PROBING
If a collision occurs, try the next cell sequently
F(i)=i
hi (x)=(hash(x) + i) mod TableSize,
Try hash(x) mod TableSize,(hash(x) + 1) mod
TableSize, (hash(x) + 2) mod
TableSize,(hash(x) +3) mod
TableSize,…………
17
Let us consider a simple
hash function as “key
mod 7” and sequence of
keys as 76, 93, 40, 47,
10, 73, 10,55
Here, (5+1) mod 7=6
(5+2) mod 7=0
18
QUADRATIC PROBING
Eliminate primary clustering
F(i)=i2
hi (x)=(hash(x) + i2) mod TableSize,
Try hash(x) mod TableSize,(hash(x) + 12) mod
TableSize, (hash(x) + 22) mod TableSize,(hash(x)
+32) mod TableSize,…………
The table must be at most half full and tablesize
must be prime, otherwisw insertion may fail(always
have a collision)
19
QUADRATIC PROBING
0 49
1
2 58
3 69
4
5
6
7
8 18
9 89
Insert : 89,18,49,58,69
Insert 89,try cell 9
Insert 18,try cell 8
Insert 49, 9+11 try cell 9, 0
Insert58,8+11,8+22;try cell 8,9,2
Insert 69,9+11,9+22,try cell 9,0,3
20
INSERT ALGORITHM
 Hash-insert(T,k) // T= array that represent hash table
 i = 0 to m // m = number of indices ,i=look counter
 Repeat
• j = h(k,i) //J=hash value
• if T[j] == NIL
• T[j] = k
• return j
• else i = i + 1
 until i == m
 error “hash table overflow”
21
SEARCH ALGORITHM
 Hash-Search(T,k)
 i = 0
 repeat
• j = h(k,i)
• If T[j] == k
• Return T[ j]
• i = i + 1
 until T[j]== NIL or i == m
 return NIL
22
IMPLEMENTATION
• #include<stdio.h>
• #include<limits.h>
•
•
• void insert(int ary[],int hFn, int size){
• int element,pos,n=0;
•
• printf("Enter key element to insertn");
• scanf("%d",&element);
•
• pos = element%hFn;
23
while(ary[pos=! size) { // INT_MAX indicates that cell is empty. So if cell is empty loop will
break and goto bottom of the loop to insert element
if(ary[pos]== INT_MAX)
break;
pos = (pos+1)%hFn;
n++;
if(n==size)
break;
}
if(n==size)
printf("Hash table was full of elementsnNo Place to insert this elementnn");
else
ary[pos] = element; //Inserting element
}
24
• void search(int ary[],int hFn,int size){
• int element,pos,n=0;
•
• printf("Enter element you want to searchn");
• scanf("%d",&element);
•
• pos = element%hFn;
•
• while(n++ != size){
• if(ary[pos]==element){
• printf("Element found at index %dn",pos);
• break;
• }
25
• else
• if(ary[pos]==INT_MAX ||ary[pos]!=INT_MIN)
• pos = (pos+1) %hFn;
• }
• if(--n==size) printf("Element not found in hash tablen");
• }
• void display(int ary[],int size){
• int i;
•
• printf("IndextValuen");
•
•
26
• for(i=0;i<size;i++)
• printf("%dt%dn",i,ary[i]);
• }
• int main(){
• int size,hFn,i,choice;
•
• printf("Enter size of hash tablen");
• scanf("%d",&size);
•
• int ary[size];
•
• printf("Enter hash function [if mod 10 enter 10]n");
• scanf("%d",&hFn);
27
• for(i=0;i<size;i++)
• ary[i]=INT_MIN; //Assigning INT_MIN indicates that
cell is empty
•
• do{
• printf("Enter your choicen");
• printf(" 1-> Insertn 2-> Deleten 3-> Displayn
4-> Searchingn 0-> Exitn");
• scanf("%d",&choice);
28
• switch(choice){
• case 1:
• insert(ary,hFn,size);
• break;
•
• case 3:
• display(ary,size);
• break;
•
29
• case 4:
• search(ary,hFn,size);
• break;
• default:
• printf("Enter correct choicen");
• break;
• }
• }while(choice);
•
• return 0;
• }
30
COMPLEXITY ANALYSIS
Since we have to search for keys at
indexes not same as the key, the
search time is not O(1). But it is still
much better than O(logn).
31
REFERENCES
 https://en.wikipedia.org/wiki/Hash_table
 Introduction to Algorithms, Third Edition by By Thomas H. Cormen, Charles E. Leiserson, Ronald L.
Rivest and Clifford Stein
32
• Hoping that you have no question to us, we conclude our presentation. Yet if
someone wishes to embarrass us, other than our honorable teacher , I shall not
bother to answer my dear mates.

Contenu connexe

Tendances

Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithmAamir Sohail
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Kuntal Bhowmick
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms HashingManishPrajapati78
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure Meghaj Mallick
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables Nifras Ismail
 
Hash table
Hash tableHash table
Hash tableVu Tran
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing conceptsLJ Projects
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmayTanmay 'Unsinkable'
 

Tendances (20)

4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Hashing
HashingHashing
Hashing
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Hash tables
Hash tablesHash tables
Hash tables
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Hashing
HashingHashing
Hashing
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables
 
Hash table
Hash tableHash table
Hash table
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Hashing
HashingHashing
Hashing
 
linear probing
linear probinglinear probing
linear probing
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmay
 
Hashing
HashingHashing
Hashing
 
Hash tables
Hash tablesHash tables
Hash tables
 

Similaire à Hash function

Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 
Hashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT iHashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT icajiwol341
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithmfarhankhan89766
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptxchin463670
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingHaripritha
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniyaTutorialsDuniya.com
 
Hashing using a different methods of technic
Hashing using a different methods of technicHashing using a different methods of technic
Hashing using a different methods of techniclokaprasaadvs
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptxkratika64
 
HASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxHASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxJITTAYASHWANTHREDDY
 
Radix and shell sort
Radix and shell sortRadix and shell sort
Radix and shell sortAaron Joaquin
 
Hashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.pptHashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.pptssusere3b1a2
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingSam Light
 
Lecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxLecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxSLekshmiNair
 

Similaire à Hash function (20)

Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Hashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT iHashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT i
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashing
 
LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
session 15 hashing.pptx
session 15   hashing.pptxsession 15   hashing.pptx
session 15 hashing.pptx
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Hashing using a different methods of technic
Hashing using a different methods of technicHashing using a different methods of technic
Hashing using a different methods of technic
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
HASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxHASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptx
 
Radix and shell sort
Radix and shell sortRadix and shell sort
Radix and shell sort
 
Hashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.pptHashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.ppt
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
Hashing
HashingHashing
Hashing
 
Lecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxLecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptx
 

Dernier

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 

Dernier (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 

Hash function

  • 1. WELCOME TO OUR PRESENTATION PRESENTED BY MD PIAS KHAN & MAHDEE TAHMID INSTRUCTED BY TANVIR AHAMMED LECTURER, JAGANNATH UNIVERSITY 1
  • 2. HASH FUNCTION Presented by Md. Pias Khan ID:B-160305005 Instructed by Md. Tanvir Alam Lecturer, JnU 2
  • 4. Introduciton A hash function is any function is that can be used to map data of arbitrary size to a data of fixed size. The values returned by them are called hash values. 4
  • 5. History ◦ The idea of hashing arose independently in different places. ◦ In January 1953, Hans Peter Luhn wrote an internal IBM memorandum that used hashing with chaining. ◦ Open addressing with linear probing (relatively prime stepping) is credited to Gene Amdahl, but Ershov (in Russia) had the same idea. 5
  • 6. Applications • Hash table. • In Database. • Rabin-Karp Algorithm. • Cryptography. • Time efficient search. 6
  • 7. First we shall look at hashing. • Hashing is used to reduce the search time for an element in an array or group of data. • For this purpose, we map the elements into the indices of the array as same as their hash key. 7
  • 8. First we shall look at hashing. In this way, we actually can find an element directly i.e. at O(1) constant time. And this is done by mapping, which is the credit of hash functions. We call each element a key and denote the array as hash table. 8
  • 9. HASH FUNCTION Must return a valid table location Easy to implement. Should be 1-to 1 mapping (avoid collision). If key1!=key2 then hash (key1)!=hash(key2) 9
  • 10. HASH FUNCTION A collision occurs when two distinct keys hash to the same location in the array. Should distributed the keys evenly. Any key value k is equally likely to hash to any of the m array locations. 10
  • 11. STANDARD HASH FUNCTION Hash value =key(mod) tablesize Example: 4112041: 12041 mod 1000 = 41 4163490: 63490 mod 1000= 490 Tablesize should be a prime number for even distribution 11
  • 12. Here , key(mod) tablesize 2 3 4 5 13 7 8 10 Key Ind ex 10 0 1 2 2 3 3 4 4 5 5 6 7 7 8 8 9 12
  • 13. COLLISION When an element is inserted, if it hashes to the same value as an already inserted elements, then we have a collision. 13
  • 14. COLLISION RESOLVING TECHNIQUES: Separate Chaining Open Addressing Linear Probing Quadratic Probing Double Hashing 14
  • 15. SEPARATE CHAINING Usually implemented using linked lists. To store an element in the hash table you must insert it into a specific linked list. If there is any collision (i.e. two different elements have same hash value) then store both the elements in the same linked list. 15
  • 16. Let us consider a simple hash function as “key mod 7” and sequence of keys as 50, 700, 76, 85, 92, 73, 101.Then, • 50 mod 7=1 • 700 mod 7=0 • 85mod 7=1 • 92 mod 7=1 • 73mod 7=3 • 101 mod 7=3 16
  • 17. LINEAR PROBING If a collision occurs, try the next cell sequently F(i)=i hi (x)=(hash(x) + i) mod TableSize, Try hash(x) mod TableSize,(hash(x) + 1) mod TableSize, (hash(x) + 2) mod TableSize,(hash(x) +3) mod TableSize,………… 17
  • 18. Let us consider a simple hash function as “key mod 7” and sequence of keys as 76, 93, 40, 47, 10, 73, 10,55 Here, (5+1) mod 7=6 (5+2) mod 7=0 18
  • 19. QUADRATIC PROBING Eliminate primary clustering F(i)=i2 hi (x)=(hash(x) + i2) mod TableSize, Try hash(x) mod TableSize,(hash(x) + 12) mod TableSize, (hash(x) + 22) mod TableSize,(hash(x) +32) mod TableSize,………… The table must be at most half full and tablesize must be prime, otherwisw insertion may fail(always have a collision) 19
  • 20. QUADRATIC PROBING 0 49 1 2 58 3 69 4 5 6 7 8 18 9 89 Insert : 89,18,49,58,69 Insert 89,try cell 9 Insert 18,try cell 8 Insert 49, 9+11 try cell 9, 0 Insert58,8+11,8+22;try cell 8,9,2 Insert 69,9+11,9+22,try cell 9,0,3 20
  • 21. INSERT ALGORITHM  Hash-insert(T,k) // T= array that represent hash table  i = 0 to m // m = number of indices ,i=look counter  Repeat • j = h(k,i) //J=hash value • if T[j] == NIL • T[j] = k • return j • else i = i + 1  until i == m  error “hash table overflow” 21
  • 22. SEARCH ALGORITHM  Hash-Search(T,k)  i = 0  repeat • j = h(k,i) • If T[j] == k • Return T[ j] • i = i + 1  until T[j]== NIL or i == m  return NIL 22
  • 23. IMPLEMENTATION • #include<stdio.h> • #include<limits.h> • • • void insert(int ary[],int hFn, int size){ • int element,pos,n=0; • • printf("Enter key element to insertn"); • scanf("%d",&element); • • pos = element%hFn; 23
  • 24. while(ary[pos=! size) { // INT_MAX indicates that cell is empty. So if cell is empty loop will break and goto bottom of the loop to insert element if(ary[pos]== INT_MAX) break; pos = (pos+1)%hFn; n++; if(n==size) break; } if(n==size) printf("Hash table was full of elementsnNo Place to insert this elementnn"); else ary[pos] = element; //Inserting element } 24
  • 25. • void search(int ary[],int hFn,int size){ • int element,pos,n=0; • • printf("Enter element you want to searchn"); • scanf("%d",&element); • • pos = element%hFn; • • while(n++ != size){ • if(ary[pos]==element){ • printf("Element found at index %dn",pos); • break; • } 25
  • 26. • else • if(ary[pos]==INT_MAX ||ary[pos]!=INT_MIN) • pos = (pos+1) %hFn; • } • if(--n==size) printf("Element not found in hash tablen"); • } • void display(int ary[],int size){ • int i; • • printf("IndextValuen"); • • 26
  • 27. • for(i=0;i<size;i++) • printf("%dt%dn",i,ary[i]); • } • int main(){ • int size,hFn,i,choice; • • printf("Enter size of hash tablen"); • scanf("%d",&size); • • int ary[size]; • • printf("Enter hash function [if mod 10 enter 10]n"); • scanf("%d",&hFn); 27
  • 28. • for(i=0;i<size;i++) • ary[i]=INT_MIN; //Assigning INT_MIN indicates that cell is empty • • do{ • printf("Enter your choicen"); • printf(" 1-> Insertn 2-> Deleten 3-> Displayn 4-> Searchingn 0-> Exitn"); • scanf("%d",&choice); 28
  • 29. • switch(choice){ • case 1: • insert(ary,hFn,size); • break; • • case 3: • display(ary,size); • break; • 29
  • 30. • case 4: • search(ary,hFn,size); • break; • default: • printf("Enter correct choicen"); • break; • } • }while(choice); • • return 0; • } 30
  • 31. COMPLEXITY ANALYSIS Since we have to search for keys at indexes not same as the key, the search time is not O(1). But it is still much better than O(logn). 31
  • 32. REFERENCES  https://en.wikipedia.org/wiki/Hash_table  Introduction to Algorithms, Third Edition by By Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein 32
  • 33. • Hoping that you have no question to us, we conclude our presentation. Yet if someone wishes to embarrass us, other than our honorable teacher , I shall not bother to answer my dear mates.