Hashing | Algorithms - Computer Science Engineering (CSE) PDF Download

Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data.

Thus, it becomes a data structure in which insertion and search operations are very fast irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash technique to generate an index where an element is to be inserted or is to be located from.

Hashing

Hashing is a technique to convert a range of key values into a range of indexes of an array. We're going to use modulo operator to get a range of key values. Consider an example of hash table of size 20, and the following items are to be stored. Item are in the (key,value) format.

Hashing,Algorithms,GATE,CSE,ITE

  • (1,20)
  • (2,70)
  • (42,80)
  • (4,25)
  • (12,44)
  • (14,32)
  • (17,11)
  • (13,78)
  • (37,98)
     
Sr. No.KeyHashArray Index
111 % 20 = 11
222 % 20 = 22
34242 % 20 = 22
444 % 20 = 44
51212 % 20 = 1212
61414 % 20 = 1414
71717 % 20 = 1717
81313 % 20 = 1313
93737 % 20 = 1717

Linear Probing

As we can see, it may happen that the hashing technique is used to create an already used index of the array. In such a case, we can search the next empty location in the array by looking into the next cell until we find an empty cell. This technique is called linear probing.
 

Sr. No.KeyHashArray IndexAfter Linear Probing, Array Index
111 % 20 = 111
222 % 20 = 222
34242 % 20 = 223
444 % 20 = 444
51212 % 20 = 121212
61414 % 20 = 141414
71717 % 20 = 171717
81313 % 20 = 131313
93737 % 20 = 171718


Basic Operations

Following are the basic primary operations of a hash table.

  • Search − Searches an element in a hash table.

  • Insert − inserts an element in a hash table.

  • delete − Deletes an element from a hash table.

DataItem

Define a data item having some data and key, based on which the search is to be conducted in a hash table.

struct DataItem {
int data;
int key;
};

Hash Method

Define a hashing method to compute the hash code of the key of the data item.

int hashCode(int key){
return key % SIZE;
}

Search Operation

Whenever an element is to be searched, compute the hash code of the key passed and locate the element using that hash code as index in the array. Use linear probing to get the element ahead if the element is not found at the computed hash code.

Example

struct DataItem *search(int key) {
//get the hash
int hashIndex = hashCode(key);

//move in array until an empty
while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];

//go to next cell
++hashIndex;

//wrap around the table
hashIndex %= SIZE;
}

return NULL;
}

Insert Operation

Whenever an element is to be inserted, compute the hash code of the key passed and locate the index using that hash code as an index in the array. Use linear probing for empty location, if an element is found at the computed hash code.

Example

void insert(int key,int data) {
   struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
   item->data = data;  
   item->key = key;     

   //get the hash 
   int hashIndex = hashCode(key);

   //move in array until an empty or deleted cell
   while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
      //go to next cell
      ++hashIndex;
        
      //wrap around the table
      hashIndex %= SIZE;
   }
    
   hashArray[hashIndex] = item;        
}

Delete Operation

Whenever an element is to be deleted, compute the hash code of the key passed and locate the index using that hash code as an index in the array. Use linear probing to get the element ahead if an element is not found at the computed hash code. When found, store a dummy item there to keep the performance of the hash table intact.

Example

struct DataItem* delete(struct DataItem* item) {
int key = item->key;

//get the hash
int hashIndex = hashCode(key);

//move in array until an empty
while(hashArray[hashIndex] !=NULL) {

if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];

//assign a dummy item at deleted position
hashArray[hashIndex] = dummyItem;
return temp;
}

//go to next cell
++hashIndex;

//wrap around the table
hashIndex %= SIZE;
}

return NULL;
}
The document Hashing | Algorithms - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Algorithms.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
81 videos|80 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Hashing - Algorithms - Computer Science Engineering (CSE)

1. What is hashing in computer science engineering (CSE)?
Ans. Hashing in computer science engineering (CSE) is a technique used to map data to a fixed-size array called a hash table. It is used to efficiently store and retrieve data by converting the data into an index of an array using a hash function. This allows for quick access and retrieval of data based on its unique key.
2. How does hashing work in computer science engineering (CSE)?
Ans. Hashing works by taking an input, such as a data value or key, and applying a hash function to it. The hash function converts the input into a fixed-size hash code, which is used as an index in the hash table. The data is then stored at this index, allowing for fast retrieval in constant time.
3. What are the advantages of using hashing in computer science engineering (CSE)?
Ans. There are several advantages of using hashing in computer science engineering (CSE): - Fast data retrieval: Hashing allows for constant-time retrieval of data, as the data can be directly accessed using the hash code as an index. - Efficient storage: Hashing allows for efficient storage of data, as it eliminates the need for sequential searching or sorting of data. - Collision resolution: Hashing techniques also provide methods to handle collisions, which occur when two different inputs produce the same hash code. These collisions can be resolved using techniques such as chaining or open addressing. - Security: Hash functions are widely used in computer security, such as password hashing, digital signatures, and data integrity checks.
4. What are the different types of hash functions used in computer science engineering (CSE)?
Ans. There are various types of hash functions used in computer science engineering (CSE), including: - Division method: This method calculates the hash code by dividing the key by the size of the hash table and taking the remainder as the hash code. - Multiplication method: This method involves multiplying the key by a constant value between 0 and 1, and then taking the fractional part of the result as the hash code. - Folding method: This method involves dividing the key into several parts and performing some arithmetic operations on those parts to obtain the hash code. - Digit extraction method: This method extracts certain digits from the key and uses them to calculate the hash code. - Mid-square method: This method involves squaring the key and then extracting a portion of the resulting value as the hash code.
5. How is collision resolution handled in hashing in computer science engineering (CSE)?
Ans. Collision resolution in hashing is the process of handling situations where two different inputs produce the same hash code. There are various methods to handle collisions, including: - Chaining: In this method, each index of the hash table contains a linked list of data elements. When a collision occurs, the data is stored in the linked list at the corresponding index. - Open addressing: In this method, when a collision occurs, the hash function is applied again to find the next available index in the hash table. This process continues until an empty slot is found. - Linear probing: This is a type of open addressing where the next available index is searched linearly, i.e., by incrementing the index by a fixed value until an empty slot is found. - Quadratic probing: This is another type of open addressing where the next available index is searched using a quadratic function, which involves incrementing the index by a quadratic value until an empty slot is found. - Double hashing: This method involves applying a second hash function to the key when a collision occurs, which helps in finding the next available index in the hash table.
81 videos|80 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Viva Questions

,

Hashing | Algorithms - Computer Science Engineering (CSE)

,

Summary

,

Semester Notes

,

study material

,

Extra Questions

,

Exam

,

Important questions

,

mock tests for examination

,

ppt

,

Hashing | Algorithms - Computer Science Engineering (CSE)

,

Previous Year Questions with Solutions

,

Objective type Questions

,

past year papers

,

shortcuts and tricks

,

Hashing | Algorithms - Computer Science Engineering (CSE)

,

Sample Paper

,

video lectures

,

Free

,

practice quizzes

,

pdf

,

MCQs

;