Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.

Similar presentations


Presentation on theme: "Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee."— Presentation transcript:

1 Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee

2  Hash table:  data structure designed for fast table operations  allows add, find, and delete in O(1) expected case  main idea: store key and value in an array  use a hash function, called h, to convert a key into an array index  store element with key = k at array index = h(k)  key by itself probably too large to use as an index: (ex) ssn, credit card num, name

3  Problem: Store positive integers in a table  Desired performance: Following operations should be O(1)  Add an integer to the table  Find if an integer is in the table  Delete an integer from the table

4  Solution:  Array of size M = 10  Array elements initialized to -1  Store integer k at location k mod 10  These obviously work: ▪ add(3) ▪ add(25) ▪ find(3) ▪ find(9) ▪ find(25) ▪ delete(25)

5  If different keys are hashed to the same indexes, we call it collision.  Collisions cause problems:  a collision occurs if h(k1) = h(k2) and (k1 ≠ k2)

6  [Solutions]  Open Addressing ▪ Linear Probing ▪ Quadratic Probing ▪ Double hashing  Separate Chaining

7  Fill  filled sequence: a sequence of filled cells in a hash table.  clustering: filling a continuous sequence. Fig 11.6 in page 530  It ’ s critical when designing a hash table to ensure that it never becomes more than half or at the most two- thirds full.  Find: page 533  probe: the process of finding an appropriate cell following a collision

8  Insert: page 534  probe length: the number of steps they take to insert a key.  Delete: page 534  Delete isn ’ t accomplished by simply removing a data item from a cell because we will have a problem to find a key.  Use a special key or -1

9  Duplicates Allowed?  Searching all the same keys wastes time. Thus, in the majority of cases, duplicated key is not allowed.

10  In quadratic probing, probes go to x+1, x+2 2, x+3 2, x+4 2, …

11  Goal: To eliminate secondary clustering  Use two hash functions.  Secondary hash function must  Not be the same as the primary hash function.  Never output a 0 (otherwise, there would be no step; every probe would land on the same cell, and the algorithm would go into an endless loop.

12  Example  Primary hash function ▪ Index = key % arraySize  Secondary hash function ▪ stepSize = constant – (key % constant)  Note: Double hashing requires that the size of the hash table is a prime number. Why?

13  Duplicates: allowed. But might lower the performance  Deletion: no special problem  Table Size: not important  Buckets vs. Linked list


Download ppt "Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee."

Similar presentations


Ads by Google