• No results found

hashing.docx

N/A
N/A
Protected

Academic year: 2020

Share "hashing.docx"

Copied!
14
0
0

Loading.... (view fulltext now)

Full text

(1)

OBJECT: Introduction and implementation of hashing (hash tables)

In computing, a hash table (hash map) is a data structure which implements an associative  array abstract data type, a structure that can map keys to values. A hash table uses a hash  function to compute an index into an array of buckets or slots, from which the desired value can  be found.

Ideally, the hash function will assign each key to a unique bucket, but most hash table designs  employ an imperfect hash function, which might cause hash collisions where the hash function  generates the same index for more than one key. Such collisions must be accommodated in some way.

In a well-dimensioned hash table, the average cost (number of instructions) for each lookup is  independent of the number of elements stored in the table. Many hash table designs also allow  arbitrary insertions and deletions of key-value pairs, at (amortized]) constant average cost per  operation. 

In many situations, hash tables turn out to be more efficient than search trees or any  other table lookup structure. For this reason, they are widely used in many kinds of  computer software, particularly for associative arrays, database indexing, caches, and sets. The idea of hashing is to distribute the entries (key/value pairs) across an array of buckets. Given a key, the algorithm computes an index that suggests where the entry can be found:

index = f(key, array_size) Often this is done in two steps: hash = hashfunc(key)

index = hash % array_size

In this method, the hash is independent of the array size, and it is then reduced to an index (a  number between 0 and array_size − 1) using the modulo operator (%).

In the case that the array size is a power of two, the remainder operation is reduced to masking,  which improves speed, but can increase problems with a poor hash function.

Choosing a hash function:

A good hash function and implementation algorithm are essential for good hash table  performance, but may be difficult to achieve. 

(2)

Uniformity is sometimes difficult to ensure by design, but may be evaluated empirically using  statistical tests, e.g., a Pearson's chi-squared test for discrete uniform distributions

Perfect hash function:

If all keys are known ahead of time, a perfect hash function can be used to create a perfect hash  table that has no collisions. If minimal perfect hashing is used, every location in the hash table  can be used as well.

Perfect hashing allows for constant time lookups in all cases. This is in contrast to most chaining  and open addressing methods, where the time for lookup is low on average, but may be very  large, O(n), for instance when all the keys hash to a few values.

Hash Functions:

A hash function is a function which transforms a key into a natural number called a hash value,  i.e.

f : K → H,

where K is the set of keys and H is a set of natural numbers. Function f is a many-to-one  function. If two different

keys, say k1 and k2, k1 6= k2 have the same hash value, i.e. f(k1)f(k2) then the two keys are said to collide and the

corresponding records are called synonyms. Two restrictions are imposed on f: 1. For any k   ∈K the value should be obtained as fast as possible.

2.  It must minimize the number of collisions. An example of hash function is:

f(k) = γ(k) modulus B,

where γ is a function which maps a key to a natural number, and B is a natural number, possibly  prime. The expression

of function γ depends on the keys. If the keys are numeric, the γ(k) = k. The simplest function γ  on alphanumeric keys

is the sum of the (ASCII) codes for each character of the key. IMPLEMENTATION:

(3)

#include<cstdlib> #include<string> #include<cstdio> using namespace std;

const int TABLE_SIZE = 128;

 

/*

* HashEntry Class Declaration

*/

class HashEntry

{

    public:

        int key;

        int value;

        HashEntry(int key, int value)

        {

      this->key = key;

      this->value = value;

        }

};

 

/*

* HashMap Class Declaration

*/

(4)

{

    private:

        HashEntry **table;

    public:   

        HashMap()

{

      table = new HashEntry * [TABLE_SIZE];

      for (int i = 0; i< TABLE_SIZE; i++)

      {

      table[i] = NULL;

      }

        }

        /*

* Hash Function

*/

        int HashFunc(int key)

        {

      return key % TABLE_SIZE;

        }

        /*

* Insert Element at a key

*/

void Insert(int key, int value)

{

(5)

      while (table[hash] != NULL && table[hash]->key != key)

      {

      hash = HashFunc(hash + 1);

      }

      if (table[hash] != NULL)

      delete table[hash];

      table[hash] = new HashEntry(key, value);

}

        /*

* Search Element at a key

*/

        int Search(int key)

{

    int  hash = HashFunc(key);

    while (table[hash] != NULL && table[hash]->key != key)

    {

        hash = HashFunc(hash + 1);

    }

    if (table[hash] == NULL)

        return -1;

    else

        return table[hash]->value;

        }

 

(6)

* Remove Element at a key

*/

        void Remove(int key)

{

    int hash = HashFunc(key);

    while (table[hash] != NULL)

    {

        if (table[hash]->key == key)

      break;

        hash = HashFunc(hash + 1);

    }

      if (table[hash] == NULL)

    {

      cout<<"No Element found at key "<<key<<endl;

      return;

      }

      else

      {

      delete table[hash];

      }

      cout<<"Element Deleted"<<endl;

        }

        ~HashMap()

{

(7)

      {

      if (table[i] != NULL)

      delete table[i];

      delete[] table;

      }

        }

};

