CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing...
Transcript of CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing...
![Page 1: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/1.jpg)
Hashing
14 September 2020 OSU CSE 1
![Page 2: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/2.jpg)
Performance of Set (and Map)
• How long does it take to execute each of the methods of Set2 (similarly Map2), which use a Queue as the data representation?
• Assume that each call to a Queue kernel method executes in constant time, i.e., that the duration of a call is independent of the values of all the arguments, including the receiver
14 September 2020 OSU CSE 2
![Page 3: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/3.jpg)
Standard Methods
• For almost every type in the OSU CSE components, including Queue, each of the three Standard methods (newInstance, clear, and transferFrom) takes constant time to execute
14 September 2020 OSU CSE 3
![Page 4: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/4.jpg)
Queue Kernel Methods
14 September 2020 OSU CSE 4
Method (Op) Execution Time (TOp)
enqueue(x) Tenqueue = c1
dequeue(x) Tdequeue = c2
length Tlength = c3
![Page 5: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/5.jpg)
Set Kernel Methods
14 September 2020 OSU CSE 5
Method Execution Time
add(x)
remove(x)
contains(x)
size
![Page 6: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/6.jpg)
Set Kernel Methods
14 September 2020 OSU CSE 6
Method Execution Time
add(x)
remove(x)
contains(x)
size
Look at the method body in Set2, and
figure out how much work it does...
![Page 7: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/7.jpg)
Set Kernel Methods
14 September 2020 OSU CSE 7
Method Execution Time
add(x) c4
remove(x)
contains(x)
size
It simply enqueuesits argument; plus,
there is some constant-time
overhead just to make the call to add.
![Page 8: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/8.jpg)
Set Kernel Methods
14 September 2020 OSU CSE 8
Method Execution Time
add(x) c4
remove(x)
contains(x)
size
Look at the method body in Set2, and
figure out how much work it does...
![Page 9: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/9.jpg)
Set Kernel Methods
14 September 2020 OSU CSE 9
Method Execution Time
add(x) c4
remove(x) c5•|this| + c6
contains(x)
size
It has to search through a Queuecontaining all the Set’s elements.
![Page 10: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/10.jpg)
Set Kernel Methods
14 September 2020 OSU CSE 10
Method Execution Time
add(x) c4
remove(x) c5•|this| + c6
contains(x)
size
Raising the question: a worst case, an
average case, ...?
![Page 11: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/11.jpg)
Set Kernel Methods
14 September 2020 OSU CSE 11
Method Execution Time
add(x) c4
remove(x) c5•|this| + c6
contains(x) c7•|this| + c8
size c9
![Page 12: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/12.jpg)
Linear Search
• Linear search is the algorithm that examines—potentially—every item in a collection (e.g., code like moveToFront in Set2 and Map2) until it finds what it’s looking for– The name reflects the fact that its execution
time is a linear function of the size of the collection (e.g., c7•|this| + c8)
14 September 2020 OSU CSE 12
![Page 13: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/13.jpg)
Some Common Execution Times
14 September 2020 OSU CSE 13
T(n)
n
Execution (“running”) time of some code as
a function of the “size” of its input.
![Page 14: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/14.jpg)
Some Common Execution Times
14 September 2020 OSU CSE 14
T(n)
n
“Size” of the input for some code.
![Page 15: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/15.jpg)
Some Common Execution Times
14 September 2020 OSU CSE 15
T(n)
n
Constant time, e.g.,c
![Page 16: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/16.jpg)
Some Common Execution Times
14 September 2020 OSU CSE 16
T(n)
n
Log time, e.g.,a•log(n) + b
![Page 17: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/17.jpg)
Some Common Execution Times
14 September 2020 OSU CSE 17
T(n)
n
Linear time, e.g.,a•n + b
![Page 18: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/18.jpg)
Some Common Execution Times
14 September 2020 OSU CSE 18
T(n)
n
n log n time, e.g.,a•n•log(n) + b
![Page 19: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/19.jpg)
Some Common Execution Times
14 September 2020 OSU CSE 19
T(n)
n
Quadratic time, e.g.,a•n2 + b•n + c
![Page 20: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/20.jpg)
Some Common Execution Times
14 September 2020 OSU CSE 20
T(n)
n
Exponential time, e.g.,2n
![Page 21: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/21.jpg)
Faster Execution?
• Option 1 (preferred): Reduce the order of magnitude of the running time– Example: Change from quadratic time to
linear time, or linear time to log time• Option 2 (better than nothing): Reduce the
constant factor that multiplies the dominant term of the running time– Example: Change from a larger slope for a
linear function to a smaller slope
14 September 2020 OSU CSE 21
![Page 22: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/22.jpg)
Faster Execution
14 September 2020 OSU CSE 22
T(n)
n
Reduce by order of magnitude:a•n + b
![Page 23: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/23.jpg)
Faster Execution
14 September 2020 OSU CSE 23
T(n)
n
Reduce by order of magnitude:c•log(n) + d
![Page 24: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/24.jpg)
Faster Execution
14 September 2020 OSU CSE 24
T(n)
n
Reduce by a constant factor:
a•n + b
![Page 25: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/25.jpg)
Faster Execution
14 September 2020 OSU CSE 25
T(n)
n
Reduce by a constant factor:(a/10)•n + b
![Page 26: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/26.jpg)
Example: Faster Linear Search
• Goal: Reduce the constant factor in the execution time of linear search, i.e., reduce it from a•n + b to something like(a/10)•n + b
• Approach: Reduce the number of items that need to be examined to find the one you’re looking for, because, e.g.:(a/10)•n + b = a•(n/10) + b
14 September 2020 OSU CSE 26
![Page 27: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/27.jpg)
Hashing: The Intuition
• Instead of searching through all the items, store the items in many smaller bucketsand search through only one bucket that1. Can be quickly identified, and2. Must contain the item you’re looking for
14 September 2020 OSU CSE 27
![Page 28: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/28.jpg)
Hashing: The Intuition
• Instead of searching through all the items, store the items in many smaller bucketsand search through only one bucket that1. Can be quickly identified, and2. Must contain the item you’re looking for
14 September 2020 OSU CSE 28
![Page 29: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/29.jpg)
Hashing: The Intuition
• Instead of searching through all the items, store the items in many smaller bucketsand search through only one bucket that1. Can be quickly identified, and2. Must contain the item you’re looking for
14 September 2020 OSU CSE 29
![Page 30: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/30.jpg)
How To Identify The Bucket
• Suppose you need to search through nitems of type T, and you decide to organize the items into m buckets
• Given x of type T, compute from it some integer value h(x)
• Look in bucket number h(x) mod m
14 September 2020 OSU CSE 30
![Page 31: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/31.jpg)
How To Identify The Bucket
• Suppose you need to search through nitems of type T, and you decide to organize the items into m buckets
• Given x of type T, compute from it some integer value h(x)
• Look in bucket number h(x) mod m
14 September 2020 OSU CSE 31
The buckets have indices 0, 1, ..., m-1 in an array of buckets called a
hashtable.
![Page 32: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/32.jpg)
How To Identify The Bucket
• Suppose you need to search through nitems of type T, and you decide to organize the items into m buckets
• Given x of type T, compute from it some integer value h(x)
• Look in bucket number h(x) mod m
14 September 2020 OSU CSE 32
The function that maps each value of type T to an integer is called the
hash function.
![Page 33: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/33.jpg)
How To Identify The Bucket
• Suppose you need to search through nitems of type T, and you decide to organize the items into m buckets
• Given x of type T, compute from it some integer value h(x)
• Look in bucket number h(x) mod m
14 September 2020 OSU CSE 33
By “reducing” the hash function result modulo m, you are guaranteed to get the index of some bucket.
![Page 34: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/34.jpg)
How To Identify The Bucket
• Suppose you need to search through nitems of type T, and you decide to organize the items into m buckets
• Given x of type T, compute from it some integer value h(x)
• Look in bucket number h(x) mod m
14 September 2020 OSU CSE 34
The insight for hashing:if you put the item in this bucket when you store it, then it is the
only place you need to look for it when searching.
![Page 35: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/35.jpg)
Set Representation With Hashing
• Suppose the data representation for a new Set implementation, say Set4, uses an instance variable like this:/**
* Buckets for hashing.
*/
private Set<T>[] hashTable;
14 September 2020 OSU CSE 35
![Page 36: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/36.jpg)
Set Representation With Hashing
• Suppose the data representation for a new Set implementation, say Set4, uses an instance variable like this:/**
* Buckets for hashing.
*/
private Set<T>[] hashTable;
14 September 2020 OSU CSE 36
Abstract Set:
Data representation using several “little Sets”:
![Page 37: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/37.jpg)
Set Representation With Hashing
• Suppose the data representation for a new Set implementation, say Set4, uses an instance variable like this:/**
* Buckets for hashing.
*/
private Set<T>[] hashTable;
14 September 2020 OSU CSE 37
Can we really do this: use Sets in the
representation of a Set? Why is it not circular?
![Page 38: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/38.jpg)
Details
• Suppose further (for illustration purposes) that:– T = Integer– h(x) = x– m = |$this.hashTable| = 3
14 September 2020 OSU CSE 38
![Page 39: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/39.jpg)
Details
• Suppose further (for illustration purposes) that:– T = Integer– h(x) = x– m = |$this.hashTable| = 3
14 September 2020 OSU CSE 39
Here and in upcoming contracts, we’ll model
Java arrays as mathematical strings.
![Page 40: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/40.jpg)
Examples
14 September 2020 OSU CSE 40
Abstract(this)
Concrete($this.hashTable)
{} <{}, {}, {}>
{13}
{5, 13}
{-2, 13}
![Page 41: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/41.jpg)
Examples
14 September 2020 OSU CSE 41
Abstract(this)
Concrete($this.hashTable)
{} <{}, {}, {}>
{13} <{}, {13}, {}>
{5, 13}
{-2, 13}
![Page 42: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/42.jpg)
Examples
14 September 2020 OSU CSE 42
Abstract(this)
Concrete($this.hashTable)
{} <{}, { }, {}>
{13} <{}, {13}, {}>
{5, 13}
{-2, 13}
Why is 13 in bucket 1?h(x) mod m= h(13) mod 3= 13 mod 3= 1
![Page 43: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/43.jpg)
Examples
14 September 2020 OSU CSE 43
Abstract(this)
Concrete($this.hashTable)
{} <{}, {}, {}>
{13} <{}, {13}, {}>
{5, 13} <{}, {13}, {5}>
{-2, 13}
![Page 44: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/44.jpg)
Examples
14 September 2020 OSU CSE 44
Abstract(this)
Concrete($this.hashTable)
{} <{}, {}, {}>
{13} <{}, {13}, {}>
{5, 13} <{}, {13}, {5}>
{-2, 13} <{}, {-2, 13}, {}>
![Page 45: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/45.jpg)
Two-Level Thinking
14 September 2020 OSU CSE 45
{13}{} {5, 13}
{-2, 13}
<{}, {13}, {}>
<{}, {}, {}><{}, {-2, 13}, {}>
<{}, {13}, {5}>
![Page 46: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/46.jpg)
The hashCode Method
• In Java, the type Object defines this instance method to compute h, i.e., as the programmatic version of a hash function: public int hashCode()
• As a best practice, nearly every type should override the default implementation of this method, which by default rarely meets the requirements necessary for the hashing idea to work!
14 September 2020 OSU CSE 46
![Page 47: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/47.jpg)
Requirements
• The only requirement for the hashing idea to give correct behavior is that the hash function h should be a total function
• In programming terms:– hashCode has no precondition (so everyPhoneNumber value has an int hash value)
– hashCode always returns the same int hash value for the same PhoneNumber value
14 September 2020 OSU CSE 47
![Page 48: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/48.jpg)
Requirements
• The only requirement for the hashing idea to give correct behavior is that the hash function h should be a total function
• In programming terms:– hashCode has no precondition (so everyPhoneNumber value has an int hash value)
– hashCode always returns the same int hash value for the same PhoneNumber value
14 September 2020 OSU CSE 48
This is the part that is not satisfied by the default
implementation of hashCodethat comes with Object.
![Page 49: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/49.jpg)
Good Hash Functions
• To result in good performance of the hashing idea (not just correct behavior), hashCode should also:– Give different output values for different input
values– Execute in constant time
14 September 2020 OSU CSE 49
![Page 50: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/50.jpg)
Good Hash Functions
• To result in good performance of the hashing idea (not just correct behavior), hashCode should also:– Give different output values for different input
values– Execute in constant time
14 September 2020 OSU CSE 50
Why can this not always be achieved?
![Page 51: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/51.jpg)
Example
• Suppose type T is PhoneNumber, modeled as follows:PHONE_NUMBER_MODEL isstring of integer
exemplar p
constraint|p| = 10 andentries(p) is subset of{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
14 September 2020 OSU CSE 51
![Page 52: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/52.jpg)
Example
• Suppose type T is PhoneNumber, modeled as follows:PHONE_NUMBER_MODEL isstring of integer
exemplar p
constraint|p| = 10 andentries(p) is subset of{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
14 September 2020 OSU CSE 52
Maybe you need aSet<PhoneNumber> in developing a “contacts
app” for a phone.
![Page 53: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/53.jpg)
Possible Hash Functions
• The length of the phone-number string:h("6142926446") = 10
• The numerical value of the area code (first three digits) of the phone-number string:h("6142926446") = 614
• The numerical value of the last four digits of the phone-number string:h("6142926446") = 6446
14 September 2020 OSU CSE 53
![Page 54: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/54.jpg)
Possible Hash Functions
• The length of the phone-number string:h("6142926446") = 10
• The numerical value of the area code (first three digits) of the phone-number string:h("6142926446") = 614
• The numerical value of the last four digits of the phone-number string:h("6142926446") = 6446
14 September 2020 OSU CSE 54
There are many more options as well; how do you choose one?
![Page 55: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/55.jpg)
An Empirical Matter
• How well hashing distributes the data among buckets depends, in part, on the data themselves
• Your worst enemy, knowing your hash function, could always provide data that would result in no performance gain over linear search– Everything might fall into one bucket...
14 September 2020 OSU CSE 55
![Page 56: CSE 2231 - Hashingweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/09.Hashing.pdfhashing idea to work! 8 February 2019 OSU CSE 45. Requirements • The only requirement for](https://reader034.fdocuments.in/reader034/viewer/2022050501/5f93aaf46c44737ee6573f94/html5/thumbnails/56.jpg)
Resources• Big Java (4th ed), Sections 16.3-16.4
– https://library.ohio-state.edu/record=b8540788~S7
14 September 2020 OSU CSE 56