SlideShare una empresa de Scribd logo
1 de 12
Descargar para leer sin conexión
Hashing 
Dictionary 
Direct-access Table 
Chaining 
Hash Function
Dictionary 
● Abstract Data type, maintain set of items with keys. 
– Insert (item) – also replacing existed item 
– Delete (item) 
– Search (key): return the item with given key or report if 
doesn't exist (null). 
Item → (key, value)
Simple Approach: DAT 
Storing items directly into 
giant array which is the 
index of item as key. 
0 NULL 
1 ITEM 1 
2 NULL 
3 NULL 
4 ITEM 2 
... 
... 
m-1 ...
Direct-access 
Binary search 
items[11] 
0 null 
1 null 
... ... 
7 7 
8 null 
9 9 
10 null 
11 11 
Direct access 
i→ 1 2 4 5 7 9 11 
Search for 11, low: 0, upper: 5, mid = 5/2 = 2 
| check for 11 
i→ 2 2 4 5 7 9 11 
Low = 3, upper = 5, mid = 4 
|check for 11 
i→ 3 2 4 5 7 9 11 
Low = 5, upper = 5, mid = 5 
Found 11 | 
i→ 4 2 4 5 7 9 11
Two Big Problem 
(1)Keys may be negative integers 
● Maps keys into non negative integers (such as String or 
string of bits) 
(2)Gigantic memory 
● Hashing (cut into piece)
Big idea 
Reduce universe () of all keys (integers) 
down to reasonable size of m for table. 0 NULL 
1 ITEM 1 
2 NULL 
3 NULL 
4 ITEM 2 
... 
... 
m-1 ... 
k1 
k2 
k3 
k4 
keyspace 
h(k2) = 1 
h(k3) = 4 
h(k) 
Ideal: m = (n)
Problem 
● There probably collision 
– i.e: 
h(kx) = h(kj) but kx ≠ kj 
0 NULL 
1 ITEM 1 
2 NULL 
3 NULL 
4 ITEM 2 
... 
... 
m-1 ... 
k1 
k2 
k3 
k4 
keyspace 
h(k1) = 4 
h(k4) = 4 
h(k)
Chaining 
● Use linked list to store value with collide keys. 
0 NULL / 
1 ITEM 1 / 
2 NULL / 
3 NULL / 
4 ITEM 2 ITEM 3 / 
... 
... / 
m-1 ... / 
k1 
k2 
k3 
k4 
keyspace 
h(k1) = 4 
h(k4) = 4 
h(k) Worst cases: 
- a bunch of keys is 
mapped into the same 
index
Length of chain 
Expected length of chain for n keys with m slots: 
n 
m 
=⍺ 
⍺ = load factor of table 
⍺ = O(1) if m = O(n)
Hash functions 
(1) Division Method: 
h(k )=k modm 
(2) Multiplication method 
h(k )=[(a . k )mod 2w ]≫(w−r ) 
a = integer 
w = bit of machine 
r = bits to shift 
m=2r
Cont... 
(3) Universal hashing 
h(k )=[(a . k+b)mod p ]modm 
Random integer 
∈ {0,1,..., p-1} 
Prime > || 
Worst cases: k1 ≠ k2 
Pr 
[h (k 1)=h (k 2)] 
= 
1 
m
Sample of code

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Hashing
HashingHashing
Hashing
 
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)
 
18 hashing
18 hashing18 hashing
18 hashing
 
Hash table
Hash tableHash table
Hash table
 
Open addressiing &rehashing,extendiblevhashing
Open addressiing &rehashing,extendiblevhashingOpen addressiing &rehashing,extendiblevhashing
Open addressiing &rehashing,extendiblevhashing
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmay
 
Hashing
HashingHashing
Hashing
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Hashing data
Hashing dataHashing data
Hashing data
 
Hashing
HashingHashing
Hashing
 
Hash tables
Hash tablesHash tables
Hash tables
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Hash tables
Hash tablesHash tables
Hash tables
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Hashing
HashingHashing
Hashing
 

