Académique Documents
Professionnel Documents
Culture Documents
complexity
Agenda
Algorithm Analysis
What is complexity?
Types of complexities
Methods of measuring complexity
Algorithm Analysis(1/5)
Measures the efficiency of an algorithm or its
implementation as a program as the input size
becomes very large
We evaluate a new algorithm by comparing its
performance with that of previous approaches
Comparisons are asymptotic analyses of classes of
algorithms
Complexity
The complexity of an algorithm is simply the amount
of work the algorithm performs to complete its task.
Complexity
A measure of the performance of an algorithm
An algorithms performance depends on
internal factors
external factors
External Factors
Speed of the computer on which it is run
Quality of the compiler
Size of the input to the algorithm
Internal Factors
The algorithms efficiency, in terms of:
Time required to run
Space (memory storage)required to run
Note:
Complexity measures the internal factors (usually more interested in time
than space)
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
Plot the results
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
Basic Operations
What can be done in a constant time,
regardless of the size of the input
Basic arithmetic operations, e.g. add,
subtract, shift,
Relational operations, e.g. <, >, <=,
Logical operations, e.g. and, or, not
More complex arithmetic operations, e.g.
multiply, divide, log,
Etc.
Non-basic Operations
When arithmetic, relational and logical
operations are not basic operations?
-Operations depend on the input size
-Arithmetic operations on very large
numbers
-Logical operations on very long bit strings
-Etc.
Instance Size
Number of elements
Arrays, matrices
Graphs (nodes or vertices)
Sets
Number of bits/bytes
Numbers
Bit strings
Maximum values
Numbers
Space Complexity
The space needed by an algorithm is the sum
of a fixed part and a variable part
The fixed part includes space for
Instructions
Simple variables
Fixed size component variables
Space for constants
Etc..
Cont
The variable part includes space for
Component variables whose size is dependant on
the particular problem instance being solved
Recursion stack space
Etc..
Time Complexity
The time complexity of a problem is
the number of steps that it takes to solve an
instance of the problem as a function of the size of
the input (usually measured in bits), using the most
efficient algorithm.
Time Complexity
How do we measure?
1. Count a particular operation (operation counts)
2. Count the number of steps
(step counts)
3. Asymptotic complexity
Operation Count
for (int i = 1; i < n; i++)
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
How many comparisons are made?
The number of compares depends on
a[]s and t as well as on n.
Operation Count
Worst case count = maximum count
Best case count = minimum count
Average count
4 compares
i compares
Step Count
The operation-count method omits accounting for the
time spent on all but the chosen operation
The step-count method count for all the time spent in
all parts of the program
A program step is loosely defined to be a
syntactically or semantically meaningful segment of a
program for which the execution time is independent
of the instance characteristics.
However, n adds cannot be counted as one step.
Step Count
steps/execution (s/e)
1
for (int i = 1; i < n; i++)
{
0
// insert a[i] into a[0:i-1]
0
int t = a[i];
1
int j;
0
for (j = i - 1; j >= 0 && t < a[j]; j--)
1
a[j + 1] = a[j];
1
a[j + 1] = t;
1
}
0
Step Count
s/e
1
for (int i = 1; i < n; i++)
{
0
// insert a[i] into a[0:i-1]
0
int t = a[i];
1
int j;
0
for (j = i - 1; j >= 0 && t < a[j]; j--) 1
a[j + 1] = a[j];
1
a[j + 1] = t;
1
}
0
frequency
n-1
0
0
n-1
0
(n-1)n/2
(n-1)n/2
n-1
n-1
Step Count
Total step counts
= (n-1) + 0 + 0 + (n-1) + 0 + (n-1)n/2 + (n-1)n/2
+(n-1) + (n-1)
= n2 + 3n 4
Asymptotic Complexity
Introduction
Why are asymptotic notations important?
They give a simple characterization of an algorithms
efficiency.
They allow the comparison of the performances of various
algorithms.
For large values of components/inputs, the multiplicative
constants and lower order terms of an exact running time are
dominated by the effects of the input size (the number of
components).
Analysis
Worst case
Provides an upper bound on running
time
An absolute guarantee
Average case
Provides the expected running time
Very useful, but treat with care: what is
average?
Random (equally likely) inputs
Real-life inputs
Asymptotic Overview
A way to describe behavior of functions in the limit.
Describe growth of functions.
Focus on whats important by abstracting away low order
terms and constant factors.
Indicate running times of algorithms.
A way to compare sizes of functions.
Examples:
n steps vs. n+5 steps
n steps vs. n2 steps
Running time of an algorithm as a function of input size n for large
n.
Expressed using only the highest-order term in the expression for
the exact running time.
Asymptotic Notations
}
Intuitively: Set of all functions that
have the same rate of growth as g(n).
}
Technically, f(n) (g(n)).
Example
T(n) = f(n) = Q(g(n))
if c1*g(n) <= f(n) <= c2*g(n) for all n > n0, where c1, c2 and n0 are
constants > 0
c2*g(n)
f(n)
c1*g(n)
n0
-notation (Best-Case)
For function g(n), we define (g(n)),
big-Omega of n, as the set:
(g(n)) = {f(n) :
positive constants c and n0,
such that n n0,
we have 0 cg(n) f(n)}
if f(n) >= c*g(n) for all n > n0, where c and n0 are
constants > 0
f(n)
c*g(n)
n0
>=
(g(n)), functions that grow at least as fast as g(n)
=
(g(n)), functions that grow at the same rate as g(n)
g(n)
<=
O(g(n)), functions that grow no faster than g(n)
Relations Between , O,
f(n)
g(n)
lim
f (n)
g (n)
o-notation
-notation
For a given function g(n), the set little-omega:
(f(n))
n0
Contd..
Generally, an algorithm with a run-time of (n log n)
will perform better than an algorithm with a run-time of order
(n2 ), provided that n is sufficiently large.
However, for small values of n, the (n2 ) algorithm may run
faster due to having smaller constant factors.
The value of n at which the (n log n) algorithm first
outperforms the (n2 ) algorithm is called the break-even
point for the (n log n) algorithm.
Examples
Determine whether following is true or false.
100n + 5 O(n2)
Solution:
100n + 5 100n + n for all n 5
101n 101n2
Where c =101 and n0 = 5.
Omega(n2)
4 n2
6 n2 + 9
5 n2 + 2 n
4 n3 + 3 n2
6 n6 + 3 n4
Thetha n2
4 n2
6 n2 + 9
5 n2 + 2 n
O(n2)
3 log n + 8
5n + 7
2 log n
4 n2
6 n2 + 9
5 n2 + 2 n
Exercise
solution
Performance Classification
f(n)
1
log n
Classification
Constant: run time is fixed, and does not depend upon n. Most instructions are
executed once, or only a few times, regardless of the amount of information being
processed
Logarithmic: when n increases, so does run time, but much slower. Common in
programs which solve large problems by transforming them into smaller problems.
Linear: run time varies directly with n. Typically, a small amount of processing is
done on each element.
n log n
When n doubles, run time slightly more than doubles. Common in programs which
break a problem down into smaller sub-problems, solves them independently, then
combines solutions
n2
Quadratic: when n doubles, runtime increases fourfold. Practical only for small
problems; typically the program processes all pairs of input (e.g. in a double nested
loop).
n3
2n
Exponential: when n doubles, run time squares. This is often the result of a natural,
brute force solution.
Example
O(1)
constant
log
O(log N)
O(N)
O(N log N)
linear
n-log-n
O(N2)
quadratic
O(N3)
cubic
O(2N)
exponential
Growth rates
O(N2)
O(Nlog N)
Number of Inputs
Analyzing Loops
Analyzing Nested Loops
Analyzing Sequence of Statements
Analyzing Conditional Statements
Analyzing Loops[1]
Any loop has two parts:
How many iterations are performed?
How many steps per iteration?
int sum = 0,j;
for (j=0; j < N; j++)
sum = sum +j;
Loop executes N times (0..N-1)
O(1) steps per iteration
Analyzing Loops[2]
What about this for loop?
int sum =0, j;
for (j=0; j < 100; j++)
sum = sum +j;
Loop executes 100 times
O(1) steps per iteration
Total time is 100 * O(1) = O(100 * 1) = O(100)
O(N2)
O(N)
O(1)
Best Case
Best case is defined as which input of size n is
cheapest among all inputs of size n.
The best case for my algorithm is n=1
because that is the fastest. WRONG!
Misunderstanding
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.
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..
Load the entire data into main memory and check
one by one
Faster process but takes a lot of space
Example
3. Conditional Statements
Sum=0;
for(i=1;i<N;i++)
for(j=1;j<i*i;j++)
if(j%i==0)
for(k=0;k<j;k++)
Sum++;
Analyze the complexity of the above algorithm for
different inputs
Summary
Analysis of algorithms
Complexity
Even with High Speed Processor and large
memory ,Asymptotically low algorithm is not
efficient
Trade Off between Time Complexity and
Space Complexity
References
Fundamentals of Computer Algorithms
Ellis Horowitz,Sartaj Sahni,Sanguthevar
Rajasekaran
Algorithm Design
Micheal T. GoodRich,Robert Tamassia
Analysis of Algorithms
Jeffrey J. McConnell
Thank You