SlideShare a Scribd company logo
1 of 33
Maps & Hash Tables
Map ADT
• models a searchable collection of (key, value) entries.
• requires each key to be unique.
• association of keys to values defines a mapping.
Maps
• allow to store elements so they can be located quickly using
keys.
• stores key-value pairs (k, v), which we call entries, where k is
the key and v is its corresponding value.
Conceptual illustration of Maps
• Keys (labels) are assigned to values (diskettes) by a user.
• The resulting entries (labeled diskettes) are inserted into the map
(file cabinet).
• The keys can be used later to retrieve or remove values.
Applications
• address book.
• student-record database.
Map ADT methods
• size():
• Deterines the size of Map M.
• isEmpty():
• Test whether M is empty.
• get(k):
• If M contains an entry e=(k,v), where k is key, then return the value v, else
return null.
Map ADT methods
• put(k,v):
• If M does not have an entry (k,v) , then add entry (k,v) to M and
return null; else, replace with v the existing value of the entry with “k”
key and return the old value.
• remove(k):
• Remove from M the entry with key equal to k, and return its value.
•keySet():
• Return an iterable collection containing all the keys stored in M.
Map ADT methods
• values():
• Return an iterable collection containing all the values associated with keys
stored in M.
• entrySet():
• Return an iterable collection containing all the key-value entries in M.
Map ADT representation
Operation Output
put(5,A) null
put(7,B) null
put(2,C) null
put(8,D) null
put(2,E) C
get(7) B
get(4) null
get(2) E
remove(2) E
entrySet() (5,A),
(7,B),
(8,D)
keySet() 5,7,8
(7,B)
,
(5,A), (2,C), (8,D),(2,E),
A Simple List-Based Map Implementation
• Using doubly-linked list
Performance of a List-Based Map
In unsorted list
• Put(k,v)  O(1) time
• Get(k), remove(key)  O(n) time.
Hash Table
• One of the most efficient ways to implement a map such
that the keys serves as the address for the associated values
is to use a hash table.
• Recall that maps are collection of entries (k,v), where the
keys associated with values are typically thought of as
addresses for those values.
Hash Table components
• In general, a hash table consists of two major components, a
bucket array and a hash function.
• A bucket array
• A hash function.
Bucket Array
• Consider array A of size N (array size)
• each cell is a bucket (i.e. a collection of (k,v))
• The keys of entries are integers in the range of [0, N-1], each
bucket holds at most one entry.
• Search, insertion and removal in the bucket array seems to take
O(1) time.
Bucket Array(cont.)
• It has two drawbacks:
• As the space used is proportional to N (array size).
• if N >> number of entries n present in the map, there is a waste of space.
• keys are required to be integers (range [0, N − 1]), which is often not the case.
• Overcome:
• Use the bucket array in conjunction with a "good" mapping from the keys to the
integers in the range [0,N − 1] like hash functions.
Hash functions (h)
• Is second part of hash table structure.
• Hash function value, h(k), is an index into the bucket array,
instead of k.
• So entry (k, v) is stored in the bucket A[h(k)].
Evaluation of a hash function, h(k),
• Consists of two functions:
• mapping the key k to an integer, called the hash code.
• mapping the hash code to an integer within the range of
indices ([0, N − 1]) of a bucket array, called the
compression function.
• Hash codes may be generated by casting to an integer,
summing components, Polynomial hash codes etc.
One simple compression function is the division method,
which maps an integer i to
i (mod N)
where N, the size of the bucket array, is a fixed positive
integer.
Collision Handling Schemes
Collision occurs when different elements are mapped to the same cell.
Some collision resolution methods:
• Separate Chaining
• Open Addressing
Separate chaining
Separate chaining
• To index the n entries of map in a bucket array of capacity N.
• Each bucket has to be of size n/N called as the load factor of
the hash table.
• So the expected running time of operations is O(n/N).
• These operations can be implemented to run in O(1) time,
provided n is O(N).
Open Addressing
Linear probing
• Distance between probes is constant (i.e. 1, when probe
examines consequent slots).
• When an entry into a bucket A[i] is already occupied,
where i = h(k) then :
• Try next at A[(i + 1) modN]. If this is also occupied, then
• Try A[(i + 2) mod N], and so on.
• until we find an empty bucket that can accept the new entry.
Open Addressing with Linear Probing Strategy
Insert keys 18, 41, 22, 44, 59, 32, 31, 73 in this order to a bucket, using
h(k) = k (mod 13).
41 18 44 59 32 22 31 73
0 1 2 3 4 5 6 7 8 9 10 11 12
Quadratic probing
• trying the buckets
A[h(k) + j2] (mod N), for j = 0,1,..., N −1
until finding an empty bucket.
• N has to be a prime number.
• Bucket array must be less than half full.
Quadratic probing
Double Hashing
• Handles collision , by placing an item in the series:
H(k) = (h(k) + j × h’(k)) mod N for j = 0,1,...N −1.
• h’(k) cannot have zero values.
• Table size N must be a prime number.
• Common choice of compression function :
h’(k) = q – (k mod q) where q < N is a prime.
Open Addressing with Double Hashing Strategy
Insert keys 18, 41, 22, 44, 59, 32, 31, 73 in this order to a
bucket, using double-hashing resolution where:
h(k) = k (mod 13) and h’(k) = 7 – k (mode 7).
41 18 445932 2231 73
0 1 2 3 4 5 6 7 8 9 10 11 12
H(k) = (h(k) + j × h’(k))
= 5 + 1 x (7 – 44%7)
= 10
H(k) = (h(k) + j × h’(k))
= 5 + 1 x (7 – 31%7)
= 9
H(k) = (h(k) + j × h’(k))
= 5 + 2 x (7 – 31%7)
= 13
• Worst-case for insertions, removal, and searches, on a hash
table take O(n) time.
• The worst-case  all the keys inserted into the map collide.
• The load factor α = n/N affects the performance of hash
table.
Ordered Maps
• To keep the entries in a map sorted according to some order
• To look up keys and values based on this ordering.
• Performs the usual map operations, maintaining an order relation
for the keys.
• The worst-case time for searching in hash tables is O(n).
• A list implementation of an ordered array (known as ordered search
table), has O(lgn) as the worst-case time for searching.
Searching algorithm – Binary search
Algorithm BinarySearch(S, k, low, high)
if low > high then
return null
else
mid ← [(low + high)/2 ]
e ← S.get(mid)
if k = e.getKey() then
return e
else if k < e.getKey() then
return BinarySearch(S, k, low, mid-1)
else
return BinarySearch(S, k, mid+1, high)
Illustration on an ordered search table
Execution of binary search algorithm to perform get(22)
Maps&hash tables

