Beruflich Dokumente
Kultur Dokumente
Hash Tables
Hash Table (or Dictionary) is a data structure designed for O(1) average-case add, remove, and find operations when a search key is known If two keys collide (hash to the same value), running times of all these operations may degenerate to O(n) Made up of an array of key-value pairs, a hash function, and a collision-handling scheme
http://research.cs.vt.edu/AVresearch/hashing/openhash.php
Hash Function
Used to map the search key to the index of a slot in the table where the corresponding record is supposedly stored. Mapping may consist of 2 conversions
map
input
type could be any primitive or user defined type can always map binary data to an integer
map
someInteger
Hash Function maps each key k in our hash table to the index, an integer in the range [0,N-1], where N is the size of the hash table. For this assignment, we will be using the following hash functions:
Summing
Summing Components
Example:
hash(dog)
= 'd' + 'o' + 'g' = 100 + 111 + 103 = 314 hash(god) = 'g' + 'o' + 'd' = 103 + 111 + 100 = 314 Regardless of the table size, these two keys will collide using this hash function!
Cyclic Shift
as summing components, but perform a 5-bit cyclic shift on the sum before adding each character's ascii value
int hash(string const & key) { unsigned int h = 0; for(int i = 0; i < key.size(); ++i) { h = (h << 5 | h >> 27); h += (unsigned int) key[i]; } return hash((int) h); }
Polynomial Hash
a polynomial in some non-zero constant a that takes components (x[0],x[1],...,x[k-1]) with a != 1. This can represented mathematically as:
h(x)
Example:
Let a = 2. hash(man) = 'm'*a^(2) + 'a'*a + 'n' = 109*4 + 97*2 + 110 = 436 + 194 + 110 = 740
Collision-Handling Schemes
Collision when two keys hash to the same table index Collision-Handling Schemes a technique to allow multiple entries with keys that hash to the same value to both exist in the table at the same time For this lab we'll describe two simple schemes:
Separate
Separate Chaining
Separate Chaining a simple collision-handling scheme where each table cell contains a linked list of entries. When a collision occurs, the new entry can be added to the linked list.
http://www.isr.umd.edu/~austin/ence200.d/java-examples.htm
Linear Probing
Linear Probing simple collision-handling scheme where each table cell contains only one element, but collisions are handled by performing a linear search for the next empty cell. Each step of this search is called a probe, beginning with the 0th probe.
http://codeidol.com/java/javagenerics/Maps/Implementing-Map
=c*i+d
some positive non-zero constant integers c and d
for
parts of separate chaining and linear probing collision-handling schemes comparing the running times of different combinations of hash functions and collision handling schemes
use