com ompsci psci 201 201 obj bjects ects trade deoff offs
play

Com ompsci psci 201 201 Obj bjects, ects, Trade deoff offs, - PowerPoint PPT Presentation

Com ompsci psci 201 201 Obj bjects, ects, Trade deoff offs, s, NBody dy Susan Rodger January 24, 2020 1/24/2020 Compsci 201, Spring 2020 1 E is for Encryption Why SSH and SSL work Exception A Throwable you may


  1. Com ompsci psci 201 201 Obj bjects, ects, Trade deoff offs, s, NBody dy Susan Rodger January 24, 2020 1/24/2020 Compsci 201, Spring 2020 1

  2. E is for … • Encryption • Why SSH and SSL work • Exception • A Throwable you may catch, sometimes you may rethrow 1/24/2020 Compsci 201, Spring 2020 2

  3. Announcements • Assignment ignment P0 - grace ace perio iod to to to today 11:59p 59pm • With late penalty last change one week later • APT APT-1 1 du due e – now in grace ace perio riod d to today 11:59p 59pm • Do not accept after grace period • Discussio cussion n 3 on Januar uary 27 • Prediscussion, do before • APT APT-2 2 du due e Janua uary ry 28 • Assignmen ignment P1 du due Thursda rsday, , Jan 30 1/24/2020 Compsci 201, Spring 2020 3

  4. When you submit an APT • Su Submit mit a REFL FLECT fo form fo for each ch APT • Submit mit REFLE LECT T fo form m fo for each ch Assignmen ignment 1/24/2020 Compsci 201, Spring 2020 4

  5. From Last Time … Go over WOTO: Correctness Counts http://b ://bit.l it.ly/201spr y/201spring20 ing20-0122 0122-2 1/24/2020 Compsci 201, Spring 2020 5

  6. PFTD • Objects bjects fro rom the e grou round d up • What is java.lang.Object? Its methods? • .equals(), .toString(), later .hashCode() • Conc ncep epts ts in P1: Arra rays, , Scan anners, ners, Testin ing • Completing P1 with minimal angst • Arra rayList yList fro rom high to to low level el (mos ostl tly Frid iday) • Fits into Collections hierarchy • How to build it or do it yourself: diyad 1/24/2020 Compsci 201, Spring 2020 6

  7. Charles Isbell • Context matters: Threads • Machine learning researcher • Systems that interact intelligently with many other intelligence agents • Dean College of Computing @ gtech • Rethinking education: Online Masters in Computer Science http://www.pbs.org/newshour/bb/online-graduate-programs-offer- degrees-significant-savings/ For me, the differences are simple to state: Computationalists grok that models, languages and machines are equivalent . 1/24/2020 Compsci 201, Spring 2020 7

  8. Algorithmic Tradeoffs • We will use e a pro roblem lem to to underst erstand and algo gorithmic rithmic trad ade-of offs and how ArrayList List works • java.util.ArrayList is "growable array", but more! • What is the class, what is the package • Package is a collection of related classes • Given en a list t of word rds, s, find the e unique ue word rds • Algorithms with ArrayLists • Alternative with Set data structure 1/24/2020 Compsci 201, Spring 2020 8

  9. Array and ArrayList • Array can n hold primiti itive or Object ect types • int[] and String[] work • Fixed size, cannot grow • Use java. a.ut util il clas ass ArrayList fo for gro rowth, th, more https:// ps://do docs cs.o .oracle cle.co com/e m/en/ n/java/j /javase/11/do se/11/docs cs/a /api pi/j /java.ba .base/ja se/java/u /uti til/Ar l/ArrayLis yList. t.ht html ml • Contain object types, not primitives • Use .get(),.set() and not [] for indexing 1/24/2020 Compsci 201, Spring 2020 9

  10. Look at Code: ArrayListUnique • Pro roblem lem • Read words from a file • Want the unique words in sorted order 1/24/2020 Compsci 201, Spring 2020 10

  11. Tradeoffs: Algorithmic Approaches • https://cour urse sework. k.cs.duk uke.edu/ u/20 201sp 1spring20/cl ing20/class sscode/sr src • Read d word rds s fro rom m a file, , sto tore e in Arra rayList List, class ss is ArrayListUnique – why arra ray doesn sn't 't work? • Tradeoffs in creating sorted list of unique words • Algorithmic rithmic concep cepts ts with ArrayList List met ethod hods • Compare three different algorithmic approaches • Reasoning with and learning about Java code 1/24/2020 Compsci 201, Spring 2020 11

  12. Method A: Add each word to a sorted list • Code e in metho ethodA: : pro roces cess s each ch word rd in list st, , add X to to list t of sorted ed, , unique que word rds • If X already in sorted-list? Nothing to do • If X greater than all words in list? Add at end • Some word greater than X? shift to make room 1/24/2020 Compsci 201, Spring 2020 12

  13. Example: insert “egg” “egg” “egg” comes after “cat” 1/24/2020 Compsci 201, Spring 2020 13

  14. Method A: How to shift to add "in middle" • Find first st element ement bigger ger than an String ring X at at index ex k • 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) 1/24/2020 Compsci 201, Spring 2020 16

  15. Method A: Processing every string • Add unique ue element ments fro rom list t to to ret, et, keep ep sorted ed • Code reason: flag, break, best, worst cases … 1/24/2020 Compsci 201, Spring 2020 26

  16. Method A: Details of shifting to add X • Don't 't know about ut list.add(k,X) then … • Shift from end to index > X, then add X 1/24/2020 Compsci 201, Spring 2020 27

  17. Method B: Tradeoff: Sort first, keep unique • There e are e du duplic icates es in list, t, but it's s sorted ed • 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 28

  18. Example: Sorted with duplicates k copy 1/24/2020 Compsci 201, Spring 2020 29

  19. Comparing Tradeoffs: Performance • Bot oth h methodA and methodB pro roces cess every ery word rd in the e list t of word rds • In loop body in methodA , shift happens • Could every element be shifted every time? • Shift 1, then 2, then 3, then … then shift N • Total work done? 1 + 2 + … + N 1/24/2020 Compsci 201, Spring 2020 38

  20. Sort first, why is this faster? • Bot oth h metho ethodA and meth ethodB odB pro roces cess s every ery word rd in the e list t of word rds • In loop body in methodB , NO shift happens • But, all strings sorted before loop • Sorting ting takes es N x l log N fo for N strings rings • Shifti fting ng takes es 1 + 2 2 + .. + N = = N(N+1)/ )/2 • If N = = one millio lion? n? One billio ion n opera ratio tions ns/sec /second nd • Sorting is 20 million, shifting is 0.5 trillion 1/24/2020 Compsci 201, Spring 2020 39

  21. Method C: What if we use API, other classes • A s set t contains tains no duplic licates, es, a TreeSe eeSet mainta ntains ins unique ue element ments s in sorted ed ord rder er • Create set, contains no duplicates • Create ArrayList from set • Where are the loops? 1/24/2020 Compsci 201, Spring 2020 40

  22. What you will know … • Which ch of metho ethodA, , meth ethodB dB, , method ethodC is bet etter er? • It depends, but on what does it depend? • How does s metho ethodA scale ale as as # word rds s increa eases? es? • 1 + 2 + … + N = N(N+1)/2, just say no! • What at is log 2 (1,024)? or log 2 (1,048,576)? • Well, 2 10 = 1024 so … 1/24/2020 Compsci 201, Spring 2020 41

  23. Why is methodA slow? • Add unique ue elements ments fro rom list t to to ret, et, keep ep sorted ed • Code reason: flag, break, best, worst cases … 1/24/2020 Compsci 201, Spring 2020 42

  24. Java Concepts • Loops execut ecute until il loop-guar uard is false se • break exits loop early • continue re-checks guard, skipping body • Some e loops need d initia tiali lizatio tion n bef efore e loop gu guar ard • aka "priming the loop", e.g., done = false • if (!done) same e as as if (done == false) 1/24/2020 Compsci 201, Spring 2020 43

  25. Tradeoff: Sort first, keep unique • There e are e duplic icates es in list, t, but it's s sorted ed • 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

  26. WOTO http://bit.l ://bit.ly/201spri y/201spring20 ng20-01 0124 24-1 1/24/2020 Compsci 201, Spring 2020 45

  27. Measurement and Analysis • We measur sured ed runtimes ntimes empiric irically lly • Same on laptop tomorrow? Next year? • What about your computer, super computer? • Math athema ematic ical al anal alysi sis of runtim times es • Machine independent • Compare algorithms without timing them! 1/24/2020 Compsci 201, Spring 2020 46

  28. Analysis via Pictures • Reverse erse alphab habetic tical l ord rder er, , shift ft all stri ring ngs • Shift 1, then 2, then …, finally N strings • 1+2+ … + N = N(N+1)/2 • Roughly N 2 • Square with side N? 1/24/2020 Compsci 201, Spring 2020 47

  29. Joy Buolamwini • Found nded ed Algorithmic rithmic Justic tice e Leag ague ue • Rhodes Scholar, Anita Borg Scholar • TedX: Fighting Algorithmic Bias • Facial Recognition Bias • MIT MS with Ethan Zuckerman 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

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