SlideShare una empresa de Scribd logo
1 de 41
Zahoor Jan
Lecture-05
1
 Definition: the ideal table data structure is merely an
array of some fixed size, containing the elements.
 Consist : an array and a mapping function
(known as hash function)
 Used for performing insertion, deletion and lookup
on average in constant time.
2
14
3
Storage Space Storing k
Direct
Addressing
|U| Store in slot k
Hashing m Store in slot h(k)
 Advantage: Requires less storage and runs in O(1)
time.
 Comparison
4
16
5
 How can we solve the problem of collisions?
 Solution 1:Solution 1: ChainingChaining
 Solution 2:Solution 2: Open addressingOpen addressing
6
 Put all the elements that hash to same slot in a
linked list.
 Worst case : All n keys hash to the same slot
resulting in a linked list of length n, running time:
O(n)
 Best and AverageAverage time: O(1)
7
20
8
 Assume simple uniform hashing: each key in table is
equally likely to be hashed to any slot
 Given n keys and m slots in the table: the load factor
α = n/m = average # keys per slot
 What will be the average cost of an unsuccessful
search for a key?
O(1+ α)
9
 What will be the average cost of a successful
search?
A: O(1 + α/2) = O(1 + α)
10
 So the cost of searching = O(1 + α)
 If the number of keys n is proportional to the
number of slots in the table, what is α?
 A: α = O(1)
◦ In other words, we can make the expected cost of
searching constant if we make α constant
11
 Nature of keys
 Hash functions
 Division method
 Multiplication method
 Open Addressing (Linear and Quadratic probing,
Double hashing)
12
Most hash functions assume that universe of keys is
the set N = {0, 1, 2,…} of natural numbers
If keys are not N, ways to be found to interpret them
as N
A character key can be interpreted as an integer
expressed in suitable Radix notation.
13
 Example: The identifier pt might be interpreted as a
pair of decimal integers (112, 116) as p = 112 and t
= 116 in ASCII notation. What is the problem?
 Using a product/addition of ASCII codes is indifferent
to the order of characters
 Solution: Using 128-radix notation this becomes
(112.128) + 116 = 14,452
14
A hash function is a mapping between a set of input
values (Keys) and a set of integers, known as hash
values.
Keys Hash values
Hash
function
15
 Rule1: The hash value is fully determined by the data
being hashed.
 Rule2: The hash function uses all the input data.
 Rule3: The hash function uniformly distributes the
data across the entire set of possible hash values.
 Rule4: The hash function generates very different
hash values for similar strings.
16
int hash(char *str, int table_size)
{
int sum=0;
//sum up all the characters in the string
for(;*str; str++)
sum+=*str
//return sum mod table_size
return sum%table_size;
}
17
 Rule1: Satisfies, the hash value is fully determined
by the data being hashed, the hash value is just the
sum of all input characters.
 Rule2: Satisfies, Every character is summed.
18
 Rule3: Breaks, from looking at it, it is not obvious that it
doesn’t uniformly distribute the strings, but if you were to
analyze this function for larger input string, you will see
certain statistical properties which are bad for a hash
function.
 Rule4: Breaks, hash the string “CAT”, now hash the
string “ACT”, they are the same, a slight variation in the
string should result in different hash values, but with this
function often they don’t.
19
Division method
Multiplication method
20
The division method requires two steps.
1. The key must be transformed into an
integer.
2. The value must be telescoped into range
0 to m-1
21
 We map a key k into one of the m slots by taking the
remainder of k divided by m, so the hash function is of
form
h(k)= k mod m
 For example , if m=12, key is 100 then
h(k)=100 mod 12= 4.
 Advantage?
22
M should not be a
power of 2, since if
m=2p
then h(k) is just
the p lowest order
bits of k.
Disadvantage!
Key Binary K mod 8
8 1000 0
7 111 7
12 1100 4
34 100010 2
56 111000 0
78 1001110 6
90 1011010 2
23 10111 7
45 101101 5
67 1000011 3 23
Unless it is known that probability
distribution on keys makes all lower order
p-bit patterns equally likely,
it is better to make the hash function
dependent on all the bits of the key.
24
 Power of 10 should be avoided, if application deals
with decimal numbers as keys.
 Good values of m are primes not close to the exact
powers of 2 (or 10).
25
 Using a random real number ff in the range (0,1).
 The fractional part of the product ff*key yields a number
in the range 0 to 1.
 When this number is multiplied by m (hash table size),
