SlideShare a Scribd company logo
Dictionaries 
… 
the dictionary ADT 
… 
binary search 
… 
Hashing
Dictionaries 
… 
Dictionaries store elements so that they can be located quickly using keys 
… 
Adictionary may hold bank accounts 
… 
each account is an object that is identified by an account number 
… 
each account stores a wealth of additional information 
… 
including the current balance, 
… 
the name and address of the account holder, and 
… 
the history of deposits and withdrawals performed 
… 
an application wishing to operate on an account would have to provide the account number as a search key
The Dictionary ADT 
… 
A dictionary is an abstract model of a database 
… 
A dictionary stores key-element pairs 
… 
The main operation supported by a dictionary is searching by key 
… 
simple container methods: size(), isEmpty(), elements() 
… 
query methods: findElem(k), findAllElem(k) 
… 
update methods: insertItem(k,e), removeElem(k), removeAllElem(k) 
… 
special element: NIL, returned by an unsuccessful search
The Dictionary ADT 
…Supporting order (methods min, max, successor, predecessor ) is not required, thus it is enough that keys are comparable for equality
The Dictionary ADT 
…Different data structures to realize dictionaries 
… 
arrays, linked lists (inefficient) 
… 
Hash table (used in Java...) 
… 
Binary trees 
… 
Red/Black trees 
… 
AVL trees 
… 
B-trees 
… 
In Java: 
… 
java.util.Dictionary –abstract class 
… 
java.util.Map –interface
Searching 
INPUT 
•sequence of numbers (database) 
• 
a single number (query) 
OUTPUT 
•index of the found number or NIL 
a1, a2, a3,….,an; q 
2 5 4 10 7; 5 
2 5 4 10 7; 9 
j 
2 
NIL
BinarySearch 
…Idea: Divide and conquer, a key design technique 
… 
narrow down the search range in stages 
… 
findElement(22)
A recursive procedure 
Algorithm BinarySearch(A, k, low, high) 
iflow > high thenreturnNil 
elsemid ←(low+high) / 2 
ifk = A[mid] then returnmid 
elseifk < A[mid] then 
returnBinarySearch(A, k, low, mid-1) 
else returnBinarySearch(A, k, mid+1, high)
An iterative procedure 
INPUT: A[1..n] –a sorted (non-decreasing) array of integers, key–an integer. 
OUTPUT: an index j such that A[j] = k. 
NIL, if ∀j (1 ≤ j ≤ n): A[j] ≠ k 
low←1 
high← n 
do 
mid←(low+high)/2 
if A[mid] = kthenreturn mid 
else if A[mid] > kthen high← mid-1 
else low← mid+1 
while low<= high 
return NIL
Running Time of Binary Search 
…The range of candidate items to be searched is halved after each comparison 
… 
In the array-based implementation, access by rank takes O(1) time, thus binary search runs in O(log n) time
Searching in an unsorted array 
INPUT: A[1..n] –an array of integers, q–an integer. 
OUTPUT: an index jsuch that A[j] = q. NIL, if ∀j(1≤j≤n): A[j] ≠ q 
j ← 1 
while j ≤ n and A[j]≠ q 
doj++ 
if j≤ n thenreturnj 
else return NIL 
…Worst-case running time: O(n), average-case: O(n) 
… 
We can’t do better. This is a lower bound for the problem of searching in an arbitrary sequence.
The Problem 
T&T is a large phone company, and they want toprovide caller ID capability: 
… 
given a phone number, return the caller’s name 
… 
phone numbers range from 0 to r = 108-1 
… 
There are n phone numbers, n << r. 
… 
want to do this as efficiently as possible
Using an unordered sequence 
…searching and removing takes O(n) time 
… 
inserting takes O(1) time 
… 
applications to log files (frequent insertions, rare searches and removals)
Using an ordered sequence 
…searching takes O(log n) time (binary search) 
… 
inserting and removing takes O(n) time 
… 
application to look-up tables (frequent searches, rare insertions and removals)
Other Suboptimal ways 
direct addressing: an array indexed by key: 
… 
takes O(1) time, 
… 
O(r)spacewhere r is the range of numbers (108) 
… 
huge amount of wasted space 
(null) 
(null) 
Ankur 
(null) 
(null) 
0000-0000 
0000-0000 
9635-8904 
0000-0000 
0000-0000
Another Solution 
…Can do better, with a Hash table--O(1) expected time, O(n+m) space, where mis table size 
… 
Like an array, but come up with a function to map the large range into one which we can manage 
… 
e.g., take the original key, modulo the (relatively small) size of the array, and use that as an index 
… 
Insert (9635-8904, Ankur) into a hashed array with, say, five slots. 96358904 mod 5 = 4 
(null) 
(null) 
(null) 
(null) 
Ankur 
0 
1 
2 
3 
4
An Example 
„Let keys be entry no’s of students in CSL201. eg. 2004CS10110. 
„ 
There are 100 students in the class. We create a hash table of size, say 100. 
„ 
Hash function is, say, last two digits. 
„ 
Then 2004CS10110 goes to location 10. 
„ 
Where does 2004CS50310 go? 
„ 
Also to location 10. We have a collision!!
Collision Resolution 
…How to deal with two keys which hash to the same spot in the array? 
… 
Use chaining 
… 
Set up an array of links (a table), indexed by the keys, to lists of items with the same key 
… 
Most efficient (time-wise) collision resolution scheme
Collision resolution (2) 
…To find/insert/delete an element 
… 
using h, look up its position in table T 
… 
Search/insert/delete the element in the linked list of the hashed slot
Analysis of Hashing 
…An element with key k is stored in slot h(k) (instead of slot kwithout hashing) 
… 
The hash function hmaps the universe Uof keys into the slots of hash table T[0...m-1] 
… 
Assume time to compute h(k) is Θ(1) 
{}:0,1,...,1hUm→−
Analysis of Hashing(2) 
…An good hash function is one which distributes keys evenly amongst the slots. 
… 
An ideal hash function would pick a slot, uniformly at random and hash the key to it. 
… 
However, this is not a hash function since we would not know which slot to look into when searching for a key. 
… 
For our analysis we will use this simple uniformhash function 
… 
Given hash table Twith mslots holding nelements, the load factoris defined as α=n/m
Analysis of Hashing(3) 
Unsuccessful search 
… 
element is not in the linked list 
… 
Simple uniformhashing yields an average list length α= n/m 
… 
expected number of elements to be examined α 
… 
search time O(1+α) (includes computing the hash value)
Analysis of Hashing (4) 
Successful search 
… 
assume that a new element is inserted at the end of the linked list 
… 
upon insertion of the i-th element, the expected length of the list is (i-1)/m 
… 
in case of a successful search, the expected number of elements examined is 1 more that the number of elements examined when the sought- for element was inserted!
Analysis of Hashing (5) 
…The expected number of elements examined is thus 
… 
Considering the time for computing the hash function, we obtain 
() () 11111111111211211221122nniiiinmnmnnnmnmnmmm α == −⎛⎞+=+−⎜⎟ ⎝⎠ − =+⋅ − =+ =+− +− ΣΣ(2/21/2)(1)mααΘ+−=Θ+
Analysis of Hashing (6) 
Assuming the number of hash table slots is proportional to the number of elements in the table 
… 
n=O(m) 
… 
α= n/m = O(m)/m = O(1) 
… 
searching takes constant time on average 
… 
insertion takes O(1) worst-case time 
… 
deletion takes O(1) worst-case time when the lists are doubly-linked

More Related Content

PDF
Lec16
Nikhil Chilwant
 
PPT
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
Array Presentation (EngineerBaBu.com)
EngineerBabu
 

What's hot (20)

PPTX
Arrays
Trupti Agrawal
 
PDF
Functional programming with haskell
faradjpour
 
PPT
Algorithm
sultanarun
 
PPT
Sparse Matrix and Polynomial
Aroosa Rajput
 
PPTX
Queue Implementation Using Array & Linked List
PTCL
 
PPT
Java: Introduction to Arrays
Tareq Hasan
 
PPTX
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
PDF
2-D array
Swarup Kumar Boro
 
PDF
Introduction to haskell
Luca Molteni
 
PPTX
18. Java associative arrays
Intro C# Book
 
PDF
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
 
PDF
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
PDF
The Functional Programming Triad of fold, scan and iterate
Philip Schwarz
 
PDF
Data Structures In Scala
Knoldus Inc.
 
PPT
Queue Data Structure
Zidny Nafan
 
PPT
Priority queues
Yeela Mehroz
 
PDF
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Philip Schwarz
 
Functional programming with haskell
faradjpour
 
Algorithm
sultanarun
 
Sparse Matrix and Polynomial
Aroosa Rajput
 
Queue Implementation Using Array & Linked List
PTCL
 
