lec15

# lec15 - Outline • Deﬁnition • Insertion • Deletion...

This preview shows pages 1–5. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Outline • Deﬁnition • Insertion • Deletion • Search • Analysis Lecture 15, Double Hashing p. 1 Double Hashing: Deﬁnitions • Motivation: What if we have a lot of collisions in an open-addressing hashtable? Other than linear probing, how can we scatter keys better? • Two hash functions: ﬁrst hash function as before; the second hash function decides the increment step. • Double hashing: use a second hash function to get a ﬁxed increment to use for the probe sequence. • What is a good second hashing function? To reach every possible slot in the table. Be relative prime to the table size. Lecture 15, Double Hashing p. 2 Operations: • Insertion – First slot is determined by the ﬁrst hash function – If there is a collision, the ﬁxed increment is determined by the second hash function – Until an empty slot is reached, or the table is full. • Deletion – Mark it, instead of deleting it • Search – First slot to search: determined by the ﬁrst hash function – If it is not, the ﬁxed increment is determined by the second hash function – Until an empty slot is reached, or the key is found. Lecture 15, Double Hashing p. 3 Analysis: on Search/Insertion • Assumption. Each probe equally likely hits each table position ∞ ( n )i i=0 M 1 = 1−(n/M ) (a geometric • A miss: series) • A insertion: the same as above • A hit: the same as insert the key as the j -th key. – Expected time to insert the ﬁrst key: 1 – Expected time to insert the second key, i.e. a 1 table with one key: 1−1/M – Expected time to insert the third key, a table 1 with two keys: 1−2/M Lecture 15, Double Hashing p. 4 – Expected time to insert the forth key, a table 1 with three keys: 1−3/M – The total expected time to insert n keys: 1 + 1 1 1 + 1−(2/M ) + ... + 1−((n−1)/M ) 1−(1/M ) M M M – = 1 + M −1 + M −2 + ... + M −n+1 (diﬀerence of two harmonic series) 1 – total time M ln 1−n/M 1 – average time M/n ln 1−n/M Lecture 15, Double Hashing p. 5 ...
View Full Document

## This note was uploaded on 07/17/2010 for the course CS 240 taught by Professor Ortiz during the Spring '09 term at Waterloo.

### Page1 / 5

lec15 - Outline • Deﬁnition • Insertion • Deletion...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online