You are on page 1of 15

Rate of Growth of An

Algorithm
ALGORITHM DEIGN AND ANALYSIS

PREAPARED BY: ABDUL JALIL NIAZAI


How to Compare Algorithms
 To compare algorithms, let us define a few objective measures:

Execution times? Not a good measure as execution times are specific to a particular computer.
Number of statements executed? Not a good measure, since the number of statements varies
with the programming language as well as the style of the individual programmer.
Ideal solution? Let us assume that we express the running time of a given algorithm as a function
of the input size n (i.e., f(n)) and compare these different functions corresponding to running
times. This kind of comparison is independent of machine time, programming style, etc.

Algorithm 1: f(n) = n
Algorithm 2: f(n) = n2

PREAPARED BY: ABDUL JALIL NIAZAI


What is Rate of Growth?
 The rate at which the running time increases as a function of input is called rate of
growth.
 Let us assume that you go to a shop to buy a car and a bicycle. If your friend sees
you there and asks what you are buying, then in general you say buying a car. This is
because the cost of the car is high compared to the cost of the bicycle
(approximating the cost of the bicycle to the cost of the car).

 As an example, in the case below, n4, 2n2, 100n and 500 are the individual costs of
some function and approximate to n4 since n4 is the highest rate of growth.

PREAPARED BY: ABDUL JALIL NIAZAI


Commonly Used Rates of Growth?
 Below is the list of growth rates you will come across in this course.

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity (Running Time)-Example-1

n+1

f(n) = 2n + 1
O(n)

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity (Running Time)-Example-2

n+1

n*(n+1)

n*(n)

f(n) = 2n2 + 2n + 1
O(n2)

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity (Running Time)-Example-3
i j no. of times

0 0x 0
1 0 1
2 0 2
1
3 0 3
1
2
.
.
n n Total no. of executions

Here, this algorithm is same to previous algorithm, however, in this 1 + 2 + 3 + 4 + 5 +…+ n = n*(n+1)/2
algorithm the inner loop executes correspond to outer loop(i) value. f(n) = n2 + n / 2
O(n2)

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity (Running Time)-Example-4
i p

1 0+1 = 1 Assume p > n

2 1+2 = 3 p = k * ( k+1) / 2

3 1+2+3 k * (k+1) / 2 > n


.
. K2>n
. K> 𝒏 O( 𝒏)
k 1+2+3+4+…+k

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity (Running Time)-Example-6

i
Assume i >= n
1
1*2=2 Since i = 2k
2 * 2 = 22
22 * 2 = 23 Therefore,
. 2k > = n
. 2k = n
. k = log2n

2k O(log2n)

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity (Running Time)-Example-7

i
Assume i < 1
n
Since i = n/2k
n/2
Therefore,
n/22 n / 2k < 1
n/23
n /2k = 1
.
.
. n = 2k
k = log2n
n/2k
O(log2n)

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity (Running Time)-Example-8

i*i<n

i * i > = n , The loop will be stopped

i2 = n

i= 𝒏

Hence, the time complexity is O( 𝒏)

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity (Running Time)-Example-9

Assignment: Analysis find the time complexity of this


algorithm.

Note: These two loops are independent( not a nested


loop) of each other.

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity (Running Time)-Example-10

Assignment: Analysis find the time complexity of this


algorithm.

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity (Running Time)-Example-10

n * log2n

n * log2n

f(n) = 2 n log n + n

Hence, the time complexity is O(n log n)

PREAPARED BY: ABDUL JALIL NIAZAI


Time Complexity- Conclusion

O(n)
O(n)
O(n)
O(log2n)
O(log3n)
O(log2n)

PREAPARED BY: ABDUL JALIL NIAZAI

You might also like