SlideShare a Scribd company logo
1 of 12
• Concept Of hashing
• Need of Hashing
• Hash Collision
• Dealing with Hash Collision
• Resolving Hash Collisions by Open
  Addressing
• Primary clustering
• Double Rehash
Concept Of hashing
• Hashing: hashing is a technique for performing almost
  constant time in case of insertion deletion and find
  operation.
• taking a very simple example, as array with its index
  as key is the example of table.
• So each index (key) can be used for accessing values
  in the constant search time.
• Mapping key must be simple to compute and must help
  in identifying the associated records.
• Function that help us in generating such type of keys
  is termed as Hash Function.
Hashing
• let h(key) is hashing function that returns the hash
  code. h(key) = key%1000, which can produce any value
  between 0 and 999. as shown in figure:
Need of Hashing
• Hashing maps large data sets of variable length to
  smaller data sets of a fixed length. For example, an
  inventory file of a company having more than 100
  items and the key to each record is a seven digit part
  number. To use direct indexing using entire seven
  digit key, an array of 10 million elements would be
  required. Which clearly is wastage of space, since
  company is unlikely to stock more than few thousand
  parts.
• Hence hashing provides an alternative to convert
  seven digit key into an integer within limited range.
  The values returned by a hash function are called
  hash values, hash codes.
• Suppose two keys k1 and k2 hashes
  such that h(k1) = h(k2). Here two
  keys hashes into the same value and
  are supposed to occupy same slot in
  hash table ,which is unacceptable.
• Such a situation is termed as hash
  collision.
Dealing with Hash Collision
• Two methods to deal with hash collision are:
• Rehashing and Chaining
  Rehashing: invokes a secondary hash function
  (say Rh(key)), which is applied successively until
  an empty slot is found, where a record can be
  placed.

  Chaining: builds a Linked list of items whose key
  hashes to same value. During search this short
  linked list is traversed sequentially for the desired
  key. This technique requires extra link field to
  each table position.
hashing
Analysis:
• The worst case running time for insertion is
  O(1).
• Deletion of an element x can be accomplished
  in O(1) time if the lists are doubly linked.
• In the worst case behaviour of chain-hashing,
  all n keys hash to the same slot, creating a
  list of length n. The worst-case time for
  search is thus θ(n) plus the time to compute
  the hash function.
A good hash function is one that minimizes
  collision and spreads the records uniformly
  throughout the table. that is why it is
  desirable to have larger array size than
  actual number of records.
      More formally, suppose we want to store
  a set of size n in a table of size m. The
  ratio α = n/m is called a load factor, that
  is, the average number of elements stored
  in a Table.
Resolving Hash Collisions by
     Open Addressing
• Simplest method of resolving the hash
  collision is to place record into the next
  available position in the array.
• e.g. if key = 7803497.
• Then using hash function h(key) = key%
  1000 will produce 497. However if the
  497th position is already occupied by key =
  2885497, then next available position is
  chosen.
• The above technique is termed as Linear
  probing.
• the approach however a some pitfall called
  primary clustering problem.
• Primary clustering: the phenomenon
  where two keys that hashes into
  different values compete with each
  other in successive rehashes. Primary
  clustering is the result of the
  formation of blocks of occupied
  positions.
Eliminating primary
               clustering
Solution 1: allow the rehash function to depend on
the number of times the particular function is
applied for hash value.
Rh(I,j) yields I the hash value if the key is being
rehashed for jth time.
ist rehash yeilds rh1 = rh(h(key),1)
2nd rehash yeilds rh2 =rh(rh1 +2)%tablesize and so
on.
Solution 2: rather than always moving one spot,
move i2 spots from the point of collision, where i is
the number of attempts to resolve the collision. Ie.
The rehash of h(key) will be (h(key)+ sqr(i))%table
size. The method is called as Quadratic Rehash.
Double Rehash
• Both the solutions for eliminating the primary
  clustering suffers from another pitfall called
  secondary clustering. A phenomenon in which two
  keys hashes into same hash value then follows the
  same rehash path.
• One way of eliminating all types of clustering is to
  use double hash technique, which uses two hash
  functions: h1(key) and h2(key). The h1(key)
  determines the location for insertion, if occupied,
  then rehash function rh(i+h2(key))%tablesize is
  successively used untill an empty location is found.

More Related Content

What's hot (20)

Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Extensible hashing
Extensible hashingExtensible hashing
Extensible hashing
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Hashing and Hashtable, application of hashing, advantages of hashing, disadva...
Hashing and Hashtable, application of hashing, advantages of hashing, disadva...Hashing and Hashtable, application of hashing, advantages of hashing, disadva...
Hashing and Hashtable, application of hashing, advantages of hashing, disadva...
 
Hashing
HashingHashing
Hashing
 
linear probing
linear probinglinear probing
linear probing
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Heap sort
Heap sortHeap sort
Heap sort
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Hashing
HashingHashing
Hashing
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
 
Dijkstra
DijkstraDijkstra
Dijkstra
 

Similar to Concept of hashing

Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptxkratika64
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 
Lecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxLecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxSLekshmiNair
 
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 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
 
