Compsc sci 201 201 Colle lectio ions ns, Hashing hing, O - - PowerPoint PPT Presentation

compsc sci 201 201 colle lectio ions ns hashing hing o
SMART_READER_LITE
LIVE PREVIEW

Compsc sci 201 201 Colle lectio ions ns, Hashing hing, O - - PowerPoint PPT Presentation

Compsc sci 201 201 Colle lectio ions ns, Hashing hing, O Objects Susan Rodger February 5, 2020 2/5/2020 CompSci 201, Spring 2020 1 Glitchy App? 2/5/2020 CompSci 201, Spring 2020 2 H is for Hashing What better way to have


slide-1
SLIDE 1

Compsc sci 201 201 Colle lectio ions ns, Hashing hing, O Objects

2/5/2020 CompSci 201, Spring 2020 1

Susan Rodger February 5, 2020

slide-2
SLIDE 2

Glitchy App?

2/5/2020 CompSci 201, Spring 2020 2

slide-3
SLIDE 3

H is for …

  • Hashing
  • What better way to have a bucket list?
  • Hexadecimal
  • ABC is 10,11,12
  • Base 16 > Base 2?

2/5/2020 CompSci 201, Spring 2020 4

slide-4
SLIDE 4

Announcements

  • Assig

ignm nment nt P P2 out l lat ater er t this is w week

  • APT

APT-3 d due ue T Tue ues, F Feb eb 4, 4, Extended t to Thur Thurs F Feb eb 6 6

  • Last chance to turn in Friday til 11:59pm
  • Dis

iscussion 5 5 on n Feb eb 10 10

  • Prepare for exam
  • Exam

am nex next w week eek, F Feb eb 14 14

2/5/2020 CompSci 201, Spring 2020 5

slide-5
SLIDE 5

PFWBVDW

  • Interfaces

aces: L List, S , Set, a , and M Map

  • When it makes sense to use general type
  • Empirical and Analytical measures of efficiency
  • Maps: API and P

Problem lem S Solving ving

  • Keys and Values
  • Big

ig-Oh and and O O-Not

  • tation
  • Building a mathematical formalism with intuition

2/5/2020 CompSci 201, Spring 2020 6

slide-6
SLIDE 6

Midterm Coming Feb 14

  • How much

ch c code e have you w u written w en with p h paper er a and a writi ting u ute tensil? il?

  • Tests should measure what you've practiced
  • Practice writing code on paper!
  • Midterm r

m revie iew a and p d previo ious te tests ts

  • These are the best practice available
  • Will practice in Discussion
  • Logistic

tics

  • Start on time, end on time, accommodations
  • 1 page front and back of notes you bring and leave

2/5/2020 CompSci 201, Spring 2020 7

slide-7
SLIDE 7

Breakfast 201 was yummy!

  • Wed
  • ed. F

Feb eb 5 5 9:30 9:30am

  • 30

30 minu inutes, d dis iscuss w wha hatever w wit ith h me

  • Enjo

joy br breakfast

  • More

re b bre reakfas asts coming ingl…

2/5/2020 CompSci 201, Spring 2020 8

slide-8
SLIDE 8

The hashCode contract

  • Every o
  • bject

t has s .hashCode() metho hod

  • Inherited from Object, but typically overridden
  • Use @Override and read online
  • Must r

st resp spect . t .equals( s(): I If a.equals(b) ?

  • a.hashCode() == b.hashCode()
  • Converse not true! There will be collisions

2/5/2020 CompSci 201, Spring 2020 9

slide-9
SLIDE 9

When Strings Collide

  • Gene

enerate strings t tha hat w will c ill coll llid ide

  • Find

ind s suc uch s strings in in the he wild ild

2/5/2020 CompSci 201, Spring 2020 10

String hashCode ayay 3009136 ayBZ 3009136 bZay 3009136 bZbZ 3009136 String hashCode buzzards

  • 931102253

righto

  • 931102253

snitz 109586548 unprecludible 109586548

slide-10
SLIDE 10

Default: Object.equals, .hashCode When you do not override…

  • For O

Obj bjects ts p and q d q:

  • p.equals(q) is the same as p == q
  • Do p and q reference/point to same object
  • For O

Obj bject p t p

  • p.hashCode() is location in memory of object
  • Th

Thus: i if f p == q then en

  • p.hashCode() == q.hashCode()

2/5/2020 CompSci 201, Spring 2020 11

slide-11
SLIDE 11

Summary: ArrayList and HashSet

  • Both h

hav ave . e .add, .addAll, and mor more

  • Both iterable:for(Elt e : collection)
  • Both h

h have . .cont ntains lever eraging . .equal uals

  • HashSet also uses .hashCode to reduce the

collection iterated over: locker collisions

  • Object

ect h hygien ene w e when en devel eloping y your ur c classes es

  • .toString(), .equals(), .hashCode()

2/5/2020 CompSci 201, Spring 2020 13

slide-12
SLIDE 12

When Strings Collide

2/5/2020 CompSci 201, Spring 2020 14

https://www.youtube.com/watch?v=HeTShE2PiQI

slide-13
SLIDE 13

When Strings Collide

  • Gene

enerate strings t tha hat w will c ill coll llid ide

  • Find

ind s suc uch s strings in in the he wild ild

2/5/2020 CompSci 201, Spring 2020 15

String hashCode ayay 3009136 ayBZ 3009136 bZay 3009136 bZbZ 3009136 String hashCode buzzards

  • 931102253

righto

  • 931102253

snitz 109586548 unprecludible 109586548

slide-14
SLIDE 14

Concept: Inheritance

  • In Java, e

ever ery c class e extend ends O Object ect

  • Gets methods by default: .toString, .hashCode,

.equals, and more

  • Inherit metho

hod + + implement entatio ion

  • Subclass can

n overri rride bas ase c class metho hods

  • Make .equals work for Point class

2/5/2020 CompSci 201, Spring 2020 16

slide-15
SLIDE 15

Work in 201

  • How

w impo portant a are APT APTs?

  • How important are APT quizzes?
  • How important

nt a are as e assig ignm nment ents?

  • Earlier assignments, later assignments?
  • How im

important: r read eadin ing and and W WOTO in in-class

  • How important is reading?

2/5/2020 CompSci 201, Spring 2020 17

slide-16
SLIDE 16

Alphabetical Order

  • En

Encryption? Maybe n not

  • https://www2.cs.duke.edu/csed/newapt/encryption.html
  • Think about high-level algorithm
  • Apply your algorithm to: "pop", "array", "deeds"
  • Wha

hat do w we nee e need to do t to code alg algorithm?

  • Recall: 'b' + 1 == 'c'
  • Recall: array['h'] is allowed, 'h' can be index

2/5/2020 CompSci 201, Spring 2020 18

slide-17
SLIDE 17

Idea with Encryption APT

int int[] allcha lchars = new i int[256] 256]; int int nex nextLet is is 'a' String ans answer = = ""; ""; messa ssage i is feed ans answer is is ch ch is is

2/5/2020 CompSci 201, Spring 2020 19

96 97 98 99 100 ‘a’ 101 102 103 ‘f’ ‘e’ ‘d’ ‘b’ ‘c’ ‘g’

slide-18
SLIDE 18

How often does a string occur?

  • Stri

rings gs s stored i in Arra rrayList?

  • Call

Collections.frequency(list,word)

  • If in array a rather than ArrayList?

Collections.frequency(Arrays.asList(a), word) ArrayList<String> list is [“cat”, “cat”, “dog”, “fish”, “dog”, “cat”] Collections.frequency(list, “dog”) is Collections.frequency(list, “cat”) is

2/5/2020 CompSci 201, Spring 2020 36

slide-19
SLIDE 19

How often does a string occur?

  • Is

Is Collec lectio ions ns.f .freq eque uency ncy ef efficient icient? D Does i it mat atter?

  • Use Collections.frequency
  • Can create parallel arrays or use HashMap
  • Keep count[k] # occurrences of word[k]
  • Use HashMap if you know that

2/5/2020 CompSci 201, Spring 2020 38

slide-20
SLIDE 20

WOTO (correctness counts)

http:// //bi bit.ly/2 /201spr pring20-02 0205 05-1

2/5/2020 CompSci 201, Spring 2020 39

slide-21
SLIDE 21

Shafi Goldwasser

  • 2012 Turing Award Winner
  • RCS professor of computer science

at MIT

  • Twice Godel Prize winner
  • Grace Murray Hopper Award
  • National Academy
  • Co-inventor of zero-knowledge

proof protocols

Work on what you like, what feels right, I know of no other way to end up doing creative work

2/5/2020 CompSci 201, Spring 2020 40

slide-22
SLIDE 22

Why use an interface?

2/5/2020 CompSci 201, Spring 2020 41

slide-23
SLIDE 23

What is a Java Interface?

  • An enforcea

