Compsc sci 201 201 Bi Big-Oh, I Interfac aces es, M , Maps - - PowerPoint PPT Presentation

compsc sci 201 201 bi big oh i interfac aces es m maps
SMART_READER_LITE
LIVE PREVIEW

Compsc sci 201 201 Bi Big-Oh, I Interfac aces es, M , Maps - - PowerPoint PPT Presentation

Compsc sci 201 201 Bi Big-Oh, I Interfac aces es, M , Maps Susan Rodger February 7, 2020 2/7/2020 CompSci 201, Spring 2020 1 I is for Interface LinkedList implements List Inheritance LinkedList extends


slide-1
SLIDE 1

Compsc sci 201 201 Bi Big-Oh, I Interfac aces es, M , Maps

2/7/2020 CompSci 201, Spring 2020 1

Susan Rodger February 7, 2020

slide-2
SLIDE 2

I is for …

  • Interface
  • LinkedList implements List
  • Inheritance
  • LinkedList extends AbstractSequentialList

2/7/2020 CompSci 201, Spring 2020 2

slide-3
SLIDE 3

Announcements

  • Assignment P

P2 2 out ut t today, due Thur ue Thur. F Feb eb 13 13

  • Get it done early, great practice for exam
  • APT

APT-3 d 3 due y e yesterday

  • Last chance to turn in today 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/7/2020 CompSci 201, Spring 2020 3

slide-4
SLIDE 4

PFOWBE

  • Big

ig-Oh and and O O-Not

  • tation
  • Building a mathematical formalism with intuition
  • 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

2/7/2020 CompSci 201, Spring 2020 4

slide-5
SLIDE 5

Review ListSplicer.java, removeFirst

  • https://c

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

  • Decla

claratio ions ns – using ing L List<> i inter erfac ace

  • Method removeFirst, parameter list
  • Method removeFirst pass either list

2/7/2020 CompSci 201, Spring 2020 5

slide-6
SLIDE 6

list.remove(0)

2/7/2020 CompSci 201, Spring 2020 6

  • lis

list is is Link inkedList o

  • r Arra

rrayList, c , call L ll List<> m metho hods

  • If lis

list is is Ar ArrayList – cal all r l rem emove f for Ar ArrayList

  • If lis

list is is L Link inkedList, call all r rem emove f for L Link inkedList

slide-7
SLIDE 7

list.remove(0)

2/7/2020 CompSci 201, Spring 2020 7

  • 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-8
SLIDE 8

Access all elements randomly

2/7/2020 CompSci 201, Spring 2020 8

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

Measuring Efficiency

  • Whic

hich is is f faster, L Link inkedList o

  • r Ar

ArrayList?

  • What does it depend on?
  • Empirically: depend on computer used?
  • ArrayLis

ist.r .rem emove(0) 0): :

  • y = 0.0064x2 – 0.0156x + 0.0238
  • R2 = 0.9984

2/7/2020 CompSci 201, Spring 2020 9

slide-10
SLIDE 10

Random Access Efficiency

  • Random A

Access f s for L Lists ts

  • alist.get(N/2) versus llist.get(N/2)
  • Does this depend on N?
  • Link

inkedList rand andom acc access o

  • f n

n elem elements n n tim imes

  • y = 0.0129x2 – 0.7137x + 1.3337
  • R2 = 0.9889
  • Arra

rrayList rand andom ac access of n elem n elements n t n tim imes

  • y – 0.0002x + 5E-05, R2 = 0.8169

2/7/2020 CompSci 201, Spring 2020 10

slide-11
SLIDE 11

Big-Oh aka O-Notation

  • Intuit

uitio ion: b n: beha havio vior in the l limit it m mat atters

  • What happens as N gets large, where we

measure performance in terms of N

  • For polynomials: leading term, no coefficients

y = 3x y = 6x - 2 y = 15x + 44 y = x2 y = x2 - 6x+ 9 y = 3x2 + 4x

  • The first family is O(n), the second is O(n2)

