SlideShare a Scribd company logo
HASH TABLE
VU QUANG TRAN
EXAMPLE
• Design a system to store employees' information using their phone number as
key
• Operations: Insert, Search, Delete
• Some possible data structures:
• Array
• Linked List
• Balanced Binary Search Tree
• Direct Access Table
EXAMPLE
• Design a system to store employees' information using their phone number as
key
• Operations: Insert, Search, Delete
• Some possible data structures:
• Array: O(n) Search, Delete
• Linked List: O(n) Search
• Balanced Binary Search Tree: O(log n) All
• Direct Access Table: Space Wastage
=> Hash Table: O(1) All
BASICS
• Data Structure that implements Associative
Array
• Map key to corresponding value
• Use Hash function to compute index of key-
value pairs into an array of buckets
• O(1) complexity on average and O(n) in worst
case
HASHING
• Distribute the entries (key-value pairs) across an array of buckets
• Hash function: Map data of arbitrary size to data of fixed size
• Two steps:
1. hash = hash_func(key)
2. index = hash % table_size
CHOOSING A HASH FUNCTION
• Easy to compute
• Uniform Distribution
TYPES OF HASH FUNCTION
• Two types:
• Cryptographic hash
• Non-cryptographic hash
• Non-cryptographic hash provides weaker
guarantees than cryptographic hash in
exchange for performance improvements
• Example:
• Crypto: BLAKE2b, SHA-512, MD5, …
• Non-crypto: MurmurHash, xxHash, ...
• Cryptographic hash aims to provide
certain security guarantees
• Main properties of cryptographic hash:
• Deterministic
• Quick
• One-way function
• Avalanche effect
• Collision resistant
• Pre-image attack resistant
COLLISION RESOLUTION
• Two or more keys result in a same hash value
• Practically unavoidable
• Handling techniques:
• Separate chaining
• Open addressing
COLLISION RESOLUTION
SEPARATE CHAINING
• Make each cell of hash table point to a linked list of records that have same hash
function value
COLLISION RESOLUTION
SEPARATE CHAINING
• Make each cell of hash table point to a linked list of records that have same hash
function value
• Advantages:
• Simple to implement
• Hash table never fills up
• Disadvantages:
• Cache performance
• Space wastage
• Search time can become O(n) if chain
gets long
COLLISION RESOLUTION
OPEN ADDRESSING
• All elements are stored in the hash table itself
• Operations:
• Insert: Keep probing until an empty slot is found
• Search: Keep probing until key is found or an empty slot is reached
• Delete: If we simply delete a key, then search may fail. So slots of deleted keys are
marked specially as DELETED
COLLISION RESOLUTION
OPEN ADDRESSING
Types:
• Linear probing: Linearly probe for next slot
index = [hash(x) + i] % S
COLLISION RESOLUTION
OPEN ADDRESSING
Types:
• Linear probing: Linearly probe for next slot => Clustering
index = [hash(x) + i] % S
COLLISION RESOLUTION
OPEN ADDRESSING
Types:
• Linear probing: Linearly probe for next slot => Clustering
index = [hash(x) + i] % S
• Quadratic probing: Look for i^2 slot in ith iteration
index = [hash(x) + i^2] % S
COLLISION RESOLUTION
OPEN ADDRESSING
Types:
• Linear probing: Linearly probe for next slot => Clustering
index = [hash(x) + i] % S
• Quadratic probing: Look for i^2 slot in ith iteration
index = [hash(x) + i^2] % S
• Double hashing: Use another hash function hash2(x) and look for i*hash2(x) in ith
iteration
index = [hash(x) + i*hash2(x)] % S
COLLISION RESOLUTION
OPEN ADDRESSING
Comparison:
• Linear probing:
• Easy to compute
• Best cache performance
• Suffers from Clustering
• Quadratic probing:
• Lies between cache performance and clustering
• Double hashing:
• Poor cache performance
• No clustering
• More computation time
COLLISION RESOLUTION
OPEN ADDRESSING
• Advantages:
• Better cache performance
• Better space usage
• Disadvantages:
• Harder to implement
• Hash table may become full
• Clustering
DYNAMIC RESIZING
• Load factor = number of entries / number of buckets
• When load factor is too low or too high => Dynamic resizing
• Approaches:
• Complete resizing
• Incremental resizing
USAGE
• Associative Array
• Database Indexing
• Cache
• Set
• …
REFERENCE
• Wikipedia
• GeeksForGeeks
THANK YOU