the integer portion of the product gives the hash value
in the range 0 to m-1
26
 Choose m = 2P
 For a constant A, 0 < A < 1:
 h(k) =  m (kA - kA) 
 Value of A should not be close to 0 or 1
 Knuth says good value of A is 0.618033
 If k=123456, m=10000,and A as above
h(k)= 10000.(123456*A- 123456*A)
= 10000. (0.0041151)
=41
27
 For mm ∈∈ (4, 15)(4, 15), and k = {contents first news item
www.dawn.com} and mapping function k mod
m, and other hash function given on slide-28,
plot histogram results using Excel. Contrast and
compare results on the basis of mm. June 15, 2010
08:59 am.
28
 So far we have studied hashing with chaining, using a
linked-list to store keys that hash to the same location.
 Maintaining linked lists involves using pointers which is
complex and inefficient in both storage and time
requirements.
 Another option is to store all the keys directly in the table.
This is known as open addressing, where collisions are
resolved by systematically examining other table indexes, i 0 ,
i 1 , i 2 , … until an empty slot is located.
29
◦ Another approach for collision resolution.
◦ All elements are stored in the hash table itself (so no
pointers involved as in chaining).
◦ To insert: if slot is full, try another slot, and another, until an
open slot is found (probing)
◦ To search, follow same sequence of probes as would be
used when inserting the element
30
 The key is first mapped to a slot:
 If there is a collision subsequent probes are
performed:
 If the offset constant, c and m are not relatively
