1 / 23

Analysis & Maps: Understanding Complexity and Maps in Compsci 201

This article provides an analysis of complexity and usage of maps in computer science, covering topics such as O-notation, runtime analysis, and the concept of maps. It also includes a review of problems and solutions related to maps. The article aims to help students prepare for their midterm in Compsci 201.

crumpler
Télécharger la présentation

Analysis & Maps: Understanding Complexity and Maps in Compsci 201

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Compsci 201Analysis + Maps + Midterm Owen Astrachan ola@cs.duke.edu September 21, 2018 Compsci 201, Fall 2018, Analysis + Maps + Midterm

  2. G is for … • Git • Version control that's so au courant • GPL • First open source license • Google • How to find Stack Overflow Compsci 201, Fall 2018, Analysis + Maps + Midterm

  3. PFTFbM • Review O-Notation aka big-Oh • Understanding the big ideas behind big-Oh • Maps: solution to many problems • Review .equals and .hashCode • Don’t call us, we’ll call you: calling.equals()? • List.contains(), Set.add(), … • Midterm • What to expect and how to prepare Compsci 201, Fall 2018, Analysis + Maps + Midterm

  4. Review: Problems and Solutions the 2 fox 4 cried 1 fat 3 tears 5 • What word occurs the most in a text-file? • What element occurs most in list? • https://github.com/astrachano/classcode201fall18/blob/master/src/CountingStringsBenchmark.java • Parallel arrays: word[k] occurs count[k] times • Use ArrayLists: 2 “the”, 3 “fat”, 4 “fox” 0 1 2 3 4 Compsci 201, Fall 2018, Analysis + Maps + Midterm

  5. What is complexity of this code? • ArrayList<String>, ArrayList<Integer> • words.get(k) occurs counter.get(k) • Search for each word and …if occurs at k • +1 to counter.get(k), else add at end • Complexity of search? O(M) for M different words • One search is O(M) – what about all searches? • Tracking all words. First time zero, then one, … Compsci 201, Fall 2018, Analysis + Maps + Midterm

  6. Tracking N strings • Complexity of search? O(M) for M different words • One search is O(M) – what about all searches? • 1 + 2 + 3 + … + N = N(N+1)/2 O(n2) Compsci 201, Fall 2018, Analysis + Maps + Midterm

  7. Understanding O-notation • This is an upper bound and in the limit • Coefficients don’t matter, order of growth • N + N + N + N is O(N) --- why? • N*N is O(N2) – why? • O(1) means independent of N, constant time • In analyzing code and code fragments • Account for each statement • How many teams is each statement executed? Compsci 201, Fall 2018, Analysis + Maps + Midterm

  8. Analyze using big-Oh • What is runtime of stuff(N) • How to reason about this • What is return value of stuff(N) • What if code changes to sum += k Compsci 201, Fall 2018, Analysis + Maps + Midterm

  9. WOTO http://bit.ly/201fall18-sept21-1 Compsci 201, Fall 2018, Analysis + Maps + Midterm

  10. O(N2) too slow, solution? • Could use binary search to locate • Still need to shift values when adding • Faster in practice? Yes!!! • HashMap is better: search is O(1) instead of O(N) • Constant time, independent of N • Hash the key, increment corresponding counter • Map a key to a value • Key here is string, value is # occurrences Compsci 201, Fall 2018, Analysis + Maps + Midterm

  11. Map conceptually (key,value) • Search engine: (K,V) is (query, list of web pages) • Key is word or phrase, Value: list of pages • Maps query to list of web pages/URLs • Color Name/RGB triple: (K,V) is (name, (r,g,b)) • Duke Blue maps to (0,0,156) • Dartmouth Green maps to (0,105,62) • Stanford Cardinal maps to (140,21,21) Compsci 201, Fall 2018, Analysis + Maps + Midterm

  12. A Rose by Any Other Name… Compsci 201, Fall 2018, Analysis + Maps + Midterm

  13. Examining Map Code • Why initialize value to 0 when stored in map? • What does map.get(key) return? • What does map.put(key,value) do? HashMap<String,Integer> map = newHashMap<>(); Scanner s = new Scanner(new File(f)); while (s.hasNext()){ String w = s.next(); if (! map.containsKey(w)) { map.put(w, 0); } map.put(w, map.get(w) + 1); } // map complete here Compsci 201, Fall 2018, Analysis + Maps + Midterm

  14. Similar to Anonymous APT • Mapping characters to # occurrences • int[] counts = {0,0, …0}; • counts[ch] = counts[ch] + 1 • Key is character (index into array) • Value is int, array element: counts[ch] • Words? Key is a String, value is Integer • Map<String,Integer> map Compsci 201, Fall 2018, Analysis + Maps + Midterm

  15. Primitives and Wrapper Classes • In Anonymous code we see the first line of code, in mapping solution the second. Differences? int[] counts, Map<String,Integer> map counts[ch] += 1. // update # occurs map.put(w, map.get(w) + 1) • Integer wraps an int, but it’s immutable • Must create new one, cannot increment += 1 Compsci 201, Fall 2018, Analysis + Maps + Midterm

  16. Map concepts, HashMap concepts • Key values should be immutable, cannot change • If you change a key, you change it's hashCode, so where does it go? What Bucket? • Keys unique, there's a KeySet! • HashMap: key uses .hashCode(), value anything • How big is the set of lockers? Can it change? • Big enough, but can grow if needed Compsci 201, Fall 2018, Analysis + Maps + Midterm

  17. The java.util.Map interface, concepts • HashMap <Key,Value> or <K,V> • Map.Entry<K,V> is inner class Compsci 201, Fall 2018, Analysis + Maps + Midterm

  18. HashMap Internals • What does map.get(key) actually do? • Find h = key.hashCode() • Find the hth bucket/locker/location of map/table • Look at all the values in that bucket/locker • Could be ArrayList or LinkedList or … • Traverse searching for .equals(key) • What is best case? Average case? Worst Case Compsci 201, Fall 2018, Analysis + Maps + Midterm

  19. WOTO http://bit.ly/201fall18-sept19-2 Compsci 201, Fall 2018, Analysis + Maps + Midterm

  20. Maria Klawe • President of Harvey Mudd • Dean of Engineering at Princeton, ACM Fellow, College Dropout (and re-enroller) I personally believe that the most important thing we have to do today is use technology to address societal problems, especially in developing regions Coding is today's language of creativity. All our children deserve a chance to become creators instead consumers of computer science. Compsci 201, Fall 2018, Analysis + Maps + Midterm

  21. Calling .equals, .hashCode • When are these methods called? • Typically in list.add or set.add or … • Searching in a hash bucket/locker … • Using StringWrapper to understand internals • Instrumenting code for understanding rather than benchmarking • https://github.com/astrachano/classcode201fall18/blob/master/src/WrappedStringDriver.java Compsci 201, Fall 2018, Analysis + Maps + Midterm

  22. LiveCoding Demo • Add print statements to .hashCode, .equals • We know they’re called • Instrumenting with static class variables • static is per class, instance is per object • Change .hashCode to return 7 • Everything in same locker/bucket, changes? Compsci 201, Fall 2018, Analysis + Maps + Midterm

  23. Midterm • Review syllabus for policies • Missing midterm, re-weighting • Notes you can bring, logistics of midterm • Practice midterm and discussion section • Pre-discussion essential • Map questions will be “read only” Compsci 201, Fall 2018, Analysis + Maps + Midterm

More Related