More Related Content

What's hot

Hashing
HashingHashing
Hashing
Ghaffar Khan
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Red black tree
Red black treeRed black tree
Red black tree
Rajendran
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
Saurabh Kumar
 
Heap sort
Heap sortHeap sort
Heap sort
Mohd Arif
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
Home
 
Hashing
HashingHashing
Searching
SearchingSearching
Searching
Ashim Lamichhane
 
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
 
Hashing
HashingHashing
Hashing
LavanyaJ28
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
Meghaj Mallick
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Hash tables
Hash tablesHash tables
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 

What's hot (20)

Hashing
HashingHashing
Hashing
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Red black tree
Red black treeRed black tree
Red black tree
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Heap sort
Heap sortHeap sort
Heap sort
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Hashing
HashingHashing
Hashing
 
Searching
SearchingSearching
Searching
 
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
 
Hashing
HashingHashing
Hashing
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Linked List
Linked ListLinked List
Linked List
 
Hash tables
Hash tablesHash tables
Hash tables
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 

Similar to Hash table

Hashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT iHashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT i
cajiwol341
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing Tables
Chinmaya M. N
 
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
Tribhuvan University
 
LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
MuhammadUmerIhtisham
 
hashing in data strutures advanced in languae java
hashing in data strutures advanced in languae javahashing in data strutures advanced in languae java
hashing in data strutures advanced in languae java
ishasharma835109
 
Hashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesHashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniques
ssuserec8a711
 
Hashing
HashingHashing
Hash presentation
Hash presentationHash presentation
Hash presentation
omercode
 
Hash Functions FTW
Hash Functions FTWHash Functions FTW
Hash Functions FTW
sunnygleason
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
 
Hashing
HashingHashing
Hashing
grahamwell
 
Hashing
HashingHashing
Hashing
grahamwell
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
AgonySingh
 
Hash based inventory system
Hash based inventory systemHash based inventory system
Hash based inventory system
DADITIRUMALATARUN
 
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
lokaprasaadvs
 
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptxunit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
BabaShaikh3
 
Hash function
Hash functionHash function
Hash function
MDPiasKhan
 
session 15 hashing.pptx
session 15   hashing.pptxsession 15   hashing.pptx
session 15 hashing.pptx
rajneeshsingh46738
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
Jawad Khan
 

Similar to Hash table (20)

Hashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT iHashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT i
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing Tables
 
Hashing
HashingHashing
Hashing
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and 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 in data strutures advanced in languae java
hashing in data strutures advanced in languae javahashing in data strutures advanced in languae java
hashing in data strutures advanced in languae java
 
Hashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesHashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniques
 
Hashing
HashingHashing
Hashing
 
Hash presentation
Hash presentationHash presentation
Hash presentation
 
Hash Functions FTW
Hash Functions FTWHash Functions FTW
Hash Functions FTW
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Hash based inventory system
Hash based inventory systemHash based inventory system
Hash based inventory system
 
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
 
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptxunit-1-dsa-hashing-2022_compressed-1-converted.pptx
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
 
Hash function
Hash functionHash function
Hash function
 
session 15 hashing.pptx
session 15   hashing.pptxsession 15   hashing.pptx
session 15 hashing.pptx
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
 

Recently uploaded

4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
Social Samosa
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
Timothy Spann
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
Bill641377
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
nuttdpt
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
g4dpvqap0
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
Walaa Eldin Moustafa
 
State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
kuntobimo2016
 
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
74nqk8xf
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
apvysm8
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Aggregage
 
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdfUdemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Fernanda Palhano
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
javier ramirez
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
AlessioFois2
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
v7oacc3l
 

Recently uploaded (20)

4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
 
State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
 
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
 
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdfUdemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
 

