Presentation is loading. Please wait.

Presentation is loading. Please wait.

"He's off the map!" - Eternal Sunshine of the Spotless Mind

Similar presentations


Presentation on theme: ""He's off the map!" - Eternal Sunshine of the Spotless Mind"— Presentation transcript:

1 "He's off the map!" - Eternal Sunshine of the Spotless Mind
Map ADT "He's off the map!" - Eternal Sunshine of the Spotless Mind

2 Data Structures Given a program that counts the frequency of all the words in a file. Assume words are anything set off by whitespace. CS Maps

3 Performance using ArrayList
Title Size (kb) Total Words Distinct Words Time (sec) small sample 0.6 89 25 0.001 2BR02B 34 5,638 1,975 0.051 Alice in Wonderland 120 29,460 6,017 0.741 Adventures of Sherlock Holmes 581 107,533 15,213 4.144 2008 CIA Factbook 10,030 1,330,100 74,042 CS Maps

4 Order? Title Size Total Words Distinct Words Time
Express change in size as factor of previous file. Title Size Total Words Distinct Words Time small sample 0.6 89 25 0.001 2BR02B 57x 63x 79x 51x Alice in Wonderland 3.5x 5.2x 3x 14.5x Adventures of Sherlock Holmes 4.8x 3.7x 2.5x 5.6x 2008 CIA Factbook 17.3x 12.4x 4.9x 41.7x O(Total Words * Distinct Words) ?? CS Maps

5 Question Given it takes 2.88 minutes for the 2008 CIA Factbook with 1,330,100 total words and 74,042 distinct words, how long will take for another text with 1,000x total words and 100x distinct words? an hour a day a week a month half a year 2.88 / (1,330,100 * 74, 042) = ? / (1,330,100,000 * 7,404,200) 2.88 = ? / (1,000 * 100) = 288, 000 minutes = 28.5 days CS Maps

6 Question Given it takes 2.88 minutes for the 2008 CIA Factbook with 1,330,100 total words and 74,042 distinct words, how long will take for another text with 1,000x total words and 100x distinct words? an hour a day a week a month half a year CS Maps

7 Why So Slow? Write a contains method for array-based list.
public boolean contains(E target) { } CS Maps

8 Why So Slow? Write a contains method for array-based list.
public boolean contains(E target) { boolean result = false; int i = 0; while(!result && i < size()) if(list[i] == target) result = true; i++; } return result; CS Maps

9 A Faster Way – Maps A -> 65
A data structure optimized for a very specific kind of search / access. Also known as: Dictionary, search table, or associative array. A -> 65 CS Maps

10 Maps Models a searchable collection of key-value entries.
Main operations of a map are for searching, inserting, and deleting items Multiple entries with the same key are not allowed. In a map we access by asking "give me the value associated with this key." Applications: address book student-record database CS Maps

11 Keys and Values Dictionary Analogy:
The key in a dictionary is a word: foo The value in a dictionary is the definition: First on the standard list of metasyntactic variables used in syntax examples. A key and its associated value form a pair that is stored in a map. To retrieve a value, need the key for that value. An indexed List can be viewed as a Map with integer keys. CS Maps

12 More on Keys and Values Keys must be unique.
A given key can only represent one value. But one value may be represented by multiple keys. Like synonyms in the dictionary. Example: factor: n. See coefficient of X. factor is a key associated with the same value (definition) as the key coefficient of X. CS Maps

13 The Map ADT get(k): if the map M has an entry with key k, return its associated value; else, return null. put(k, v): insert entry (k, v) into the map M; if key k is not already in M, then return null; else, return old value associated with k. remove(k): if the map M has an entry with key k, remove it from M and return its associated value; else, return null. size, isEmpty entrySet(): return an iterable collection of the entries in M. keySet(): return an iterable collection of the keys in M. values(): return an iterator of the values in M. CS Maps

14 Example Operation Output Map isEmpty() true Ø put(5,A) null (5,A)
put(7,B) null (5,A),(7,B) put(2,C) null (5,A),(7,B),(2,C) put(8,D) null (5,A),(7,B),(2,C),(8,D) put(2,E) C (5,A),(7,B),(2,E),(8,D) get(7) B (5,A),(7,B),(2,E),(8,D) get(4) null (5,A),(7,B),(2,E),(8,D) get(2) E (5,A),(7,B),(2,E),(8,D) size() 4 (5,A),(7,B),(2,E),(8,D) remove(5) A (7,B),(2,E),(8,D) remove(2) E (7,B),(8,D) get(2) null (7,B),(8,D) isEmpty() false (7,B),(8,D)