Java: Introduction to Arrays
Tareq Hasan
 
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
Introduction to haskell
Luca Molteni
 
18. Java associative arrays
Intro C# Book
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
 
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
The Functional Programming Triad of fold, scan and iterate
Philip Schwarz
 
Data Structures In Scala
Knoldus Inc.
 
Queue Data Structure
Zidny Nafan
 
Priority queues
Yeela Mehroz
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Philip Schwarz
 
Ad

Viewers also liked (10)

PDF
Cursos sql server .net visual basic octubre 2010
Servicios Educativos Softrain C.A.
 
PDF
List classes
Ravi_Kant_Sahu
 
PPTX
Java Stack (Pilha)
Samuel Santos
 
Cursos sql server .net visual basic octubre 2010
Servicios Educativos Softrain C.A.
 
List classes
Ravi_Kant_Sahu
 
Java Stack (Pilha)
Samuel Santos
 
Ad

Similar to Lec4 (20)

PPTX
Unit 8 searching and hashing
Dabbal Singh Mahara
 
PPTX
Unit viii searching and hashing
Tribhuvan University
 
PDF
Unit-9 Searching .pdf
Yatru Harsha Hiski
 
PDF
Data Structures Design Notes.pdf
AmuthachenthiruK
 
PPTX
searching techniques.pptx
Dr.Shweta
 
PPT
4.4 hashing02
Krish_ver2
 
PPT
Analysis Of Algorithms - Hashing
Sam Light
 
PDF
hashing.pdf
Yuvraj919347
 
PPTX
hashing1.pptx Data Structures and Algorithms
snehalkulkarni78
 
PDF
Algorithms notes tutorials duniya
TutorialsDuniya.com
 
PPT
Hashing
amoldkul
 
PPT
Hashing PPT
Saurabh Kumar
 
PPTX
session 15 hashing.pptx
rajneeshsingh46738
 
PPT
Search techniques and Hashing
Thirunavukarasu Mani
 
PPT
Hashing
debolina13
 
PPT
Searching.ppt
p83629918
 
PPT
13-hashing.ppt
soniya555961
 
PPT
Hashing and collision for database systems
janakiraman123
 
PDF
08 Hash Tables
Andres Mendez-Vazquez
 
PPTX
hashing in data strutures advanced in languae java
ishasharma835109
 
Unit 8 searching and hashing
Dabbal Singh Mahara
 
Unit viii searching and hashing
Tribhuvan University
 
Unit-9 Searching .pdf
Yatru Harsha Hiski
 
Data Structures Design Notes.pdf
AmuthachenthiruK
 
searching techniques.pptx
Dr.Shweta
 
4.4 hashing02
Krish_ver2
 
Analysis Of Algorithms - Hashing
Sam Light
 
hashing.pdf
Yuvraj919347
 
hashing1.pptx Data Structures and Algorithms
snehalkulkarni78
 
Algorithms notes tutorials duniya
TutorialsDuniya.com
 
Hashing
amoldkul
 
Hashing PPT
Saurabh Kumar
 
session 15 hashing.pptx
rajneeshsingh46738
 
Search techniques and Hashing
Thirunavukarasu Mani
 
Hashing
debolina13
 
Searching.ppt
p83629918
 
13-hashing.ppt
soniya555961
 
Hashing and collision for database systems
janakiraman123
 
