Complexity of An Algorithm
Complexity of An Algorithm
Complexity of An Algorithm
Algorithm
Dr. Nitin Paharia
Associate Professor, Department of CSE,
PSIT, Kanpur
Outline
• Complexity of an algorithm?
• What is time complexity?
• What is space complexity?
• What is asymptotic notation?
Complexity of an Algorithm
• Multiple solutions(algorithm) of any given problem for computer.
• Some are good and some are bad
• The selection of an algorithm depends on it’s goodness(efficiency),
which is determined through complexity of algorithm.
• The complexity of an algorithm can be defined as function
describing it’s efficiency in processing the given amount of data.
• There are two main complexity measures of the efficiency of an
algorithm: Time and Space
• We need approximate counting that is machine independent.
Time Complexity
Time Complexity- Time complexity is a function describing numbers of
times elementary operations need to be performed for given amount of
data in an algorithm. An elementary operation is any one of the
arithmetic operations (addition, subtraction, multiplication, division) or
a comparison between two numbers or the execution of a branching
instruction.
■ Example: n 1/2
= Ω( lg n) .
Use the definition with c = 1 and n0 = 16. Checks
1/2
OK. Let n > 16: n > (1) lg n if and only
n
if n > Diff
(log n)^2
( lg
n )2 16 16 0
17 16.71 0.29
by squaring both sides. 18 17.39 0.61
19 18.04 0.96
20 18.68 1.32
This is an example of polynomial vs. log. 21 19.29 1.71
22 19.89 2.11
23 20.46 2.54
24 21.02 2.98
Theta (θ)