More Related Content

What's hot

Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 

What's hot (20)

Lec5
Lec5Lec5
Lec5
 
Lecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 btLecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 bt
 
chapter-8.ppt
chapter-8.pptchapter-8.ppt
chapter-8.ppt
 
3.6 radix sort
3.6 radix sort3.6 radix sort
3.6 radix sort
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Nikit
NikitNikit
Nikit
 
Admission for b.tech
Admission for b.techAdmission for b.tech
Admission for b.tech
 
Merge sort
Merge sortMerge sort
Merge sort
 
Radix sorting
Radix sortingRadix sorting
Radix sorting
 
Queues-handouts
Queues-handoutsQueues-handouts
Queues-handouts
 
Bucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmBucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision Algorithm
 
Counting sort
Counting sortCounting sort
Counting sort
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of queue
 
Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained
 
Presentation
PresentationPresentation
Presentation
 
Queue
QueueQueue
Queue
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
Merge sort
Merge sortMerge sort
Merge sort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
IIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RIIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into R
 

Similar to Maps&hash tables

Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
chidabdu
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
Sajid Marwat
 
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptxPPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
CHANDUS31
 

Similar to Maps&hash tables (20)

LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
Hashing using a different methods of technic
Hashing using a different methods of technicHashing using a different methods of technic
Hashing using a different methods of technic
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Lec5
Lec5Lec5
Lec5
 
03.01 hash tables
03.01 hash tables03.01 hash tables
03.01 hash tables
 
Hashing
HashingHashing
Hashing
 
Sets, maps and hash tables (Java Collections)
Sets, maps and hash tables (Java Collections)Sets, maps and hash tables (Java Collections)
Sets, maps and hash tables (Java Collections)
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
Hash tables
Hash tablesHash tables
Hash tables
 
8. Hash table
8. Hash table8. Hash table
8. Hash table
 
L21_Hashing.pdf
L21_Hashing.pdfL21_Hashing.pdf
L21_Hashing.pdf
 