Similar a Hashing Algorithm

Similar a Hashing Algorithm (20)

Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
Lec5
Lec5Lec5
Lec5
 
Data structure lecture 4
Data structure lecture 4Data structure lecture 4
Data structure lecture 4
 
Lec5
Lec5Lec5
Lec5
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
03.01 hash tables
03.01 hash tables03.01 hash tables
03.01 hash tables
 
searching.pdf
searching.pdfsearching.pdf
searching.pdf
 
Algo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.pptAlgo-Exercises-2-hash-AVL-Tree.ppt
Algo-Exercises-2-hash-AVL-Tree.ppt
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Lec4
Lec4Lec4
Lec4
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
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
 
Sorting
SortingSorting
Sorting
 
Lec34
Lec34Lec34
Lec34
 
L21_Hashing.pdf
L21_Hashing.pdfL21_Hashing.pdf
L21_Hashing.pdf
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Array 2
Array 2Array 2
Array 2
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
 

Último

Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Último (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Hashing Algorithm

  • 1. Hashing Dictionary Direct-access Table Chaining Hash Function
  • 2. Dictionary ● Abstract Data type, maintain set of items with keys. – Insert (item) – also replacing existed item – Delete (item) – Search (key): return the item with given key or report if doesn't exist (null). Item → (key, value)
  • 3. Simple Approach: DAT Storing items directly into giant array which is the index of item as key. 0 NULL 1 ITEM 1 2 NULL 3 NULL 4 ITEM 2 ... ... m-1 ...
  • 4. Direct-access Binary search items[11] 0 null 1 null ... ... 7 7 8 null 9 9 10 null 11 11 Direct access i→ 1 2 4 5 7 9 11 Search for 11, low: 0, upper: 5, mid = 5/2 = 2 | check for 11 i→ 2 2 4 5 7 9 11 Low = 3, upper = 5, mid = 4 |check for 11 i→ 3 2 4 5 7 9 11 Low = 5, upper = 5, mid = 5 Found 11 | i→ 4 2 4 5 7 9 11
  • 5. Two Big Problem (1)Keys may be negative integers ● Maps keys into non negative integers (such as String or string of bits) (2)Gigantic memory ● Hashing (cut into piece)
  • 6. Big idea Reduce universe () of all keys (integers) down to reasonable size of m for table. 0 NULL 1 ITEM 1 2 NULL 3 NULL 4 ITEM 2 ... ... m-1 ... k1 k2 k3 k4 keyspace h(k2) = 1 h(k3) = 4 h(k) Ideal: m = (n)
  • 7. Problem ● There probably collision – i.e: h(kx) = h(kj) but kx ≠ kj 0 NULL 1 ITEM 1 2 NULL 3 NULL 4 ITEM 2 ... ... m-1 ... k1 k2 k3 k4 keyspace h(k1) = 4 h(k4) = 4 h(k)
  • 8. Chaining ● Use linked list to store value with collide keys. 0 NULL / 1 ITEM 1 / 2 NULL / 3 NULL / 4 ITEM 2 ITEM 3 / ... ... / m-1 ... / k1 k2 k3 k4 keyspace h(k1) = 4 h(k4) = 4 h(k) Worst cases: - a bunch of keys is mapped into the same index
  • 9. Length of chain Expected length of chain for n keys with m slots: n m =⍺ ⍺ = load factor of table ⍺ = O(1) if m = O(n)
  • 10. Hash functions (1) Division Method: h(k )=k modm (2) Multiplication method h(k )=[(a . k )mod 2w ]≫(w−r ) a = integer w = bit of machine r = bits to shift m=2r
  • 11. Cont... (3) Universal hashing h(k )=[(a . k+b)mod p ]modm Random integer ∈ {0,1,..., p-1} Prime > || Worst cases: k1 ≠ k2 Pr [h (k 1)=h (k 2)] = 1 m