prime, we will not examine all the cells. Ex.:
◦ Consider m=4 and c=2, then only every other slot is
checked.
When c=1 the collision resolution is done as a linear
search. This is known as linear probing.
)(index 10 ki h==
0formod)(1 ≥+=+ jmcii jj
0 1 2 3
31
HASH_INSERT(T,k)
1 i ← 0
2 repeat j ← h(k,i)
3 if T[j] = NIL
4 then T[j] = k
5 return j
6 else i ← i +1
7 until i = m
8 error “ hash table overflow”
32
HASH_SEARCH(T,k)
1 i ← 0
2 repeat j ← h(k,i)
3 if T[j] = k
4 then return j
5 i ← i +1
6 until T[j] = NIL or i = m
7 return NIL
33
 Worst case for inserting a key is θ(n)
 Worst case for searching is θ(n)
34
35
Whenever there is a collision, one strategy is to
look for the next unused slot and use it.
Linear Probing.
36
When searching for an empty slot, one has to
remember to wrap around (like a circular array)
 Even with a good hash function, linear probing has its problems:
◦ The position of the initial mapping i 0 of key k is called the home
position of k.
◦ When several insertions map to the same home position, they end up
placed contiguously in the table. This collection of keys with the same
home position is called a cluster.
◦ As clusters grow, the probability that a key will map to the middle of a
cluster increases, increasing the rate of the cluster’s growth. This
tendency of linear probing to place items together is known as
primary clustering.
◦ As these clusters grow, they merge with other clusters forming even
bigger clusters which grow even faster.
37
h(k,i) = (h’(k) + c1i + c2i 2
) mod m for i = 0,1,…,m − 1.
◦ Leads to a secondary clustering (milder form of clustering)
◦ The clustering effect can be improved by increasing the
order to the probing function (cubic). However the hash
function becomes more expensive to compute
38
 Recall that in open addressing the sequence of probes follows
 We can solve the problem of primary clustering in linear probing by having
the keys which map to the same home position use differing probe
sequences. In other words, the different values for c should be used for
different keys.
 Double hashing refers to the scheme of using another hash function for c
0formod)(1 ≥+=+ jmcii jj
1)(0and0formod))(( 221 −≤<≥+=+ mkjmkii jj hh
39
 Lecture is prepared using information from
Chapter 11 “Hash Tables” of book “Introduction to
Algorithms” By Thomas H. Cormen et al
 Book is available in the library, make photocopy of
this chapter
40
41
Compare hash based search with
Binary search on best case basis.
What value of load factor will result in
fewer collisions?

Más contenido relacionado

La actualidad más candente

Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructurerajshreemuthiah
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms HashingManishPrajapati78
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmayTanmay 'Unsinkable'
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingHaripritha
 
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15sumitbardhan
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables Nifras Ismail
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Kuntal Bhowmick
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its usesJawad Khan
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithmAamir Sohail
 

La actualidad más candente (20)

Hashing
HashingHashing
Hashing
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmay
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashing
 
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15
 
Hashing
HashingHashing
Hashing
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
Ds 8
Ds 8Ds 8
Ds 8
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Hash tables
Hash tablesHash tables
Hash tables
 
Hashing
HashingHashing
Hashing
 
linear probing
linear probinglinear probing
linear probing
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Hashing
HashingHashing
Hashing
 

Destacado (12)

Hash Functions
Hash FunctionsHash Functions
Hash Functions
 
Heaps
HeapsHeaps
Heaps
 
Hashing
HashingHashing
Hashing
 
Hash functions
Hash functionsHash functions
Hash functions
 
Hashing
HashingHashing
Hashing
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
Hash tables
Hash tablesHash tables
Hash tables
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Heap sort
Heap sort Heap sort
Heap sort
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 
Heap sort
Heap sortHeap sort
Heap sort
 
Hashing
HashingHashing
Hashing
 

Similar a Advance algorithm hashing lec II

Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptxkratika64
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingSam Light
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 
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
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
HASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxHASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxJITTAYASHWANTHREDDY
 
Hashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfHashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfJaithoonBibi
 
Hash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxHash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxmy6305874
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingzukun
 

Similar a Advance algorithm hashing lec II (20)

13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Lec5
Lec5Lec5
Lec5
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Lec5
Lec5Lec5
Lec5
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
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
 
LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
HASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxHASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptx
 
Hashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfHashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdf
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
session 15 hashing.pptx
session 15   hashing.pptxsession 15   hashing.pptx
session 15 hashing.pptx
 
Hash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxHash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptx
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sorting
 

Más de Sajid Marwat

Trusted computing introduction and technical overview
Trusted computing introduction and technical overviewTrusted computing introduction and technical overview
Trusted computing introduction and technical overviewSajid Marwat
 
Digital Rights Management and Trusted Computing Base
Digital Rights Management and Trusted Computing BaseDigital Rights Management and Trusted Computing Base
Digital Rights Management and Trusted Computing BaseSajid Marwat
 
Automata definitions
Automata definitionsAutomata definitions
Automata definitionsSajid Marwat
 
Thr cellular concept
Thr cellular conceptThr cellular concept
Thr cellular conceptSajid Marwat
 
Computer System Overview,
Computer System Overview, Computer System Overview,
Computer System Overview, Sajid Marwat
 
4 g LTE, LTE Advance
4 g LTE, LTE Advance 4 g LTE, LTE Advance
4 g LTE, LTE Advance Sajid Marwat
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
GSM Network 3G Technologies
GSM Network 3G TechnologiesGSM Network 3G Technologies
GSM Network 3G TechnologiesSajid Marwat
 
WiMAX (IEEE 802.16)
WiMAX (IEEE 802.16)WiMAX (IEEE 802.16)
WiMAX (IEEE 802.16)Sajid Marwat
 
Radio over Fiber Technology for WiMAX Systems
 Radio over Fiber Technology for WiMAX Systems Radio over Fiber Technology for WiMAX Systems
Radio over Fiber Technology for WiMAX Systems Sajid Marwat
 
Knowledge Representation & Reasoning
Knowledge Representation & ReasoningKnowledge Representation & Reasoning
Knowledge Representation & ReasoningSajid Marwat
 
top level view of computer function and interconnection
top level view of computer function and interconnectiontop level view of computer function and interconnection
top level view of computer function and interconnectionSajid Marwat
 

Más de Sajid Marwat (13)

Trusted computing introduction and technical overview
Trusted computing introduction and technical overviewTrusted computing introduction and technical overview
Trusted computing introduction and technical overview
 
Digital Rights Management and Trusted Computing Base
Digital Rights Management and Trusted Computing BaseDigital Rights Management and Trusted Computing Base
Digital Rights Management and Trusted Computing Base
 
Manet
ManetManet
Manet
 
Automata definitions
Automata definitionsAutomata definitions
Automata definitions
 
Thr cellular concept
Thr cellular conceptThr cellular concept
Thr cellular concept
 
Computer System Overview,
Computer System Overview, Computer System Overview,
Computer System Overview,
 
4 g LTE, LTE Advance
4 g LTE, LTE Advance 4 g LTE, LTE Advance
4 g LTE, LTE Advance
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
GSM Network 3G Technologies
GSM Network 3G TechnologiesGSM Network 3G Technologies
GSM Network 3G Technologies
 
WiMAX (IEEE 802.16)
WiMAX (IEEE 802.16)WiMAX (IEEE 802.16)
WiMAX (IEEE 802.16)
 
Radio over Fiber Technology for WiMAX Systems
 Radio over Fiber Technology for WiMAX Systems Radio over Fiber Technology for WiMAX Systems
Radio over Fiber Technology for WiMAX Systems
 
Knowledge Representation & Reasoning
Knowledge Representation & ReasoningKnowledge Representation & Reasoning
Knowledge Representation & Reasoning
 
top level view of computer function and interconnection
top level view of computer function and interconnectiontop level view of computer function and interconnection
top level view of computer function and interconnection
 

Último

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Último (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Advance algorithm hashing lec II

  • 2.  Definition: the ideal table data structure is merely an array of some fixed size, containing the elements.  Consist : an array and a mapping function (known as hash function)  Used for performing insertion, deletion and lookup on average in constant time. 2
  • 4. Storage Space Storing k Direct Addressing |U| Store in slot k Hashing m Store in slot h(k)  Advantage: Requires less storage and runs in O(1) time.  Comparison 4
  • 6.  How can we solve the problem of collisions?  Solution 1:Solution 1: ChainingChaining  Solution 2:Solution 2: Open addressingOpen addressing 6
  • 7.  Put all the elements that hash to same slot in a linked list.  Worst case : All n keys hash to the same slot resulting in a linked list of length n, running time: O(n)  Best and AverageAverage time: O(1) 7
  • 9.  Assume simple uniform hashing: each key in table is equally likely to be hashed to any slot  Given n keys and m slots in the table: the load factor α = n/m = average # keys per slot  What will be the average cost of an unsuccessful search for a key? O(1+ α) 9
  • 10.  What will be the average cost of a successful search? A: O(1 + α/2) = O(1 + α) 10
  • 11.  So the cost of searching = O(1 + α)  If the number of keys n is proportional to the number of slots in the table, what is α?  A: α = O(1) ◦ In other words, we can make the expected cost of searching constant if we make α constant 11
  • 12.  Nature of keys  Hash functions  Division method  Multiplication method  Open Addressing (Linear and Quadratic probing, Double hashing) 12
  • 13. Most hash functions assume that universe of keys is the set N = {0, 1, 2,…} of natural numbers If keys are not N, ways to be found to interpret them as N A character key can be interpreted as an integer expressed in suitable Radix notation. 13
  • 14.  Example: The identifier pt might be interpreted as a pair of decimal integers (112, 116) as p = 112 and t = 116 in ASCII notation. What is the problem?  Using a product/addition of ASCII codes is indifferent to the order of characters  Solution: Using 128-radix notation this becomes (112.128) + 116 = 14,452 14
  • 15. A hash function is a mapping between a set of input values (Keys) and a set of integers, known as hash values. Keys Hash values Hash function 15
  • 16.  Rule1: The hash value is fully determined by the data being hashed.  Rule2: The hash function uses all the input data.  Rule3: The hash function uniformly distributes the data across the entire set of possible hash values.  Rule4: The hash function generates very different hash values for similar strings. 16
  • 17. int hash(char *str, int table_size) { int sum=0; //sum up all the characters in the string for(;*str; str++) sum+=*str //return sum mod table_size return sum%table_size; } 17
  • 18.  Rule1: Satisfies, the hash value is fully determined by the data being hashed, the hash value is just the sum of all input characters.  Rule2: Satisfies, Every character is summed. 18
  • 19.  Rule3: Breaks, from looking at it, it is not obvious that it doesn’t uniformly distribute the strings, but if you were to analyze this function for larger input string, you will see certain statistical properties which are bad for a hash function.  Rule4: Breaks, hash the string “CAT”, now hash the string “ACT”, they are the same, a slight variation in the string should result in different hash values, but with this function often they don’t. 19
  • 21. The division method requires two steps. 1. The key must be transformed into an integer. 2. The value must be telescoped into range 0 to m-1 21
  • 22.  We map a key k into one of the m slots by taking the remainder of k divided by m, so the hash function is of form h(k)= k mod m  For example , if m=12, key is 100 then h(k)=100 mod 12= 4.  Advantage? 22
  • 23. M should not be a power of 2, since if m=2p then h(k) is just the p lowest order bits of k. Disadvantage! Key Binary K mod 8 8 1000 0 7 111 7 12 1100 4 34 100010 2 56 111000 0 78 1001110 6 90 1011010 2 23 10111 7 45 101101 5 67 1000011 3 23
  • 24. Unless it is known that probability distribution on keys makes all lower order p-bit patterns equally likely, it is better to make the hash function dependent on all the bits of the key. 24
  • 25.  Power of 10 should be avoided, if application deals with decimal numbers as keys.  Good values of m are primes not close to the exact powers of 2 (or 10). 25
  • 26.  Using a random real number ff in the range (0,1).  The fractional part of the product ff*key yields a number in the range 0 to 1.  When this number is multiplied by m (hash table size), the integer portion of the product gives the hash value in the range 0 to m-1 26
  • 27.  Choose m = 2P  For a constant A, 0 < A < 1:  h(k) =  m (kA - kA)   Value of A should not be close to 0 or 1  Knuth says good value of A is 0.618033  If k=123456, m=10000,and A as above h(k)= 10000.(123456*A- 123456*A) = 10000. (0.0041151) =41 27
  • 28.  For mm ∈∈ (4, 15)(4, 15), and k = {contents first news item www.dawn.com} and mapping function k mod m, and other hash function given on slide-28, plot histogram results using Excel. Contrast and compare results on the basis of mm. June 15, 2010 08:59 am. 28
  • 29.  So far we have studied hashing with chaining, using a linked-list to store keys that hash to the same location.  Maintaining linked lists involves using pointers which is complex and inefficient in both storage and time requirements.  Another option is to store all the keys directly in the table. This is known as open addressing, where collisions are resolved by systematically examining other table indexes, i 0 , i 1 , i 2 , … until an empty slot is located. 29
  • 30. ◦ Another approach for collision resolution. ◦ All elements are stored in the hash table itself (so no pointers involved as in chaining). ◦ To insert: if slot is full, try another slot, and another, until an open slot is found (probing) ◦ To search, follow same sequence of probes as would be used when inserting the element 30
  • 31.  The key is first mapped to a slot:  If there is a collision subsequent probes are performed:  If the offset constant, c and m are not relatively prime, we will not examine all the cells. Ex.: ◦ Consider m=4 and c=2, then only every other slot is checked. When c=1 the collision resolution is done as a linear search. This is known as linear probing. )(index 10 ki h== 0formod)(1 ≥+=+ jmcii jj 0 1 2 3 31
  • 32. HASH_INSERT(T,k) 1 i ← 0 2 repeat j ← h(k,i) 3 if T[j] = NIL 4 then T[j] = k 5 return j 6 else i ← i +1 7 until i = m 8 error “ hash table overflow” 32
  • 33. HASH_SEARCH(T,k) 1 i ← 0 2 repeat j ← h(k,i) 3 if T[j] = k 4 then return j 5 i ← i +1 6 until T[j] = NIL or i = m 7 return NIL 33
  • 34.  Worst case for inserting a key is θ(n)  Worst case for searching is θ(n) 34
  • 35. 35 Whenever there is a collision, one strategy is to look for the next unused slot and use it. Linear Probing.
  • 36. 36 When searching for an empty slot, one has to remember to wrap around (like a circular array)
  • 37.  Even with a good hash function, linear probing has its problems: ◦ The position of the initial mapping i 0 of key k is called the home position of k. ◦ When several insertions map to the same home position, they end up placed contiguously in the table. This collection of keys with the same home position is called a cluster. ◦ As clusters grow, the probability that a key will map to the middle of a cluster increases, increasing the rate of the cluster’s growth. This tendency of linear probing to place items together is known as primary clustering. ◦ As these clusters grow, they merge with other clusters forming even bigger clusters which grow even faster. 37
  • 38. h(k,i) = (h’(k) + c1i + c2i 2 ) mod m for i = 0,1,…,m − 1. ◦ Leads to a secondary clustering (milder form of clustering) ◦ The clustering effect can be improved by increasing the order to the probing function (cubic). However the hash function becomes more expensive to compute 38
  • 39.  Recall that in open addressing the sequence of probes follows  We can solve the problem of primary clustering in linear probing by having the keys which map to the same home position use differing probe sequences. In other words, the different values for c should be used for different keys.  Double hashing refers to the scheme of using another hash function for c 0formod)(1 ≥+=+ jmcii jj 1)(0and0formod))(( 221 −≤<≥+=+ mkjmkii jj hh 39
  • 40.  Lecture is prepared using information from Chapter 11 “Hash Tables” of book “Introduction to Algorithms” By Thomas H. Cormen et al  Book is available in the library, make photocopy of this chapter 40
  • 41. 41 Compare hash based search with Binary search on best case basis. What value of load factor will result in fewer collisions?