Complexity Space N Time
Complexity Space N Time
Algorithms
MSIT
Agenda
What is Algorithm?
What is need for analysis?
What is complexity?
Types of complexities
Methods of measuring complexity
Algorithm
A clearly specified set of instructions to
solve a problem.
Characteristics:
Input: Zero or more quantities are externally supplied
Definiteness: Each instruction is clear and unambiguous
Finiteness: The algorithm terminates in a finite number
of steps.
Effectiveness: Each instruction must be primitive and
feasible
Output: At least one quantity is produced
Algorithm
Need for analysis
To determine resource consumption
CPU time
Memory space
Compare different methods for
solving the same problem before
actually implementing them and
running the programs.
To find an efficient algorithm
Complexity
8
Two ways of finding
complexity
Experimental study
Theoretical Analysis
Experimental study
Write a program implementing the
algorithm
Run the program with inputs of varying
size and composition
Get an accurate measure of the actual
running time
Use a method like
System.currentTimeMillis()
Plot the results
Example
a. Sum=0;
for(i=0;i<N;i++)
for(j=0;j<i;j++)
Sum++;
Java Code – Simple Program
import java.io.*;
class for1
{
public static void main(String args[]) throws Exception
{
int N,Sum;
N=10000; // N value to be changed.
Sum=0;
long start=System.currentTimeMillis();
int i,j;
for(i=0;i<N;i++)
for(j=0;j<i;j++)
Sum++;
long end=System.currentTimeMillis();
long time=end-start;
System.out.println(" The start time is : "+start);
System.out.println(" The end time is : "+end);
System.out.println(" The time taken is : "+time);
}
}
Example graph
Time in millisec
Limitations of Experiments
It is necessary to implement the
algorithm, which may be difficult
Results may not be indicative of the
running time on other inputs not included
in the experiment.
In order to compare two algorithms, the
same hardware and software
environments must be used
Experimental data though important is not
sufficient
Theoretical Analysis
Uses a high-level description of the
algorithm instead of an implementation
Characterizes running time as a function of
the input size, n.
Takes into account all possible inputs
Allows us to evaluate the speed of an
algorithm independent of the
hardware/software environment
Space Complexity
The space needed by an algorithm is the
sum of a fixed part and a variable part
f(n)=1+n+n2
O(Nlog N)
Time
Number of Inputs
Best, average, worst-case
complexity
In some cases, it is important to consider
the best, worst and/or average (or typical)
performance of an algorithm:
E.g., when sorting a list into order, if it is
already in order then the algorithm may
have very little work to do
The worst-case analysis gives a bound for
all possible input (and may be easier to
calculate than the average case)
Comparision of two algorithms
Consider two algorithms, A and B, for solving a
given problem.
TA(n),TB( n) is time complexity of A,B
respectively (where n is a measure of the
problem size. )
One possibility arises if we know the problem
size a priori.
For example, suppose the problem size is n0 and
TA(n0)<TB(n0). Then clearly algorithm A is better than
algorithm B for problem size .
In the general case,
we have no a priori knowledge of the problem size.
Cont..
Limitation:
don't know the problem size beforehand
it is not true that one of the functions is less
than or equal the other over the entire range
of problem sizes.
we consider the asymptotic behavior of
the two functions for very large problem
sizes.
Asymptotic Notations
Big-Oh
Omega
Theta
Small-Oh
Small Omega
Big-Oh Notation (O)
fori 1 to n
x=x+y -- order is n
fori 1 to n
for j 1 to n
x=x+y -- order is n2
Time Complexity Vs Space
Complexity
Achieving both is difficult and best case
There is always trade off
If memory available is large
Need not compensate on Time Complexity
If fastness of Execution is not main
concern, Memory available is less
Can’t compensate on space complexity
Example
Size of data = 10 MB
Check if a word is present in the data or
not
Two ways
Better Space Complexity
Better Time Complexity
Contd..
a. Sum=0;
for(i=0;i<N;i++)
for(j=0;j<i*i;j++)
for(k=0;k<j;k++)
Sum++;
Jeffrey J. McConnell
Thank You