Docsity

Hashing - Data Structures and Algorithm - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Hashing - Data Structures and Algorithm - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Hashing

Dictionary / Dynamic Set Operations

Perhaps the most important class of data structures maintain a set of items, indexed by keys.

  • Search(S,k) – A query that, given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or nil if no such element belongs to S.
  • Insert(S,x) – A modifying operation that augments the set S with the element x.
  • Delete(S,x) – Given a pointer x to an element in the set S, remove x from S. Observe we are given a pointer to an element x, not a key value.

Problem of the Day

You are given the task of reading in n numbers and then printing them out in sorted order. Suppose you have access to a balanced dictionary data structure, which supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in O(log n) time.

  • Explain how you can use this dictionary to sort in O(n log n) time using only the following abstract opera- tions: minimum, successor, insert, search.
  • Explain how you can use this dictionary to sort in O(n log n) time using only the following abstract opera- tions: minimum, insert, delete, search.
  • Explain how you can use this dictionary to sort in O(n log n) time using only the following abstract opera- tions: insert and in-order traversal.

Collisions

Collisions are the set of keys mapped to the same bucket. If the keys are uniformly distributed, then each bucket should contain very few keys! The resulting short lists are easily searched!

0 1 2 3 4 5 6 7 8 9 10 11

Hash Functions

It is the job of the hash function to map keys to integers. A good hash function:

  1. Is cheap to evaluate
  2. Tends to use all positions from 0... M with uniform frequency.

The first step is usually to map the key to a big integer, for example

h =

keylength∑ i=

128 i^ × char(key[i])

Bad Hash Functions

The first three digits of the Social Security Number

0 1 2 3 4 5 6 7 8 9

Good Hash Functions

The last three digits of the Social Security Number

0 1 2 3 4 5 6 7 8 9

Substring Pattern Matching

Input: A text string t and a pattern string p. Problem: Does t contain the pattern p as a substring, and if so where? E.g: Is Skiena in the Bible?

Brute Force Search

The simplest algorithm to search for the presence of pattern string p in text t overlays the pattern string at every position in the text, and checks whether every pattern character matches the corresponding text character. This runs in O(nm) time, where n = |t| and m = |p|.

The Catch

This reduces string matching to n − m + 2 hash value computations (the n − m + 1 windows of t, plus one hash of p), plus what should be a very small number of O(m) time verification steps. The catch is that it takes O(m) time to compute a hash func- tion on an m-character string, and O(n) such computations seems to leave us with an O(mn) algorithm again.

The Trick

Look closely at our string hash function, applied to the m characters starting from the jth position of string S:

H(S, j) =

m∑− 1 i=

αm−(i+1)^ × char(si+j )

A little algebra reveals that

H(S, j + 1) = (H(S, j) − αm−^1 char(sj))α + sj+m

Thus once we know the hash value from the j position, we can find the hash value from the (j + 1)st position for the cost of two multiplications, one addition, and one subtraction. This can be done in constant time.