Compsci 201, Fall 2016 9.1 TAFTW (Take Aways for the Week) ● Graded work this week: Ø APT Quiz, details and overview Ø Markov assignment, details and overview ● Concepts: Empirical and Analytical Analysis Ø Algorithms and Data Structures Ø Benchmarking and empirical analyses Ø Terminology, mathematics, analytical analyses ● Java idioms: Interfaces: general and Comparable ● Software Engineering: Unit Testing and JUnit Compsci 201, Fall 2016 9.2 APT Quiz and Markov Overview ● APT Quiz – meant to demonstrate mastery of concepts. If you don't do this now, you'll have an opportunity to demonstrate mastery later Ø Self check on where you are, help us too Ø Validate your own work with APTs Ø It's ok to do a green dance, partial dance ok too! ● Markov Assignment Ø Basics of Java Objects, real/interesting scenario Ø Do not leave this until the last two days Compsci 201, Fall 2016 9.3 Comparing objects and tradeoffs ● How are objects compared in Java? Ø When would you want to compare? Ø What can’t be compared? ● Empirical and Analytical Analysis Ø Why are some lists different? Ø Why is adding in the middle fast? Ø Why is adding in the middle slow? ● How do you measure performance? Compsci 201, Fall 2016 9.4 From Comparable to … TreeMap/Sort ● When a class implements Comparable then … Ø Instances are comparable to each other • “apple” < “zebra ”, 6 > 2 • Sorting Strings, Sorting WordPairs, … • Method compareTo invoked when … ● Comparable<…> types the parameter to compareTo Ø Return < 0, == 0, > 0 according to results of comparison
10
Embed
TAFTW (Take Aways for the Week) APT Quiz and Markov Overviewdb.cs.duke.edu/courses/compsci201/fall16/notes/sept28/slides9-4up.pdf · Compsci201, Fall 2016 9.1 TAFTW (Take Aways for
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Compsci 201, Fall 2016 9.1
TAFTW (Take Aways for the Week)● Graded work this week:
Ø APT Quiz, details and overviewØ Markov assignment, details and overview
● Concepts: Empirical and Analytical AnalysisØ Algorithms and Data StructuresØ Benchmarking and empirical analysesØ Terminology, mathematics, analytical analyses
● Java idioms: Interfaces: general and Comparable● Software Engineering: Unit Testing and JUnit
Compsci 201, Fall 2016 9.2
APT Quiz and Markov Overview● APT Quiz – meant to demonstrate mastery of
concepts. If you don't do this now, you'll have an opportunity to demonstrate mastery laterØ Self check on where you are, help us tooØ Validate your own work with APTsØ It's ok to do a green dance, partial dance ok too!
● Markov AssignmentØ Basics of Java Objects, real/interesting scenarioØ Do not leave this until the last two days
Compsci 201, Fall 2016 9.3
Comparing objects and tradeoffs● How are objects compared in Java?
Ø When would you want to compare?Ø What can’t be compared?
● Empirical and Analytical AnalysisØ Why are some lists different?Ø Why is adding in the middle fast?Ø Why is adding in the middle slow?
● How do you measure performance?
Compsci 201, Fall 2016 9.4
From Comparable to … TreeMap/Sort● When a class implements Comparable then …
Ø Instances are comparable to each other•“apple” < “zebra”, 6 > 2•Sorting Strings, Sorting WordPairs, …•Method compareTo invoked when …
● Comparable<…> types the parameter to compareTo
Ø Return < 0, == 0, > 0 according to results of comparison
ArrayList and LinkedList as ADTs● As an ADT (abstract data type) ArrayList supports
Ø Constant-time or O(1) access to the k-th elementØ Amortized linear or O(n) storage/time with add
• Total storage used in n-element vector is approx. 2n, spread over all accesses/additions (why?)
Ø Add/remove in middle is "expensive" O(n), why?
● What's underneath here? How Implemented?Ø Concrete: array – contiguous memory, must be
contiguous to support random accessØ Element 20 = beginning + 20 x size of a pointer
Compsci 201, Fall 2016 9.28
ArrayList and LinkedList as ADTs● LinkedList as ADT
Ø Constant-time or O(1) insertion/deletion anywhere, but…
Ø Linear or O(n) time to find where, sequential search
● Linked good for add/remove at frontØ Splicing into middle, also for 'sparse' structures
● What's underneath? How ImplementedØ Low-level linked lists, self-referential structuresØ More memory intensive than array: two pointers
Compsci 201, Fall 2016 9.29
Inheritance and Interfaces● Interfaces provide method names and parameters
Ø The method signature we can expect and use!Ø What can we do to an ArrayList? To a
LinkedList?Ø What can we do to a Map or Set or
PriorityQueue?Ø java.util.Collection is an interface
● New in Java 8: Interfaces can have code!
Compsci 201, Fall 2016 9.30
Nancy Leveson: Software SafetyFounded the field ● Mathematical and
engineering aspectsØ Air traffic controlØ Microsoft word
"C++ is not state-of-the-art, it's only state-of-the-practice, which in recent years has been going backwards"
● Software and steam engines once deadly dangerous?●http://sunnyday.mit.edu/steam.pdf
● THERAC 25: Radiation machine killed many people●http://sunnyday.mit.edu/papers/therac.pdf
Compsci 201, Fall 2016 9.31
Big-Oh, O-notation: concepts & caveats● Count how many times “simple” statements execute
Ø In the body of a loop, what matters? (e.g., another loop?)
Ø Assume statements take a second, cost a penny?• What's good, what’s bad about this assumption?
● If a loop is inside a loop:Ø Tricky because the inner loop can depend on the
outer, use math and reasoning● In real life: cache behavior, memory behavior,
swapping behavior, library gotchas, things we don’t understand,…
Compsci 201, Fall 2016 9.32
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 Nis O(N), but it is also O(N2); we try to provide tight bounds.
Compsci 201, Fall 2016 9.33
More on O-notation, big-Oh● 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:Ø A function g(N) is O(f(N)) if there exist
constants c and n such that g(N) < cf(N) for all N > n
cf(N)
g(N)
x = n
Compsci 201, Fall 2016 9.34
Notations for measuring complexity● O-notation/big-Oh: O(n2) is used in algorithmic
analysis, e.g., Compsci 330 at Duke. Upper bound in the limitØ Correct to say that linear algorithm is O(n2), but
useful?
● Omega is lower bound: Ω(n log n) is a lower bound for comparison based sortsØ Can't do better than that, a little hard to proveØ We can still engineer good sorts: TimSort!
Compsci 201, Fall 2016 9.35
Simple examples of array/loops: O?for(int k=0; k < list.length; k += 1) {