SlideShare una empresa de Scribd logo
1 de 17
Descargar para leer sin conexión
Comp 122, Spring 2004
Hash Tables – 1
Comp 122
Dictionary
• Dictionary:
– Dynamic-set data structure for storing items indexed
using keys.
– Supports operations Insert, Search, and Delete.
– Applications:
• Symbol table of a compiler.
• Memory-management tables in operating systems.
• Large-scale distributed systems.
• Hash Tables:
– Effective way of implementing dictionaries.
– Generalization of ordinary arrays.
Comp 122
Direct-address Tables
• Direct-address Tables are ordinary arrays.
• Facilitate direct addressing.
– Element whose key is k is obtained by indexing into
the kth
position of the array.
• Applicable when we can afford to allocate an
array with one position for every possible key.
– i.e. when the universe of keys U is small.
• Dictionary operations can be implemented to take
O(1) time.
– Details in Sec. 11.1.
Comp 122
Hash Tables
• Notation:
– U – Universe of all possible keys.
– K – Set of keys actually stored in the dictionary.
– |K| = n.
• When U is very large,
– Arrays are not practical.
– |K| << |U|.
• Use a table of size proportional to |K| – The hash tables.
– However, we lose the direct-addressing ability.
– Define functions that map keys to slots of the hash table.
Comp 122
Hashing
• Hash function h: Mapping from U to the slots of a
hash table T[0..m–1].
h : U → {0,1,…, m–1}
• With arrays, key k maps to slot A[k].
• With hash tables, key k maps or “hashes” to slot
T[h[k]].
• h[k] is the hash value of key k.
Comp 122
Hashing
0
m–1
h(k1)
h(k4)
h(k2)=h(k5)
h(k3)
U
(universe of keys)
K
(actual
keys)
k1
k2
k3
k5
k4
collision
Comp 122
Issues with Hashing
• Multiple keys can hash to the same slot –
collisions are possible.
– Design hash functions such that collisions are
minimized.
– But avoiding collisions is impossible.
• Design collision-resolution techniques.
• Search will cost Ө(n) time in the worst case.
– However, all operations can be made to have an
expected complexity of Ө(1).
Comp 122
Methods of Resolution
• Chaining:
– Store all elements that hash to the
same slot in a linked list.
– Store a pointer to the head of the
linked list in the hash table slot.
• Open Addressing:
– All elements stored in hash table itself.
– When collisions occur, use a systematic
(consistent) procedure to store
elements in free slots of the table.
k2
0
m–1
k1 k4
k5 k6
k7 k3
k8
Comp 122
Collision Resolution by Chaining
0
m–1
h(k1)=h(k4)
h(k2)=h(k5)=h(k6)
h(k3)=h(k7)
U
(universe of keys)
K
(actual
keys)
k1
k2
k3
k5
k4
k6
k7k8
h(k8)
X
X
X
Comp 122
k2
Collision Resolution by Chaining
0
m–1
U
(universe of keys)
K
(actual
keys)
k1
k2
k3
k5
k4
k6
k7k8
k1 k4
k5 k6
k7 k3
k8
Comp 122
Hashing with Chaining
Dictionary Operations:
• Chained-Hash-Insert (T, x)
– Insert x at the head of list T[h(key[x])].
– Worst-case complexity – O(1).
• Chained-Hash-Delete (T, x)
– Delete x from the list T[h(key[x])].
– Worst-case complexity – proportional to length of list with
singly-linked lists. O(1) with doubly-linked lists.
• Chained-Hash-Search (T, k)
– Search an element with key k in list T[h(k)].
– Worst-case complexity – proportional to length of list.
Comp 122
Analysis on Chained-Hash-Search
• Load factor α=n/m = average keys per slot.
– m – number of slots.
– n – number of elements stored in the hash table.
• Worst-case complexity: Θ(n) + time to compute h(k).
• Average depends on how h distributes keys among m slots.
• Assume
– Simple uniform hashing.
• Any key is equally likely to hash into any of the m slots,
independent of where any other key hashes to.
– O(1) time to compute h(k).
• Time to search for an element with key k is Θ(|T[h(k)]|).
• Expected length of a linked list = load factor = α = n/m.
Comp 122
Expected Cost of an Unsuccessful Search
Proof:
• Any key not already in the table is equally likely to hash
to any of the m slots.
• To search unsuccessfully for any key k, need to search to
the end of the list T[h(k)], whose expected length is α.
• Adding the time to compute the hash function, the total
time required is Θ(1+α).
Theorem:
An unsuccessful search takes expected time Θ(1+α).
Comp 122
Expected Cost of a Successful Search
Proof:
• The probability that a list is searched is proportional to the number of
elements it contains.
• Assume that the element being searched for is equally likely to be any of
the n elements in the table.
• The number of elements examined during a successful search for an
element x is 1 more than the number of elements that appear before x
in x’s list.
– These are the elements inserted after x was inserted.
• Goal:
– Find the average, over the n elements x in the table, of how many elements
were inserted into x’s list after x was inserted.
Theorem:
A successful search takes expected time Θ(1+α).
Comp 122
Expected Cost of a Successful Search
Proof (contd):
• Let xi be the ith
element inserted into the table, and let ki = key[xi].
• Define indicator random variables Xij = I{h(ki) = h(kj)}, for all i, j.
• Simple uniform hashing ⇒ Pr{h(ki) = h(kj)} = 1/m
⇒ E[Xij] = 1/m.
• Expected number of elements examined in a successful search is:
Theorem:
A successful search takes expected time Θ(1+α).












