SlideShare uma empresa Scribd logo
1 de 19
Presented by:
P.Meenakshi(M.Tech.,)
HASHING
Hashing is the transformation of a string of
characters into a usually shorter fixed-length value
or key that represents the original string. Hashing is
used to index and retrieve items in a database
because it is faster to find the item using the shorter
hashed key than to find it using the original value.
Hashing is nothing but by implementing a hash
table.
Hashing is a technique used for performing
insertions, deletions and finds in constant average
time
Meenakshi(M.tech)
Hash table
• Hash table ADT which supports only a subset of the operations
allowed by binary search trees.
• Hash table structure is merely an array of some fixed size,
containing the items.
Key could be an integer, a string, etc.
e.g. a name or Id that is a part of a large employee structure
• The size of the array is TableSize.
• The items that are stored in the hash table are indexed by values
from 0 to TableSize – 1.
• Each key is mapped into some number in the range 0 to TableSize
– 1.
• The mapping is called a hash function.
Meenakshi(M.Tech)
Hash Function
A hash function is used to map a dictionaries pairs
into the positions of a hash table.
A bucket array is used for a hash table.
The hash function:
must be simple to compute.
must distribute the keys evenly among the cells.
If we know which keys will occur in advance we can
write perfect hash functions
Meenakshi(M.Tech)
Hash functions
If the input keys are integers then simply Key mod
TableSize is a general strategy.
Unless key happens to have some undesirable
properties.
e.g. all keys end in 0 and we use mod 10
If the keys are strings, hash function needs more care.
First convert it into a numeric value.
Meenakshi(M.Tech)
Hash functions
Some of the methods are used for creating a
hash function:
Division method
Multiplication method
Truncation method
Folding method
Extraction method
Meenakshi(M.Tech)
Hash Function Methods
Division method:
Map a key k into one of m slots by taking the
remainder of k divided by m.
h(k)= k mod m
Ex: If table size m=12, key k=100 then
h(100) = 100 mod 12
=4
Truncation method:
Ignore some digits of the key and use the rest
as the array index.
Ex: Student ID’s are key 123456789 then select 9
digit number select just the 4th and 8th position digits
as the index is i.e. 48 as the index.
Meenakshi(M.Tech)
Hash Function Methods
Folding method:
Partition the key into several pieces, i.e. two or
three or more pieces. Each of the individual parts is
combined using any of the basic operations such as
addition or multiplication
Ex: consider a number 123456. partition the number
12|34|56. Add these three parts 12+34+56=112 index of the
hash table to store the number 123456.
Extraction method:
In this method computing the address uses only a
part of the key. From a student id 928324312 the method
may use first four digits, 9283, or the last four digits, 4312,
or the combination of the first two and last two digits 9212
Meenakshi(M.Tech)
9
Hash function for strings:
a l ikey
KeySize = 3;
98 108 105
hash(“ali”) = (105 * 1 + 108*37 + 98*372) % 10,007 = 8172
0 1 2 i
key[i]
hash
function
ali
……
……
0
1
2
8172
10,006 (TableSize)
“ali”
Meenakshi(M.Tech)
Collision Resolution
• If, when an element is inserted, it hashes to the
same value as an already inserted element, then
we have a collision and need to resolve it.
• There are several methods for dealing with
this:
– Separate chaining
– Open addressing
• Linear Probing
• Quadratic Probing
• Double Hashing
Meenakshi(M.Tech)
Collision Resolution Example
Hash
Function
9
0
1
2
3
Key
K=9
K=13
K=17
Hashed
value 1
Hash _table(I, J)
Meenakshi(M.Tech)
Collision Resolution
The most popular techniques for collision resolution are:
Separate Chaining
Open addressing
Separate Chaining:
The hash table is implemented as an array of linked
list.
The idea is to keep a list of all elements that hash to
the same value.
The array elements are pointers to the first nodes of the
lists.
A new item is inserted to the front of the list.
Meenakshi(M.Tech)
Collision Resolution
Advantages of Separate Chaining:
• Better space utilization for large items.
• Simple collision handling: searching linked list.
• Overflow: we can store more items than the hash
table size.
• Deletion is quick and easy: deletion from the linked
list.
• It Is a simple and efficient method
• Table size need not be a prime number
Meenakshi(M.Tech)
Operations
• Initialization: all entries are set to NULL
• Find:
– locate the cell using hash function.
– sequential search on the linked list in that cell.
• Insertion:
– Locate the cell using hash function.
– (If the item does not exist) insert it as the first item in
the list.
• Deletion:
– Locate the cell using hash function.
– Delete the item from the linked list.
Meenakshi(M.Tech)
Example
Let us take some keys 23, 13, 21, 14, 7, 8, 15 in the
same order, in a hash table of size 7 using separate
chaining with the hash function.
h(23)=23%7=2
h(13)=13%7=6
h(21)=21%7=0
h(14)=14%7=0(collision)
h(7) =7%7 =0(collision)
h(8) =8%7 =1
h(15)=15%7=1(collision)
Meenakshi(M.Tech)
Open Addressing
In an open addressing hashing system, all the data go inside
the table.
Thus, a bigger table is needed.
Generally the load factor should be below 0.5.
If a collision occurs, alternative cells are tried until an empty
cell is found.
There are three common collision resolution strategies:
Linear Probing
Quadratic probing
Double hashing
Meenakshi(M.Tech)
Linear probing is s technique for resolving hash collisions of
values of hash function.
In which an interval between probes will be fixed to 1
h(x)= x mod 10
Quadratic probing: In which an interval between probes is
increased by adding the successive outputs of a quadratic
polynomial to the starting value given by the hash function.
Quadratic Probing eliminates primary clustering problem of
linear probing.
h(key)=(h(key)+1)mod 10
Collision Resolution
Meenakshi(M.Tech)
Real world example
Library Meenakshi(M.Tech)
Hashing

