Lec-03-Complexity of Algorithm
Lec-03-Complexity of Algorithm
Complexity of Algorithms
1
Algorithm
An Algorithm is any well-defined
computational procedure that takes some
values or set of values as input and produces
some values or set of values as output.
2
NOTION OF ALGORITHM
problem
algorith
m
Algorithmic solution
3
Suppose computers were infinitely fast and
computer memory are free.
4
– Demonstrate that solution, methods and so with
correct answer.
6
Complexity
Algorithmic complexity is concerned about
how fast or slow particular algorithm
performs.
We define complexity as a numerical
function T(n) - time versus the input size n.
Such an analysis is independent of
machine type, programming style, etc.
7
Complexity
n 5,000n [1.1n]
10 50,000 3
10
Complexity
This means that algorithm B cannot be used for
large inputs, while algorithm A is still feasible.
12
Asymptotic Efficiency Algorithm
When the input size is large enough so that
the rate of growth/order of growth of the
running time is relevant.
13
Asymptotic Notation
Asymptotic notations are convenient for
describing the worst-case running time
function T(n), which is defined only on
integer input size.
14
Asymptotic Notation
Let f(n) and g(n) be two positive functions,
representing the number of basic
calculations (operations, instructions) that
an algorithm takes (or the number of
memory words an algorithm needs).
15
Is input size everything that matters?
17
Asymptotic Notation
O – Big O
W - Big Omega
Q - Big Theta
18
Types of Analysis
Worst-case: (usually done)
upper bound on running time
maximum running time of algorithm on any input of
size n
Average-case: (sometimes done)
we take all possible inputs and calculate computing time
for all of the inputs
sum all the calculated values and divide the sum by total
number of inputs
we must know (or predict) distribution of cases
Best-case: (bogus)
lower bound on running time of an algorithm
minimum running time of algorithm on any input of
size n
O-Notation
For a given function g(n)
O (g(n)) = {f(n) : there exist positive constants c and
n0 such that 0 £ f(n) £ c g(n) for all n ³n0 }
20
W- Notation
For a given function g(n)
W(g(n)) = {f(n) : there exist positive
constants c and n0 such that 0 £ c g(n) £ f(n)
for all n ³ n0}
22
Relations Between Q, O, W
For any two function f(n) and g(n),
we have f(n) = Q(g(n)) if and only
if f(n) = O(g(n)) and f(n) =W(g(n))
That is
Q(g(n)) = O(g(n)) Ç W (g(n))
23
Asymptotic Upper Bound
Example:
Show that f(x) = x2 + 2x + 1 is O(x2).
f(x) is O(x2).
Asymptotic Upper Bound
We say is in the order of , or
Growth rate of is constant, that is, it is not dependent on
problem size.
is in the order of , or
Growth rate of is roughly proportional to the growth rate of .
is in the order of , or
Growth rate of is roughly proportional to the growth rate of .
In general, any function is faster- growing than any
function.
For large , a algorithm runs a lot slower than a algorithm.
Asymptotic Upper Bound
Consider the example of buying elephants and goldfish:
Cost: cost_of_elephants + cost_of_goldfish
Cost ~ cost_of_elephants (approximation)
Easier way: Discard the low order terms, as well as
constants in a function since they are relatively
insignificant for large n
6n + 4 ~ n
n4 + 100n2 + 10n + 50 ~ n4
i.e., we say that n4 + 100n2 + 10n + 50 and n4 have the
same rate of growth
The Growth of Functions
Popular functions g(n) are
nlogn, 1, 2n, n2, n!, n, n3, log n.
Listed from slowest to fastest growth:
•1
• log n
•n
• n log n
• n2
• n3
• 2n
• n!
27
Growth of Functions
Comparing Growth Rates
n2 n log2 n
2n
T(n)
log2 n
Problem Size
29
Some Examples
Determine the time complexity for the following
algorithm.
count = 0;
for(i=0; i<10000; i++)
count++;
Some Examples
Determine the time complexity for the following
algorithm. 𝑶(𝟏)
count = 0;
for(i=0; i<10000; i++)
count++;
Some Examples
Determine the time complexity for the following
algorithm.
count = 0;
for(i=0; i<N; i++)
count++;
Some Examples
Determine the time complexity for the following
algorithm. 𝑶(𝒏)
count = 0;
for(i=0; i<N; i++)
count++;
Some Examples
Determine the time complexity for the following
algorithm.
sum = 0;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
sum += arr[i][j];
Some Examples
Determine the time complexity for the following
algorithm. 𝑶(𝒏𝟐)
sum = 0;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
sum += arr[i][j];
Some Examples
Determine the time complexity for the following
algorithm.
count = 0;
for(i=1; i<N; i=i*2)
count++;
Some Examples
Determine the time complexity for the following
algorithm. 𝑶(log𝒏)
count = 0;
for(i=N; i>1; i=i/2)
count++;
Thank You
38