Docsity

Hashing-Data Structures and Representation-Lecture Handout, Exercises of Data Structures and Algorithms

Toggle sidebar
bg1
Hashing
What is Hashing?
A process whereby an item is placed into a structure based on a key-to-
address transformation.
Hashing is a way of performing optimal searches and retrievals. Essentially,
it increases speed, betters ease of transfer, improves retrieval, optimizes
searching of data, reduces overhead. Thus, one objective of hashing is to
optimize disk accesses and packing density. The ultimate goal, however, is
to minimize disk space and access time by inserting and retrieving a record
from the table in only one seek.
The Advantages and Disadvantages of Hashing
One advantage of hashing is that no index storage space is required,
whereas inserting into other structures such as trees does normally require
an index. Such an index could be in the form of a queue. In addition,
hashing provides the advantage of rapid updates.
One disadvantage to using hashing as an insert and retrieval tool is that it
usually lacks locality and sequential retrieval by key. Consequently, insertion
and retrieval are more random. Another disadvantage is the inability to use
duplicate keys. This is a problem when key values are very small (i.e. one or
two digits).
Some Real World Applications of Hashing
Database indexing
• Some database systems have a separate file of files known as
indexes. When a request is made for data, the key information is
first found in the appropriate index file which references the
exact record location of the data in the database file. The key
information in the index file is sometimes stored as a hashed
value.
Symbol tables e.g. FORTRAN variable names
• It is very important in any language that the references to
variables can be looked up quickly. This speeds up the execution
of the program.
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Hashing-Data Structures and Representation-Lecture Handout and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Hashing

What is Hashing?

A process whereby an item is placed into a structure based on a key-to- address transformation.

Hashing is a way of performing optimal searches and retrievals. Essentially, it increases speed, betters ease of transfer, improves retrieval, optimizes searching of data, reduces overhead. Thus, one objective of hashing is to optimize disk accesses and packing density. The ultimate goal, however, is to minimize disk space and access time by inserting and retrieving a record from the table in only one seek.

The Advantages and Disadvantages of Hashing

One advantage of hashing is that no index storage space is required, whereas inserting into other structures such as trees does normally require an index. Such an index could be in the form of a queue. In addition, hashing provides the advantage of rapid updates.

One disadvantage to using hashing as an insert and retrieval tool is that it usually lacks locality and sequential retrieval by key. Consequently, insertion and retrieval are more random. Another disadvantage is the inability to use duplicate keys. This is a problem when key values are very small (i.e. one or two digits).

Some Real World Applications of Hashing

Database indexing

  • Some database systems have a separate file of files known as indexes. When a request is made for data, the key information is first found in the appropriate index file which references the exact record location of the data in the database file. The key information in the index file is sometimes stored as a hashed value.

Symbol tables e.g. FORTRAN variable names

  • It is very important in any language that the references to variables can be looked up quickly. This speeds up the execution of the program.

File and Directory hashing in high performance file systems

  • Two complementary techniques are often used to improve the performance of file access. One is caching which simply saves information in memory. The second is hashing. In large file systems that are typical of file servers, it is common to use hashing to improve performance. Looking up the file location in memory is much quicker using hashing than most other methods.

A Student Number Example:

University student information often needs to be stored and retrieved. The data to be stored (student numbers) is normally in integer format or numeric string format which can easily be converted to integers. The length of the integers is normally uniform for all students (e.g. a 6 digit number). In this case the range of values would be from 100000 to 999999.

The first thing to do is to set up a hash table. If we set up a table with 999,999 positions, we will have a huge table with many positions that will never be used. The values from 0 to 99,999 will never be used, as well there are values which have not yet been assigned to students. A better method would be to create a hash table whose size is the number of students.

Next, we must establish a hashing algorithm. A simple algorithm would be to simply subtract 100,000 from any student number. This will eliminate all of the values between 900,000 and 999,999, inclusive. The student number 100000 will be stored in position 0 in the hash table and so on. This still requires a very large hash table that will have many unused positions.

