You are on page 1of 3

Calculating Complexity of code are: (Assumptions

the time complexity of a piece n:. All


Five guidelines for finding out

steps take precisely same time to execute.)

Loops
Nested loops
Consecutive statements
If-then-else statements
Logarithmic complexity
1. Loops
The running time of a loop is, at most, equal to the running time of the statements inside the
loop (including tests) multiplied by the number of iterations. For instance, consider the
followingloop:
for i in range(n)
Loop executed n times
m =m+ 2 All the steps in this loop take
constant time, say c

So, the total time taken by the above loop to execute itself is

Total time = c *n= Cn i.e., O(n)


Time taken by
one step L the
Total Time taken by
loop to execute
all its steps
No of steps in the loop

In loop executing N times, the body of the loop gets executed


a
N -1 times, the condition
evaluates to false and loop terminates without executing the
body.
2. Nested loops
total running time
To compute complexity
of nested loops, analyze inside out. For nested loops,
sizes of all the loops.
is the product of the
consider the following code:
For instance,
for i in range(n):
Outer loop
executes n
for j in range(n):
fimes Inner loop execules n times

K K+1
All these steps take constant time, say c

shown nested loops to execute is:


So the total time taken by the above

Total time = c * n* n cn i.e. O(n)


Total Time taken by
Time taken by one step the nested loops to
execute all steps
No of steps in the outer loop.

No of steps in the inner loop

3. Consecutive statements
simply add the time complexities of
each
consecutive statements,
To compute the complexity of
statement. For instance,

X =X+ 1 takes constant time. say Co

for i in range (n):- loopl executes n times

m=m +2
takes constant times say c

Outer loopp for j in range (n):


executes n
times for k in range (n):
a a+1 inner loop executes n times

takes constant time, say C2

So the total time taken by the above shown code to execute is


Total time = Co+ Cn + C2n i.e. O()
Considering only the dominant
Time taken by statement1 erm which is n

Time taken by loop1


NOTE
Time taken by nested loop
counting the nested
Simple programs can be analyzed by
over n items yields
loops of the program. A single loop
A loop
A within a loop yields fn)= n.
loop
fn)=n.
within another loop inside a third loop yields fn)= n*.
4. 1-then-else stalements
consider worst-case runnine
To compute time complexity of
if-then-else statement, we
ning time,
t

the test, plus time taken by either the then partod


part or
which means, we consider the time taken by
the clse part (whichezrr is larger).
For instance, consider the following code :

if len(1ist 1) != len (list2)


The true part's step takes costant tinme, say e
Comparison test takes
returnFalse
constant time say c'o
else

Test condition takes constant time say c,


for i in range(n):
Loop repeats n times if list1[i] != list2[i]:
Takes constant time say
returnFalse- 3

The else part has a loop (n times) which


in total takes times as ( + C3) *

So the total time taken by the above shown code to execute is:

Total time = Co+ C+ (Ca+ Ca) *n i.e., O(N)

Time taken by test Considering only the dominant


term which is N.
Time taken by then part

Time taken by else part

NOTE
5. Logarithmic Complexity
The logarithmic complexity means that an algorithms' Given a series of for loops that are
sequential, the slowest of them
performance time has logarithmic factor eg. an algorithm is determines the asymptotic behavior
Olog N) if it takes a constant time to cut the problem size by a of the program. Two nested loops
fraction (usually by %4). folowed by a single loop is asymp-
totically the same as the nested
An example algorithm having logarithmic complexity is
loops alone, because the nested
binary search algorithm. loops dominate the simple loop.
But, the best performance isn't always desirable. There can be
a tradeoff between:
Ease of understanding, writing and debugging
Efficient use of time and space

So, maximum performance is not always desirable. However, it is still useful to compare the
performance of different algorithms, even if the optimal algorithm may not be adopted.

You might also like