Complexity New
Complexity New
• internal factors
time required to run a program, space or memory required
• external factors
size of input to the algorithm, speed of the computer on which it is run, quality of the
compiler
External factors can be controlled by the user. Usually efficiency is measured in terms of
space and time only.
Types of complexity
The two factors which specify the complexity or efficiency of an algorithm are
• time complexity
• space complexity
Time complexity
It is defined as the number of steps taken to solve an instance of the problem as a function of the
size of the input. it is a concept that measure the time required to run an algorithm. An algorithm
which takes the least number of steps is considered as the most efficient.
Space complexity
It is a concept that measures the amount of space or memory required by an algorithm. the space
complexity is measured with big O notation which is used to determine the number of steps
involved in an algorithm for solving a problem.
Three cases to analyze an algorithm
Three cases to state the degree of performance of an algorithm are
1
Big O Notation
It is the unit of measurement of an algorithm.
Big O Notation is used to determine the number of steps involved in an algorithm for
solving problem. The number of steps depends on the size of the input data.
Dominant term
The dominant term is the term that grows the fastest. It plays a vital role and affects the
performance of the function .
Example
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
…………
}}
for (int j=1;j<=n;j++)
{
…………
}
Complexity = O(n2) +O(n)
Here O(n2) is the dominant term while calculating the complexity
Constants
In determining Big O complexity, lower order terms or low priority terms and constants
are dropped and not required. But even if two algorithms have same Big O complexity,
one may be faster than the other. Constants helps to find out the performance of
algorithms when they have the same Big O notations.
Calculating complexity
• Simple statements or group of simple statements O(1)
They run in constant time
int a;
a=5;
++a;
Complexity = O(1)
2
• Loops (single loop) -O(N)
Running time of single loop=running time of statements in it * number of iterations
Eg for (int i=1;i<=n;i++)
{
m=m+2;
}
Total time = c*n
=cn
=n
O(n)
• Nested loop - O(N2)
Running time of nested loop=running time of statements in it * product of the sizes of all
loops.
Ex for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
m=m+2;
}}
=cn 2
=n
O(n 2)
3 loops =O(n 3 )
4 loops = O(n 4)
• Consecutive statements
Running time= Add the time complexities of each statement
x=x+1;
for (int i=1;i<=n;i++)
{
p=p*2;
}
for (int j=1;j<=n;j++)
{
m=m+2;
}
Total time = c0+ c1*n+ c2*n
c0+ n(c1+c2)
3
= O(n)
• Simple if else statement = O(1)
• Nested if else statement - O(n)
• If else statement with loops =O(n2)
Compare the two complexities O(n 2 )and O(2 n )and state which is better and why?
O(n2) is better than O( 2 n)
Because if n=10, then n 2= 100
If 2 n= 2 10= 1024. So 100 times will finish fast. Therefore the complexity in
O(n2) is better than O( 2 n).
4
5