Section 2
Runtime
Big-Oh Review
Main ideas:
�
Big-Omega and Big-Theta
.
Practice
Practice
Practice
False
Worksheet problems
1. Prove that f(n) ∈ O(g)
How to Approach these Problems
When trying to prove something like f(n) ∈ O(g), f(n) ∈ Ω(g), or f(n) ∈ Θ(g), you need to find a c and n0.
1a) Proof - Final Solution
Let c = 70 and n0= 1. Show ∀n >= 1, 7n <= 70 * n/10
7n <= 70 * n/10
≡ 7n <= 7n
≡ n <= n
This is true because n = n ∀n >= 1 (and also for ∀n in general), meaning that n <= n too.
1b) Proof - Final Solution
Let c = 1 and n0= 20. Show ∀n >= 20, 1000 <= 1 * 3n2
1000 <= 1 * 3n2
≡ 1000/3 <= n2
This is true because n2 is strictly increasing and 202 > 1000/3
1c) Proof - Final Solution
Let c = 1 and n0= 2. Show ∀n >= 2, 2n <= 1 * 32n
2n <= 1 * 32n
≡ 2n <= 9n
≡ 1 <= (9/2)n
≡ 1 <= 4.5n
This is true because 4.5n is strictly increasing and 4.52 >= 1
1d) Proof - Final Solution
Let c = 10 and n0= 1. Show ∀n >= 1, 7n2 + 3n <= 10n4
7n2 + 3n <= 10n4
(no more simplifying necessary unless you want to)
This is true because both sides are strictly increasing and an exponent of 4 is larger than exponent of 2. Also, 10(1)4 = 7(1)2 + 3(1)
1e) Proof - Final Solution
Let c = 3 and n0= 2. Show ∀n >= 2, n + 2nlog(n) <= 3nlog(n)
n + 2nlog(n) <= 3nlog(n)
≡ n <= nlog(n)
≡ 1 <= log(n) we can do this because n >= 2
This is true because log(n) is strictly increasing and log(2) = 1
Worksheet problems
2. We provide functions f(n) and g(n). Prove that f(n) ∈ Θ(g)
2a) Proof - Final Solution
We proved the first part in Q1. Now, let c = 70 and n0= 1 and show ∀n >= 1, 7n >= 70 * n/10
7n >= 70 * n/10
≡ 7n >= 7n
≡ n >= n
This is true because n = n ∀n >= 1 (and also for ∀n in general), meaning that n >= n too. Therefore, we proved that f(n) ∈ Θ(g).
2b) Proof - Final Solution Part 1
First let c = 1 and n0= 3 and show ∀n >= 3, n3 + 10n <= 1 * 3n3
n3 + 10n <= 1 * 3n3
≡ 10n <= 2n3
≡ 5n <= n3
≡ 5 <= n2 we can do this because n >= 3
This is true because n2 is strictly increasing and 32 >= 5
2b) Proof - Final Solution Part 2
First let c = ⅓ and n0= 1 and show ∀n >= 1, n3 + 10n >= ⅓ * 3n3
n3 + 10n >= ⅓ * 3n3
≡ n3 + 10n >= n3
≡ 10n >= 0
≡ n >= 0
This is true because we claimed that n >= 1, and 1 > 0. Therefore, we proved that f(n) ∈ Θ(g).
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).