Download presentation
Presentation is loading. Please wait.
Published byIsaac Bailey Modified over 8 years ago
1
AP National Conference, 2004 1 mjs@cs.cmu.edu AP CS A and AB: New/Experienced A Tall Order? Mark Stehlik mjs@cs.cmu.edu http://www.cs.cmu.edu/~mjs
2
AP National Conference, 2004 2 mjs@cs.cmu.edu Schedule l 8:30 – 10:00 Intro, resources, what’s new l 10:15 - 12:00 Design Inheritance, Interfaces & Abstract Classes l 12:45 – 3:30 Collections -> Analysis -> Big O Reading sample questions and their rubrics
3
AP National Conference, 2004 3 mjs@cs.cmu.edu Intro l A v. AB? l Years teaching? l Java experience? l The cards (years teaching A/AB, 2 things) l Materials (AB q/ref, A2 & AB1 samples, role play)
4
AP National Conference, 2004 4 mjs@cs.cmu.edu Resources l AP Central Course descriptions Java subset Sample syllabi l AP list-serve l JETT workshops 8/6 & 7 at Florida International
5
AP National Conference, 2004 5 mjs@cs.cmu.edu What’s new? l 2-D arrays to AB l Well, Java… No reference parameters Collections l But also… Design Analysis Priority Queues
6
AP National Conference, 2004 6 mjs@cs.cmu.edu Design l Interfaces – proscribe a set of behaviors methods are public by default NO implementation can be provided NO instance variables can be declared cannot, therefore, be instantiated what does that mean? examples: stack and queue l Classes realize interfaces by implementing all methods
7
AP National Conference, 2004 7 mjs@cs.cmu.edu Design… l vs. Abstract classes some methods implemented others designated as abstract (which forces the class to be abstract) – these must be implemented by class(es) that extend this abstract class (ultimately) can have instance variables
8
AP National Conference, 2004 8 mjs@cs.cmu.edu Design… l Let’s look at A2 l Let’s look at some other classes
9
AP National Conference, 2004 9 mjs@cs.cmu.edu Collections l Lists, Sets, Maps l Raises the level of design (and discourse) l But, then, what is (are) the issue(s)?
10
AP National Conference, 2004 10 mjs@cs.cmu.edu Collections… l What do they do? List ordered (positionally) – a sequence duplicates? Set unordered collection duplicates? Map Maps (unique) keys to values
11
AP National Conference, 2004 11 mjs@cs.cmu.edu Collections… l How do they do it? Analyze their algorithms But what does that mean?
12
AP National Conference, 2004 12 mjs@cs.cmu.edu Algorithm Analysis (kudos to ola) l How do you measure performance? It’s faster! It’s more elegant! It’s safer! It’s cooler! l Use mathematics to analyze the algorithm (performance as function of input size) l Implementation is another matter cache, compiler optimizations, OS, memory,…
13
AP National Conference, 2004 13 mjs@cs.cmu.edu What do we need? l Need empirical tests and mathematical tools Compare by running 30 seconds vs. 3 seconds, 5 hours vs. 2 minutes Two weeks to implement code l We need a vocabulary to discuss tradeoffs
14
AP National Conference, 2004 14 mjs@cs.cmu.edu What is big-Oh about? l 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: linear, quadratic y = 3x y = 6x-2 y = 15x + 44 y = x 2 y = x 2 -6x+9 y = 3x 2 +4x
15
AP National Conference, 2004 15 mjs@cs.cmu.edu O-notation, family of functions first family is O(n), the second is O(n 2 ) Intuition: family of curves, 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, quadruple the time
16
AP National Conference, 2004 16 mjs@cs.cmu.edu Reasoning about algorithms l We have an O(n) algorithm, For 5,000 elements takes 3.2 seconds For 10,000 elements takes 6.4 seconds For 15,000 elements takes ….? l We have an O(n 2 ) algorithm For 5,000 elements takes 2.4 seconds For 10,000 elements takes 9.6 seconds For 15,000 elements takes …?
17
AP National Conference, 2004 17 mjs@cs.cmu.edu More formal definition O-notation is an upper-bound, this means that N is O(N), but it is also O(N 2 ) ; 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) n cf(N) g(N) x = n
18
AP National Conference, 2004 18 mjs@cs.cmu.edu Other definitions l g(n) is O(f(n)) if lim g(n)/f(n) = c as n -> inf l Informally, think of O as “<=“ l Similarly, there’s a notation for lower bounds…
19
AP National Conference, 2004 19 mjs@cs.cmu.edu Big-Oh calculations from code l Search for element in array: What is complexity (using O-notation)? If array doubles, what happens to time? for(int i=0; i < a.length; i++) { if (a[i].equals(target)) return true; } return false; l Best case? Average case? Worst case?
20
AP National Conference, 2004 20 mjs@cs.cmu.edu Measures of complexity l Worst case Good upper-bound on behavior Never get worse than this l Average case What does average mean? Averaged over all inputs? Assuming uniformly distributed random data?
21
AP National Conference, 2004 21 mjs@cs.cmu.edu Some helpful mathematics l 1 + 2 + 3 + 4 + … + N N(N+1)/2 = N 2 /2 + N/2 is O(N 2 ) l N + N + N + …. + N (total of N times) N*N = N 2 which is O(N 2 ) l 1 + 2 + 4 + … + 2 N 2 N+1 – 1 = 2 x 2 N – 1 which is O(2 N )
22
AP National Conference, 2004 22 mjs@cs.cmu.edu The usual suspects l O(1) l O(log n) l O(n) l O(n log n) O( n 2 ) O( n 3 ) l … O( 2 n )
23
AP National Conference, 2004 23 mjs@cs.cmu.edu Multiplying and adding big-Oh l Suppose we do a linear search then we do another one What is the complexity? If we do 100 linear searches? If we do n searches on an array of size n?
24
AP National Conference, 2004 24 mjs@cs.cmu.edu Multiplying and adding l Binary search followed by linear search? What are big-Oh complexities? Sum? 50 binary searches? N searches? l What is the number of elements in the list (1,2,2,3,3,3)? What about (1,2,2, …, n,n,…,n)? How can we reason about this?
25
AP National Conference, 2004 25 mjs@cs.cmu.edu Analysis for AP collections l ListQueue l ArrayStack (where’s the top?) l Lists, Maps, Sets
26
AP National Conference, 2004 26 mjs@cs.cmu.edu Priority Queues l Idea l Implementation(s)
27
AP National Conference, 2004 27 mjs@cs.cmu.edu Language details l No reference parameters All passes are by value (primitives/objects) l How do you change something Through “modifier” methods Through returning a reference to a modified object (x = “changed x”)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.