Docsity

Computational Complexity: Big O, Big Omega, and Big Theta Notations, Study notes of Computer Science

Toggle sidebar
bg1
1
Chapter 2
Computational Complexity
Computational Complexity
Compares growth of two functions
Independent of constant multipliers and
lower-order effects
Metrics
“Big O” Notation
O()
“Big Omega” Notation
Ω
()
“Big Theta” Notation
Θ
()
Big “O” Notation
f(n) =O(g(n))
If and only if
there exist two constants
c > 0
and
n0 > 0
,
such that
f(n)
≤
cg(n)
for all
n ≥ n0
iff ∃
c, n0 > 0
s.t.
∀
n
≥
n0 : 0
≤
f(n)
≤
cg(n)
n0
f(n)
cg(n)
f(n)
is
eventually
upper-
bounded by
g(n)
Big “Omega” Notation
f(n) =
Ω(g(n))
iff
∃
c, n0 > 0
s.t.
∀ n ≥ n0 , 0 ≤ cg(n) ≤ f(n)
f(n)
cg(n)
n0
f(n) is eventually
lower-bounded by g(n)
Big “Theta” Notation
f(n) =
Θ(g(n))
iff
∃
c1, c2, n0 > 0
s.t.
0 ≤ c1g(n) ≤ f(n) ≤ c2g(n), ∀ n >= n0
f(n)
c1g(n)
n0
c2g(n)
f(n)
has the
same long-term
rate of
growth as
g(n)
Examples
3n2 + 17
Ω(1),
Ω(n),
Ω(n2)
lower bounds
O(n2), O(n3),
... upper bounds
Θ(n2)
exact bound
pf3
pf4
pf5

Partial preview of the text

Download Computational Complexity: Big O, Big Omega, and Big Theta Notations and more Study notes Computer Science in PDF only on Docsity!

Chapter 2

Computational Complexity

Computational Complexity

Compares growth of two functions

Independent of constant multipliers and

lower-order effects

Metrics

“Big O” Notation O()

“Big Omega” Notation Ω ()

“Big Theta” Notation Θ ()

Big “O” Notation

 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)

n 0

f(n)

cg(n)

f(n) is eventually

upper-

bounded by g(n)

Big “Omega” Notation

 f(n) = Ω(g(n))

 iff ∃ c, n 0 > 0 s.t. ∀ n ≥ n0 , 0 ≤ cg(n) ≤ f(n)

f(n)

cg(n)

n 0

f(n) is eventually

lower-bounded by g(n)

Big “Theta” Notation

 f(n) = Θ(g(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

f(n)

c 1 g(n)

n 0

c 2 g(n)

f(n) has the

same long-term

rate of

growth as g(n)

Examples

3n^2 + 17

 Ω(1), Ω(n), Ω(n^2 )  lower bounds

 O(n^2 ), O(n^3 ), ...  upper bounds

 Θ(n^2 )  exact bound

Analogous to Real Numbers

 f(n) = O(g(n)) (a ≤ b)

 f(n) = Ω(g(n)) (a ≥ b)

 f(n) = Θ(g(n)) (a = b)

 …The above analogy is not quite accurate, but its

convenient to think of function complexity in these terms

 Caveat: The “hidden constants” in the Big-notations can

have have real practical implications.

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

 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(f(n))

 Then f(n) = Θ(g(n))

 If f(n) = Ω(g(n)) and g(n) = Ω(f(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

 Given an unsorted vector a[], find if the element X

occurs in a[]

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 a, int X) unsigned int low = 0, high = a.size()-1; while (low int <= mid high) = (low{ + high) / 2; if (a[mid] low < =X) mid + 1; else if( (^) higha[mid] = >mid X - ) 1; else return mid; } return NOT_FOUND; }  Input size: n = a.size()  Complexity: O( k iterations x (1 comparison + 1 assignment ) per loop ) = O(log(n)) Recursion long factorial( int n ) { if( n <= 1 ) return 1; else return n * factorial( n - 1 ); } long fib( int n ) { if ( n <= 1) return 1; else return fib( n – 1 ) + fib( n – 2 ); } This is really a simple loop disguised as recursion Complexity = O(n) Fibonacci Series: Terrible way to Implement recursion Complexity = O( (3/2)N^ ) That’s Exponential !! Euclid’s Algorithm  Find the greatest

common divisor (gcd)

between m and n

 Given that m ≥ n

 Complexity = O(log(N))

 Exercise:

 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**