module5 - Key-indexed Search Suppose that the items in a...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Key-indexed Search Suppose that the items in a dictionary all have keys that are elements of a small universe U , e.g., U = { ,...,M- 1 } . Then we can store the item with key value k in T [ k ], where T is an array (sometimes called a table ) of length M . This is called direct addressing . If we want to search for any item, we just look up the item’s key in the table T in time O (1). This is called key-indexed search . Insert and Delete also take time O (1), assuming there are no duplicate keys. This is very efficient, but we require Θ( M ) space to store the table T . This may not be practical when M is large. Furthermore, it is certainly a waste of space when when M is large compared to n (the number of items in the dictionary). 103 / 121 Hash Tables A more general technique is to use a hash function h to determine where the item having a given key should be stored in the table T . The function h : U → { ,...,M- 1 } , where M is the size of the hash table T . Then an item with key k is stored in the slot T [ h ( k )]. The value h ( k ) is called the hash value of the key k . When U = { ,...,M- 1 } and h ( k ) = k for all k , we have direct addressing, as described on the previous slide. 104 / 121 Examples of Hash Functions Two common methods of constructing hash functions are the division method and the multiplication method . In the division method, the hash function is defined to be h ( k ) = k mod M. For this hash function, M is usually chosen to be a prime number that is not too close to a power of two. In the multiplication method, we choose a constant A where < A < 1. Then we define h ( k ) = b M ( kA- b kA c ) c . A popular choice for A is the golden ratio , A = ( √ 5- 1) / 2. 105 / 121 Collisions In general, | U | > M , so there must exist collisions , i.e., there are distinct values k,k ∈ U such that h ( k ) = h ( k ). If we want to insert two items, having keys k and k , into the hash table, then we have a problem because they are assigned to the same slot. We will study two techniques to resolve collisions: chaining and open addressing . 106 / 121 Hash Table Chaining Chaining means that each hash table slot T [ j ] points to an unsorted linked list consisting of all the keys having hash value equal to j . To search for the item having key k , we compute the hash value h ( k ), and then do a linear search of the linked list in slot T [ h ( k )]. To insert a new item having key k into the table, compute the hash value h ( k ), and then insert the new item at the beginning of the linked list in the slot T [ h ( k )]....
View Full Document

This note was uploaded on 02/21/2012 for the course PSYCH 101 taught by Professor Ennis during the Winter '09 term at Waterloo.

Page1 / 19

module5 - Key-indexed Search Suppose that the items in a...

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

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