Lecture 5: Asymptotic Analysis II
CSE 373: Data Structures and Algorithms
1
Please log onto PollEv.com/champk to answer the daily lecture participation question
Please log onto https://edstem.org/us/courses/21257/discussion/1361663 to submit live lecture questions
Warm Up
CSE 373 22 SP – CHAMPION
2
public void mystery2(ArrayList<String> list) {
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
System.out.println(list.get(0));
}
}
}
+2
n(2)
Possible answer
T(n) = 4n2
n(n(2))
Construct a mathematical function modeling the runtime for the following functions
Approach
-> start with basic operations, work inside out for control structures
Please fill out the Poll at- PollEv.com/champk
Announcements
Partners
Kasey OH posted
CSE 373 22 SP – CHAMPION
3
Questions?
CSE 373 SP 18 - KASEY CHAMPION
4
Big O
CSE 373 19 SU - ROBBIE WEBER
Code to Big-Oh
CSE 373 20 AU – SCHAFER
6
CODE
BIG-OH
for (i = 0; i < n; i++) {
a[i] = 1;
b[i] = 2;
}
O(n)
?
Meet Algorithmic Analysis
CSE 373 20 AU – SCHAFER
7
COMPLEXITY
CLASS
CODE
Code Modeling
RUNTIME
FUNCTION
Asymptotic Analysis
1
2
for (i = 0; i < n; i++) {
a[i] = 1;
b[i] = 2;
}
O(n)
f(n) = 2n
Where are we?
CSE 373 20 AU – SCHAFER
8
COMPLEXITY
CLASS
CODE
Code Modeling
RUNTIME
FUNCTION
Asymptotic Analysis
1
2
for (i = 0; i < n; i++) {
a[i] = 1;
b[i] = 2;
}
O(n)
f(n) = 2n
Finding a Big Oh
CSE 373 20 AU – SCHAFER
9
= 9n2 + 3n + 3
≈ 9n2
≈ n2
f(n) is O(n2)
f(n) = (9n+3)n + 3
COMPLEXITY
CLASS
RUNTIME
FUNCTION
Asymptotic Analysis
2
We have an expression for f(n). How do we get the O() that we’ve been talking about?
1. Find the “dominating term” and delete all others.
-The “dominating” term is the one that is largest as n gets bigger. In this class, often the largest power of n.
2. Remove any constant factors.
Can we really throw away all that info?
CSE 373 20 SP – CHAMPION & CHUN
10
Simple
We don’t care about tiny differences in implementation, want the big picture result
Decisive
Produce a clear comparison indicating which code takes “longer”
Our goals:
Function growth
CSE 332 SU 18 - ROBBIE WEBER
11
…but since both are linear eventually look similar at large input sizes
whereas h(n) has a distinctly different growth rate
The growth rate for f(n) and g(n) looks very different for small numbers of input
But for very small input values h(n) actually has a slower growth rate than either f(n) or g(n)
Imagine you have three possible algorithms to choose between.
Each has already been reduced to its mathematical model
Definition: Big-O
CSE 332 SU 18 - ROBBIE WEBER
12
Big-O
We wanted to find an upper bound on our algorithm’s running time, but
-We don’t want to care about constant factors.
-We only care about what happens as n gets large.
Applying Big O Definition
CSE 332 SU 18 - ROBBIE WEBER
13
Show that
is
Big-O
Apply definition term by term
Add up all your truths
Exercise: Proving Big O
CSE 332 SU 18 - ROBBIE WEBER
14
Big-O
Writing Big-O Proofs
CSE 332 SU 18 - ROBBIE WEBER
15
Writing Big-O Proofs
CSE 332 SU 18 - ROBBIE WEBER
16
Note: Big-O definition is just an upper-bound, not always an exact bound
CSE 332 SU 18 - ROBBIE WEBER
17
Note: Big-O definition is just an upper-bound, not always an exact bound (plots)
What do we want to look for on a plot to determine if one function is in the big-O of the other?
You can sanity check that your g(n) function (the dominating one) overtakes or is equal to your f(n) function after some point and continues that greater-than-or-equal-to trend towards infinity
CSE 373 SP 22 - CHAMPION
18
If we want the most-informative upper bound, we’ll ask you for a simplified, tight big-O bound.
O(n^2 ) is the tight bound for the function f(n) = 10n2+15n. See the graph below – the tight big-O bound is the smallest upper bound within the definition of big-O.
If you zoom out a bunch, the your tight bound and your function will be overlapping compared to other complexity classes.
n3
n5
n4
10n2 + 15n
n2
10n2 + 15n
Questions?
CSE 373 SP 18 - KASEY CHAMPION
19
Uncharted Waters: a different type of code model
CSE 332 SU 18 - ROBBIE WEBER
20
Find a model f(n) for the running time of this code on input n. What’s the Big-O?
boolean isPrime(int n){
int toTest = 2;
while(toTest < n){
if(toTest % n == 0) {
return true;
} else {
toTest++;
}
}
return false;
}
Number of iterations?
-Smallest divisor of n
Prime Checking Runtime
CSE 332 SU 18 - ROBBIE WEBER
21
This is why we have definitions!
CSE 332 SU 18 - ROBBIE WEBER
22
Big-O isn’t everything
CSE 332 SU 18 - ROBBIE WEBER
23
Our prime finding code is O(n). But so is, for example, printing all the elements of a list.
Your experience running these two pieces of code is going to be very different.
It’s disappointing that the O() are the same – that’s not very precise.
Could we have some way of pointing out the list code always takes AT LEAST n operations?
CSE 332 SU 18 - ROBBIE WEBER
24
The formal definition of Big-Omega is the flipped version of Big-Oh.
When we make Big-Oh statements about a function and say f(n) is O(g(n)) we’re saying that f(n) grows at most as fast as g(n).
But with Big-Omega statements like f(n) is Ω(g(n)), we’re saying that f(n) will grows at least as fast as g(n).
Visually: what is the lower limit of this function?
What is bounded on the bottom by?
Big-Omega definition Plots
CSE 373 SP 18 - KASEY CHAMPION
25
2n3
n2
n
1
n3
CSE 332 SU 18 - ROBBIE WEBER
26
Note: this right graph’s tight O bound is O(n) and its tight Omega bound is Omega(n). This is what most of the functions we’ll deal with will look like, but there exists some code that would produce runtime functions like on the left.
f(n) = n
prime runtime function
O, and Omega, and Theta [oh my?]
Big Theta is “equal to”
CSE 332 SU 18 - ROBBIE WEBER
27
f(n) = n
To define a big-Theta, you expect the tight big-Oh and tight big-Omega bounds to be touching on the graph (meaning they’re the same complexity class)
O, and Omega, and Theta [oh my?]
CSE 332 SU 18 - ROBBIE WEBER
28
Examples
CSE 332 SU 18 - ROBBIE WEBER
29