Hash table

  • 2. EXAMPLE • Design a system to store employees' information using their phone number as key • Operations: Insert, Search, Delete • Some possible data structures: • Array • Linked List • Balanced Binary Search Tree • Direct Access Table
  • 3. EXAMPLE • Design a system to store employees' information using their phone number as key • Operations: Insert, Search, Delete • Some possible data structures: • Array: O(n) Search, Delete • Linked List: O(n) Search • Balanced Binary Search Tree: O(log n) All • Direct Access Table: Space Wastage => Hash Table: O(1) All
  • 4. BASICS • Data Structure that implements Associative Array • Map key to corresponding value • Use Hash function to compute index of key- value pairs into an array of buckets • O(1) complexity on average and O(n) in worst case
  • 5. HASHING • Distribute the entries (key-value pairs) across an array of buckets • Hash function: Map data of arbitrary size to data of fixed size • Two steps: 1. hash = hash_func(key) 2. index = hash % table_size
  • 6. CHOOSING A HASH FUNCTION • Easy to compute • Uniform Distribution
  • 7. TYPES OF HASH FUNCTION • Two types: • Cryptographic hash • Non-cryptographic hash • Non-cryptographic hash provides weaker guarantees than cryptographic hash in exchange for performance improvements • Example: • Crypto: BLAKE2b, SHA-512, MD5, … • Non-crypto: MurmurHash, xxHash, ... • Cryptographic hash aims to provide certain security guarantees • Main properties of cryptographic hash: • Deterministic • Quick • One-way function • Avalanche effect • Collision resistant • Pre-image attack resistant
  • 8. COLLISION RESOLUTION • Two or more keys result in a same hash value • Practically unavoidable • Handling techniques: • Separate chaining • Open addressing
  • 9. COLLISION RESOLUTION SEPARATE CHAINING • Make each cell of hash table point to a linked list of records that have same hash function value
  • 10. COLLISION RESOLUTION SEPARATE CHAINING • Make each cell of hash table point to a linked list of records that have same hash function value • Advantages: • Simple to implement • Hash table never fills up • Disadvantages: • Cache performance • Space wastage • Search time can become O(n) if chain gets long
  • 11. COLLISION RESOLUTION OPEN ADDRESSING • All elements are stored in the hash table itself • Operations: • Insert: Keep probing until an empty slot is found • Search: Keep probing until key is found or an empty slot is reached • Delete: If we simply delete a key, then search may fail. So slots of deleted keys are marked specially as DELETED
  • 12. COLLISION RESOLUTION OPEN ADDRESSING Types: • Linear probing: Linearly probe for next slot index = [hash(x) + i] % S
  • 13. COLLISION RESOLUTION OPEN ADDRESSING Types: • Linear probing: Linearly probe for next slot => Clustering index = [hash(x) + i] % S
  • 14. COLLISION RESOLUTION OPEN ADDRESSING Types: • Linear probing: Linearly probe for next slot => Clustering index = [hash(x) + i] % S • Quadratic probing: Look for i^2 slot in ith iteration index = [hash(x) + i^2] % S
  • 15. COLLISION RESOLUTION OPEN ADDRESSING Types: • Linear probing: Linearly probe for next slot => Clustering index = [hash(x) + i] % S • Quadratic probing: Look for i^2 slot in ith iteration index = [hash(x) + i^2] % S • Double hashing: Use another hash function hash2(x) and look for i*hash2(x) in ith iteration index = [hash(x) + i*hash2(x)] % S
  • 16. COLLISION RESOLUTION OPEN ADDRESSING Comparison: • Linear probing: • Easy to compute • Best cache performance • Suffers from Clustering • Quadratic probing: • Lies between cache performance and clustering • Double hashing: • Poor cache performance • No clustering • More computation time
  • 17. COLLISION RESOLUTION OPEN ADDRESSING • Advantages: • Better cache performance • Better space usage • Disadvantages: • Harder to implement • Hash table may become full • Clustering
  • 18. DYNAMIC RESIZING • Load factor = number of entries / number of buckets • When load factor is too low or too high => Dynamic resizing • Approaches: • Complete resizing • Incremental resizing
  • 19. USAGE • Associative Array • Database Indexing • Cache • Set • …

Editor's Notes

  1. Associative Array is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection.
  2. Collision occurs when a newly inserted key maps to an already occupied slot in hash table
  3. Example: Hash function = key % 7
  4. Space wastage: some buckets may never be used, extra space to store links
  5. At any point, size of table must be greater than or equal to total number of keys Insert can insert an item in a deleted slot, but search doesn’t stop at a deleted slot.
  6. Example: Hash function = key % 7
  7. Clustering: Many consecutive elements form groups and it starts taking time to find a free slot or to search an element
  8. Better space usage: bucket co the luu key cua bucket khac neu co collision, no extra space for links
  9. During the resize, allocate the new hash table, but keep the old table unchanged. In each lookup or delete operation, check both tables. Perform insertion operations only in the new table. At each insertion also move r elements from the old table to the new table. When all elements are removed from the old table, deallocate it.