Lecture 5: Case Analysis
CSE 373: Data Structures and Algorithms
1
Please log onto Live Lecture Ed Thread to submit live lecture questions
Research Study with Leah
CSE 373 22 SP – CHAMPION
2
Announcements
CSE 373 22 SP – CHAMPION
3
P1 Deques
CSE 373 SP 18 - KASEY CHAMPION
4
P1: Deques
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
ArrayDeque
LinkedDeque
P1: Sentinel Nodes
Tired of running into these?
Find yourself writing case after case in your linked node code?
Client View:
Implementation:
[3, 9]
Introducing
Sentinel Nodes
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
P1: Working with a Partner
Questions?
CSE 373 SP 18 - KASEY CHAMPION
9
Big O
CSE 373 19 SU - ROBBIE WEBER
Definition: Big-O
CSE 332 SU 18 - ROBBIE WEBER
11
Big-O
Note: Big-O definition is just an upper-bound, not always an exact bound
CSE 332 SU 18 - ROBBIE WEBER
12
Big-O is just an upper bound thay may lose and may not describe the function fully. For example the following is true:
This is a big idea!
Note: Big-O definition is just an upper-bound, not always an exact bound (plots)
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 18 - KASEY CHAMPION
13
n3
n5
n4
10n2 + 15n
The visual representation of big-O and asymptotic analysis is a big idea!
Tight Big-O Definition Plots
CSE 373 SP 18 - KASEY CHAMPION
14
n2
10n2 + 15n
Uncharted Waters: a different type of code model
CSE 373 SP 22 - CHAMPION
15
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(n % toTest == 0) {
return false;
} else {
toTest++;
}
}
return true;
}
CSE 332 SU 18 - ROBBIE WEBER
16
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
17
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
18
Examples
CSE 332 SU 18 - ROBBIE WEBER
19
Questions
CSE 373 20 SP – CHAMPION & CHUN
20
Our Upgraded Tool: Asymptotic Analysis
CSE 373 SP 22 - CHAMPION
TIGHT
BIG-OH
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.
Our Upgraded Tool: Asymptotic Analysis
CSE 373 SP 22 - CHAMPION
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.
Algorithmic Analysis Roadmap
CSE 373 SP 22 - CHAMPION
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?
Case Analysis
CSE 373 SP 18 - KASEY CHAMPION
24
Case Study: Linear Search
CSE 373 SP 22 - CHAMPION
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
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:
Case Analysis
CSE 373 SP 22 - CHAMPION
Worst
Best
Other Cases
CSE 373 19 SU - ROBBIE WEBER
!
!
CSE 373 19 SU - ROBBIE WEBER
| Big-O | Big-Omega | Big-Theta |
Worst Case | No matter what, as gets bigger, the code takes at most this much time | Under certain circumstances, as gets bigger, the code takes at least this much time | On the worst input, as gets bigger, the code takes precisely this much time (up to constants). |
Best Case | Under certain circumstances, even as gets bigger, the code takes at most this much time. | No matter what, even as gets bigger, the code takes at least this much time. | On the best input, even as gets bigger, the code takes precisely this much time (up to constants) |
“worst input”: input that causes the code to run slowest.
Other cases
CSE 373 19 SU - ROBBIE WEBER
How to do case analysis
CSE 373 19 SU - ROBBIE WEBER
Algorithmic Analysis Roadmap
CODE
BEST CASE
FUNCTION
for (i = 0; i < n; i++) {
if (arr[i] == toFind) {
return i;
}
}
return -1;
f(n) = 2
TIGHT
BIG-OH
2
TIGHT
BIG-OMEGA
BIG-THETA
O(n)
Θ(n)
1
Asymptotic
Analysis
WORST CASE
FUNCTION
OTHER CASE
FUNCTION
Case Analysis
f(n) = 3n+1
Review Algorithmic Analysis Roadmap
CODE
BEST CASE
FUNCTION
for (i = 0; i < n; i++) {
if (arr[i] == toFind) {
return i;
}
}
return -1;
f(n) = 2
TIGHT
BIG-OH
2
TIGHT
BIG-OMEGA
BIG-THETA
O(1)
Θ(1)
1
Asymptotic
Analysis
WORST CASE
FUNCTION
OTHER CASE
FUNCTION
Case Analysis
f(n) = 3n+1
When to do Case Analysis?
f(n)
n
toFind position
At front
(Best Case)
Not present
(Worst Case)
Questions
CSE 373 20 SP – CHAMPION & CHUN
35