Lecture 5: Big O and
Case Analysis
CSE 373: Data Structures and Algorithms
1
CSE 373 24WI
CSE 373 24WI
1
Warm Up
Which of the following statements are TRUE?
Select all options that apply:
CSE 373 24WI
2
Announcements
CSE 373 24WI
3
P1 Deques
CSE 373 24WI
4
P1: Deques
ArrayDeque<E>
DEQUEUE ADT
State
Collection of ordered items
Count of items
Behavior
addFirst(item) add to front
addLast(item) add to end
removeFirst() remove from front
removeLast() remove from end
size() count of items
isEmpty() count is 0?
get(index) get 0-indexed element
LinkedDeque<E>
CSE 373 24WI
5
P1: Sentinel Nodes
Reduce code complexity & bugs
Tradeoff: a tiny amount of extra storage space for more reliable, easier-to-develop code
Tired of running into these?
Find yourself writing case after case in your linked node code?
Client View:
Implementation:
[3, 9]
Introducing
Sentinel Nodes
CSE 373 24WI
6
P1: Gradescope & Testing
1. Write Implementation
2. Think about edge cases, Write your own tests
3. Run your own tests
4. Run Gradescope tests as a double-check
CSE 373 24WI
7
P1: Working with a Partner
CSE 373 24WI
8
Questions?
CSE 373 24WI
9
Big O Proofs
CSE 373 24WI
10
O, and Omega, and Theta [oh my?]
Big-O is an upper bound
Big-Theta |
f(n) is Θ(g(n)) if f(n) is O(g(n)) and f(n) is Ω(g(n)). In other words, there exist positive constants c1, c2, n₀ such that for all n ≥ n₀ c₁ · g(n) ≤ f(n) ≤ c₂ · g(n) |
Big-Omega |
f(n) is Ω(g(n)) if there exist positive constants c, n₀, such that for all n ≥ n₀, f(n) ≥ c · g(n) |
Big-O |
f(n) is O(g(n)) if there exist positive constants c, n₀, such that for all n ≥ n₀, f(n) ≤ c · g(n) |
Big-Omega is a lower bound
Big Theta is “equal to”
CSE 373 24WI
11
Examples
Big-Theta |
f(n) is Θ(g(n)) if f(n) is O(g(n)) and f(n) is Ω(g(n)). In other words, there exist positive constants c1, c2, n₀ such that for all n ≥ n₀ c₁ · g(n) ≤ f(n) ≤ c₂ · g(n) |
Big-Omega |
f(n) is Ω(g(n)) if there exist positive constants c, n₀, such that for all n ≥ n₀, f(n) ≥ c · g(n) |
Big-O |
f(n) is O(g(n)) if there exist positive constants c, n₀, such that for all n ≥ n₀, f(n) ≤ c · g(n) |
CSE 373 24WI
12
Comparing Complexity Classes
CSE 373 24WI
13
Big-O is an upper-bound, not a fit
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
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
CSE 373 24WI
14
Big-Omega definition Plots
2n3
n2
n
1
n3
CSE 373 24WI
15
Simplified, tight big-O
In this course, we’ll essentially use:
For this course:
CSE 373 24WI
16
Questions?
CSE 373 24WI
17
Where are we?
for (i = 0; i < n; i++) {
a[i] = 1;
b[i] = 2;
}
O(n)
f(n) = 2n
CODE
COMPLEXITY
CLASS
Code Modeling
RUNTIME
FUNCTION
Asymptotic Analysis
1
2
CSE 373 24WI
18
Our Upgraded Tool: Asymptotic Analysis
TIGHT
BIG-O
RUNTIME
FUNCTION
Asymptotic
Analysis
2
O(n2)
TIGHT
BIG-OMEGA
BIG-THETA
Θ(n2)
f(n) = 10n2 + 13n + 2
We’ve upgraded our Asymptotic Analysis tool to convey more useful information! Having 3 different types of bounds means we can still characterize the function in simple terms, but describe it more thoroughly than just Big-Oh.
Ω(n2)
CSE 373 24WI
19
Our Upgraded Tool: Asymptotic Analysis
TIGHT
BIG-OH
RUNTIME
FUNCTION
Asymptotic
Analysis
2
O(n)
TIGHT
BIG-OMEGA
BIG-THETA
Does not exist for this function
isPrime()
Big-Theta doesn’t always exist for every function! But the information that Big-Theta doesn’t exist can itself be a useful characterization of the function.
Ω(1)
CSE 373 24WI
20
Algorithmic Analysis Roadmap
CODE
Code Modeling
RUNTIME
FUNCTION
1
for (i = 0; i < n; i++) {
a[i] = 1;
b[i] = 2;
}
f(n) = 2n
TIGHT
BIG-OH
Asymptotic
Analysis
2
TIGHT
BIG-OMEGA
BIG-THETA
O(n)
Θ(n)
We just finished building this tool to characterize a function in terms of some useful bounds!
Now, let’s look at this tool in more depth. How exactly are we coming up with that function?
Ω(n)
CSE 373 24WI
21
Case Analysis
CSE 373 24WI
22
Case Study: Linear Search
int linearSearch(int[] arr, int toFind) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == toFind)
return i;
}
return -1;
}
2 | 3 | 9 | 4 | 5 |
arr
toFind
2
2 | 3 | 9 | 4 | 5 |
toFind
8
i
arr
i
i
i
i
i
The number of operations doesn’t only depend on n.
Even once the since of the data is fixed there are different behaviors based on the organization of the data.
CSE 373 24WI
23
Best Case
Worst Case
On Lucky Earth
On Unlucky Earth (where it’s 2020 every year)
2 | 3 | 9 | 4 | 5 |
arr
toFind
2
i
2 | 3 | 9 | 4 | 5 |
arr
toFind
8
i
f(n) = 3n + 1
f(n) = 2
O(1)
Θ(1)
O(n)
Θ(n)
After asymptotic analysis:
After asymptotic analysis:
CSE 373 24WI
24
Case Analysis
Case: a description of inputs/state for an algorithm that is specific enough to build a code model (runtime function) whose only parameter is the input size
Worst
Best
Other Cases
CSE 373 24WI
25
Caution: Common Misunderstanding
Best/Worst case is based on all variation other than value of n
Incorrect - based on specific values of n
Correct - based on state of data structure regardless of n
CSE 373 24WI
26
Other cases
“Assume X won’t happen” case
“Average” case
“In-practice” case
All of these can be combined with any of O, Ω, and θ
CSE 373 24WI
27
How to do case analysis
CSE 373 24WI
28
Algorithmic Analysis Roadmap
TIGHT
BIG-OMEGA
BIG-THETA
TIGHT
BIG-O
Asymptotic
Analysis
OTHER CASE
FUNCTION
WORST CASE
FUNCTION
BEST CASE
FUNCTION
Case Analysis
CODE
2
for (i = 0; i < n; i++) {
if (arr[i] == toFind) {
return i;
}
}
return -1;
f(n) = 2
O(n)
Θ(n)
f(n) = 3n+1
Ω(n)
1
CSE 373 24WI
29
Review Algorithmic Analysis Roadmap
for (i = 0; i < n; i++) {
if (arr[i] == toFind) {
return i;
}
}
return -1;
TIGHT
BIG-OMEGA
BIG-THETA
TIGHT
BIG-O
Asymptotic
Analysis
OTHER CASE
FUNCTION
WORST CASE
FUNCTION
BEST CASE
FUNCTION
Case Analysis
CODE
2
1
f(n) = 2
f(n) = 3n+1
O(1)
Θ(1)
Ω(1)
CSE 373 24WI
30
When to do Case Analysis?
f(n)
n
toFind position
At front
(Best Case)
Not present
(Worst Case)
CSE 373 24WI
31
Appendix
CSE 373 24WI
32
Applying Big O Definition
Show that
is
Apply definition term by term
Add up all your truths
Big-O |
f(n) is O(g(n)) if there exist positive constants c, n₀, such that for all n ≥ n₀, f(n) ≤ c · g(n) |
CSE 373 24WI
33
Exercise: Proving Big O
Big-O |
f(n) is O(g(n)) if there exist positive constants c, n₀, such that for all n ≥ n₀, f(n) ≤ c · g(n) |
CSE 373 24WI
34
Writing Big-O Proofs
CSE 373 24WI
35
Big-O as an upper bound
Big-O is just an upper bound. It doesn’t have to be a good upper bound
If we want the best upper bound, we’ll ask you for a simplified, tight big-O bound. O(n²) is the tight bound for this example.
It is (almost always) technically correct to say your code runs in time O(n!).
DO NOT TRY TO PULL THIS TRICK IN AN INTERVIEW (or exam)!
CSE 373 24WI
36
Big-O is an upper-bound, not a fit
CSE 373 24WI
37