Mais conteúdo relacionado

Mais procurados

Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm KristinaBorooah
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms HashingManishPrajapati78
 
Circular linked list
Circular linked listCircular linked list
Circular linked listchauhankapil
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)Home
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Hash table
Hash tableHash table
Hash tableVu Tran
 
trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure Meghaj Mallick
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its usesJawad Khan
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries LectureFelipe Costa
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingSam Light
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 

Mais procurados (20)

Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Hash table
Hash tableHash table
Hash table
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Hashing
HashingHashing
Hashing
 
Hash tables
Hash tablesHash tables
Hash tables
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Linked list
Linked listLinked list
Linked list
 
Radix sorting
Radix sortingRadix sorting
Radix sorting
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Merge sort
Merge sortMerge sort
Merge sort
 

Semelhante a Hashing

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
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptxkratika64
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
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
 
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
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniyaTutorialsDuniya.com
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptxchin463670
 

Semelhante a Hashing (20)

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
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
hashing.pdf
hashing.pdfhashing.pdf
hashing.pdf
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
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
 
Hash function
Hash functionHash function
Hash function
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
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)
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Hashing 1
Hashing 1Hashing 1
Hashing 1
 
Hashing data
Hashing dataHashing data
Hashing data
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 

Último

4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 

Último (20)

4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 

