Beruflich Dokumente
Kultur Dokumente
STIA2024
Data Structures & Algorithm
Analysis
1
Chapter Contents
What is Hashing?
Hash Functions
Computing Hash Codes
Compression a Hash Code into an Index for
the Hash Table
Resolving Collisions
Open Addressing with Linear Probing
Open Addressing with Quadratic Probing
Separate Chaining
2
Learning Objective
To describe the basic idea of hashing,
To describe the purpose of a hash table, and a hash
function,
To describe how a hash function compresses a hash
code into an index to hash table,
To explain what collisions are and why they occur,
To describe open addressing as a method to resolve
collisions,
To describe linear probing, and quadratic probingas
particular open addressing schemes,
To describe separate chaining as method to resolve
collisions, and
To describe the relative efficiencies of various
3
collisions resolution techniques.
Chapter Contents (ctd.)
Efficiency
The Load Factor
The Cost of Open Addressing
The Cost of Separate Chaining
4
What is Hashing?
A technique that determines an index or location
for storage of an item in a data structure
The hash function receives the search key
Returns the index of an element in an array
called the hash table
The index is known as the hash index
Hashing can be excellent choice when searching is
the primary task.
A technique that ideally can result in O(1) search
time.
A perfect hash function maps each search key into
a different integer suitable as an index to the hash
table
5
What is Hashing?
7
What is Hashing?
9
Computing Hash Codes
We will override the hashCode method of
Object
Guidelines
If a class overrides the method equals, it should
override hashCode
If the method equals considers two objects equal,
hashCode must return the same value for both
objects
If an object invokes hashCode more than once
during execution of program on the same data, it
must return the same hash code
If an object's hash code during one execution of a
program can differ from its hash code during
10
another execution of the same program
Computing Hash Codes
The hash code for a string, s
int hash = 0;
int n = s.length();
for (int i = 0; i < n; i++)
hash = g * hash + s.charAt(i); // g is a positive constant
13
Open Addressing with Linear
Probing
Open addressing scheme locates alternate
location in hash table that is available, or
open.
Locating an open location in a hash table is
called probing.
Linear probing
Resolves a collision by examining
consecutive locations in hash table,
beginning at the original hash index and
locating the next available location.
If collision occurs at hashTable[k], look
successively at location k + 1, k + 2, …
14
Open Addressing with Linear
Probing
18
Open Addressing with Linear
Probing
19
Open Addressing with Linear
Probing
22
Open Addressing, Quadratic
Probing
23
Separate Chaining
Alter the structure of the hash table
Each location can represent multiple
values
Each location called a bucket
Bucket can be a/an
List
Sorted list
Chain of linked nodes
Array
Vector
Resolving collisions by using buckets
that are linked chains. 24
Separate Chaining
29
Efficiency Observations
30
Load Factor
31
Load Factor
32
Cost of Open Addressing
Note: Reasonable
efficiency requires
only < 0.5
Note: Reasonable
efficiency requires
only < 1
36
Conclusion
Q & A Session
37