



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




f(n) =O(g(n)) If and only if there exist two constants c > 0 and n 0 > 0 , such that f(n) ≤ cg(n) for all n ≥ n 0 iff ∃ c, n 0 > 0 s.t. ∀ n ≥ n 0 : 0 ≤ f(n) ≤ cg(n)
iff ∃ c 1 , c 2 , n 0 > 0 s.t. 0 ≤ c 1 g(n) ≤ f(n) ≤ c 2 g(n), ∀ n >= n 0
Analogous to Real Numbers
Transitivity f(n) = O(g(n)) (a ≤ b) f(n) = Ω(g(n)) (a ≥ b) f(n) = Θ(g(n)) (a = b) If f(n) = O(g(n)) and g(n) = O(h(n)) Then f(n) = O(h(n)) If f(n) = Ω(g(n)) and g(n) = Ω(h(n)) Then f(n) = Ω(h(n)) If f(n) = Θ(g(n)) and g(n) = Θ(h(n)) Then f(n) = Θ(h(n)) Symmetry/ Anti-symmetry f(n) = Θ(g(n)) (a = b) f(n) = O(g(n)) (a ≤ b) f(n) = Ω(g(n)) (a ≥ b) f(n) = Θ(g(n)) iff g(n) = Θ(f(n)) f(n) = O(g(n)) iff g(n) = Ω(f(n)) Reflexivity f(n) = O(g(n)) (a ≤ b) f(n) = Ω(g(n)) (a ≥ b) f(n) = Θ(g(n)) (a = b) f(n) = O(f(n)) f(n) = Ω(f(n)) f(n) = Θ(f(n)) Dichotomy
Then f(n) = Θ(g(n))
Then f(n) = Θ(g(n)) Arithmetic Properties Additive Property If e(n) = O(g(n)) and f(n) = O(h(n)) Then e(n) + f(n) = O(g(n) + h(n)) Multiplicative Property If e(n) = O(g(n)) and f(n) = O(h(n)) Then e(n)•f(n) = O(g(n) • h(n))
Running Time Calculations - Loops for (j = 0; j < n; ++j) { // 3 atomics } Complexity = Θ(3n) = Θ(n) Loops with Break for (j = 0; j < n; ++j) { // 3 atomics if (condition) break; } Upper bound = O(4n) = O(n) Lower bound = Ω(4) = Ω(1) Complexity = O(n) Why don’t we have a Θ (…) notation here? Loops in Sequence for (j = 0; j < n; ++j) { // 3 atomics } for (j = 0; j < n; ++j) { // 5 atomics } Complexity = Θ(3n + 5n) = Θ(n) Nested Loops for (j = 0; j < n; ++j) { // 2 atomics for (k = 0; k < n; ++k) { // 3 atomics } } Complexity = Θ((2 + 3n)n) = Θ(n^2 ) Consecutive Statements for (i = 0; i < n; ++i) { // 1 atomic if(condition) break; } for (j = 0; j < n; ++j) { // 1 atomic if(condition) break; for (k = 0; k < n; ++k) { // 3 atomics } if(condition) break; } Complexity = O(2n) + O((2+3n)n) = O(n) + O(n^2 ) = ?? = O(n^2 ) If-then-else if(condition) i = 0; else for ( j = 0; j < n; j++) a[j] = j; Complexity = ?? = O(1) + max ( O(1), O(N)) = O(1) + O(N) = O(N)
Sequential Search
for (i = 0; i < n; i++) { if (a[i] == X) return true; } return false; Input size: n = a.size() Complexity = O(n) Binary Search Given a sorted vector a[], find the location of element X unsigned { int binary_search(vector
Given that m ≥ n
Why is it O(log(N))? Exponentiation Calculate xn Example: x^11 = x^5 * x^5 * x x^5 = x^2 * x^2 * x x^2 = x * x Complexity = O( logN ) Why didn’t we implement the recursion as follows? pow(x,n/2)pow(x,n/2)x**