15 Implementing a Map One easy implementation uses an Unsorted List ADT that uses a double-linked list. Other common implementations of maps use a binary search tree or a hash table as the internal storage container. CS Maps

16 A Simple List-Based Map
We can implement a Map ADT using an Unsorted List. Store the items of the map in a list S in arbitrary order. tail head nodes/positions entries 9 c 6 5 8 CS Maps

17 The get(k) Algorithm Algorithm get(k) itr <- S.iterator
while itr.hasNext do entry <- itr.next // next entry in list if entry.getKey = k then return entry.getValue return null //no entry with key value of k CS Maps

18 The put(k,v) Algorithm Algorithm put(k,v): itr <- S.iterator
while itr.hasNext do entry <- B.next // next entry in list if entry.getKey = k then value <- entry.getValue entry.setValue(v) entry.setKey(k) return value // return the old value S.addLast((k,v)) count <- count + 1 // increment num of entries return null // no entry with key value k CS Maps

19 The remove(k) Algorithm
Algorithm remove(k): itr <- S.iterator while itr.has do entry <- itr.next if entry.getKey = k then value <- entry.getValue S.remove(entry) count = count – 1 return value // return value return null // no entry with key value k CS Maps

20 Performance of a List-Based Map
Takes O(n) time in the worst-case for all operations. Same for average case. Why? The Unsorted List implementation is effective only for Maps of small size. More often use Binary Trees or Hash Tables. CS Maps

21 Results with HashMap Title Size (kb) Total Words Distinct Words
Time List Time Map small sample 0.6 89 25 0.001 0.0008 2BR02B 34 5,638 1,975 0.051 0.0140 Alice in Wonderland 120 29,460 6,017 0.741 0.0720 Adventures of Sherlock Holmes 581 107,533 15,213 4.144 0.2500 2008 CIA Factbook 10,030 1,330,100 74,042 4.0000 CS Maps

22 Order? Title Size Total Words Distinct Words Time List Time Map
small sample 0.6 89 25 0.001 0.0008 2BR02B 57x 63x 79x 51x 18x Alice in Wonderland 3.5x 5.2x 3.0x 14.5x 5x Adventures of Sherlock Holmes 4.8x 3.7x 2.5x 5.6x 2008 CIA Factbook 17x 12.3x 42x 16x O(Total Words)? CS Maps

23 Question Given it takes 4 seconds for the 2008 CIA Factbook with 1,330,100 total words and 74,042 distinct words, how long will take for another text with 1,000x total words and 100x distinct words? an hour a day ½ a week a week a month 4 / (1,330,100 * 74, 042) = ? / (1,330,100,000 * 7,404,200) 4 = ? / (1,000 * 100) = 400, 000 seconds = 4.6 days CS Maps

24 Question Given it takes 4 seconds for the 2008 CIA Factbook with 1,330,100 total words and 74,042 distinct words, how long will take for another text with 1,000x total words and 100x distinct words? an hour a day ½ a week a week a month 4 / (1,330,100 * 74, 042) = ? / (1,330,100,000 * 7,404,200) 4 = ? / (1,000 * 100) = 400, 000 seconds = 4.6 days CS Maps

25 CS Maps

26 The Map<K, V> Interface in Java
void clear() Removes all mappings from this map (optional operation). boolean containsKey(Object key) Returns true if this map contains a mapping for the specified key. boolean containsValue(Object value) Returns true if this map maps one or more keys to the specified value. Set<K> keySet() Returns a Set view of the keys contained in this map. CS Maps

27 The Map Interface Continued
V get(Object key) Returns the value to which this map maps the specified key. boolean isEmpty() Returns true if this map contains no key-value mappings. V put(K key, V value) Associates the specified value with the specified key in this map CS Maps

28 The Map Interface Continued
V remove(Object key) Removes the mapping for this key from this map if it is present int size() Returns the number of key-value mappings in this map. Collection<V> values() Returns a collection view of the values contained in this map. CS Maps


Download ppt ""He's off the map!" - Eternal Sunshine of the Spotless Mind"

Similar presentations


Ads by Google