compsc sci 201 201 bi big oh i interfac aces es m maps
play

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


  1. 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

  2. I is for … • Interface • LinkedList implements List • Inheritance • LinkedList extends AbstractSequentialList 2/7/2020 CompSci 201, Spring 2020 2

  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

  4. PFOWBE • Big ig-Oh and and O O-Not otation • 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

  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

  6. list.remove(0) • lis list is is Link inkedList o or 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 2/7/2020 CompSci 201, Spring 2020 6

  7. list.remove(0) • Wha hat is is “faster”? LinkedLis ist or or Arra rrayList RemoveFirst 1.4 y = 0.0064x 2 - 0.0156x + 0.0238 1.2 R² = 0.9984 1 0.8 0.6 0.4 0.2 y = -4E-05x + 0.0009 0 linked array Linear (linked) Poly. (array) 2/7/2020 CompSci 201, Spring 2020 7

  8. Access all elements randomly • Wha hat is is “faster”? LinkedLis ist or or Arra rrayList Random Access 25 y = 0.1292x 2 - 0.7137x + 1.3337 20 R² = 0.9889 15 10 5 y = 0.0002x + 5E-05 R² = 0.8169 0 10000 20000 30000 40000 50000 60000 70000 80000 90000 100000110000120000130000140000150000 linked array Poly. (linked) Linear (array) 2/7/2020 CompSci 201, Spring 2020 8

  9. Measuring Efficiency • Whic hich is is f faster, L Link inkedList o or Ar ArrayList? • What does it depend on? • Empirically: depend on computer used? • ArrayLis ist.r .rem emove(0) 0): : • y = 0.0064x 2 – 0.0156x + 0.0238 • R 2 = 0.9984 2/7/2020 CompSci 201, Spring 2020 9

  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 of n n elem elements n n tim imes • y = 0.0129x 2 – 0.7137x + 1.3337 • R 2 = 0.9889 • Arra rrayList rand andom ac access of n elem n elements n t n tim imes • y – 0.0002x + 5E-05, R 2 = 0.8169 2/7/2020 CompSci 201, Spring 2020 10

  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 = x 2 y = x 2 - 6x+ 9 y = 3x 2 + 4x • The first family is O( n ) , the second is O( n 2 ) 2/7/2020 CompSci 201, Spring 2020 11

  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 N 2 microseconds • Which is better? Does it depend? • If an alg an algorithm is is O O(N) it it’s als also O(N 2 ) 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

  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 2 10 = 1024 • If 12 seconds for 2 10 then 24 seconds for 2 20 • Seque uent ntial/line ial/linear ar s sear arch: ch: e every e elem ement nt o of list • # elements examined? O(N) • If 12 seconds for 2 10 then 24 seconds for 2 11 • Double input, double time 2/7/2020 CompSci 201, Spring 2020 13

  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 or slow s sorting ing, e e.g., ., bubble • # elements examined? O(N 2 ) • 12 seconds for 2 10 then 144 seconds for 2 11 • Double the input, square the time 2/7/2020 CompSci 201, Spring 2020 14

  15. Running times in seconds machine: 10 9 instructions/sec N O(log N) O(N) O(N log N) O(N 2 ) 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

  16. WOTO http:// //bi bit.ly/2 /201spr pring20-02 0207 07-1 2/7/2020 CompSci 201, Spring 2020 19

  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

  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

  19. Problems and Solutions • Str tring th that o occu ccurs m most st i in a list o of 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” the fox cried fat tears 0 1 2 3 4 2 4 1 3 5 2/7/2020 CompSci 201, Spring 2020 31

  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

  21. What is complexity of this code? • 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 2/7/2020 CompSci 201, Spring 2020 34

  22. Tracking N strings • Complex lexit ity o of 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(N 2 ) 2/7/2020 CompSci 201, Spring 2020 35

  23. Should we be more precise? • Addin ing M M dif ifferent w words w will b ill be e O(M 2 ) • 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

  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

  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(N 2 ) – 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

  26. Why coeffficients don’t matter 2/7/2020 CompSci 201, Spring 2020 39

  27. Just Say No.. When you can O(n 2 ) 2/7/2020 CompSci 201, Spring 2020 41

  28. Example: Analyze using big-Oh • Wha hat is is runt untime o of stuff(N) • How to reason about this • What is retur urn v n value ue o of stuff(N) • What if code changes to sum += k 2/7/2020 CompSci 201, Spring 2020 42

  29. Counting for O-notation • Why hy is is O O(1) 1) c complexity o of 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

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