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 (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

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
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingSam Light
 
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)Fulvio Corno
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructurerajshreemuthiah
 
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.pptxCHANDUS31
 

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

Insertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority QueuesInsertion & Selection Sort - using Priority Queues
Insertion & Selection Sort - using Priority QueuesPriyanka Rana
 
Content package - Mobile computing
Content package - Mobile computingContent package - Mobile computing
Content package - Mobile computingPriyanka Rana
 
Scrum retrospective
Scrum retrospective Scrum retrospective
Scrum retrospective Priyanka Rana
 
Convergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototypingConvergent divergent thinking &amp; wireframeprototyping
Convergent divergent thinking &amp; wireframeprototypingPriyanka Rana
 
Sketching&storyboarding
Sketching&storyboardingSketching&storyboarding
Sketching&storyboardingPriyanka Rana
 
Building better prototype
Building better prototypeBuilding better prototype
Building better prototypePriyanka Rana
 
Mobile presence & location based marketing
Mobile presence & location based marketingMobile presence & location based marketing
Mobile presence & location based marketingPriyanka Rana
 
User friendliness of website
User friendliness of websiteUser friendliness of website
User friendliness of websitePriyanka Rana
 
E-commerce Marketing & advertising
E-commerce Marketing & advertisingE-commerce Marketing & advertising
E-commerce Marketing & advertisingPriyanka Rana
 
E commerce business proposal
E commerce business proposalE commerce business proposal
E commerce business proposalPriyanka Rana
 
E-strategic Management-1
E-strategic Management-1E-strategic Management-1
E-strategic Management-1Priyanka Rana
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data StructurePriyanka Rana
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresPriyanka 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

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Recently uploaded (20)

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 

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;