Presentation is loading. Please wait.

Presentation is loading. Please wait.

Maps, Dictionaries, Hashtables

Similar presentations


Presentation on theme: "Maps, Dictionaries, Hashtables"— Presentation transcript:

1 Maps, Dictionaries, Hashtables
(chapter 8 in text)

2 Maps, Dictionaries Store a bunch of elements that need to be looked up quickly Each element stored as an entry: {key, value} Only difference between maps and dictionaries is that the key has to be unique for maps

3 Map ADT Important methods: put(k,v) get(k) remove(k)

4 Dictionary ADT Important Methods find(k) findAll(k) insert(k,v)
remove(e)

5 Motivating Example You’ve been hired by a small college to do an online registration webpage for them. You want a data structure in which you can store all the students, then when they successfully login, you can get the Student object and add/remove classes from it.

6 Idea 1 Hold students in a list Downside:
Searching for a student takes time 0(n) Student 1 Student 3 Student 2

7 Idea 2 Students have student numbers. Why not create an array to hold all the students and we get a student by student number index Array – Good: O(1) time to find a student Bad: Using space for 10,000,000 students to store a few thousand

8 Solution – Hash Table Create an array of buckets of size N, where n < N Expect O(1) performance on average for finding items if well implemented N { , StudentObj2} { , StudentObj1}

9 Hash Function Keys not necessarily integers Step 1: Get hashcode
hashcode is the mapping of a key to an integer Step 2: Use compression function to map hash code to an integer from 0..N-1 note: mapping not necessarily unique. We’ll need to deal with collisions

10 Hashcode Important: Avoid collisions
Our example could just use student number Let’s consider other strategies

11 Strategy 1 – Summing Components
eg. Suppose key is a String – “hello” Could get hashcode by summing characters: int hashcode = ‘h’ + ‘e’ + ‘l’ + ‘l’ + ‘o’ Problem: collision with “olleh”

12 Strategy 2: Polynomial Hash Codes
Form: x0ak-1 + X1ak-2 + … + Xk-2a + X0a + Xk-1 where a is some non-zero constant Example: “hello”, a =33 int hashcode = ‘h’*334 + ‘e’*333 + ‘l’*332 + ‘l’*33 +’’o’

13 Compression Functions
Map hashcode (some integer) to range 0..N Simple method: |i| mod N Note – choice of N important eg. hashCodes {200,205,210,…600} if N is 100, each code collides with 3 others if N is 101, no collisions

14 Collision Handling Two distinct keys mapped to same entry in the hashtable => collision Makes operations more complicated

15 Separate Chaining When collision occurs, add to end of a list
{0111, “item1”} {0111, “item1”} {0314, “item2”} {0314, “item2”} {3421,“item3”}

16 Linear Probing (an open addressing scheme)
When collision occurs, go to next bucket Note: Complicates removals Try to insert Final insertion here {0111, “item1”} {0111, “item1”} {0314, “item2”} {0314, “item2”}

17 Load Factor – n/N Must be < 1 Too small – Waste space
Too large – Too many collisions Should be < 0.5 for open addressing Should be < 0.9 for separate chaining


Download ppt "Maps, Dictionaries, Hashtables"

Similar presentations


Ads by Google