Hashing

  • 2. HASHING Hashing is the transformation of a string of characters into a usually shorter fixed-length value or key that represents the original string. Hashing is used to index and retrieve items in a database because it is faster to find the item using the shorter hashed key than to find it using the original value. Hashing is nothing but by implementing a hash table. Hashing is a technique used for performing insertions, deletions and finds in constant average time Meenakshi(M.tech)
  • 3. Hash table • Hash table ADT which supports only a subset of the operations allowed by binary search trees. • Hash table structure is merely an array of some fixed size, containing the items. Key could be an integer, a string, etc. e.g. a name or Id that is a part of a large employee structure • The size of the array is TableSize. • The items that are stored in the hash table are indexed by values from 0 to TableSize – 1. • Each key is mapped into some number in the range 0 to TableSize – 1. • The mapping is called a hash function. Meenakshi(M.Tech)
  • 4. Hash Function A hash function is used to map a dictionaries pairs into the positions of a hash table. A bucket array is used for a hash table. The hash function: must be simple to compute. must distribute the keys evenly among the cells. If we know which keys will occur in advance we can write perfect hash functions Meenakshi(M.Tech)
  • 5. Hash functions If the input keys are integers then simply Key mod TableSize is a general strategy. Unless key happens to have some undesirable properties. e.g. all keys end in 0 and we use mod 10 If the keys are strings, hash function needs more care. First convert it into a numeric value. Meenakshi(M.Tech)
  • 6. Hash functions Some of the methods are used for creating a hash function: Division method Multiplication method Truncation method Folding method Extraction method Meenakshi(M.Tech)
  • 7. Hash Function Methods Division method: Map a key k into one of m slots by taking the remainder of k divided by m. h(k)= k mod m Ex: If table size m=12, key k=100 then h(100) = 100 mod 12 =4 Truncation method: Ignore some digits of the key and use the rest as the array index. Ex: Student ID’s are key 123456789 then select 9 digit number select just the 4th and 8th position digits as the index is i.e. 48 as the index. Meenakshi(M.Tech)
  • 8. Hash Function Methods Folding method: Partition the key into several pieces, i.e. two or three or more pieces. Each of the individual parts is combined using any of the basic operations such as addition or multiplication Ex: consider a number 123456. partition the number 12|34|56. Add these three parts 12+34+56=112 index of the hash table to store the number 123456. Extraction method: In this method computing the address uses only a part of the key. From a student id 928324312 the method may use first four digits, 9283, or the last four digits, 4312, or the combination of the first two and last two digits 9212 Meenakshi(M.Tech)
  • 9. 9 Hash function for strings: a l ikey KeySize = 3; 98 108 105 hash(“ali”) = (105 * 1 + 108*37 + 98*372) % 10,007 = 8172 0 1 2 i key[i] hash function ali …… …… 0 1 2 8172 10,006 (TableSize) “ali” Meenakshi(M.Tech)
  • 10. Collision Resolution • If, when an element is inserted, it hashes to the same value as an already inserted element, then we have a collision and need to resolve it. • There are several methods for dealing with this: – Separate chaining – Open addressing • Linear Probing • Quadratic Probing • Double Hashing Meenakshi(M.Tech)
  • 12. Collision Resolution The most popular techniques for collision resolution are: Separate Chaining Open addressing Separate Chaining: The hash table is implemented as an array of linked list. The idea is to keep a list of all elements that hash to the same value. The array elements are pointers to the first nodes of the lists. A new item is inserted to the front of the list. Meenakshi(M.Tech)
  • 13. Collision Resolution Advantages of Separate Chaining: • Better space utilization for large items. • Simple collision handling: searching linked list. • Overflow: we can store more items than the hash table size. • Deletion is quick and easy: deletion from the linked list. • It Is a simple and efficient method • Table size need not be a prime number Meenakshi(M.Tech)
  • 14. Operations • Initialization: all entries are set to NULL • Find: – locate the cell using hash function. – sequential search on the linked list in that cell. • Insertion: – Locate the cell using hash function. – (If the item does not exist) insert it as the first item in the list. • Deletion: – Locate the cell using hash function. – Delete the item from the linked list. Meenakshi(M.Tech)
  • 15. Example Let us take some keys 23, 13, 21, 14, 7, 8, 15 in the same order, in a hash table of size 7 using separate chaining with the hash function. h(23)=23%7=2 h(13)=13%7=6 h(21)=21%7=0 h(14)=14%7=0(collision) h(7) =7%7 =0(collision) h(8) =8%7 =1 h(15)=15%7=1(collision) Meenakshi(M.Tech)
  • 16. Open Addressing In an open addressing hashing system, all the data go inside the table. Thus, a bigger table is needed. Generally the load factor should be below 0.5. If a collision occurs, alternative cells are tried until an empty cell is found. There are three common collision resolution strategies: Linear Probing Quadratic probing Double hashing Meenakshi(M.Tech)
  • 17. Linear probing is s technique for resolving hash collisions of values of hash function. In which an interval between probes will be fixed to 1 h(x)= x mod 10 Quadratic probing: In which an interval between probes is increased by adding the successive outputs of a quadratic polynomial to the starting value given by the hash function. Quadratic Probing eliminates primary clustering problem of linear probing. h(key)=(h(key)+1)mod 10 Collision Resolution Meenakshi(M.Tech)
  • 18. Real world example Library Meenakshi(M.Tech)