2/7/2020 CompSci 201, Spring 2020 11

slide-12
SLIDE 12

More on O-Notation

  • Provid

vides es t theo eoretic ical a l analysis

  • is. Independent of, and

can obscure some, empirical details

  • Compare: 20N hours v N2 microseconds
  • Which is better? Does it depend?
  • If an alg

an algorithm is is O O(N) it it’s als also O(N2) f fro rom a m a technic chnical, m , mat athe hematical p ical perspect ectiv ive

  • O is an upper bound, in the limit
  • We try to provide tight, or best bounds/analysis

2/7/2020 CompSci 201, Spring 2020 12

slide-13
SLIDE 13

Big-Oh for Algorithms

  • Binary s

search: h: g gues ess number 1 1-1024, 024, hi, i,lo lo,correct

  • # of guesses? O(log N) note 210 = 1024
  • If 12 seconds for 210 then 24 seconds for 220
  • Seque

uent ntial/line ial/linear ar s sear arch: ch: e every e elem ement nt o

  • f list
  • # elements examined? O(N)
  • If 12 seconds for 210 then 24 seconds for 211
  • Double input, double time

2/7/2020 CompSci 201, Spring 2020 13

slide-14
SLIDE 14

Big-Oh for More Algorithms

  • Efficie

icient nt s sorting ing: : merge, q , quick ck, Tim

  • # elements examined? O(N log N)
  • More time than linear, but not terrible
  • Looking

ng at at e ever ery p pair, o

  • r slow s

sorting ing, e e.g., ., bubble

  • # elements examined? O(N2)
  • 12 seconds for 210 then 144 seconds for 211
  • Double the input, square the time

2/7/2020 CompSci 201, Spring 2020 14

slide-15
SLIDE 15

Running times in seconds machine: 109 instructions/sec

N O(log N) O(N) O(N log N) O(N2) 10 3E-9 1E-8 3.3E-8 0.0000001 100 7E-9 1E-7 6.64E-7 0.0001 1,000 1E-8 1E-6 0.00001 0.001 10,000 1.3E-8 0.00001 0.0001329 0.102 100,000 1.7E-8 0.0001 0.001661 10.008 1,000,000 0.00000002 0.001 0.0199 16.7 min 1,000,000,000 0.00000003 1.002 65.8 31.8 years

2/7/2020 CompSci 201, Spring 2020 15

slide-16
SLIDE 16

WOTO

http:// //bi bit.ly/2 /201spr pring20-02 0207 07-1

2/7/2020 CompSci 201, Spring 2020 19

slide-17
SLIDE 17

What do they all have in common?

  • The

They all t all took a a CompSci course a at Duke! e!

2/7/2020 CompSci 201, Spring 2020 20

slide-18
SLIDE 18

ArrayList Methods

Arra rrayList<String> words ds = = new w Arraylis list<>(); words ds.add dd(“ (“cat”); ); words ds.add dd(“ (“fish”); ); words ds.add dd(“do dog”) ”); Stri ring b g b = word rds.ge get(1 (1); ); wo words.set(2, “ 2, “frog”); int int c c = words.ind index exOf(“ (“cat”); ); wo words.set(1, 1, word rds.ge get(c)); ));

2/7/2020 CompSci 201, Spring 2020 22

slide-19
SLIDE 19

Problems and Solutions

  • Str

tring th that o

  • ccu

ccurs m most st i in a list o

  • f str

trings?

  • CountingStringsBenchmark.java, two ideas
  • See also CountingStringsFile for same ideas
  • https://coursework.cs.duke.edu/201spring20/classcode
  • Parallel arrays: word[k] occurs count[k] times
  • Use ArrayLists: 2 “the”, 3 “fat”, 4 “fox”

2/7/2020 CompSci 201, Spring 2020 31

the fox cried fat tears 2 4 1 3 5

0 1 2 3 4

slide-20
SLIDE 20

