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
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
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
I is for …
2/7/2020 CompSci 201, Spring 2020 2
Announcements
P2 2 out ut t today, due Thur ue Thur. F Feb eb 13 13
APT-3 d 3 due y e yesterday
iscussion 5 5 on n Feb eb 10 10
am nex next w week eek, F Feb eb 14 14
2/7/2020 CompSci 201, Spring 2020 3
PFOWBE
ig-Oh and and O O-Not
aces: L List, S , Set, a , and M Map
Problem lem S Solving ving
2/7/2020 CompSci 201, Spring 2020 4
Review ListSplicer.java, removeFirst
//course sework.cs.d s.duke.e .edu/2 /201sp spring20/classc sscode/
claratio ions ns – using ing L List<> i inter erfac ace
2/7/2020 CompSci 201, Spring 2020 5
list.remove(0)
2/7/2020 CompSci 201, Spring 2020 6
list is is Link inkedList o
rrayList, c , call L ll List<> m metho hods
list is is Ar ArrayList – cal all r l rem emove f for Ar ArrayList
list is is L Link inkedList, call all r rem emove f for L Link inkedList
list.remove(0)
2/7/2020 CompSci 201, Spring 2020 7
hat is is “faster”? LinkedLis ist or
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.4RemoveFirst
linked array Linear (linked)Access all elements randomly
2/7/2020 CompSci 201, Spring 2020 8
hat is is “faster”? LinkedLis ist or
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 100000110000120000130000140000150000Random Access
linked arrayMeasuring Efficiency
hich is is f faster, L Link inkedList o
ArrayList?
ist.r .rem emove(0) 0): :
2/7/2020 CompSci 201, Spring 2020 9
Random Access Efficiency
Access f s for L Lists ts
inkedList rand andom acc access o
n elem elements n n tim imes
rrayList rand andom ac access of n elem n elements n t n tim imes
2/7/2020 CompSci 201, Spring 2020 10
Big-Oh aka O-Notation
uitio ion: b n: beha havio vior in the l limit it m mat atters
measure performance in terms of N
y = 3x y = 6x - 2 y = 15x + 44 y = x2 y = x2 - 6x+ 9 y = 3x2 + 4x
2/7/2020 CompSci 201, Spring 2020 11
More on O-Notation
vides es t theo eoretic ical a l analysis
can obscure some, empirical details
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
2/7/2020 CompSci 201, Spring 2020 12
Big-Oh for Algorithms
search: h: g gues ess number 1 1-1024, 024, hi, i,lo lo,correct
uent ntial/line ial/linear ar s sear arch: ch: e every e elem ement nt o
2/7/2020 CompSci 201, Spring 2020 13
Big-Oh for More Algorithms
icient nt s sorting ing: : merge, q , quick ck, Tim
ng at at e ever ery p pair, o
sorting ing, e e.g., ., bubble
2/7/2020 CompSci 201, Spring 2020 14
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
WOTO
http:// //bi bit.ly/2 /201spr pring20-02 0207 07-1
2/7/2020 CompSci 201, Spring 2020 19
What do they all have in common?
They all t all took a a CompSci course a at Duke! e!
2/7/2020 CompSci 201, Spring 2020 20
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
Problems and Solutions
tring th that o
ccurs m most st i in a list o
trings?
2/7/2020 CompSci 201, Spring 2020 31
the fox cried fat tears 2 4 1 3 5
0 1 2 3 4
How does the code work?
ss each s string s s
2/7/2020 CompSci 201, Spring 2020 33
What is complexity of this code?
2/7/2020 CompSci 201, Spring 2020 34
earch f for eac each w word and and … … if if occurs a at k k
arch c h complex lexit ity? O O(M) w when M n M differ erent nt words
some of M words
Tracking N strings
2/7/2020 CompSci 201, Spring 2020 35
lexit ity o
h? O O(M) f for M differ erent nt w words
Should we be more precise?
ing M M dif ifferent w words w will b ill be e O(M2)
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.
different words
for easier analysis.
2/7/2020 CompSci 201, Spring 2020 36
CountingStringsFile.java
enerate an an Ar ArrayList of S Stri rings gs
2/7/2020 CompSci 201, Spring 2020 37
Understanding O-notation
This is is an an up upper er b bound und and and in in the lim he limit it
rder o r of growth
n anal analyzing c code and and c code f e fragments
2/7/2020 CompSci 201, Spring 2020 38
Why coeffficients don’t matter
2/7/2020 CompSci 201, Spring 2020 39
Just Say No.. When you can
2/7/2020 CompSci 201, Spring 2020 41
Example: Analyze using big-Oh
2/7/2020 CompSci 201, Spring 2020 42
hat is is runt untime o
urn v n value ue o
Counting for O-notation
hy is is O O(1) 1) c complexity o
um + += n
2/7/2020 CompSci 201, Spring 2020 43
Example 2: Analyzing O-Notation
hat is is big ig-Oh o
untim ime o
all calc(N) ?
hat is is big ig-Oh o h of va value ue r retur urned ned b by calc(N) ?
2/7/2020 CompSci 201, Spring 2020 44
WOTO
http:// //bi bit.ly/2 /201spr pring20-02 0207 07-2
2/7/2020 CompSci 201, Spring 2020 52