Hash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxHash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxmy6305874
 
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
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15sumitbardhan
 
Hash presentation
Hash presentationHash presentation
Hash presentationomercode
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructurerajshreemuthiah
 

Similar to Concept of hashing (20)

Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Lecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxLecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptx
 
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
HashingHashing
Hashing
 
LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
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
 
Hash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxHash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.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
 
Hashing
HashingHashing
Hashing
 
HASHING.ppt.pptx
HASHING.ppt.pptxHASHING.ppt.pptx
HASHING.ppt.pptx
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15
 
Hash presentation
Hash presentationHash presentation
Hash presentation
 
Unit4 Part3.pptx
Unit4 Part3.pptxUnit4 Part3.pptx
Unit4 Part3.pptx
 
Hashing
HashingHashing
Hashing
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Hash pre
Hash preHash pre
Hash pre
 

Recently uploaded

ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
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
 
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
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
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
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
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
 
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.
 
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
 

Recently uploaded (20)

YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
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
 
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
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
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
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
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
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
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...
 
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
 

Concept of hashing

  • 1. • Concept Of hashing • Need of Hashing • Hash Collision • Dealing with Hash Collision • Resolving Hash Collisions by Open Addressing • Primary clustering • Double Rehash
  • 2. Concept Of hashing • Hashing: hashing is a technique for performing almost constant time in case of insertion deletion and find operation. • taking a very simple example, as array with its index as key is the example of table. • So each index (key) can be used for accessing values in the constant search time. • Mapping key must be simple to compute and must help in identifying the associated records. • Function that help us in generating such type of keys is termed as Hash Function.
  • 3. Hashing • let h(key) is hashing function that returns the hash code. h(key) = key%1000, which can produce any value between 0 and 999. as shown in figure:
  • 4. Need of Hashing • Hashing maps large data sets of variable length to smaller data sets of a fixed length. For example, an inventory file of a company having more than 100 items and the key to each record is a seven digit part number. To use direct indexing using entire seven digit key, an array of 10 million elements would be required. Which clearly is wastage of space, since company is unlikely to stock more than few thousand parts. • Hence hashing provides an alternative to convert seven digit key into an integer within limited range. The values returned by a hash function are called hash values, hash codes.
  • 5. • Suppose two keys k1 and k2 hashes such that h(k1) = h(k2). Here two keys hashes into the same value and are supposed to occupy same slot in hash table ,which is unacceptable. • Such a situation is termed as hash collision.
  • 6. Dealing with Hash Collision • Two methods to deal with hash collision are: • Rehashing and Chaining Rehashing: invokes a secondary hash function (say Rh(key)), which is applied successively until an empty slot is found, where a record can be placed. Chaining: builds a Linked list of items whose key hashes to same value. During search this short linked list is traversed sequentially for the desired key. This technique requires extra link field to each table position.
  • 7. hashing Analysis: • The worst case running time for insertion is O(1). • Deletion of an element x can be accomplished in O(1) time if the lists are doubly linked. • In the worst case behaviour of chain-hashing, all n keys hash to the same slot, creating a list of length n. The worst-case time for search is thus θ(n) plus the time to compute the hash function.
  • 8. A good hash function is one that minimizes collision and spreads the records uniformly throughout the table. that is why it is desirable to have larger array size than actual number of records. More formally, suppose we want to store a set of size n in a table of size m. The ratio α = n/m is called a load factor, that is, the average number of elements stored in a Table.
  • 9. Resolving Hash Collisions by Open Addressing • Simplest method of resolving the hash collision is to place record into the next available position in the array. • e.g. if key = 7803497. • Then using hash function h(key) = key% 1000 will produce 497. However if the 497th position is already occupied by key = 2885497, then next available position is chosen. • The above technique is termed as Linear probing. • the approach however a some pitfall called primary clustering problem.
  • 10. • Primary clustering: the phenomenon where two keys that hashes into different values compete with each other in successive rehashes. Primary clustering is the result of the formation of blocks of occupied positions.
  • 11. Eliminating primary clustering Solution 1: allow the rehash function to depend on the number of times the particular function is applied for hash value. Rh(I,j) yields I the hash value if the key is being rehashed for jth time. ist rehash yeilds rh1 = rh(h(key),1) 2nd rehash yeilds rh2 =rh(rh1 +2)%tablesize and so on. Solution 2: rather than always moving one spot, move i2 spots from the point of collision, where i is the number of attempts to resolve the collision. Ie. The rehash of h(key) will be (h(key)+ sqr(i))%table size. The method is called as Quadratic Rehash.
  • 12. Double Rehash • Both the solutions for eliminating the primary clustering suffers from another pitfall called secondary clustering. A phenomenon in which two keys hashes into same hash value then follows the same rehash path. • One way of eliminating all types of clustering is to use double hash technique, which uses two hash functions: h1(key) and h2(key). The h1(key) determines the location for insertion, if occupied, then rehash function rh(i+h2(key))%tablesize is successively used untill an empty location is found.

Editor's Notes

  1. Rafi dar