Section 2
Runtime
Big-Oh Review
Main ideas:
�
Big-Omega and Big-Theta
Practice
Practice
Worksheet problems
1. Prove that f(n) ∈ O(g)
How to Approach these Problems
When trying to prove something like f(n) ∈ O(g) or f(n) ∈ Ω(g), you need to find a c and n0.
1a) Proof - Final Solution
1b) Proof - Final Solution
1c) Proof - Final Solution
1d) Proof - Final Solution
1e) Proof - Final Solution
Worksheet problems
2. We provide functions f(n) and g(n). Prove that f(n) ∈ Θ(g)
How to Approach these Problems
When trying to prove a function f(n) ∈ Θ(g), you should show that f(n) ∈ O(g) and f(n) ∈ Ω(g).
2a) Proof - Final Solution
2b) Proof - Final Solution Part 1
2b) Proof - Final Solution Part 2
Worksheet problems: Q3
Get the Θ(·) bound of each function below.
We will construct equations for each function and then simplify them to get a closed form.
3a) f(n) = worst case running time
First function (lines 3-5) always runs once.
Worst case for lines 7-16 is if every value in array is unique. This means for every iteration of the outer loop, the inner loop will iterate through the rest of the array. Total number of iterations is n + (n-1) + (n-2) + … + 1.
There is a formula for this sum: n * (n+1) / 2
f(n) = n + n(n+1)/2 => n2. This is quadratic running time
Do something similar to Q2 proofs to prove that n + n(n+1)/2 ∈ Θ(n2).
3b) g(n) = best case running time
First function (lines 3-5) always runs once.
Best case for lines 7-16 is if every value in array is the same. This means the inner loop will only run once: for the first iteration of the outer loop. Then, it will not run because every index will be visited after the first time.
g(n) = n + n => n
This is linear running time
Do something similar to Q2 proofs to prove that n + n ∈ Θ(n).