08 Hash Tables
Andres Mendez-Vazquez
 
hashing in data strutures advanced in languae java
ishasharma835109
 

More from Nikhil Chilwant (20)

TXT
The joyless economy
Nikhil Chilwant
 
PPTX
IIT Delhi training pioneer ct (acemicromatic )
Nikhil Chilwant
 
PPT
Lec39
Nikhil Chilwant
 
PPT
Lec38
Nikhil Chilwant
 
PPT
Lec37
Nikhil Chilwant
 
PPT
Lec36
Nikhil Chilwant
 
PPT
Lec35
Nikhil Chilwant
 
PPT
Lec34
Nikhil Chilwant
 
PPT
Lec33
Nikhil Chilwant
 
PPT
Lec32
Nikhil Chilwant
 
PPT
Lec30
Nikhil Chilwant
 
PPT
Lec29
Nikhil Chilwant
 
PPT
Lec28
Nikhil Chilwant
 
PPT
Lec27
Nikhil Chilwant
 
PPT
Lec26
Nikhil Chilwant
 
PPT
Lec25
Nikhil Chilwant
 
PPT
Lec24
Nikhil Chilwant
 
PPT
Lec23
Nikhil Chilwant
 
PPT
Lec21
Nikhil Chilwant
 
PPT
Lec20
Nikhil Chilwant
 

Recently uploaded (20)

PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 