Linear sorting
Linear sortingLinear sorting
Linear sorting
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
Lecture24
Lecture24Lecture24
Lecture24
 
Data Structures- Hashing
Data Structures- Hashing Data Structures- Hashing
Data Structures- Hashing
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptxPPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
 

More from Priyanka Rana

More from Priyanka Rana (17)

Insertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority QueuesInsertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority Queues
 
Scrum values
Scrum valuesScrum values
Scrum values
 
Usability testing
Usability testing  Usability testing
Usability testing
 
Content package - Mobile computing
Content package - Mobile computingContent package - Mobile computing
Content package - Mobile computing
 
Scrum retrospective
Scrum retrospective Scrum retrospective
Scrum retrospective
 
Convergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototypingConvergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototyping
 
Sketching&storyboarding
Sketching&storyboardingSketching&storyboarding
Sketching&storyboarding
 
Building better prototype
Building better prototypeBuilding better prototype
Building better prototype
 
Mobile presence & location based marketing
Mobile presence & location based marketingMobile presence & location based marketing
Mobile presence & location based marketing
 
User friendliness of website
User friendliness of websiteUser friendliness of website
User friendliness of website
 
E-commerce Marketing & advertising
E-commerce Marketing & advertisingE-commerce Marketing & advertising
E-commerce Marketing & advertising
 
E commerce business proposal
E commerce business proposalE commerce business proposal
E commerce business proposal
 
E-strategic Management-1
E-strategic Management-1E-strategic Management-1
E-strategic Management-1
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
Linked list
Linked listLinked list
Linked list
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+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...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
+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...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

