SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Tendances (20)

08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Backtracking Algorithm.ppt
Backtracking Algorithm.pptBacktracking Algorithm.ppt
Backtracking Algorithm.ppt
 
Red black trees
Red black treesRed black trees
Red black trees
 
Hashing
HashingHashing
Hashing
 
Disjoint sets union, find
Disjoint sets  union, findDisjoint sets  union, find
Disjoint sets union, find
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Heap sort
Heap sortHeap sort
Heap sort
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Hashing
HashingHashing
Hashing
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Radix sorting
Radix sortingRadix sorting
Radix sorting
 
Hashing
HashingHashing
Hashing
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 

Similaire à 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
 

Similaire à 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
HashingHashing
Hashing
 
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
 

Dernier

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 

Dernier (20)

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
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.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

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)