Lec4

  • 1. Dictionaries … the dictionary ADT … binary search … Hashing
  • 2. Dictionaries … Dictionaries store elements so that they can be located quickly using keys … Adictionary may hold bank accounts … each account is an object that is identified by an account number … each account stores a wealth of additional information … including the current balance, … the name and address of the account holder, and … the history of deposits and withdrawals performed … an application wishing to operate on an account would have to provide the account number as a search key
  • 3. The Dictionary ADT … A dictionary is an abstract model of a database … A dictionary stores key-element pairs … The main operation supported by a dictionary is searching by key … simple container methods: size(), isEmpty(), elements() … query methods: findElem(k), findAllElem(k) … update methods: insertItem(k,e), removeElem(k), removeAllElem(k) … special element: NIL, returned by an unsuccessful search
  • 4. The Dictionary ADT …Supporting order (methods min, max, successor, predecessor ) is not required, thus it is enough that keys are comparable for equality
  • 5. The Dictionary ADT …Different data structures to realize dictionaries … arrays, linked lists (inefficient) … Hash table (used in Java...) … Binary trees … Red/Black trees … AVL trees … B-trees … In Java: … java.util.Dictionary –abstract class … java.util.Map –interface
  • 6. Searching INPUT •sequence of numbers (database) • a single number (query) OUTPUT •index of the found number or NIL a1, a2, a3,….,an; q 2 5 4 10 7; 5 2 5 4 10 7; 9 j 2 NIL
  • 7. BinarySearch …Idea: Divide and conquer, a key design technique … narrow down the search range in stages … findElement(22)
  • 8. A recursive procedure Algorithm BinarySearch(A, k, low, high) iflow > high thenreturnNil elsemid ←(low+high) / 2 ifk = A[mid] then returnmid elseifk < A[mid] then returnBinarySearch(A, k, low, mid-1) else returnBinarySearch(A, k, mid+1, high)
  • 9. An iterative procedure INPUT: A[1..n] –a sorted (non-decreasing) array of integers, key–an integer. OUTPUT: an index j such that A[j] = k. NIL, if ∀j (1 ≤ j ≤ n): A[j] ≠ k low←1 high← n do mid←(low+high)/2 if A[mid] = kthenreturn mid else if A[mid] > kthen high← mid-1 else low← mid+1 while low<= high return NIL
  • 10. Running Time of Binary Search …The range of candidate items to be searched is halved after each comparison … In the array-based implementation, access by rank takes O(1) time, thus binary search runs in O(log n) time
  • 11. Searching in an unsorted array INPUT: A[1..n] –an array of integers, q–an integer. OUTPUT: an index jsuch that A[j] = q. NIL, if ∀j(1≤j≤n): A[j] ≠ q j ← 1 while j ≤ n and A[j]≠ q doj++ if j≤ n thenreturnj else return NIL …Worst-case running time: O(n), average-case: O(n) … We can’t do better. This is a lower bound for the problem of searching in an arbitrary sequence.
  • 12. The Problem T&T is a large phone company, and they want toprovide caller ID capability: … given a phone number, return the caller’s name … phone numbers range from 0 to r = 108-1 … There are n phone numbers, n << r. … want to do this as efficiently as possible
  • 13. Using an unordered sequence …searching and removing takes O(n) time … inserting takes O(1) time … applications to log files (frequent insertions, rare searches and removals)
  • 14. Using an ordered sequence …searching takes O(log n) time (binary search) … inserting and removing takes O(n) time … application to look-up tables (frequent searches, rare insertions and removals)
  • 15. Other Suboptimal ways direct addressing: an array indexed by key: … takes O(1) time, … O(r)spacewhere r is the range of numbers (108) … huge amount of wasted space (null) (null) Ankur (null) (null) 0000-0000 0000-0000 9635-8904 0000-0000 0000-0000
  • 16. Another Solution …Can do better, with a Hash table--O(1) expected time, O(n+m) space, where mis table size … Like an array, but come up with a function to map the large range into one which we can manage … e.g., take the original key, modulo the (relatively small) size of the array, and use that as an index … Insert (9635-8904, Ankur) into a hashed array with, say, five slots. 96358904 mod 5 = 4 (null) (null) (null) (null) Ankur 0 1 2 3 4
  • 17. An Example „Let keys be entry no’s of students in CSL201. eg. 2004CS10110. „ There are 100 students in the class. We create a hash table of size, say 100. „ Hash function is, say, last two digits. „ Then 2004CS10110 goes to location 10. „ Where does 2004CS50310 go? „ Also to location 10. We have a collision!!
  • 18. Collision Resolution …How to deal with two keys which hash to the same spot in the array? … Use chaining … Set up an array of links (a table), indexed by the keys, to lists of items with the same key … Most efficient (time-wise) collision resolution scheme
  • 19. Collision resolution (2) …To find/insert/delete an element … using h, look up its position in table T … Search/insert/delete the element in the linked list of the hashed slot
  • 20. Analysis of Hashing …An element with key k is stored in slot h(k) (instead of slot kwithout hashing) … The hash function hmaps the universe Uof keys into the slots of hash table T[0...m-1] … Assume time to compute h(k) is Θ(1) {}:0,1,...,1hUm→−
  • 21. Analysis of Hashing(2) …An good hash function is one which distributes keys evenly amongst the slots. … An ideal hash function would pick a slot, uniformly at random and hash the key to it. … However, this is not a hash function since we would not know which slot to look into when searching for a key. … For our analysis we will use this simple uniformhash function … Given hash table Twith mslots holding nelements, the load factoris defined as α=n/m
  • 22. Analysis of Hashing(3) Unsuccessful search … element is not in the linked list … Simple uniformhashing yields an average list length α= n/m … expected number of elements to be examined α … search time O(1+α) (includes computing the hash value)
  • 23. Analysis of Hashing (4) Successful search … assume that a new element is inserted at the end of the linked list … upon insertion of the i-th element, the expected length of the list is (i-1)/m … in case of a successful search, the expected number of elements examined is 1 more that the number of elements examined when the sought- for element was inserted!
  • 24. Analysis of Hashing (5) …The expected number of elements examined is thus … Considering the time for computing the hash function, we obtain () () 11111111111211211221122nniiiinmnmnnnmnmnmmm α == −⎛⎞+=+−⎜⎟ ⎝⎠ − =+⋅ − =+ =+− +− ΣΣ(2/21/2)(1)mααΘ+−=Θ+
  • 25. Analysis of Hashing (6) Assuming the number of hash table slots is proportional to the number of elements in the table … n=O(m) … α= n/m = O(m)/m = O(1) … searching takes constant time on average … insertion takes O(1) worst-case time … deletion takes O(1) worst-case time when the lists are doubly-linked