Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Today’s Topics Any questions about Dijkstra's algorithm or any part of the lab? Hash tables and functions

3 Hashing is used to allow very efficient insertion, removal, and retrieval of items. Consider retrieval (searching) with several structures –To find data in an unordered linear list structure O(n)‏ –To find data in an order linear list structure O(log n)‏ –To find data in a BST or a Heap O(log n)‏ What orders are better than log n ? Hashes

4 Hashing is used to allow –inserting an item –removing an item –searching for an item all in constant time (in the average case). Hashing does not provide efficient sorting nor efficient finding of the minimum or maximum item etc. Hashes

5 We want to insert our items (of any type (String, int, double, etc.)) into a structure that allows fast retrieval. Terms: –Hash Table (an array of references to objects(items))‏ table_size is the number of places to store –Hash Function (calculates a hash value (an integer) based on some key data about the item we are adding to the hash table.)‏ –Hash Value (the value returned by the hash function)‏ the hash value must be an integer value within [0, table_size – 1] this gives us the index in the hash table, where we wish to store the item. Hashes

6 Just to give an idea of how to insert and retrieve items into a hash table (this does not use a good hash function)‏ –Consider our items are simply ints –Consider our Hash Function to be f(x) = x % n (this is not a typical hash function)‏ –The hash function returns a hash value which is modded by the size of our hash table array to compute the index where we wish to store our item. –example on the board (assume n=8, add items 24, 3, 17, 31)‏ Then we can reverse the process to see if a particular item is in our hash table. Hashes

7 In our example (assume n=8, add items 24, 3, 17, 31), what if we needed to insert item 11 into our hash? There'd be a collision. There are several strategies to handle collisions –Assume the hash value computed was H –the chosen strategy effects how retrieval is handled too –Open Addressing (aka Probing hash table) Place item in next open slot (linear probing)‏ –H+1, or H+2 or H+3... Place item in next open slot –H+1 2, or H+2 2, or H+3 2, or H+4 2,... –Wraparound is allowed Hashes

8 There are several strategies to handle collisions –Another technique besides Open Addressing, is Separate chaining Each array element stores a linked list Examples of these techniques on the board. Hashes

9 Typically we won't know our keys ahead of time (before we create our hash)‏ But if we did know all our keys ahead of time, would that help us in any way? Hashes

10 Let's come up with a hash table to store Strings –we'll need to decide on the size of our table –we'll need to decide whether we will use separate chaining or open addressing hashing - we'll need to create a hash function. We'll also allow insertion and retrieval (determine if an item exists in the hash). Hashes

11 Strategies for best performance –want items to be distributed evenly (uniformly) throughout the hash table and we want few (or no) collisions so that depends on our data items, our choice of hash function and our size of the hash table –also need to decide whether to use a probing (linear or quadratic) hash table or to use a hash table where collisions are handled by adding the item to a list for the index (hash value)‏ another method is called double hashing. –if choices are done well we get the retrieval time to be a constant, but the worst case is O(n)‏ –we also need to consider the computations needed to insert (computing the hash value)‏ Hashes


Download ppt "CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google