Chapter 2
Chapter 2
Complexityanalysis
Complexity analysis
01/29/18 BY MS. SHAISTA QADIR 1
PRESENTED BY
Shaista Qadir
Lecturer king khalid university
2.
Contents
Contents
algorithm andits properties
Computational Complexity
time Complexity
spaCe Complexity
Complexity analysis and asymptotiC
notations.
Big-oh-notation (o)
omega-notation ( )
Ω
theta-notation ( )
Θ
the Best, average, and Worst Case analyses.
Complexity analyses examples.
Comparing groWth rates
01/29/18 BY MS. SHAISTA QADIR 2
3.
algorithm and its
algorithmand its
properties
properties
01/29/18 BY MS. SHAISTA QADIR 3
An algorithm is a step by step procedure for calculating a
function.
An algorithm can exist for a day to day task, a simple
program or a complex calculation.
To write an algorithm for a problem one should know
◦ The input to be supplied for the program.
◦ The operation to be performed on the input to perform the
task.
◦ The output to be obtained.
◦ Any assumptions or initializations to be made for solving
the problem.
4.
algorithm and its
algorithmand its
properties
properties
01/29/18 BY MS. SHAISTA QADIR 4
An algorithm for a problem has the following properties:
Finiteness: An algorithm should have a finite number of steps
and should terminate after these finite number of steps.
Definiteness: Every step in the algorithm should be very clear.
The steps shall be explained in only one way and executed
without any confusion.
Effectiveness: The instructions of the algorithm are realizable.
The steps in the algorithm shall be executed in finite amount of
time.
Input: An algorithm can accept no input or it can accept one to
many inputs.
Output: The algorithm should produce a minimum of one
output.
5.
Computational Complexity
Computational Complexity
01/29/18BY MS. SHAISTA QADIR 5
The same problem can frequently be solved with
algorithms that differ in efficiency.
To compare the efficiency of algorithms, a measure of
the degree of difficulty of an algorithm called
computational complexity was developed by Juris
Hartmanis and Richard E.Stearns.
Computational complexity indicates how much effort is
needed to apply an algorithm or how costly it is.
Computational Complexity is measured in terms of two
efficiencies to design good algorithms.
◦ Space efficiency
◦ Time efficiency
6.
Computational Complexity
Computational Complexity
(
(SpaCe effiCienCy
SpaCe effiCienCy )
)
01/29/18 BY MS. SHAISTA QADIR 6
These efficiency measures helps us to understand which
algorithm is better than other.
Space Efficiency:
◦ Space Efficiency is on how efficiently the algorithm
consumes memory space.
◦ Space required will depend upon
program size (constant)
data size (constant and dynamic)
◦ These days machines come with large memory sizes to
solve this problem.
7.
Computational Complexity
Computational Complexity
(SpaCe effiCienCy )
( SpaCe effiCienCy )
01/29/18 BY MS. SHAISTA QADIR 7
Two space Components
◦ Fixed static part (Cp): space for numbers, constants,
size of input, output and program .
◦ Variable dynamic part (Sp): space for variables whose
size is problem dependent and dynamic.
Total space requirement for an algorithm is the sum of
fixed and dynamic part.
◦ S(P)= Cp+Sp
8.
Computational Complexity
Computational Complexity
(illuStration )
( illuStration )
01/29/18 BY MS. SHAISTA QADIR 8
Illustration: Find the total space requirement for the
following pieces of codes (neglect program space
requirement)
◦ i) code to find difference between two numbers.
int x,y,dif;
dif = x-y;
Here there are three static variables x, y, dif and no
dynamic variables. Space allotted for int variable is 4
bytes.
Therefore, S(P)= Cp+Sp
S(P)= 3x4+0 =12 bytes
9.
Computational Complexity
Computational Complexity
(illuStration )
( illuStration )
01/29/18 BY MS. SHAISTA QADIR 9
◦ ii) code to add numbers of array.
int add(int x[], int n)
{ int total = 0, i;
for(i=0; i<n; i++)
total = total + x[i];
return total; }
Here there are three static variables, int n, total, i and one
dynamic variable int x which depends upon the value n.
Space allotted for int variable is 4 bytes.
◦ Therefore, S(P)= Cp+Sp
◦ S(P)= (3x4)+(nx4)
◦ S(P) =12 + (nx4) bytes
10.
Computational Complexity
Computational Complexity
(time effiCenCy )
( time effiCenCy )
01/29/18 BY MS. SHAISTA QADIR 10
Time Efficiency
◦ is determined based on the amount of time required to run
a program.
◦ It depends upon the number of instructions to be executed
which in turn is dependent on the amount of data. It is
hardware independent.
Time Efficiency/ Time Complexity analysis is essential to know
◦ If the algorithm is “fast enough” for my needs
◦ How longer will the algorithm take if I increase the amount
of data it must process?
◦ Which is the right algorithm, among the given a set of
algorithms that accomplish the same thing.
11.
Computational Complexity
Computational Complexity
(illustration )
( illustration )
01/29/18 BY MS. SHAISTA QADIR 11
Consider the piece of code
for (int i=1; i<= n ; i++)
for int j=1 ; j <= n; j++)
{
cout << i; 1
p = p + i; 1 2n (2n)n
}
The 1st loop runs n times, 2nd loop runs n times, the
third and fourth statements run 1 unit each.
So the total run-time is equal to 2n2
12.
Complexity analysis and
Complexityanalysis and
asymptotiC notations
asymptotiC notations
01/29/18 BY MS. SHAISTA QADIR 12
To analyze the computational complexity of algorithms
in terms of time, computer scientists used several
asymptotic notations and important notations among
them are:
Big-oh-notation (O)
Omega-notation (Ω)
Theta-notation (Θ)
Asymptotic Notations provides a formula that associates
n, the problem size, with t, the processing time required
to solve the problem.
13.
Complexity analysis andasymptotiC
Complexity analysis and asymptotiC
notations
notations
(Worst Case analyses )
(Worst Case analyses )
01/29/18 BY MS. SHAISTA QADIR 13
Big-oh-notation (O)
Big-O, “bounded above by”
Let T(n) be a measure of the time required to execute an
algorithm of problem size n.
then for some positive values of constants c and N, the
Big-Oh (O) of a function f(n) on n is given by
T(n)=O(f(n))
T(n)<=c(f(n)) where n>N
It gives the maximum value of T(n) or the WORST case
for any possible input.
14.
Complexity analysis andasymptotiC
Complexity analysis and asymptotiC
notations
notations
( Best
( Best Case analysis )
Case analysis )
01/29/18 BY MS. SHAISTA QADIR 14
Omega-notation (Ω)
Omega, “bounded below by”
Let T(n) be a measure of the time required to execute an
algorithm of problem size n.
then for some positive values of constants c and N, the
Omega (Ω) of a function f(n) on n is given by
T(n)= Ω(f(n))
T(n)>=c(f(n)) where n>N
It gives the minimum value of T(n) or the BEST case for
any possible input.
15.
Complexity analysis andasymptotiC
Complexity analysis and asymptotiC
notations
notations
( aVeraGe Case analysis )
( aVeraGe Case analysis )
01/29/18 BY MS. SHAISTA QADIR 15
Theta-notation (Θ)
Theta, “bounded above and below”
Let T(n) be a measure of the time required to execute an
algorithm of problem size n.
then for some positive values of constants c1, c2 and N,
the Theta (Θ) of a function f(n) on n is given by
T(n)= Θ(f(n))
c (f(n)) <=T(n)<= c1(c2(f(n)) where n>N
It gives the expected value of T(n) or the AVERAGE case
for any possible input.
16.
Complexity AnAlysis .
ComplexityAnAlysis .
01/29/18 BY MS. SHAISTA QADIR 16
Big O or the Order of n measurement or the Worst Case
is used mostly in the estimation of performance of a
piece of code against the amount of data.
For the piece of code,
for (int i=1; i<= n ; i++)
for int j=1 ; j <= n; j++)
{ cout << i;
p = p + i;
} T(n)=2n2
17.
Complexity AnAlysis
Complexity AnAlysis
(exAmple )
( exAmple )
01/29/18 BY MS. SHAISTA QADIR 17
To find Big O from T(n)= 2n2
1. Discarding constant terms produces :
Here no constants 2n2
2. Clearing coefficients : n2
3. Picking the most significant term: n2
T(n)=O(n2 )
18.
Complexity AnAlysis
Complexity AnAlysis
(exAmple s)
( exAmple s)
01/29/18 BY MS. SHAISTA QADIR 18
Example:1
Time =2n3
+2n+100
Step:1 Discarding constant terms produces: 2n3
+2n
Step: 2 Clearing coefficients : n3
+n
Step: 3 Picking the most significant term: n3
Time = O(n3
)
Example:2
Time =3n3
+150
Step:1 Discarding constant terms produces: 3n3
Step: 2 Clearing coefficients : n3
Step: 3 Picking the most significant term: n3
Time =O(n3
)