You are on page 1of 4

Computer System Algorithm (MCS-205) SSUET/QR/114

LAB # 03
Algorithm Complexity
OBJECTIVE

To calculate the Time Complexity to identify the best algorithm with Big.O Asymptotic Notation

THEORY

Algorithm Complexity:

Algorithm complexity is a measure which evaluates the order of the count of operations, performed by a
given or algorithm as a function of the size of the input data. To put this simpler, complexity is a rough
approximation of the number of steps necessary to execute an algorithm. When we evaluate complexity we
speak of order of operation count, not of their exact count .

For example: if we have an order of N2 operations to process N elements, then N 2/2 and 3*N2 are of one and
the same quadratic order.

Asymptotic Notations:

Asymptotic Notations are languages that allow us to analyze an algorithm’s running time by identifying its
behavior as the input size for the algorithm increases .

Big.O Asymptotic Notation:


Algorithm complexity is commonly represented with the O(f) notation, also known as asymptotic notation
or “Big O notation”, where f is the function of the size of the input data. The asymptotic computational
complexity O(f) measures the order of the consumed resources (CPU time, memory, etc.) by certain
algorithm expressed as function of the input data size.

Complexity can be constant, logarithmic, linear, n*log(n), quadratic, cubic, exponential, etc. This is
respectively the order of constant, logarithmic, linear and so on, number of steps, are executed to solve a
given problem. For simplicity, sometime instead of “algorithms complexity” or just “complexity” we use
the term “running time”.

Lab 03: Algorithm Complexity


Name: Tanzeel Ur Rehman 1 Roll no: BMCS22S-002
Computer System Algorithm (MCS-205) SSUET/QR/114

TYPICAL ALGORITHM COMPLEXITIES

Running Time Description


O(1) It takes a constant number of steps for performing a given
operation and this count does not depend on the size of the
input data.
constant

It takes the order of log(N) steps, where the base of the


logarithmic O(log(N)) logarithm is most often 2, for performing a given operation
on N elements.
It takes nearly the same amount of steps as the number of
linear O(N)
elements for performing an operation on N elements
It takes N*log(N) steps for performing a given operation on
O(n*log(n))
N elements.

It takes the order of N2 number of steps, where the N is the


quadratic O(n2)
size of the input data, for performing a given operation

It takes the order of N3 steps, where N is the size of the input


cubic O(n3)
data, for performing an operation on N elements

It takes a number of steps, which is with an exponential


exponential O(2n), O(N!), O(nk), … dependability with the size of the input data, to perform an
operation on N elements

Big-Omega:
Big-Omega, commonly written as Ω, is an Asymptotic Notation for the best case, or a floor growth rate for a
given function. It provides us with an asymptotic lower bound for the growth rate of the runtime of an
algorithm.
f(n) is Ω(g(n)), if for some real constants c (c > 0) and n 0 (n0 > 0), f(n) is >= c g(n) for every input
size n (n > n0).

EXERCISE:

A. Create a file named lab3.py. Write the name of the complexity of the following codes w.r.t Big O
notation
1. Code:
def multiply(items): ANSWER:-
result = items[0] * items[0] THIS IS LINEAR NOTATION
print (result)
multiply([4, 5, 6, 8])

Lab 03: Algorithm Complexity


Name: Tanzeel Ur Rehman 2 Roll no: BMCS22S-002
Computer System Algorithm (MCS-205) SSUET/QR/114

2. Code:
def code_2(items):
for item in items:
for item2 in
items:
print(item, ' ' ,item)
code_2 ([4, 5, 6, 8])

ANSWER:-
THIS IS QUADRATIC NOTATION.

B. Assume ( )= 2n3 - 5n2 +10n+1, determine the value of big o notation (upper bound)
SOLUTION:
2n3 - 5n2 + 10n + 1 ≤ 8n3
For n = 1
2(1)3 - 5(1)2 + 10(1) + 1 ≤ 8(1)3
2 – 5 + 10 + 1 ≤ 8
8 ≤ 8
For n = 2
2(2)3 - 5(2)2 + 10(2) + 1 ≤ 8(2)3
2(8) – 5(4) + 10(2) + 1 ≤ 8(8)
16 – 20 + 20 +1 ≤ 64
17 ≤ 64
For n = 3
2(3)3 - 5(3)2 + 10(3) + 1 ≤ 8(3)3
2(27) – 5(9) + 10(3) + 1 ≤ 8(27)
54 – 45 + 30 +1 ≤ 216
40 ≤ 216
Hence C = 8
g(n) = g(n3)

B. Assume ( )= 5n4 + 3n3 + 2n2 + 4n + 1, determine the value of big o notation(upper bound)
SOLUTION:
5n4 + 3n3 + 2n2 + 4n + 1 ≤ 15n4
For n = 1
5(1)4 + 3(1)3 + 2(1)2 + 4(1) + 1 ≤ 15(1)4
5 + 3 + 2 + 4 + 1 ≤ 15
15 ≤ 15
For n = 2
5(2)4 + 3(2)3 + 2(2)2 + 4(2) + 1 ≤ 15(2)4
5(16) + 3(8) + 2(4) + 4(2) + 1 ≤ 15(16)
80 + 24 + 8 + 8 + 1 ≤ 240
121 ≤ 240
For n = 3
5(3)4 + 3(3)3 + 2(3)2 + 4(3) + 1 ≤ 15(3)4

Lab 03: Algorithm Complexity


Name: Tanzeel Ur Rehman 3 Roll no: BMCS22S-002
Computer System Algorithm (MCS-205) SSUET/QR/114

5(81) + 3(27) + 2(9) + 4(3) + 1 ≤ 15(81)


405 + 81 + 18 + 12 + 1 ≤ 1215
517 ≤ 1215
Hence C = 15
g(n) = g(n4)

C. calculate the time complexity of the following code.

Code Cost: Repetition: Total time:


(time required (no. of times (total time required in worst case)
for line) executed)
n = [1,2,3,4,5] 1 1 1

For i in n: 1 n n

For j in n: 1 n*n n2

If i!=j and n[i]==n[j]: 1+1+1 (n2+1) + (n2+1) + 3n2 + 3


(n2+1)
Print(f ‘{n[i]} is a 1 n2 n2
duplicate’)
5n2 + n + 4

Lab 03: Algorithm Complexity


Name: Tanzeel Ur Rehman 4 Roll no: BMCS22S-002

You might also like