0% found this document useful (0 votes)
287 views13 pages

Hashing

Hashing is a technique for inserting, deleting, and finding data in constant average time using a hash table. A hash function maps data to integer indices in the table. Collisions occur when multiple data items hash to the same index, and must be resolved. Hash tables provide fast retrieval and are commonly used in databases, symbol tables, and caches.

Uploaded by

DrJayakanthan N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
287 views13 pages

Hashing

Hashing is a technique for inserting, deleting, and finding data in constant average time using a hash table. A hash function maps data to integer indices in the table. Collisions occur when multiple data items hash to the same index, and must be resolved. Hash tables provide fast retrieval and are commonly used in databases, symbol tables, and caches.

Uploaded by

DrJayakanthan N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

An Introduction to Hashing.

What is hashing?
Hashing is a method of inserting data into a
table.
Tables can be implemented in many ways.
Examples include a fixed array (limiting
number of elements), array of linked lists
(potentially unlimited number of elements)
Hashing is a technique used for performing
insertions, deletions and finds in constant
average time (i.e. O(1))
Why use hashing?

There is the potential to retrieve data faster.

Using the proper hash function will distribute


the elements throughout the table. To
retrieve the element, apply the hash
function until it is found or it is clear that it
was not found.
Hash Functions (1)
Let U be the universe of possible keys for a
set of elements. It is generally assumed that
all elements have unique integer keys.
Let m be the size of our array that will hold
the elements.
A hash function h(key) is a function that maps
U to Zm.
Hash Functions (2)
If we can define a one-to-one mapping from
U to Zm, h(k) is called a perfect hashing
function.
If we cannot define a perfect hashing
function, we must deal with collisions.
Hash Functions (3)
A collision is defined when multiple keys map onto
the same table index.
There are many ways to handle collisions. These
include “chaining”, “double hashing”, “linear
probing”, “quadratic probing”, “random probing”,
etc.
Example 1: Introduction to
Name Hashing (cont'd)
ID h(r) = id % 13
Al-Otaibi, Ziyad 985926 6
Al-Turki, Musab Ahmad Bakeer 970876 10
Al-Saegh, Radha Mahdi 980962 8
Al-Shahrani, Adel Saad 986074 11
Al-Awami, Louai Adnan Muhammad 970728 5
Al-Amer, Yousuf Jauwad 994593 2
Al-Helal, Husain Ali AbdulMohsen 996321 1

0 1 2 3 4 5 6 7 8 9 10 11
12
Yousuf
Husain

Musab
Radha
Ziyad
Louai

Adel
Hash Tables
• There are two types of Hash Tables: Open-addressed Hash Tables and Separate-
Chained Hash Tables.

• An Open-addressed Hash Table is a one-dimensional array indexed by


integer values that are computed by an index function called a hash function.

• A Separate-Chained Hash Table is a one-dimensional array of linked lists indexed


by integer values that are computed by an index function called a hash function.

• Hash tables are sometimes referred to as scatter tables..\

• Typical hash table operations are:

· Initialization.
· Insertion.
· Searching
· Deletion.
Types of Hashing
• There are two types of hashing :
1. Static hashing: In static hashing, the hash
function maps search-key values to a fixed set of
locations.
2. Dynamic hashing: In dynamic hashing a
hash table can grow to handle more items. The
associated hash function must change as the table
grows.
• The load factor of a hash table is the ratio of the
number of keys in the table to the size of the hash
table.
Hash Functions
• A hash function, h, is a function which
transforms a key from a set, K, into an index in
a table of size n:
A key can be a number, a string, a record etc.
• The size of the set of keys, |K|, to be
relatively very large.
• It is possible for different keys to hash to the
same array location.
• This situation is called collision and the
colliding keys are called synonyms.
Hash Functions (cont’d)

• A good hash function should:

· Minimize collisions.
· Be easy and quick to compute.
· Distribute key values evenly in the hash
table.
· Use all the information provided in the
key.
Some Applications of Hash Tables
• Database systems: Specifically, those that require efficient random access. Generally,
database systems try to optimize between two types of access methods: sequential and
random. Hash tables are an important part of efficient random access because they
provide a way to locate data in a constant amount of time.

• Symbol tables: The tables used by compilers to maintain information about symbols from
a program. Compilers access information about symbols frequently. Therefore, it is
important that symbol tables be implemented very efficiently.

• Data dictionaries: Data structures that support adding, deleting, and searching for data.
Although the operations of a hash table and a data dictionary are similar, other data
structures may be used to implement data dictionaries. Using a hash table is particularly
efficient.

• Network processing algorithms: Hash tables are fundamental components of several


network processing algorithms and applications, including route lookup, packet
classification, and network monitoring.

• Browser Cashes: Hash tables are used to implement browser cashes.


Problems for Which Hash Tables are not Suitable

1. Problems for which data ordering is required.


Because a hash table is an unordered data structure, certain operations are difficult and
expensive. Range queries, proximity queries, selection, and sorted traversals are possible
only if the keys are copied into a sorted data structure. There are hash table
implementations
that keep the keys in order, but they are far from efficient .

2. Problems having multidimensional data.

3. Prefix searching especially if the keys are long and of


variable-lengths.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy