SlideShare a Scribd company logo
1 of 30
Advanced Algorithm Analysis
MSCS1
Hash Table
• Hash Table is a data structure which stores data in an associative
manner. In a hash table, data is stored in an array format, where each
data value has its own unique index value. Access of data becomes
very fast if we know the index of the desired data.
• Thus, it becomes a data structure in which insertion and search
operations are very fast irrespective of the size of the data. Hash
Table uses an array as a storage medium and uses hash technique to
generate an index where an element is to be inserted or is to be
located from.
Linear vs binary search
• In linear search we have a list in random order
• But is binary search the array is in sorted form. But we need to sort it
first.
• This consume extra time.
• But somehow its better time complexity which is O(logn).
• Now we want a time complexity O(1).
• O(n) O(logn)  O(1)
• So this is the requirement which introduces hashing method.
Direct-address tables
•
Hashing
• We store the element at the same place in the table where the same
index lies.
Hashing
• It could waste a large memory if we insert 50 in a sequence.
Hashing
• The key element which
are going to process are
in hash space.
• Where we store these
element are in Hash
Table.
Hashing
• The key element which
are going to process are
in hash space.
• Where we store these
element are in Hash
Table.
Hashing
• The key element which
are going to process are
in hash space.
• Where we store these
element are in Hash
Table.
Hashing
• If we want to
insert 50 then
the problem is
more space.
Hashing
• If we want to
insert 50 then
the problem is
more space.
• If we see the
property of
functions we
have two types
of functions.
Hashing
• If we see the
property of
functions we
have two types
of functions.
• One to one
waste the
space.
Hashing
• Size of hash
table is 10
now.
Hashing
• So we are
taking a hash
table size as
10.
• Because our
hash function
defines.
Hashing
• We map the
elements
according to
the function.
Hashing
• We map the
elements
according to
the function.
• 8%10 = 8
• 3%10 = 3
• 13%10 = 3
• Here is a
collision.
Hashing
• There is one hitch: two keys may hash to the same slot. We call this
situation a collision. Fortunately, we have effective techniques for
resolving the conflict created by collisions. Of course, the ideal solution
would be to avoid collisions altogether. We might try to achieve this
goal by choosing a suitable hash function h. One idea is to
make h appear to be “random,” thus avoiding collisions or at least
minimizing their number. The very term “to hash,” evoking images of
random mixing and chopping, captures the spirit of this approach.
Resolution of collision
Resolution of collision
• Chaining
• We add a node/ linked
list to store the
element at the same
location in chains.
Resolution of collision
• Chaining
• We add a node/ linked
list to store the
element at the same
location in chains.
• If we search a 13?
Resolution of collision
• Chaining
• We add a node/ linked
list to store the element
at the same location in
chains.
• If we search a 13?
• Can be placed at 3rd
location. But if we
search for 13 then it
could take a more the
O(1) time but its better
then O(logn).
Resolution of collision
• Linear Probing
• Place 3 at location 3
and for 13 (13%10 = 3)
, we see the next
empty location and
put at location 4.
Resolution of collision
• Linear Probing
• Place 3 at
location 3 and
for 13 (13%10 =
3), here is a
collision. so we
see the next
empty location
and put at
location 4.
Resolution of collision
• Linear Probing
• Place 3 at
location 3 and
for 13 (13%10 =
3), here is a
collision. so we
see the next
empty location
and put at
location 4.
• Now if we want
to store 4 here is
a collision.
Resolution of collision
• Linear Probing
• If we search for
a 4, go to the
index 4 and
look in it. Not
found then go
to the next
element.
Resolution of collision
• Linear Probing
• Searching for 24.
• 24%10 = 4
• Go to location 4, its not
available, then move on
until the empty
location.
• Here we also get the
complexity more then
O(1).
• But its better then
O(logn).
Resolution of collision
• Linear Probing
• Problem in linear
probing is bunch of
elements at the
same place. So it
cause a problem in
searching.
• Here we also get
the complexity
more then O(1).
• But its better then
O(logn).
Resolution of collision
• Quadratic probing
• To avoid the
clustering of
elements we
introduced this
method.
Resolution of collision
• Quadratic probing
• Only difference is i
= i^2
• Which give us a
location far away.
• For 23 it could be 7.
• This will avoid the
clustering and
improve the
searching time.

More Related Content

Similar to Introduction to Hash Tables | What is a HashTable in Algorithm

Lecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxLecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxSLekshmiNair
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsMohamed Essam
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderAshin Guha Majumder
 
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
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer scienceRiazul Islam
 
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 and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfHashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfJaithoonBibi
 
Hashing_and_collision.pptx
Hashing_and_collision.pptxHashing_and_collision.pptx
Hashing_and_collision.pptxpunit444kaushik
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptxkratika64
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashingRafi Dar
 
2-Heuristic Search.ppt
2-Heuristic Search.ppt2-Heuristic Search.ppt
2-Heuristic Search.pptMIT,Imphal
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptxskilljiolms
 
Amit ppt
Amit pptAmit ppt
Amit pptamitp26
 

Similar to Introduction to Hash Tables | What is a HashTable in Algorithm (20)

Ch17 Hashing
Ch17 HashingCh17 Hashing
Ch17 Hashing
 
Lecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptxLecture14_15_Hashing.pptx
Lecture14_15_Hashing.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
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
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
Java Collections.pptx
Java Collections.pptxJava Collections.pptx
Java Collections.pptx
 
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 and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfHashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdf
 
Hashing_and_collision.pptx
Hashing_and_collision.pptxHashing_and_collision.pptx
Hashing_and_collision.pptx
 
session 15 hashing.pptx
session 15   hashing.pptxsession 15   hashing.pptx
session 15 hashing.pptx
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
2-Heuristic Search.ppt
2-Heuristic Search.ppt2-Heuristic Search.ppt
2-Heuristic Search.ppt
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptx
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Amit ppt
Amit pptAmit ppt
Amit ppt
 

More from Learning Courses Online

More from Learning Courses Online (6)

Convolutional neural networks
Convolutional neural  networksConvolutional neural  networks
Convolutional neural networks
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
 
Binary tree data structure
Binary tree data structureBinary tree data structure
Binary tree data structure
 
Fractional knapsack problem
Fractional knapsack problemFractional knapsack problem
Fractional knapsack problem
 
8 queens problem using ga
8 queens problem using ga8 queens problem using ga
8 queens problem using ga
 
Simple auto encoder
Simple auto encoderSimple auto encoder
Simple auto encoder
 

Recently uploaded

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
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
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
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 ...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Introduction to Hash Tables | What is a HashTable in Algorithm

  • 2. Hash Table • Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data. • Thus, it becomes a data structure in which insertion and search operations are very fast irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash technique to generate an index where an element is to be inserted or is to be located from.
  • 3.
  • 4. Linear vs binary search • In linear search we have a list in random order • But is binary search the array is in sorted form. But we need to sort it first. • This consume extra time. • But somehow its better time complexity which is O(logn). • Now we want a time complexity O(1). • O(n) O(logn)  O(1) • So this is the requirement which introduces hashing method.
  • 6. Hashing • We store the element at the same place in the table where the same index lies.
  • 7. Hashing • It could waste a large memory if we insert 50 in a sequence.
  • 8. Hashing • The key element which are going to process are in hash space. • Where we store these element are in Hash Table.
  • 9. Hashing • The key element which are going to process are in hash space. • Where we store these element are in Hash Table.
  • 10. Hashing • The key element which are going to process are in hash space. • Where we store these element are in Hash Table.
  • 11. Hashing • If we want to insert 50 then the problem is more space.
  • 12. Hashing • If we want to insert 50 then the problem is more space. • If we see the property of functions we have two types of functions.
  • 13. Hashing • If we see the property of functions we have two types of functions. • One to one waste the space.
  • 14. Hashing • Size of hash table is 10 now.
  • 15. Hashing • So we are taking a hash table size as 10. • Because our hash function defines.
  • 16. Hashing • We map the elements according to the function.
  • 17. Hashing • We map the elements according to the function. • 8%10 = 8 • 3%10 = 3 • 13%10 = 3 • Here is a collision.
  • 18. Hashing • There is one hitch: two keys may hash to the same slot. We call this situation a collision. Fortunately, we have effective techniques for resolving the conflict created by collisions. Of course, the ideal solution would be to avoid collisions altogether. We might try to achieve this goal by choosing a suitable hash function h. One idea is to make h appear to be “random,” thus avoiding collisions or at least minimizing their number. The very term “to hash,” evoking images of random mixing and chopping, captures the spirit of this approach.
  • 20. Resolution of collision • Chaining • We add a node/ linked list to store the element at the same location in chains.
  • 21. Resolution of collision • Chaining • We add a node/ linked list to store the element at the same location in chains. • If we search a 13?
  • 22. Resolution of collision • Chaining • We add a node/ linked list to store the element at the same location in chains. • If we search a 13? • Can be placed at 3rd location. But if we search for 13 then it could take a more the O(1) time but its better then O(logn).
  • 23. Resolution of collision • Linear Probing • Place 3 at location 3 and for 13 (13%10 = 3) , we see the next empty location and put at location 4.
  • 24. Resolution of collision • Linear Probing • Place 3 at location 3 and for 13 (13%10 = 3), here is a collision. so we see the next empty location and put at location 4.
  • 25. Resolution of collision • Linear Probing • Place 3 at location 3 and for 13 (13%10 = 3), here is a collision. so we see the next empty location and put at location 4. • Now if we want to store 4 here is a collision.
  • 26. Resolution of collision • Linear Probing • If we search for a 4, go to the index 4 and look in it. Not found then go to the next element.
  • 27. Resolution of collision • Linear Probing • Searching for 24. • 24%10 = 4 • Go to location 4, its not available, then move on until the empty location. • Here we also get the complexity more then O(1). • But its better then O(logn).
  • 28. Resolution of collision • Linear Probing • Problem in linear probing is bunch of elements at the same place. So it cause a problem in searching. • Here we also get the complexity more then O(1). • But its better then O(logn).
  • 29. Resolution of collision • Quadratic probing • To avoid the clustering of elements we introduced this method.
  • 30. Resolution of collision • Quadratic probing • Only difference is i = i^2 • Which give us a location far away. • For 23 it could be 7. • This will avoid the clustering and improve the searching time.