+∑ ∑= +=
n
i
n
ij
ijX
n
E
1 1
1
1
No. of elements inserted after xi into the same slot as xi.
Comp 122
Proof – Contd.
n
m
n
nn
n
nm
in
nm
in
nm
mn
XE
n
X
n
E
n
i
n
i
n
i
n
i
n
ij
n
i
n
ij
ij
n
i
n
ij
ij
22
1
2
1
1
2
)1(1
1
1
1
)(
1
1
1
1
1
][1
1
1
1
2
1 1
1
1 1
1 1
1 1
αα
−+=
−
+=





 +
−+=






−+=
−+=






+=






+=












+
∑ ∑
∑
∑ ∑
∑ ∑
∑ ∑
= =
=
= +=
= +=
= +=
(linearity of expectation)
Expected total time for a successful search = Time to
compute hash function + Time to search
= O(2+α/2 – α/2n) = O(1+ α).
Comp 122
Expected Cost – Interpretation
• If n = O(m), then α=n/m = O(m)/m = O(1).
⇒ Searching takes constant time on average.
• Insertion is O(1) in the worst case.
• Deletion takes O(1) worst-case time when lists are
doubly linked.
• Hence, all dictionary operations take O(1) time on
average with hash tables with chaining.

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Hashing data
Hashing dataHashing data
Hashing data
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
Hashing
HashingHashing
Hashing
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Red black tree
Red black treeRed black tree
Red black tree
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
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 in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Data Structures & Algorithm design using C
Data Structures & Algorithm design using C Data Structures & Algorithm design using C
Data Structures & Algorithm design using C
 
b+ tree
b+ treeb+ tree
b+ tree
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Quick sort
Quick sortQuick sort
Quick sort
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Destacado

18 hashing
18 hashing18 hashing
18 hashingdeonnash
 
Graphs, Edges & Nodes - Untangling the Social Web
Graphs, Edges & Nodes - Untangling the Social WebGraphs, Edges & Nodes - Untangling the Social Web
Graphs, Edges & Nodes - Untangling the Social WebJoël Perras
 
Best for b trees
Best for b treesBest for b trees
Best for b treesDineshRaaja
 
BTree, Data Structures
BTree, Data StructuresBTree, Data Structures
BTree, Data StructuresJibrael Jos
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashingRafi Dar
 
The Graph Traversal Programming Pattern
The Graph Traversal Programming PatternThe Graph Traversal Programming Pattern
The Graph Traversal Programming PatternMarko Rodriguez
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tablesadil raja
 
compiler ppt on symbol table
 compiler ppt on symbol table compiler ppt on symbol table
compiler ppt on symbol tablenadarmispapaulraj
 
Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Tech_MX
 

Destacado (15)

Hashing
HashingHashing
Hashing
 
18 hashing
18 hashing18 hashing
18 hashing
 
B trees and_b__trees
B trees and_b__treesB trees and_b__trees
B trees and_b__trees
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Avl tree
Avl treeAvl tree
Avl tree
 
Graphs, Edges & Nodes - Untangling the Social Web
Graphs, Edges & Nodes - Untangling the Social WebGraphs, Edges & Nodes - Untangling the Social Web
Graphs, Edges & Nodes - Untangling the Social Web
 
Multi ways trees
Multi ways treesMulti ways trees
Multi ways trees
 
Best for b trees
Best for b treesBest for b trees
Best for b trees
 
BTree, Data Structures
BTree, Data StructuresBTree, Data Structures
BTree, Data Structures
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
The Graph Traversal Programming Pattern
The Graph Traversal Programming PatternThe Graph Traversal Programming Pattern
The Graph Traversal Programming Pattern
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 
Tree
TreeTree
Tree
 
compiler ppt on symbol table
 compiler ppt on symbol table compiler ppt on symbol table
compiler ppt on symbol table
 
Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)
 

