Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hash Tables Computer Science and Engineering

Similar presentations


Presentation on theme: "Hash Tables Computer Science and Engineering"— Presentation transcript:

1 Hash Tables Computer Science and Engineering
Chapter 8 of Goodrich and Tomassia’s Text 2/18/2019 B. Ramamurthy

2 Topics Hashing Hash functions Hash Tables Collision
Collision resolution Chaining, linear probing, quadratic probing, double hashing Java Hash table Example 2/18/2019 B. Ramamurthy

3 Hashing Concept Another approach for storing and searching elements.
Used in applications where add and delete, besides search, are used. Worst case linear but can get O(1) best case. 2/18/2019 B. Ramamurthy

4 Hash Function A hash function h maps keys of a given type into an integer interval {0, N-1} A simple function: h(x) = x mod N A good hash function will uniformly disperse the keys in the range {0, N-1} 2/18/2019 B. Ramamurthy

5 Hash Table Hash table ADT:
Has a hash function h Array of size N Collision occurs when two keys map to the same array index. Two major collision resolution schemes are: chaining and open addressing 2/18/2019 B. Ramamurthy

6 Example Item(name, ssn) where ssn is 9 digit positive integer.
Hash table N = Hash function: last four digits of of x Use chaining to handle collision. 2/18/2019 B. Ramamurthy

7 Hash Table: Example 1 2 3 . 9996 9997 9998 9999 2/18/2019 B. Ramamurthy

8 Hash Functions Usually specified as two components:
Component1 called the hash code map, collects the parts of the data and maps them to integers (numeric data) H1: keys  integers Component2 called the compression map, takes the integers and maps them to 0 to N-1. H2: integers {0, N-1} 2/18/2019 B. Ramamurthy

9 Hash Code Maps Memory address of data
Integer cast of non-numeric data: bit/byte pattern of data Sum of all components: add the ascii values of your last name Polynomials of various parts of data. 2/18/2019 B. Ramamurthy

10 Compression maps Reminder of division (mod) h(y) = y mod N
Multiple, Add and Divide (MAD) h(y) = (a*y + b) mod N where a mod N <> 0 (otherwise everything will map to b!) 2/18/2019 B. Ramamurthy

11 Linear Probing Linear probing resolves collision by placing the colliding item in the next available empty cell. Each entry inspected is referred to as the “probe” Example: h(x) = x mod 13 Insert keys: 18, 41, 22, 44, 59, 32, 31, 73, in that order 2/18/2019 B. Ramamurthy

12 Search with linear probing: find(k)
j = h(k); probe = 0; while (p < N) 2.1 c = A[j] 2.2 if (c == null) return NOT_FOUND; else if (c.key() == k) return c.element(); else j = (j+1) mod N p = p +1; 3. return NOT_FOUND; 2/18/2019 B. Ramamurthy

13 Double Hashing h1: primary hash function
If it results in collision, resolve by applying another hash function, secondary hash function; here is an example of such a function. d(k) = q – k mod q Where q < N, Possible values of d(k) are 1,2,3,..q It cannot be 0 When collision occurs: (h(k)+j*d(k)) mod N j = 0, 1, ..N-1 2/18/2019 B. Ramamurthy

14 Example h(k) = k mod 13 d(k) = 7 – k mod 7
Insert 18, 41, 22, 44, 59, 32, 31, 73. 2/18/2019 B. Ramamurthy


Download ppt "Hash Tables Computer Science and Engineering"

Similar presentations


Ads by Google