1 / 23

CompSci 201, Analysis+Markov

CompSci 201, Analysis+Markov. Jeff Forbes February 9, 2018. H is for …. Hashing Distribute keys evenly and efficiently HTTP HyperText Transfer Protocol Hacking For the social good… Hexadecimal 0x001A57 is Duke Blue. Plan for the Day.

lfreeman
Download Presentation

CompSci 201, Analysis+Markov

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CompSci 201, Analysis+Markov Jeff Forbes February 9, 2018 Compsci 201, Fall 2017, Analysis+Markov

  2. H is for … • Hashing • Distribute keys evenly and efficiently • HTTP • HyperText Transfer Protocol • Hacking • For the social good… • Hexadecimal • 0x001A57 is Duke Blue Compsci 201, Fall 2017, Analysis+Markov

  3. Plan for the Day • How do we comparing objects in Java, so they can be ordered? • What is object-oriented programming? • How does that impact Markov? • How can we make claims about the efficiency of algorithms/programs? • Motivating empirical and mathematical analysis CompSci 201, Spring 2018, Analysis+Markov

  4. Comparing and Sorting • Arrays.sort, Collections.sort • {“ant”, “bat”, “cat”, “dog”} • What algorithm is used in sorting? • How to change to sort-in-reverse or other order • Strings are Comparable • Lexicographic order • “zebra” > “aardvark” but “Zebra” < “aardvark” CompSci 201, Spring 2018, Analysis+Markov

  5. (x,y) < (z,w) • Can we compare Point objects? • http://stackoverflow.com/questions/5178092/sorting-a-list-of-points-with-java • https://stackoverflow.com/questions/6886836/can-i-use-the-operator-to-compare-point-objects-in-java • To be “comparable”, implement the Comparable interface, supply .compareTo(..) method CompSci 201, Spring 2018, Analysis+Markov

  6. compareTo • takes another Object (of the same class) as an argument • Returns • a negative value if the current object is less than the argument, • zero if the argument is equal, and • a positive value if the current object is greater than the argument x ≤ y is equivalent to x.compareTo(y) <= 0

  7. Build on What You Know • How does .equals work? • Make sure you have the correct type • Cast, compare public boolean equals(Object o) { if (o == null || ! (o instanceof Point)) { return false; } Point p = (Point) o; return p.x == x && p.y == y; } CompSci 201, Spring 2018, Analysis+Markov

  8. Extend what you know • This is method in Point class Point implements Comparable<Point> • How do we extend to WordGram? public intcompareTo(Point p) { if (this.x < p.x) return -1; if (this.x > p.x) return 1; if (this.y < p.y) return -1; if (this.y > p.y) return 1 return 0; } CompSci 201, Spring 2018, Analysis+Markov

  9. What is a Java Interface? • An enforceable abstraction: methods required • Set and Map interfaces • Comparable interface • If Set<String> is parameter then can pass … • HashSet<String> or TreeSet<String> • Can sort String or anything that’s Comparable • Call .compareTo(..) method CompSci 201, Spring 2018, Analysis+Markov

  10. What does Object-Oriented mean? • Very common method of organizing code • Design classes, which encapsulate state and behavior • Some classes can be similar to, but different from their parent class: inheritance • Super class, subclass • Inherit behavior, use as is or modify and use or both • Hard to design a hierarchy of classes, but important • More of this in CompSci 308 or on-the-job training • We solve simple problems, don't design re-usable libraries • Simple doesn't mean it's not hard/difficult • OO in Markov?

  11. ShafiGoldwasser • 2012 Turing Award Winner • RCS professor of computer science at MIT • Twice Gödel Prize winner • Grace Murray Hopper Award • National Academy • Co-inventor of zero-knowledge proof protocols Work on what you like, what feels right, I now of no other way to end up doing creative work CompSci 201, Spring 2018, Analysis+Markov

  12. Why use an interface? CompSci 201, Spring 2018, Analysis+Markov

  13. Why use an Interface? • Work with frameworks, e.g., java.util.Collection • Iterable, Serializable, and more – use with Java • ArrayList, LinkedList, TreeSet, HashSet all … • .clear(), .contains(o), .addAll(..), .size(), ….toArray() https://docs.oracle.com/javase/9/docs/api/java/util/Collection.html CompSci 201, Spring 2018, Analysis+Markov

  14. Concept: Inheritance • In Java, every class extends Object • Gets methods by default: .toString, .hashCode, .equals, and more • Subclass can overridebaseclassmethods • Make .equals work for String • Use @Override when changing a method from parent class • Helps compiler and developers CompSci 201, Spring 2018, Analysis+Markov

  15. Computer Science • Scientific Method • Observe some feature of the natural world. • Hypothesize a model that is consistent with the observations. • Predict events using the hypothesis. • Verify the predictions by making further observations. • Validate by repeating until the hypothesis and observations agree. • Principles • Experiments we design must be reproducible;hypothesis must be falsifiable. • In CompSci 201: • Empirical & mathematicalanalysis CompSci 201, Spring 2018, Analysis+Markov

  16. Dropping Glass Balls • Tower with 100 Floors • Given 2 glass balls • Want to determine the lowest floor from which a ball can be dropped and will break • How? • Is your algorithm the most efficient one? • Generalize to n floors CompSci 201, Spring 2018, Analysis+Markov

  17. In the worst case, how many balls will I have to drop? Glass balls continued http://bit.ly/201-s18-0209-1 • Assume the number of floors is 100 • In the best case how many balls will I have to drop to determine the lowest floor where a ball will break? If there are n floors, how many balls will you have to drop? (roughly)

  18. What is big-Oh about? (preview) • Intuition: avoid details when they don’t matter, and they don’t matter when input size (N) is big enough • For polynomials, use only leading term, ignore 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) • Intuition: family of curves, generally the same shape • More formally: O(f(n)) is an upper-bound, when n is large enough the expression cf(n) is larger • Intuition: linear function: double input, double time, quadratic function: double input, quadruplethe time

  19. cf(N) g(N) x =n0 More on O-notation, big-Oh • Big-Oh hides/obscures some empirical analysis, but is good for general description of algorithm • Allows us to compare algorithms in the limit • 20N hours vs N2 microseconds: which is better? • O-notation is an upper-bound, this means that N is O(N), but it is also O(N2); we try to provide tight bounds. Formally: • g(N) ∈ O(f(N)) iff there exist constants c and n0 such that for all g(N) < cf(N), N > n

  20. Rank orders of growth • n4grows faster than n2 • n4 ∉ O(n2) • 0.001n4 is in the same growth class as 1E6n4 • 0.001n4, 1E6n4 ∈ O(n4) • Discussion Warmup • Rank orders of growth • 1 < log2n = log10n < n0.5 < n = 0.1n + 1E9 < 2n

  21. Reasoning about growth • Consider a 3-tower • How tall is a 5-tower? • How tall is a 10 tower? • How many blocks in a 5-tower? • Which best captures the height of an n-tower? http://bit.ly/201-s18-0209-2 CompSci 201, Spring 2018, Analysis+Markov

  22. Array vs. ArrayList • Run the code ArrayVsArrayList.java • https://coursework.cs.duke.edu/201spring18/classwork/blob/master/src/ArrayVsArrayList.java • Change the value of argument • Submit your data here: • Submit as many times as you want http://bit.ly/201-s18-0209-3

  23. Summary • You should be able to: • Write a compareTo method for a Comparable object • Create a subclass and override some behavior • Reflect – • What’s clear? What’s still muddy? • http://bit.ly/201-s18-reflect • Discussion warmup: • Rank orders of growth • Read Big-Oh notes CompSci 201, Spring 2018, Analysis+Markov

More Related