10 - Hash Tables Data Structures and Algorithms Andrei...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Hash Tables Data Structures and Algorithms Andrei Bulatov
Background image of page 1

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

View Full DocumentRight Arrow Icon
Algorithms – Hash Tables 10-2 The Dictionary Problem We have a dictionary Each entry of the dictionary consists of a key , a word, and an article explaining the word We to perform several simple operations with dictionary: search insert delete How should store the dictionary to perform those operations most efficiently? Goal: perform search in O(1) time, at least on average
Background image of page 2
Algorithms – Hash Tables 10-3 Straightforward Approaches We can try lists: Then search takes O(n) time Binary trees, heaps: Search takes O(log n) time
Background image of page 3

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

View Full DocumentRight Arrow Icon
Algorithms – Hash Tables 10-4 Direct Access Tables Create an array with entries indexed by all possible keys Word Antidisestablishmentarianism contains 28 letters all possible keys key article key article key article actually used keys
Background image of page 4
Algorithms – Hash Tables 10-5 Hashing The idea is - to have a smaller, more realistic table - to address an entry use not the key k itself, but a certain function h(k) computed using k The function used is called hash function, and the table --- hash table Let U be the set (universe) of all possible keys, and m is the desired size of the table Then h: U {0, …, m – 1} Key k (or the whole element) hashes to h(k) h(k) is the hash value of k
Background image of page 5

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

View Full DocumentRight Arrow Icon
Algorithms – Hash Tables 10-6 Collisions Everything is perfect as long as the hash values of all keys are different This is called a collision 1 k 2 k 3 k ) ( 1 k h ) ( 2 k h ) ( 3 k h ) ( 4 k h = 4 k
Background image of page 6
Algorithms – Hash Tables 10-7 Chaining In case of collision create a list of elements with the same hash value 1 k 2 k 3 k 4 k 6 k 5 k key article next key article next key article next key article next key article next key article next
Background image of page 7

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

View Full DocumentRight Arrow Icon
Algorithms – Hash Tables 10-8 Analysis We estimate time needed for searching in a hash table
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

10 - Hash Tables Data Structures and Algorithms Andrei...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online