/*

* Main Contains Menu

*/

int main()

{

    HashMap hash;

    int key, value;

    int choice;

    while (1)

    {

        cout<<"\n---"<<endl;

        cout<<"Operations on Hash Table"<<endl;

        cout<<"\n---"<<endl;

        cout<<"1.Insert element into the table"<<endl;

        cout<<"2.Search element from the key"<<endl;

        cout<<"3.Delete element at a key"<<endl;

        cout<<"4.Exit"<<endl;

(8)

        cin>>choice;

        switch(choice)

        {

        case 1:

      cout<<"Enter element to be inserted: ";

      cin>>value;

      cout<<"Enter key at which element to be inserted: ";

      cin>>key;

      hash.Insert(key, value);

      break;

        case 2:

      cout<<"Enter key of the element to be searched: ";

      cin>>key;

      if (hash.Search(key) == -1)

      {

        cout<<"No element found at key "<<key<<endl;

        continue;

    }

    else

    {

        cout<<"Element at key "<<key<<" : ";

        cout<<hash.Search(key)<<endl;

    }

      break;

(9)

      cout<<"Enter key of the element to be deleted: ";

      cin>>key;

      hash.Remove(key);

      break;

        case 4:

      exit(1);

        default:

       cout<<"\nEnter correct option\n";

       }

    }

    return 0;

}

 OUTPUT:

Operations on Hash Table

 

---1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 1

Enter element to be inserted: 12

Enter key at which element to be inserted: 1

 

(10)

---Operations on Hash Table

 

---1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 1

Enter element to be inserted: 24

Enter key at which element to be inserted: 2

 

---Operations on Hash Table

 

---1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 1

Enter element to be inserted: 36

Enter key at which element to be inserted: 3

 

(11)

 

---1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 1

Enter element to be inserted: 48

Enter key at which element to be inserted: 4

 

---Operations on Hash Table

 

---1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 1

Enter element to be inserted: 60

Enter key at which element to be inserted: 5

 

---Operations on Hash Table

(12)

---1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 2

Enter key of the element to be searched: 3

Element at key 3 : 36

 

---Operations on Hash Table

 

---1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 2

Enter key of the element to be searched: 5

Element at key 5 : 60

 

---Operations on Hash Table

 

(13)

---1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 3

Enter key of the element to be deleted: 4

Element Deleted

 

---Operations on Hash Table

 

---1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 2

Enter key of the element to be searched: 4

No element found at key 4

 

---Operations on Hash Table

 

(14)

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 2

Enter key of the element to be searched: 2

Element at key 2 : 24

 

---Operations on Hash Table

 

---1.Insert element into the table

2.Search element from the key

3.Delete element at a key

4.Exit

Enter your choice: 4

 

 

References

Related documents

The DCPS decreased its percentage of students scoring below basic level on 4 th grade NAEP math by ten points between 2007 and 2011, compared with an average drop of four points

To determine the direct effects of SM components, namely strategic planning (vision, mission, objectives, and environmental analysis), strategy

Archbishop Catholic Cemeteries 46 Cemeteries Catholic Charities 157 Services 6 Vicariates 31 Deaneries 356 Parishes 211 Elementary Schools 40 Secondary

The DAU’s role is to advocate for the rights, protection and equality of opportunity for PWDs living in Trinidad and Tobago by monitoring and coordinat- ing the implementation of the

IL-25 was measured in the supernatant by ELISA ( a ) and the surface expression of IL-25R on AM ϕ was detected by flow cytometry ( b ). Exosomes were isolated from BALF and

This narrative summary will be completed with a series of tables: (1) characteristics of participants; (2) characteristics of selec- tion procedures; (3) characteristics of the

OAW-IAP215-RW OmniAccess AP215 Wireless Instant Access Point, 802.11n/ac, 3x3:3, dual radio, integrated antennas –. Restricted regulatory domain: Rest

Both the current density in the nitric oxide containing atmosphere and the current ratio are highest for the LSCo5 electrode, and this is therefore the most interesting cathodes