Maps&hash tables

  • 1. Maps & Hash Tables
  • 2. Map ADT • models a searchable collection of (key, value) entries. • requires each key to be unique. • association of keys to values defines a mapping.
  • 3. Maps • allow to store elements so they can be located quickly using keys. • stores key-value pairs (k, v), which we call entries, where k is the key and v is its corresponding value.
  • 4. Conceptual illustration of Maps • Keys (labels) are assigned to values (diskettes) by a user. • The resulting entries (labeled diskettes) are inserted into the map (file cabinet). • The keys can be used later to retrieve or remove values.
  • 5. Applications • address book. • student-record database.
  • 6. Map ADT methods • size(): • Deterines the size of Map M. • isEmpty(): • Test whether M is empty. • get(k): • If M contains an entry e=(k,v), where k is key, then return the value v, else return null.
  • 7. Map ADT methods • put(k,v): • If M does not have an entry (k,v) , then add entry (k,v) to M and return null; else, replace with v the existing value of the entry with “k” key and return the old value. • remove(k): • Remove from M the entry with key equal to k, and return its value. •keySet(): • Return an iterable collection containing all the keys stored in M.
  • 8. Map ADT methods • values(): • Return an iterable collection containing all the values associated with keys stored in M. • entrySet(): • Return an iterable collection containing all the key-value entries in M.
  • 9. Map ADT representation Operation Output put(5,A) null put(7,B) null put(2,C) null put(8,D) null put(2,E) C get(7) B get(4) null get(2) E remove(2) E entrySet() (5,A), (7,B), (8,D) keySet() 5,7,8 (7,B) , (5,A), (2,C), (8,D),(2,E),
  • 10. A Simple List-Based Map Implementation • Using doubly-linked list
  • 11. Performance of a List-Based Map In unsorted list • Put(k,v)  O(1) time • Get(k), remove(key)  O(n) time.
  • 12. Hash Table • One of the most efficient ways to implement a map such that the keys serves as the address for the associated values is to use a hash table. • Recall that maps are collection of entries (k,v), where the keys associated with values are typically thought of as addresses for those values.
  • 13. Hash Table components • In general, a hash table consists of two major components, a bucket array and a hash function. • A bucket array • A hash function.
  • 14. Bucket Array • Consider array A of size N (array size) • each cell is a bucket (i.e. a collection of (k,v)) • The keys of entries are integers in the range of [0, N-1], each bucket holds at most one entry. • Search, insertion and removal in the bucket array seems to take O(1) time.
  • 15. Bucket Array(cont.) • It has two drawbacks: • As the space used is proportional to N (array size). • if N >> number of entries n present in the map, there is a waste of space. • keys are required to be integers (range [0, N − 1]), which is often not the case. • Overcome: • Use the bucket array in conjunction with a "good" mapping from the keys to the integers in the range [0,N − 1] like hash functions.
  • 16. Hash functions (h) • Is second part of hash table structure. • Hash function value, h(k), is an index into the bucket array, instead of k. • So entry (k, v) is stored in the bucket A[h(k)].
  • 17. Evaluation of a hash function, h(k), • Consists of two functions: • mapping the key k to an integer, called the hash code. • mapping the hash code to an integer within the range of indices ([0, N − 1]) of a bucket array, called the compression function. • Hash codes may be generated by casting to an integer, summing components, Polynomial hash codes etc.
  • 18. One simple compression function is the division method, which maps an integer i to i (mod N) where N, the size of the bucket array, is a fixed positive integer.
  • 19. Collision Handling Schemes Collision occurs when different elements are mapped to the same cell. Some collision resolution methods: • Separate Chaining • Open Addressing
  • 21. Separate chaining • To index the n entries of map in a bucket array of capacity N. • Each bucket has to be of size n/N called as the load factor of the hash table. • So the expected running time of operations is O(n/N). • These operations can be implemented to run in O(1) time, provided n is O(N).
  • 23. Linear probing • Distance between probes is constant (i.e. 1, when probe examines consequent slots). • When an entry into a bucket A[i] is already occupied, where i = h(k) then : • Try next at A[(i + 1) modN]. If this is also occupied, then • Try A[(i + 2) mod N], and so on. • until we find an empty bucket that can accept the new entry.
  • 24. Open Addressing with Linear Probing Strategy Insert keys 18, 41, 22, 44, 59, 32, 31, 73 in this order to a bucket, using h(k) = k (mod 13). 41 18 44 59 32 22 31 73 0 1 2 3 4 5 6 7 8 9 10 11 12
  • 25. Quadratic probing • trying the buckets A[h(k) + j2] (mod N), for j = 0,1,..., N −1 until finding an empty bucket. • N has to be a prime number. • Bucket array must be less than half full.
  • 27. Double Hashing • Handles collision , by placing an item in the series: H(k) = (h(k) + j × h’(k)) mod N for j = 0,1,...N −1. • h’(k) cannot have zero values. • Table size N must be a prime number. • Common choice of compression function : h’(k) = q – (k mod q) where q < N is a prime.
  • 28. Open Addressing with Double Hashing Strategy Insert keys 18, 41, 22, 44, 59, 32, 31, 73 in this order to a bucket, using double-hashing resolution where: h(k) = k (mod 13) and h’(k) = 7 – k (mode 7). 41 18 445932 2231 73 0 1 2 3 4 5 6 7 8 9 10 11 12 H(k) = (h(k) + j × h’(k)) = 5 + 1 x (7 – 44%7) = 10 H(k) = (h(k) + j × h’(k)) = 5 + 1 x (7 – 31%7) = 9 H(k) = (h(k) + j × h’(k)) = 5 + 2 x (7 – 31%7) = 13
  • 29. • Worst-case for insertions, removal, and searches, on a hash table take O(n) time. • The worst-case  all the keys inserted into the map collide. • The load factor α = n/N affects the performance of hash table.
  • 30. Ordered Maps • To keep the entries in a map sorted according to some order • To look up keys and values based on this ordering. • Performs the usual map operations, maintaining an order relation for the keys. • The worst-case time for searching in hash tables is O(n). • A list implementation of an ordered array (known as ordered search table), has O(lgn) as the worst-case time for searching.
  • 31. Searching algorithm – Binary search Algorithm BinarySearch(S, k, low, high) if low > high then return null else mid ← [(low + high)/2 ] e ← S.get(mid) if k = e.getKey() then return e else if k < e.getKey() then return BinarySearch(S, k, low, mid-1) else return BinarySearch(S, k, mid+1, high)
  • 32. Illustration on an ordered search table Execution of binary search algorithm to perform get(22)

Editor's Notes

  1. &amp;lt;number&amp;gt;
  2. &amp;lt;number&amp;gt;
  3. &amp;lt;number&amp;gt;
  4. &amp;lt;number&amp;gt;
  5. &amp;lt;number&amp;gt;
  6. &amp;lt;number&amp;gt;
  7. &amp;lt;number&amp;gt;
  8. &amp;lt;number&amp;gt;