ceable a le abstractio ion: n: m metho hods r requir uired ed

  • Set, Map, List interfaces
  • Can

an im imple lement m more t e tha han o

  • ne int

ne interface

  • Can extend only one base-class!
  • Argua

uable le: M Mammal i al is a an inter erface ace

  • Do NOT inhe

T inherit method implementations

  • Do inherit methods (names, types, etc.)

2/5/2020 CompSci 201, Spring 2020 43

slide-24
SLIDE 24

Analogy: Mammals

  • Drago

gon? ?

  • Mammals
  • Birth to live young
  • Hair
  • Warm-blooded
  • Lik

ike an an int interface!

2/5/2020 CompSci 201, Spring 2020 44

slide-25
SLIDE 25

Why use an Interface?

  • Work w

with f h frameworks, e e.g., ., java.ut util il.Collect llection

  • Iterable, Serializable, and more – use with Java
  • Arra

rrayList, , LinkedLis ist, , TreeS eSet, , Has ashS hSet all all …

  • .clear(), .contains(o),

.addAll(..), .size(), … .toArray() https:// ://docs cs.o .oracle.co com/en/ /en/java/j /javase/11/ /11/docs cs/ap api/ i/j ava.base/ e/java/ a/ut util/ il/Collec llectio ion.ht n.html

2/5/2020 CompSci 201, Spring 2020 46

slide-26
SLIDE 26

There are two kinds …

  • Ther

There ar are 10 10 kind inds o

  • f peo

eople in in the he w world ld … …

  • Those who understand binary and …
  • Is this funny?
  • Has

ashS hSet/HashMap a and TreeS eSet/TreeMap ap

  • Tradeoffs in efficiency, organization
  • LinkedLis

ist/Arra rrayList

  • Tradeoffs in efficiency, organization

2/5/2020 CompSci 201, Spring 2020 47

slide-27
SLIDE 27

Link v Array

  • Get

etting ng b between t en two e element ents

  • Unsnap/Snap v Shift/Insert

2/5/2020 CompSci 201, Spring 2020 48

slide-28
SLIDE 28

Preliminaries

  • List<..> is

is an an int interface in in java.ut util il

  • LinkedList<..> and ArrayList<..>
  • Implement the interface
  • Wha

hat is is null null?

  • Variable value
  • No object

referenced

2/5/2020 CompSci 201, Spring 2020 49

slide-29
SLIDE 29

Benchmark: Empirical Analysis

  • https://c

//course sework.cs.d s.duke.e .edu/2 /201sp spring20/classc sscode/

  • In c

class ss ListSplic icer er, m metho hod removeFirst

  • List<String> parameter
  • ArrayList<String> argument passed
  • LinkedList<String> argument passed
  • Only

ly c call L ll List<..> ..> i interface ace m met etho hods

  • At runtime, call the actual object method
  • LinkedList.add v ArrayList.add

2/5/2020 CompSci 201, Spring 2020 50

slide-30
SLIDE 30

list.remove(0)

2/5/2020 CompSci 201, Spring 2020 51

  • Wha

hat is is “faster”? L Link inkedList o

  • r Arra

rrayList

slide-31
SLIDE 31

list.remove(0) – where called

2/5/2020 CompSci 201, Spring 2020 52

slide-32
SLIDE 32

list.remove(0)

2/5/2020 CompSci 201, Spring 2020 53

  • Wha

hat is is “faster”? LinkedLis ist or

  • r Arra

rrayList

y = -4E-05x + 0.0009 y = 0.0064x2 - 0.0156x + 0.0238 R² = 0.9984 0.2 0.4 0.6 0.8 1 1.2 1.4

RemoveFirst

linked array Linear (linked)

  • Poly. (array)
slide-33
SLIDE 33

Access all elements randomly

2/5/2020 CompSci 201, Spring 2020 54

  • Wha

hat is is “faster”? L Link inkedList o

  • r Arra

rrayList

slide-34
SLIDE 34

Access all elements randomly

2/5/2020 CompSci 201, Spring 2020 55

  • Wha

hat is is “faster”? LinkedLis ist or

  • r Arra

rrayList

y = 0.1292x2 - 0.7137x + 1.3337 R² = 0.9889 y = 0.0002x + 5E-05 R² = 0.8169 5 10 15 20 25 10000 20000 30000 40000 50000 60000 70000 80000 90000 100000110000120000130000140000150000

Random Access

linked array

  • Poly. (linked)

Linear (array)