Let us assume that we are starting out as a fairly new university with a population of 100 students. Since a hash table with 100 positions does not require very much storage space, we will create a table of that size.

In order to store the student numbers in our hash table, we will use an algorithm that uses the modulus function to obtain a value from 0 to 99. 'y mod 100'. We will divide any student number by 100 and take the remainder as being the hash table position. If the students are assigned numbers sequentially beginning at 100000 and extending to 100099, we will have a 1 to 1 correspondence between student number and hash table position.

Suppose we start a new semester and 10 new students attend our university. These students will be assigned the numbers 100100 through

  1. When we use our hashing function, these number will correspond to

1- Truncation Method This is the easiest method for computing key. Here we take only part of key as address; it can be some rightmost digit or leftmost digit. Let us take some 8 digit keys 82394561,87139465,83567271, Now we can take the number of rightmost digits or leftmost digits based on the size of hash table. Suppose table size is 100 then take the 2 rightmost digits for getting the hash table address. Suppose we are taking rightmost digits address then the addresses will be 61, 65, 71, and 28. We can see that it is very easy to compute but chance of collision is more because last two digits can be same in different keys.

2- Mid-Square Method In mid square method we square the key, after getting number we take some digits form the middle of that number as an address. Let us take some 4 digit number as a key: 1337, 1273, 1391, 1026

Now take the square: 1787569, 1620529, 1934881, 105276

Now take the 3rd^ and 4th^ digit from each number and that will be the hash address of these keys. Let us assume here table size is

  1. So the hash address for these keys will be 75, 05, 48 and

3- Folding Method On easiest way to compute the key is to break the key into pieces, add them and get the hash address. Let us take some 8 digits key- 82394561, 87139465, 83567271, 85943228 Now chop them in pieces of 3, 2 and 3 digits and add them. So the address will be 82394561 = 823+94+561 = 1478 87139465= 871+39+465 = 1375 83567271= 835+67+271 = 1173 85943228= 859+43+228 = 1130 Now truncate them up to the digit based on the size of hash table. Suppose the table size is 1000. So the hash address can be from 0 to 999. So we will truncate here the higher digit of number. Now the hash address of keys will be as-

H(82394561)=

H(87139465)= 375

H(83567271)= 173

H(85943228)= 130

4- Modular Method

Modulus method is best way for getting address from key. Take the key, do the modulus operation and get the remainder as addresss fhro hash table. In C this operation is provided by % operator. It ensures that the address will be in the range of hash table. Here only one thing we should keep in mind that table size not to be in power of two, otherwise it will give more collision. The best way to minimize the collision is to take the table size a prime number. Let us take some keys.

82394561,87139465,83567271,

Let us take the table size is 97 then address will be-

82394561%97 = 45 87139465%97 = 0 83567271%97 = 25 85943228%97 = 64

So here the hash address of above keys will be 45, 0, 25 and 64.

5- Hash Function for Floating Point Numbers 6- Hash Function for Strings

Collision

A failed attempt to insert an item in a table because there is already an item in the slot the new item has hashed to.

Once a space in the table is occupied, no other item can be placed there unless the existing item is deleted first. Think of a collision as " interference ." A practical example might help solidify this idea.

You have a class in a classroom that seats 20 (20 would thus be the 'table size'). Today is the day of your midterm, so everyone shows up. The first person to show up could choose a seat at random because they would all be empty. This random seating could be done for the first 12 people or so. Then, the free seats would gradually diminish. Out of the 20 people in

backup list of elements that would have otherwise been inserted into a given position had that position been empty.

For example, let us assume that position x in the table has been previously filled by a hash insertion. Now, let us assume that the element we wish to insert into the hash table resolves to position x as well. We cannot simply overwrite the element already in position x. Each hash table entry requires a pointer to a linked list of records that can hold the element data. This list is only used when a collision occurs. The element that we are inserting, E, is then appended onto the list associated with the target position, x. The result is a linked list associated with each table position, each list containing only those elements that collided on that table position with an element already in that position.