SlideShare a Scribd company logo
DATA STRUCTURES AND ALGORITHMS
LAB 11
Bianca Tesila
FILS, May 2014
OBJECTIVES
 Dictionaries
 Hash tables
DICTIONARIES: WHAT ARE THEY?
 An ADT made of a collection of keys and a
collection of values, in which each key has a value
associated to it
 A dictionary is also
called associative array
 Useful for searching
DICTIONARIES: OPTIMAL SEARCH
 The keys must be unique
 The range of the key must be severly bounded
Otherwise… if the keys are not unique:
 construct a set of
m(keys count) lists and
store the heads of these
lists in the associative
array(the keys)
DICTIONARIES: DUPLICATE KEYS
 If we have a high number of duplicates (a lot of elements with
the same key), the search time will severely increase
 Solution: make a function to optimize the search criterion, h
=> solve collisions of keys
 We will search for T[h(k)] rather than T[k] , where: T is our
associative array, k is an index and h(k) is a mapping function
DICTIONARIES: IMPLEMENTATION
 Hash-tables
 Self-balancing binary search trees
 Radix- tree
 Prefix-tree
 Judy arrays
DICTIONARIES: BASIC OPERATIONS
 put(key, value)
 Inserts the pair (key, value) in the hash table
 If a pair (key, value’) (with the same key) already
exists, then value’ is replaced by value
 We say that the value value is associated to the key key
 get(key)
 Returns the value associated to the key key
 If no value is associated to key, then an error occurs
 hasKey(key)
 Returns 1 if the key key exists in the hash table, and 0
otherwise
HASH-TABLES: INTRODUCTION
 Data structure with an optimized lookup function (average
search time is constant, O (1)).
 How? By turning the key in a hash (code), using a hash function
 The hash function must be wisely chosen in order to minimize the
number of collisions (Risk: different values ​​produce the same
hashes).
 We cannot avoid all the collisions - they occur inherently as
hash length is fixed, and storage objects can have arbitrary
length and content.
 In the event of a collision, the values ​​stored in the same position
(the same bucket). In this case, the search is reduced to
comparing the actual values ​​in the bucket.
HASH-TABLES: EXAMPLE
HASH TABLE: HASH FUNCTIONS
 Deterministic: if called twice, they should return
the same value
 Low collision rate: buckets with small dimensions
 Good dispersion between “buckets”
HASH TABLE: IMPLEMENTATION WITH LINKED LISTS
 A hash implementation which solves the collisions is
called direct chaining
 For each bucket, we use a linked list: every list is
associated to a key(hash-coded)
 Inserting in hash table means finding the correct
index(key) and adding the element to the list that
corresponds to the found key
 Deleting means searching and removing of that element
from the list
HASH TABLE: ADVANTAGES AND DISADVANTAGES
 Advantage: the delete operation is simple and
the table resizing can be postponed a
lot because (even when all positions of hash
are used), performance is still good.
 Disadvantage: for small amount of data, the
overhead is quite large and “browsing” the data can
be time consuming (the same disadvantage as in
linked lists)
HASH TABLE: EXAMPLE
• hmax is the maximum number of linked lists in our hash-table
• the function hash will be passed as an argument (actually, a
pointer to the function will be passed)
• the key is not mandatory to be a number (think of a real
dictionary!!!): that is why we use templates
HASH TABLE: ASSIGNMENT
!!Exercise: Using the previous header, implement
the hash tables data structure and test it, for a
custom hash-function
HASH TABLE: ASSIGNMENT
Hint:
 Maintain an array H[HMAX] of linked lists
 The info field of each element of a list consists of a struct containing a key
and a value
 Each key is mapped to a value hkey=hash(key), such that 0≤hkey≤HMAX-1
 hash(key) is called the hash function and hkey is the index in a linked list
 put(k, v)
 Searches for the key k in the list H[hkey=hash(k)]
 If the key is found, then we replace the value by v
 If the key is not found, then we insert the pair (k,v) in H[hkey]
 get(k)
 Search for the key k in H[hkey=hash(k)]
 If it finds the key, then it returns its associated value; otherwise, an error
occurs
 hasKey(k)
 Search for the key k in H[hkey=hash(k)]
 If it finds the key, then it returns 1; otherwise, it returns 0

More Related Content

What's hot (7)

PDF
NOSQL Databases
hicham jabbour
 
PPTX
Hashing In Data Structure
Meghaj Mallick
 
PPTX
Overview of Storage and Indexing ...
Javed Khan
 
PPTX
Power shell basics day 5
Ashish Raj
 
PPT
Hashing
grahamwell
 
PDF
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Kuntal Bhowmick
 
PDF
Hashing Algorithm
Hayi Nukman
 
NOSQL Databases
hicham jabbour
 
Hashing In Data Structure
Meghaj Mallick
 
Overview of Storage and Indexing ...
Javed Khan
 
Power shell basics day 5
Ashish Raj
 
Hashing
grahamwell
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Kuntal Bhowmick
 
Hashing Algorithm
Hayi Nukman
 

Viewers also liked (13)

PPTX
Data structures and algorithms lab3
Bianca Teşilă
 
PPTX
Data structures and algorithms lab2
Bianca Teşilă
 
PDF
Difference between c# generics and c++ templates
Umar Ali
 
PPTX
Data structures and algorithms lab8
Bianca Teşilă
 
PPTX
Data structures and algorithms lab1
Bianca Teşilă
 
PPTX
Introduction to computer architecture and organization
Muhammad Ishaq
 
PPT
Indexing and hashing
Jeet Poria
 
PPTX
Data structures and algorithms lab5
Bianca Teşilă
 
PPTX
USMLE and Canadian Exams
Kuwait Medical Society, UK & Ireland
 
PPTX
Chapter 1 introduction to computers
haider ali
 
PPT
0. Course Introduction
Intro C# Book
 
PPT
Ppt 1
shanmugamsara
 
PDF
DSA-2012-Lect00
Haitham El-Ghareeb
 
Data structures and algorithms lab3
Bianca Teşilă
 
Data structures and algorithms lab2
Bianca Teşilă
 
Difference between c# generics and c++ templates
Umar Ali
 
Data structures and algorithms lab8
Bianca Teşilă
 
Data structures and algorithms lab1
Bianca Teşilă
 
Introduction to computer architecture and organization
Muhammad Ishaq
 
Indexing and hashing
Jeet Poria
 
Data structures and algorithms lab5
Bianca Teşilă
 
USMLE and Canadian Exams
Kuwait Medical Society, UK & Ireland
 
Chapter 1 introduction to computers
haider ali
 
0. Course Introduction
Intro C# Book
 
DSA-2012-Lect00
Haitham El-Ghareeb
 
Ad

Similar to Data structures and algorithms lab11 (20)

PPSX
Data Structure and Algorithms: What is Hash Table ppt
JUSTFUN40
 
PDF
Hash pre
Waed Shagareen
 
PPTX
Presentation.pptx
AgonySingh
 
PPTX
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
BabaShaikh3
 
PPTX
hashing in data strutures advanced in languae java
ishasharma835109
 
PPTX
linear probing
rajshreemuthiah
 
PPTX
Hashing and Collision Advanced data structure and algorithm
yogoso2948
 
PPTX
DS THEORY 35.pptx
veenatanmaipatlolla
 
PPTX
5 data structures-hashtable
irdginfo
 
PPTX
Hash table in java
siriindian
 
PPTX
hashing1.pptx Data Structures and Algorithms
snehalkulkarni78
 
PPT
11_hashtable-1.ppt. Data structure algorithm
farhankhan89766
 
PPTX
introduction to trees,graphs,hashing
Akhil Prem
 
PPTX
Hashing And Hashing Tables
Chinmaya M. N
 
PPT
Hashing PPT
Saurabh Kumar
 
PDF
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
kksrivastava1
 
PDF
Algorithms notes tutorials duniya
TutorialsDuniya.com
 
PPTX
hashing in data structures and its applications
manjeshbngowda
 
PPTX
hashing in data structure for Btech .pptx
soniasharmafdp
 
PPTX
hashing in data structure for engineering.pptx
soniasharmafdp
 
Data Structure and Algorithms: What is Hash Table ppt
JUSTFUN40
 
Hash pre
Waed Shagareen
 
Presentation.pptx
AgonySingh
 
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
BabaShaikh3
 
hashing in data strutures advanced in languae java
ishasharma835109
 
linear probing
rajshreemuthiah
 
Hashing and Collision Advanced data structure and algorithm
yogoso2948
 
DS THEORY 35.pptx
veenatanmaipatlolla
 
5 data structures-hashtable
irdginfo
 
Hash table in java
siriindian
 
hashing1.pptx Data Structures and Algorithms
snehalkulkarni78
 
11_hashtable-1.ppt. Data structure algorithm
farhankhan89766
 
introduction to trees,graphs,hashing
Akhil Prem
 
Hashing And Hashing Tables
Chinmaya M. N
 
Hashing PPT
Saurabh Kumar
 
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
kksrivastava1
 
Algorithms notes tutorials duniya
TutorialsDuniya.com
 
hashing in data structures and its applications
manjeshbngowda
 
hashing in data structure for Btech .pptx
soniasharmafdp
 
hashing in data structure for engineering.pptx
soniasharmafdp
 
Ad

More from Bianca Teşilă (6)

PDF
Akka Streams - An Adobe data-intensive story
Bianca Teşilă
 
PPTX
Data structures and algorithms lab10
Bianca Teşilă
 
PPTX
Data structures and algorithms lab9
Bianca Teşilă
 
PPTX
Data structures and algorithms lab7
Bianca Teşilă
 
PPTX
Data structures and algorithms lab6
Bianca Teşilă
 
PPTX
Data structures and algorithms lab4
Bianca Teşilă
 
Akka Streams - An Adobe data-intensive story
Bianca Teşilă
 
Data structures and algorithms lab10
Bianca Teşilă
 
Data structures and algorithms lab9
Bianca Teşilă
 
Data structures and algorithms lab7
Bianca Teşilă
 
Data structures and algorithms lab6
Bianca Teşilă
 
Data structures and algorithms lab4
Bianca Teşilă
 

Recently uploaded (20)

PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
July Patch Tuesday
Ivanti
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 

Data structures and algorithms lab11

  • 1. DATA STRUCTURES AND ALGORITHMS LAB 11 Bianca Tesila FILS, May 2014
  • 3. DICTIONARIES: WHAT ARE THEY?  An ADT made of a collection of keys and a collection of values, in which each key has a value associated to it  A dictionary is also called associative array  Useful for searching
  • 4. DICTIONARIES: OPTIMAL SEARCH  The keys must be unique  The range of the key must be severly bounded Otherwise… if the keys are not unique:  construct a set of m(keys count) lists and store the heads of these lists in the associative array(the keys)
  • 5. DICTIONARIES: DUPLICATE KEYS  If we have a high number of duplicates (a lot of elements with the same key), the search time will severely increase  Solution: make a function to optimize the search criterion, h => solve collisions of keys  We will search for T[h(k)] rather than T[k] , where: T is our associative array, k is an index and h(k) is a mapping function
  • 6. DICTIONARIES: IMPLEMENTATION  Hash-tables  Self-balancing binary search trees  Radix- tree  Prefix-tree  Judy arrays
  • 7. DICTIONARIES: BASIC OPERATIONS  put(key, value)  Inserts the pair (key, value) in the hash table  If a pair (key, value’) (with the same key) already exists, then value’ is replaced by value  We say that the value value is associated to the key key  get(key)  Returns the value associated to the key key  If no value is associated to key, then an error occurs  hasKey(key)  Returns 1 if the key key exists in the hash table, and 0 otherwise
  • 8. HASH-TABLES: INTRODUCTION  Data structure with an optimized lookup function (average search time is constant, O (1)).  How? By turning the key in a hash (code), using a hash function  The hash function must be wisely chosen in order to minimize the number of collisions (Risk: different values ​​produce the same hashes).  We cannot avoid all the collisions - they occur inherently as hash length is fixed, and storage objects can have arbitrary length and content.  In the event of a collision, the values ​​stored in the same position (the same bucket). In this case, the search is reduced to comparing the actual values ​​in the bucket.
  • 10. HASH TABLE: HASH FUNCTIONS  Deterministic: if called twice, they should return the same value  Low collision rate: buckets with small dimensions  Good dispersion between “buckets”
  • 11. HASH TABLE: IMPLEMENTATION WITH LINKED LISTS  A hash implementation which solves the collisions is called direct chaining  For each bucket, we use a linked list: every list is associated to a key(hash-coded)  Inserting in hash table means finding the correct index(key) and adding the element to the list that corresponds to the found key  Deleting means searching and removing of that element from the list
  • 12. HASH TABLE: ADVANTAGES AND DISADVANTAGES  Advantage: the delete operation is simple and the table resizing can be postponed a lot because (even when all positions of hash are used), performance is still good.  Disadvantage: for small amount of data, the overhead is quite large and “browsing” the data can be time consuming (the same disadvantage as in linked lists)
  • 13. HASH TABLE: EXAMPLE • hmax is the maximum number of linked lists in our hash-table • the function hash will be passed as an argument (actually, a pointer to the function will be passed) • the key is not mandatory to be a number (think of a real dictionary!!!): that is why we use templates
  • 14. HASH TABLE: ASSIGNMENT !!Exercise: Using the previous header, implement the hash tables data structure and test it, for a custom hash-function
  • 15. HASH TABLE: ASSIGNMENT Hint:  Maintain an array H[HMAX] of linked lists  The info field of each element of a list consists of a struct containing a key and a value  Each key is mapped to a value hkey=hash(key), such that 0≤hkey≤HMAX-1  hash(key) is called the hash function and hkey is the index in a linked list  put(k, v)  Searches for the key k in the list H[hkey=hash(k)]  If the key is found, then we replace the value by v  If the key is not found, then we insert the pair (k,v) in H[hkey]  get(k)  Search for the key k in H[hkey=hash(k)]  If it finds the key, then it returns its associated value; otherwise, an error occurs  hasKey(k)  Search for the key k in H[hkey=hash(k)]  If it finds the key, then it returns 1; otherwise, it returns 0