13-hashing - Hash Tables 1 Hash Tables 1 Comp 122 Spring...

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

View Full Document Right Arrow Icon
Comp 122, Spring 2004 Hash Tables – 1 Hash Tables – 1
Background image of page 1

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

View Full Document Right Arrow Icon
Comp 122, Fall 2003 shtables - 2 Lin / Devi Dictionary Dictionary: » Dynamic-set data structure for storing items indexed using keys . » Supports operations Insert, Search, and Delete . » Applications: Symbol table of a compiler. Memory-management tables in operating systems. Large-scale distributed systems. Hash Tables: » Effective way of implementing dictionaries. » Generalization of ordinary arrays.
Background image of page 2
Comp 122, Fall 2003 shtables - 3 Lin / Devi Direct-address Tables Direct-address Tables are ordinary arrays . Facilitate direct addressing . » Element whose key is k is obtained by indexing into the k th position of the array. Applicable when we can afford to allocate an array with one position for every possible key. » i.e. when the universe of keys U is small . Dictionary operations can be implemented to take O (1) time . » Details in Sec. 11.1.
Background image of page 3

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

View Full Document Right Arrow Icon
Comp 122, Fall 2003 shtables - 4 Lin / Devi Hash Tables Notation: » U – Universe of all possible keys. » K – Set of keys actually stored in the dictionary. » | K | = n . When U is very large , » Arrays are not practical. » | K | << | U |. Use a table of size proportional to | K | – The hash tables . » However, we lose the direct-addressing ability. » Define functions that map keys to slots of the hash table.
Background image of page 4
Comp 122, Fall 2003 shtables - 5 Lin / Devi Hashing Hash function h : Mapping from U to the slots of a hash table T [0 ..m –1] . h : U {0 , 1 ,…, m –1} With arrays, key k maps to slot A [ k ]. With hash tables, key k maps or “hashes” to slot T [ h [ k ]]. h [ k ] is the hash value of key k .
Background image of page 5

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

View Full Document Right Arrow Icon
Comp 122, Fall 2003 shtables - 6 Lin / Devi Hashing 0 m –1 h ( k 1 ) h ( k 4 ) h ( k 2 )= h ( k 5 ) h ( k 3 ) U ( universe of keys) K (actual keys) k 1 k 2 k 3 k 5 k 4 collisio n
Background image of page 6
Comp 122, Fall 2003 shtables - 7 Lin / Devi Issues with Hashing Multiple keys can hash to the same slot – collisions are possible . » Design hash functions such that collisions are minimized. » But avoiding collisions is impossible. Design collision-resolution techniques. Search will cost Ө ( n ) time in the worst case . » However, all operations can be made to have an expected complexity of Ө (1).
Background image of page 7

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

View Full Document Right Arrow Icon
Comp 122, Fall 2003 shtables - 8 Lin / Devi Methods of Resolution Chaining: » Store all elements that hash to the same slot in a linked list. » Store a pointer to the head of the linked
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.

{[ snackBarMessage ]}

Page1 / 24

13-hashing - Hash Tables 1 Hash Tables 1 Comp 122 Spring...

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