AlgorithmLec 2asymptotic Notations
AlgorithmLec 2asymptotic Notations
Asymptotic Notations
Presented By:
Asymptotic Notations
Examples
Running time vs. Asymptotic performance
3
Bounds describe the limiting behavior of algorithm complexity at large
(n). They are:
f(N) is O(g(N)) If
positive constants c and n0, such that
f(n) ≤ c × g(n) n ≥ n0
For example
T(n) = O(n100)
T(n) will never grow asymptotically faster than n100
O-NOTATION: DEFINITION
8
Prove that
5n2 is O(n3)
Proof:
According to the definition of O(), we should find a constant c s.t.
𝒇 𝒏 ≤ 𝒄 × 𝒈 𝒏 ∀ 𝒏 ≥ 𝒏𝟎
5 × 𝑛2 ≤ 𝑐 × 𝑛3 ∀ 𝑛 > 𝑛0 , divide by n3
5
≤𝑐 ∀ 𝑛 ≥ 𝑛0
𝑛
Substitute n=5 for example , then any c≥1 will satisfy the
inequality ∀ n≥ 5
it's true
Ω-NOTATION: DEFINITION
10
f(n) is Ω(g(n)) If
positive constants c and n0, such that
c × g(n) ≤ f(n) n ≥ n0
For example:
T(n) = Ω(n3)
T(n) will never grow asymptotically slower than n3
Ω-NOTATION
ASYMPTOTIC LOWER BOUND
11
Ω-NOTATION : Example
12
Prove that
5n2 is Ω(n)
Proof:
According to the definition of Ω (), we should find a constant
c s.t.
𝒄 × 𝒈 𝒏 ≤ 𝒇 𝒏 ∀ 𝒏 ≥ 𝒏𝟎
𝑐 × 𝑛 ≤ 5 × 𝑛2 ∀ 𝑛 > 𝑛0 , divide by n
𝑐 ≤ 5 × 𝑛 ∀ 𝑛 ≥ 𝑛0
Substitute n=1 for example , then any c ≤ 5 will satisfy
the inequality ∀ n ≥ 1
it's true
Θ-NOTATION: DEFINITION
13
f(n) is Θ (g(n)) If
positive constants c1,c2 ,and n0, such that
c1×g(n) ≤ f(n) ≤ c2×g(n)
n ≥ n0
Θ(): Exact order (most difficult to compute in some algorithms)
For example:-
T(n) = Θ(n3)
T(n) grows asymptotically as fast as n3.
Θ-NOTATION: DEFINITION
14
1
Θ -NOTATION : Example
15
Prove that
5n2 is Θ(n2))
Proof:
According to the definition of Θ(), we should find 2 constants c1 & c2 s.t.
c1 × 𝒈 𝒏 ≤ 𝒇 𝒏 ≤ c2 × 𝒈 𝒏 ∀ 𝒏 ≥ 𝒏𝟎
c1 × n2 ≤ 5 × 𝑛2 ≤ c2 × n2 ∀ 𝑛 ≥ 𝑛0 , divide by 𝑛2
c1 ≤ 5 ≤ c2 ∀ 𝑛 ≥ 𝑛0
So, there's exists 𝑐1 = 4 and 𝑐2 = 6 that satisfy the inequality for all n ≥ 1
(n0 = 1)
it's true
Relations Between Bounds (asymptotic notations)
16
Theorem :
For any two functions g(n) and f(n),
f(n) = (g(n)) iff
f(n) = O(g(n)) and f(n) = (g(n)).
Prove that
5n2 is Θ(n2) O(N2) & Ω(N2)
Proof:
According to the definition of Θ(), we should find 2 constants c1 & c2 s.t.
c1 × 𝒈 𝒏 ≤ 𝒇 𝒏 ≤ c2 × 𝒈 𝒏 ∀ 𝒏 > 𝒏𝟎
c1 × n2 ≤ 5 × 𝑛2 ≤ c2 × n2 ∀ 𝑛 > 𝑛0 , divide by 𝑛2
c1 ≤ 5 ≤ c2 ∀ 𝑛 > 𝑛0
So, there's exists 𝑐1 = 4 and 𝑐2 = 6 that satisfy the inequality for all n ≥ 1 (n0 = 1)
But the left side of the inequality is the prove of the definition of Ω(N2)
and right side is the prove of O(N2)
it's true
Examples
19
Θ( 𝑛 3 ):
𝑛3
5𝑛3 + 4n
105𝑛3 + 4n2 + 6n
Θ(𝑛 ):
2
𝑛2
5𝑛2 + 4n + 6
𝑛2 + 5
Examples
20
True or false?
• N2 = O(N2) true
• 2N = O(N2) true
• N = O(N2) true
• N2 = O(N) false
• 2N = O(N) true
• N = O(N) true
Examples
21
• N2 = Θ (N2 ) true
• 2N = Θ (N2 ) false
• N = Θ (N2 ) false
• N2 = Θ (N) false
• 2N = Θ (N) true
• N = Θ (N) true
A B O Ω Θ
2N+1 2N
N (Log(N))2 N Log(N)
(N-1)! N!
Example2:
26
A B O Ω Θ
2N+1 2N yes yes yes
(N-1)! N! yes no no
Example3:
27
(𝑁−1)𝑁
= = Θ (N2 )
2
Example 4:
31
Solution:
Iterator: N, N – 10, N – 2×10, N – 3×10, …, N – L×10, where L is # iterations
𝑁−1
Termination: when N – L×10 = 1 L = 10
Complexity Time = Θ(N)
Example 4:
33