How does the code work?

  • Process

ss each s string s s

  • First time words.add(s),counter.add(1)
  • Otherwise, increment count corresponding to s
  • c[x] += 1 ?

2/7/2020 CompSci 201, Spring 2020 33

slide-21
SLIDE 21

What is complexity of this code?

2/7/2020 CompSci 201, Spring 2020 34

  • Sea

earch f for eac each w word and and … … if if occurs a at k k

  • +1 to counter.get(k), else add at end
  • Sear

arch c h complex lexit ity? O O(M) w when M n M differ erent nt words

  • One search is O(M) – what about all searches?
  • Tracking all words. First time zero, then one, …
  • Avoid analyzing duplicates for the moment
  • Will take longer if we have multiple occurrences of

some of M words

slide-22
SLIDE 22

Tracking N strings

2/7/2020 CompSci 201, Spring 2020 35

  • Complex

lexit ity o

  • f search?

h? O O(M) f for M differ erent nt w words

  • Complexity of words.indexOf(..) is O(M)
  • what about all calls? 1 + 2 + … N is N(N+1)/2

O(N2)

slide-23
SLIDE 23

Should we be more precise?

  • Addin

ing M M dif ifferent w words w will b ill be e O(M2)

  • 1 + 2 + … + M = M(M+1)/2
  • Addin

ing d dup upli licates: w we e need need t to be e prec ecise ab about adding ng N N total w al words.

  • Sometimes word will be found, still O(M) for M

different words

  • We have both M and N here, but treat M == N

for easier analysis.

2/7/2020 CompSci 201, Spring 2020 36

slide-24
SLIDE 24

CountingStringsFile.java

  • Gene

enerate an an Ar ArrayList of S Stri rings gs

  • Find the word that occurs the most often
  • See three different methods

2/7/2020 CompSci 201, Spring 2020 37

slide-25
SLIDE 25

Understanding O-notation

  • This

This is is an an up upper er b bound und and and in in the lim he limit it

  • Coefficients don’t matter, ord

rder o r of growth

  • N + N + N + N = 4N is O(N) --- why?
  • N*N is O(N2) – why?
  • O(1) means independent of N, constant time
  • In

n anal analyzing c code and and c code f e fragments

  • Account for each statement
  • How many times is each statement executed?

2/7/2020 CompSci 201, Spring 2020 38

slide-26
SLIDE 26

Why coeffficients don’t matter

2/7/2020 CompSci 201, Spring 2020 39

slide-27
SLIDE 27

Just Say No.. When you can

O(n2)

2/7/2020 CompSci 201, Spring 2020 41

slide-28
SLIDE 28

Example: Analyze using big-Oh

2/7/2020 CompSci 201, Spring 2020 42

  • Wha

hat is is runt untime o

  • f stuff(N)
  • How to reason about this
  • What is retur

urn v n value ue o

  • f stuff(N)
  • What if code changes to sum += k
slide-29
SLIDE 29

Counting for O-notation

  • Why

hy is is O O(1) 1) c complexity o

  • f sum

um + += n

  • Is this O(1) for any x += y ?
  • Loop executes N times, doing O(1) per iteration
  • Total runtime for method? O(n)

2/7/2020 CompSci 201, Spring 2020 43

slide-30
SLIDE 30

Example 2: Analyzing O-Notation

  • Wha

hat is is big ig-Oh o

  • f run

untim ime o

  • f cal

all calc(N) ?

  • Num. of statements executed, O(1) for line 146?
  • Use calc(16) and generalize
  • Wha

hat is is big ig-Oh o h of va value ue r retur urned ned b by calc(N) ?

  • Table? k = 1, 2, 4, 8, 16, 32, 64

2/7/2020 CompSci 201, Spring 2020 44

slide-31
SLIDE 31

WOTO

http:// //bi bit.ly/2 /201spr pring20-02 0207 07-2

2/7/2020 CompSci 201, Spring 2020 52