Order of Growth Explained
Order of Growth Explained
The complexity function can be also be used to compare two algorithms P and
Q that perform the same task.
: n ≥ n0
20
The intuition behind O- notation is shown in Figure 1. Basics of an Algorithm
C.g(n
No )
f(n)
Matter
F(n)
Figure 1
n →n0
Figure 1
For all values of n to the right of n0, the value of f(n) is always lies on or
below
Solution:
Remark: The value of C and is not unique. For example, to satisfy the
above equation (1), we can also take . So depending on the
value of C, the value of is also changes. Thus any value of C and which
satisfy the given inequality is a valid solution.
; Let C=3
21
Introduction to
Algorithm
Value of n
6 3
15 24
27 81
: n ≥ n0
22
Basics of an Algorithm
f(n) No
f(n) = Ω g(m)
Matter
n0
n
Figure 2
Note that for all values of f(n) always lies on or above g(n).
: show that
Solution:
Hence .
23
Introduction to Let then there exist a
Algorithm positive constant C and such that
Thus .
Let and are two positive functions , each from the set of natural
numbers (domain) to the positive real numbers. In some cases, we have
24
(Note that this inequality (1) represents two conditions to be satisfied Basics of an Algorithm
simultaneously viz and
c2g(n)
f(n)
c1g(n)
f(n) = (g(n))
n
n0
Figure 3
Note that for all values of n to the right of the n0 the value of f(n) lies at or
above C1g(n) and at or below C2.g(n).
: show that
Let
Thus .
(iii) Do yourself.
The following theorems are quite useful when you are dealing
Theorem1: If
Proof:
for
Let us assume
Then .
Theorem2: If
Proof:
Since
26
Basics of an Algorithm
Theorem3: If
…….(1)
(i)
(ii)
Solution:
(i) Here , So
by Theorem1,2 and 3:
, and .
(ii) , So by
Theorem1,2 and 3:
, and .
Q.2: What is the running time to retrieve an element from an array of size n
(in worst case):
c) d) none of these
c) d)
27
Introduction to
Algorithm ………
c) d)
g)
k) where
28