e is for compsci 201 objects tradeoffs nbody
play

E is for Compsci 201 Objects, Tradeoffs, NBody Encryption Why - PowerPoint PPT Presentation

E is for Compsci 201 Objects, Tradeoffs, NBody Encryption Why SSH and SSL work Exception Susan Rodger A Throwable you may catch, sometimes you may rethrow January 24, 2020 1/24/2020 Compsci 201, Spring 2020 1 1/24/2020


  1. E is for … Compsci 201 Objects, Tradeoffs, NBody • Encryption • Why SSH and SSL work • Exception Susan Rodger • A Throwable you may catch, sometimes you may rethrow January 24, 2020 1/24/2020 Compsci 201, Spring 2020 1 1/24/2020 Compsci 201, Spring 2020 2 Announcements When you submit an APT • Assignment P0 - grace period to today 11:59pm • Submit a REFLECT form for each APT • With late penalty last change one week later • APT-1 due – now in grace period today 11:59pm • Do not accept after grace period • Discussion 3 on January 27 • Prediscussion, do before • Submit REFLECT form for each Assignment • APT-2 due January 28 • Assignment P1 due Thursday, Jan 30 1/24/2020 Compsci 201, Spring 2020 3 1/24/2020 Compsci 201, Spring 2020 4

  2. From Last Time … PFTD Go over • Objects from the ground up WOTO: Correctness Counts • What is java.lang.Object? Its methods? • .equals(), .toString(), later .hashCode() http://bit.ly/201spring20-0122-2 • Concepts in P1: Arrays, Scanners, Testing • Completing P1 with minimal angst • ArrayList from high to low level (mostly Friday) • Fits into Collections hierarchy • How to build it or do it yourself: diyad 1/24/2020 Compsci 201, Spring 2020 5 1/24/2020 Compsci 201, Spring 2020 6 Charles Isbell Algorithmic Tradeoffs • Context matters: Threads • Machine learning researcher • We will use a problem to understand algorithmic trade-offs and how ArrayList works • Systems that interact intelligently with many other intelligence agents • java.util.ArrayList is "growable array", but more! • Dean College of Computing @ gtech • What is the class, what is the package • Rethinking education: Online • Package is a collection of related classes Masters in Computer Science http://www.pbs.org/newshour/bb/online-graduate-programs-offer- • Given a list of words, find the unique words degrees-significant-savings/ • Algorithms with ArrayLists For me, the differences are simple to state: Computationalists grok that • Alternative with Set data structure models, languages and machines are equivalent . 1/24/2020 Compsci 201, Spring 2020 7 1/24/2020 Compsci 201, Spring 2020 8

  3. Array and ArrayList Look at Code: ArrayListUnique • Array can hold primitive or Object types • Problem • int[] and String[] work • Read words from a file • Fixed size, cannot grow • Want the unique words in sorted order • Use java.util class ArrayList for growth, more https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html • Contain object types, not primitives • Use .get(),.set() and not [] for indexing 1/24/2020 Compsci 201, Spring 2020 9 1/24/2020 Compsci 201, Spring 2020 10 Method A: Add each word to a sorted list Tradeoffs: Algorithmic Approaches • https://coursework.cs.duke.edu/201spring20/classcode/src • Code in methodA: process each word in list, add X to list of sorted, unique words • Read words from a file, store in ArrayList, class is ArrayListUnique – why array doesn't work? • If X already in sorted-list? Nothing to do • Tradeoffs in creating sorted list of unique words • If X greater than all words in list? Add at end • Some word greater than X? shift to make room • Algorithmic concepts with ArrayList methods • Compare three different algorithmic approaches • Reasoning with and learning about Java code 1/24/2020 Compsci 201, Spring 2020 11 1/24/2020 Compsci 201, Spring 2020 12

  4. Method A: How to shift to add "in middle" Example: insert “egg” • Find first element bigger than String X at index k “egg” • Shift right end to index > k, then add X there https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html#add(int,E) “egg” comes after “cat” 1/24/2020 Compsci 201, Spring 2020 13 1/24/2020 Compsci 201, Spring 2020 16 Method A: Processing every string Method A: Details of shifting to add X • Add unique elements from list to ret, keep sorted • Don't know about list.add(k,X) then … • Code reason: flag, break, best, worst cases … • Shift from end to index > X, then add X 1/24/2020 Compsci 201, Spring 2020 26 1/24/2020 Compsci 201, Spring 2020 27

  5. Method B: Tradeoff: Sort first, keep unique Example: Sorted with duplicates k • There are duplicates in list, but it's sorted • Process sorted elements, add to end if unique • Use of copy, why for-loop starts at 1 (priming) copy 1/24/2020 Compsci 201, Spring 2020 28 1/24/2020 Compsci 201, Spring 2020 29 Sort first, why is this faster? Comparing Tradeoffs: Performance • Both methodA and methodB process every word • Both methodA and methodB process every word in the list of words in the list of words • In loop body in methodB , NO shift happens • In loop body in methodA , shift happens • But, all strings sorted before loop • Could every element be shifted every time? • Shift 1, then 2, then 3, then … then shift N • Sorting takes N x log N for N strings • Total work done? 1 + 2 + … + N • Shifting takes 1 + 2 + .. + N = N(N+1)/2 • If N = one million? One billion operations/second • Sorting is 20 million, shifting is 0.5 trillion 1/24/2020 Compsci 201, Spring 2020 38 1/24/2020 Compsci 201, Spring 2020 39

  6. Method C: What if we use API, other classes What you will know … • A set contains no duplicates, a TreeSet maintains • Which of methodA, methodB, methodC is better? unique elements in sorted order • It depends, but on what does it depend? • Create set, contains no duplicates • Create ArrayList from set • How does methodA scale as # words increases? • Where are the loops? • 1 + 2 + … + N = N(N+1)/2, just say no! • What is log 2 (1,024)? or log 2 (1,048,576)? • Well, 2 10 = 1024 so … 1/24/2020 Compsci 201, Spring 2020 40 1/24/2020 Compsci 201, Spring 2020 41 Why is methodA slow? Java Concepts • Add unique elements from list to ret, keep sorted • Loops execute until loop-guard is false • break exits loop early • Code reason: flag, break, best, worst cases … • continue re-checks guard, skipping body • Some loops need initialization before loop guard • aka "priming the loop", e.g., done = false • if (!done) same as if (done == false) 1/24/2020 Compsci 201, Spring 2020 42 1/24/2020 Compsci 201, Spring 2020 43

  7. Tradeoff: Sort first, keep unique WOTO • There are duplicates in list, but it's sorted http://bit.ly/201spring20-0124-1 • Process sorted elements, add to end if unique • Use of copy, why for-loop starts at 1 (priming) 1/24/2020 Compsci 201, Spring 2020 44 1/24/2020 Compsci 201, Spring 2020 45 Measurement and Analysis Analysis via Pictures • We measured runtimes empirically • Reverse alphabetical order, shift all strings • Same on laptop tomorrow? Next year? • Shift 1, then 2, then …, finally N strings • What about your computer, super computer? • 1+2+ … + N = N(N+1)/2 • Roughly N 2 • Mathematical analysis of runtimes • Square with side N? • Machine independent • Compare algorithms without timing them! 1/24/2020 Compsci 201, Spring 2020 46 1/24/2020 Compsci 201, Spring 2020 47

  8. Joy Buolamwini From Point to Nbody … • Founded Algorithmic Justice League • Making a Point class to learn about objects • Rhodes Scholar, Anita Borg Scholar • What's familiar can be helpful • TedX: Fighting Algorithmic Bias • Facial Recognition Bias • Concepts for Nbody • MIT MS with Ethan Zuckerman • Constructing objects, reading from files And so in exploring this [facial recognition], I could have viewed my face not being consistently detected as, “Oh, this is a technical challenge” — but being in the space of the Center for Civic Media definitely orients me to [say], “This is not just a technical challenge … this is as much a reflection of society as other spaces where you see inequities that need to be addressed.” https://mitadmissions.org/blogs/entry/interview-joy-buolamwini / 1/24/2020 Compsci 201, Spring 2020 48 1/24/2020 Compsci 201, Spring 2020 49 What’s the Problem? Java is Object-Oriented https://coursework.cs.duke.edu/201spring20/classcode/blob/master/src/PointDriver.java • Generate points and add to list if not there • Every class is-a Object, Java parlance: extends • Which method called is static method below? • Inherits certain properties of Object.java • API: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html • 201: toString(), equals(.), hashCode() • New classes can override these methods • How do you print yourself? Compare yourself? • How can we remember this? 1/24/2020 Compsci 201, Spring 2020 50 1/24/2020 Compsci 201, Spring 2020 53

  9. Why does .contains fail ? A Few ArrayList details • Points (x,y) with 0 <= x < 2 and 0 <= y < 2 • Access to class via import statement • How many are there? How many generated? • Definition of ArrayList variable <…> • What happens when list.add(..) called? 1/24/2020 Compsci 201, Spring 2020 54 1/24/2020 Compsci 201, Spring 2020 55 What’s the solution? WOTO (2+ minutes, correctness) • How does a.contains(x) work where a is an http://bit.ly/201spring20-0124-2 ArrayList<String>, ArrayList<Point> • Code below is not ArrayList method, … http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/util/ArrayList.java • Works for String, does NOT work for Point! 1/24/2020 Compsci 201, Spring 2020 56 1/24/2020 Compsci 201, Spring 2020 62

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend