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
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Com

  • mpsci

psci 201 201 Obj bjects, ects, Trade deoff

  • ffs,

s, NBody dy

1/24/2020 Compsci 201, Spring 2020 1

Susan Rodger January 24, 2020

slide-2
SLIDE 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

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

  • stl

tly Frid iday)

  • Fits into Collections hierarchy
  • How to build it or do it yourself: diyad

1/24/2020 Compsci 201, Spring 2020 6

slide-7
SLIDE 7

Charles Isbell

1/24/2020 Compsci 201, Spring 2020 7

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.

  • 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

slide-8
SLIDE 8

Algorithmic Tradeoffs

  • We will use

e a pro roblem lem to to underst erstand and algo gorithmic rithmic trad ade-of

  • ffs 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

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 12

Method A: Add each word to a sorted list

1/24/2020 Compsci 201, Spring 2020 12

  • 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
slide-13
SLIDE 13

Example: insert “egg”

1/24/2020 Compsci 201, Spring 2020 13

“egg” “egg” comes after “cat”

slide-14
SLIDE 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

1/24/2020 Compsci 201, Spring 2020 16

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html#add(int,E)

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 18

Example: Sorted with duplicates

1/24/2020 Compsci 201, Spring 2020 29

copy k

slide-19
SLIDE 19

Comparing Tradeoffs: Performance

  • Bot
  • th

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

slide-20
SLIDE 20

Sort first, why is this faster?

  • Bot
  • th

h metho ethodA and meth ethodB

  • dB 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

slide-21
SLIDE 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

slide-22
SLIDE 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 log2(1,024)? or log2(1,048,576)?

  • Well, 210 = 1024 so …

1/24/2020 Compsci 201, Spring 2020 41

slide-23
SLIDE 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

slide-24
SLIDE 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

slide-25
SLIDE 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

slide-26
SLIDE 26

WOTO

http://bit.l ://bit.ly/201spri y/201spring20 ng20-01 0124 24-1

1/24/2020 Compsci 201, Spring 2020 45

slide-27
SLIDE 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

slide-28
SLIDE 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 N2
  • Square with side N?

1/24/2020 Compsci 201, Spring 2020 47

slide-29
SLIDE 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

1/24/2020 Compsci 201, Spring 2020 48

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/

slide-30
SLIDE 30

From Point to Nbody …

  • Making

ing a Point t class ss to to learn rn about ut object cts

  • What's familiar can be helpful
  • Conce

cepts ts fo for Nbody

  • Constructing objects, reading from files

1/24/2020 Compsci 201, Spring 2020 49

slide-31
SLIDE 31

Java is Object-Oriented

  • Every

ry class ss is is-a Object, ect, Java a parlanc lance: : extend ends

  • 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

sses can n ov

  • verride

rride thes ese e meth ethods

  • ds
  • How do you print yourself? Compare yourself?
  • How can we remember this?

1/24/2020 Compsci 201, Spring 2020 50

slide-32
SLIDE 32

What’s the Problem?

  • Gener

erate e points ts and add to to list st if not

  • t there
  • Which method called is static method below?

1/24/2020 Compsci 201, Spring 2020 53

https://coursework.cs.duke.edu/201spring20/classcode/blob/master/src/PointDriver.java

slide-33
SLIDE 33

A Few ArrayList details

  • Acces

cess to to class ss via import t statement ment

  • Def

efinit inition ion of ArrayList variable <…>

  • What

at happens ens when list.add(..) called? lled?

1/24/2020 Compsci 201, Spring 2020 54

slide-34
SLIDE 34

Why does .contains fail ?

  • Points

ts (x,y) ) with 0 <= x < 2 2 and 0 <= y < < 2

  • How many are there? How many generated?

1/24/2020 Compsci 201, Spring 2020 55

slide-35
SLIDE 35

What’s the solution?

  • How does

s a.contains(x) work k where e a is an 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

slide-36
SLIDE 36

WOTO (2+ minutes, correctness)

http://b ://bit.l it.ly/201spr y/201spring20 ing20-0124 0124-2

1/24/2020 Compsci 201, Spring 2020 62