Similar a Hash tables

Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingSam Light
 
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
 
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 icajiwol341
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 
Hash presentation
Hash presentationHash presentation
Hash presentationomercode
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
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
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 

Similar a Hash tables (20)

13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.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
 
Hashing
HashingHashing
Hashing
 
L21_Hashing.pdf
L21_Hashing.pdfL21_Hashing.pdf
L21_Hashing.pdf
 
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 Structure Download PPT i
Hashing In Data Structure Download PPT iHashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT i
 
Maps&hash tables
Maps&hash tablesMaps&hash tables
Maps&hash tables
 
Lec4
Lec4Lec4
Lec4
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Hash presentation
Hash presentationHash presentation
Hash presentation
 
Hashing
HashingHashing
Hashing
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
sorting
sortingsorting
sorting
 
Linear sorting
Linear sortingLinear sorting
Linear sorting
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
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
 
220exercises2
220exercises2220exercises2
220exercises2
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 

Más de Rajendran

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower boundsRajendran
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsRajendran
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower boundsRajendran
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statisticsRajendran
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theoremRajendran
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theoremRajendran
 
Master method
Master method Master method
Master method Rajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisRajendran
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisRajendran
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of QuicksortRajendran
 
Np completeness
Np completenessNp completeness
Np completenessRajendran
 
computer languages
computer languagescomputer languages
computer languagesRajendran
 
proving non-computability
proving non-computabilityproving non-computability
proving non-computabilityRajendran
 
the halting_problem
the halting_problemthe halting_problem
the halting_problemRajendran
 

Más de Rajendran (20)

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower bounds
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding Costs
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower bounds
 
Red black tree
Red black treeRed black tree
Red black tree
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theorem
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theorem
 
Master method
Master method Master method
Master method
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Lower bound
Lower boundLower bound
Lower bound
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of Quicksort
 
Np completeness
Np completenessNp completeness
Np completeness
 
computer languages
computer languagescomputer languages
computer languages
 
proving non-computability
proving non-computabilityproving non-computability
proving non-computability
 
the halting_problem
the halting_problemthe halting_problem
the halting_problem
 

Último

Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPCeline George
 
Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxAvaniJani1
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 

Último (20)

Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERP
 
Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptx
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 

Hash tables

  • 1. Comp 122, Spring 2004 Hash Tables – 1
  • 2. Comp 122 Dictionary • Dictionary: – Dynamic-set data structure for storing items indexed using keys. – Supports operations Insert, Search, and Delete. – Applications: • Symbol table of a compiler. • Memory-management tables in operating systems. • Large-scale distributed systems. • Hash Tables: – Effective way of implementing dictionaries. – Generalization of ordinary arrays.
  • 3. Comp 122 Direct-address Tables • Direct-address Tables are ordinary arrays. • Facilitate direct addressing. – Element whose key is k is obtained by indexing into the kth position of the array. • Applicable when we can afford to allocate an array with one position for every possible key. – i.e. when the universe of keys U is small. • Dictionary operations can be implemented to take O(1) time. – Details in Sec. 11.1.
  • 4. Comp 122 Hash Tables • Notation: – U – Universe of all possible keys. – K – Set of keys actually stored in the dictionary. – |K| = n. • When U is very large, – Arrays are not practical. – |K| << |U|. • Use a table of size proportional to |K| – The hash tables. – However, we lose the direct-addressing ability. – Define functions that map keys to slots of the hash table.
  • 5. Comp 122 Hashing • Hash function h: Mapping from U to the slots of a hash table T[0..m–1]. h : U → {0,1,…, m–1} • With arrays, key k maps to slot A[k]. • With hash tables, key k maps or “hashes” to slot T[h[k]]. • h[k] is the hash value of key k.
  • 6. Comp 122 Hashing 0 m–1 h(k1) h(k4) h(k2)=h(k5) h(k3) U (universe of keys) K (actual keys) k1 k2 k3 k5 k4 collision
  • 7. Comp 122 Issues with Hashing • Multiple keys can hash to the same slot – collisions are possible. – Design hash functions such that collisions are minimized. – But avoiding collisions is impossible. • Design collision-resolution techniques. • Search will cost Ө(n) time in the worst case. – However, all operations can be made to have an expected complexity of Ө(1).
  • 8. Comp 122 Methods of Resolution • Chaining: – Store all elements that hash to the same slot in a linked list. – Store a pointer to the head of the linked list in the hash table slot. • Open Addressing: – All elements stored in hash table itself. – When collisions occur, use a systematic (consistent) procedure to store elements in free slots of the table. k2 0 m–1 k1 k4 k5 k6 k7 k3 k8
  • 9. Comp 122 Collision Resolution by Chaining 0 m–1 h(k1)=h(k4) h(k2)=h(k5)=h(k6) h(k3)=h(k7) U (universe of keys) K (actual keys) k1 k2 k3 k5 k4 k6 k7k8 h(k8) X X X
  • 10. Comp 122 k2 Collision Resolution by Chaining 0 m–1 U (universe of keys) K (actual keys) k1 k2 k3 k5 k4 k6 k7k8 k1 k4 k5 k6 k7 k3 k8
  • 11. Comp 122 Hashing with Chaining Dictionary Operations: • Chained-Hash-Insert (T, x) – Insert x at the head of list T[h(key[x])]. – Worst-case complexity – O(1). • Chained-Hash-Delete (T, x) – Delete x from the list T[h(key[x])]. – Worst-case complexity – proportional to length of list with singly-linked lists. O(1) with doubly-linked lists. • Chained-Hash-Search (T, k) – Search an element with key k in list T[h(k)]. – Worst-case complexity – proportional to length of list.
  • 12. Comp 122 Analysis on Chained-Hash-Search • Load factor α=n/m = average keys per slot. – m – number of slots. – n – number of elements stored in the hash table. • Worst-case complexity: Θ(n) + time to compute h(k). • Average depends on how h distributes keys among m slots. • Assume – Simple uniform hashing. • Any key is equally likely to hash into any of the m slots, independent of where any other key hashes to. – O(1) time to compute h(k). • Time to search for an element with key k is Θ(|T[h(k)]|). • Expected length of a linked list = load factor = α = n/m.
  • 13. Comp 122 Expected Cost of an Unsuccessful Search Proof: • Any key not already in the table is equally likely to hash to any of the m slots. • To search unsuccessfully for any key k, need to search to the end of the list T[h(k)], whose expected length is α. • Adding the time to compute the hash function, the total time required is Θ(1+α). Theorem: An unsuccessful search takes expected time Θ(1+α).
  • 14. Comp 122 Expected Cost of a Successful Search Proof: • The probability that a list is searched is proportional to the number of elements it contains. • Assume that the element being searched for is equally likely to be any of the n elements in the table. • The number of elements examined during a successful search for an element x is 1 more than the number of elements that appear before x in x’s list. – These are the elements inserted after x was inserted. • Goal: – Find the average, over the n elements x in the table, of how many elements were inserted into x’s list after x was inserted. Theorem: A successful search takes expected time Θ(1+α).
  • 15. Comp 122 Expected Cost of a Successful Search Proof (contd): • Let xi be the ith element inserted into the table, and let ki = key[xi]. • Define indicator random variables Xij = I{h(ki) = h(kj)}, for all i, j. • Simple uniform hashing ⇒ Pr{h(ki) = h(kj)} = 1/m ⇒ E[Xij] = 1/m. • Expected number of elements examined in a successful search is: Theorem: A successful search takes expected time Θ(1+α).             +∑ ∑= += n i n ij ijX n E 1 1 1 1 No. of elements inserted after xi into the same slot as xi.
  • 16. Comp 122 Proof – Contd. n m n nn n nm in nm in nm mn XE n X n E n i n i n i n i n ij n i n ij ij n i n ij ij 22 1 2 1 1 2 )1(1 1 1 1 )( 1 1 1 1 1 ][1 1 1 1 2 1 1 1 1 1 1 1 1 1 αα −+= − +=       + −+=       −+= −+=       +=       +=             + ∑ ∑ ∑ ∑ ∑ ∑ ∑ ∑ ∑ = = = = += = += = += (linearity of expectation) Expected total time for a successful search = Time to compute hash function + Time to search = O(2+α/2 – α/2n) = O(1+ α).
  • 17. Comp 122 Expected Cost – Interpretation • If n = O(m), then α=n/m = O(m)/m = O(1). ⇒ Searching takes constant time on average. • Insertion is O(1) in the worst case. • Deletion takes O(1) worst-case time when lists are doubly linked. • Hence, all dictionary operations take O(1) time on average with hash tables with chaining.