Professional Documents
Culture Documents
Advanced Algorithms Analysis and Design: by Arslan Anjum
Advanced Algorithms Analysis and Design: by Arslan Anjum
and Design
By
Arslan Anjum
Lg n n nlgn n2 n3 2n n!
0 1 0 1 1 2 1
1 2 2 4 8 4 2
2 4 8 16 64 16 24
3 8 24 64 512 256 10080
4 16 64 256 4096 65536 Vary large No.
5 32 160 1024 3276 Vary large Vary large No.
8 No.
Arslan Anjum Advanced Algorithms Analysis and Design
Order of Growth Functions
• The running time of an algorithm depends on input
size and nature of input.
• Growth of order
O(1)< O(log n)< O(n)< O(nlogn)< O(n2)< O(n3)<…< O(2n)< O(n!)
f(n) ∈ O(g(n))
Since
f(n) ≤ c.g(n) n2 ≤ c.n2 1 ≤ c, take, c = 1, n0= 1
Then
n2 ≤ c.n2 for c = 1 and n ≥ 1
Hence, 2n2 ∈ O(n2), where c = 1 and n0= 1
Arslan Anjum Advanced Algorithms Analysis and Design
Examples
Examples
Example 3: Prove that 1000.n2 + 1000.n ∈ O(n2)
Proof:
Assume that f(n) = 1000.n2 + 1000.n, and g(n) = n2
We have to find existence of c and n0 such that
0 ≤ f(n) ≤ c.g(n) n ≥ n0
1000.n2 + 1000.n ≤ c.n2 = 1001.n2, for c = 1001
1000.n2 + 1000.n ≤ 1001.n2
Û 1000.n ≤ n2 n2 ≥ 1000.n n2 - 1000.n ≥ 0
Û n (n-1000) ≥ 0, this true for n ≥ 1000
f(n) ≤ c.g(n) n ≥ n0 and c = 1001
Hence f(n) ∈ O(g(n)) for c = 1001 and n0 = 1000
Proof:
On contrary we assume that there exist some positive
constants c and n0 such that
0 ≤ n3 ≤ c.n2 n ≥ n0
0 ≤ n3 ≤ c.n2 n ≤ c
Since c is any fixed number and n is any arbitrary
constant, therefore n ≤ c is not possible in general.
Hence our supposition is wrong and n3 ≤ c.n2,
n ≥ n0 is not true for any combination of c and n0.
And hence, n3 O(n2)
Arslan Anjum Advanced Algorithms Analysis and Design
SomeExamples
More Examples
1. n2 + n3 = O(n4)
2. n2 / log(n) = O(n . log n)
3. 5n + log(n) = O(n)
4. nlog n = O(n100)
5. 3n = O(2n . n100)
6. n! = O(3n)
7. n +1 = O(n)
8. 2n+1 = O(2n)
9. (n+1)! = O(n!)
10. 1 + c + c2 +…+ cn = O(cn) for c > 1
11. 1 + c + c2 +…+ cn = O(1) for c < 1
Intuitively:
Set of all functions whose rate of growth is the same as or higher
than that of g(n).
f(n) ∈ Ω(g(n))
Proof:
Assume that f(n) = 5.n2 , and g(n) = n
f(n) ∈ Ω(g(n)) ?
We have to find the existence of c and n0 s.t.
c.g(n) ≤ f(n) n ≥ n0
c.n ≤ 5.n2 c ≤ 5.n
if we take, c = 5 and n0= 1 then
c.n ≤ 5.n2 n ≥ n0
And hence f(n) ∈ Ω(g(n)), for c = 5 and n0= 1
Proof:
Assume that f(n) = 5.n + 10, and g(n) = n
f(n) ∈ Ω(g(n)) ?
We have to find the existence of c and n0 s.t.
c.g(n) ≤ f(n) n ≥ n0
c.n ≤ 5.n + 10 c.n ≤ 5.n + 10.n c ≤ 15.n
if we take, c = 15 and n0= 1 then
c.n ≤ 5.n + 10 n ≥ n0
And hence f(n) ∈ Ω(g(n)), for c = 15 and n0= 1
Intuitively: Set of all functions that have same rate of growth as g(n).
f(n) ∈ Θ(g(n))
Since
f(n) < c.g(n) n2 < c.n2 1 ≤ c,
Proof:
Assume that f(n) = 5.n2 , and g(n) = n
f(n) ∈ Ω(g(n)) ?
We have to prove that for any c there exists n0 s.t.,
c.g(n) < f(n) n ≥ n0
c.n < 5.n2 c < 5.n
This is true for any c, because for any arbitrary c e.g. c
= 1000000, we can choose n0 = 1000000/5 = 200000
and the above inequality does hold.
And hence f(n) ∈ ω(g(n)),
Proof:
Assume that f(n) = 5.n + 10, and g(n) = n
f(n) ∉ Ω(g(n)) ?