Sir Kashif Chapter 2
Sir Kashif Chapter 2
Session-05 , Spring-07
Chapter 2
Algorithm Analysis
f(1000) g(1000)
0.002998 0.3%
f(1000)
and this difference goes to zero as n → ∞
The justification for the pair of polynomials being similar
is that, both have the same leading term:
n2
Asymptotic Efficiency
how the running time of an algorithm increases with
the size of the input in the limit, as the size of input
increases without bound
Running Time
f(n) and g(n) are functions
over non-negative
integers
n0 Input Size
The growth rate of f(n) is
less than or equal to that
of g(n)
1: means set membership i.e. f(n) Є O(g(n))
Copyright © Kashif Javed Algorithm Analysis 2-16
Mathematical Background…
g(n) is an upper bound on
f(n)
O-notation provides an
asymptotic upper bound on
a function c g ( n)
Used for worst-case analysis f (n )
Running Time
Examples
If f(n) = 2n2 , then
n0
f(n) = O(n4) , f(n) = O(n3) and Input Size
Running Time
cg(n) f(n) for all n n0
c g ( n)
The growth rate of f(n) is
greater than or equal to
that of g(n) n0 Input Size
Running Time
case running times
c g ( n)
Examples
n3 grows faster than n2 , so n0 Input Size
we can say that n3 = W(n2)
Running Time
n n0 c 1 g (n )
Running Time
g(n) c 1 g (n )
g(n) is an asymptotically
tight bound for f(n)
n0 Input Size
Examples
½n2 – 3n = Q(n2)
6n3 ≠ Q(n2)
Rule3
logk n = O(n) for any constant k, indicating
logarithms grow very slowly
Function Name
c constant
logn logarithmic
log2 n log-squared
n linear
nlogn
n2 quadratic
n3 cubic
2n exponential
where f ‘(n) and g’(n) are the derivates of f(n) and g(n)
respectively
f( n)
Then lim 25
n g( n)
So 25n2 + n = Θ(n2)
f( n)
Then lim ?
n g( n)
An algorithm is correct
If, for every input instance, it halts with the correct
output
Incorrect algorithms
Might not halt at all on some input instances
Might halt with other than the desired answer
Resources include
• Memory (space complexity)
• Computational time (time complexity)
algorithm used
6n
5n
4n
3n
2n
1n
worst-case
6n average-case
Running time
5n
best-case
4n
3n
2n
1n
1 2 3 4 5 6 7 8 9 10 11 12 …..
Input instance size
Copyright © Kashif Javed *http://www.cs.aau.dk/~simas/ad01/index.html Algorithm Analysis 2-43
Chapter 2: Algorithm Analysis
2.1 Mathematical Background
2.2 Model
2.3 What to Analyze
2.4 Running Time Calculations
Θ(1) N 1
T( N ) recurrence relation
T( N 1) Θ(1) N 2
Example
-2, 11, -4, 13, -5, -2
Answer: 20
1 ?
i 0 j i k i
/* 9*/
}
return MaxSum;
}
Copyright © Kashif Javed Algorithm Analysis 2-59
Running Time Calculations…
int MaxSubsequenceSum( const int A[ ], int N )
Algorithm 2 {
The running time is int ThisSum, MaxSum, i, j;
/* 1*/ MaxSum = 0;
O(N2) /* 2*/ for( i = 0; i < N; i++ )
{
/* 3*/ ThisSum = 0;
Precise analysis
/* 4*/ for( j = i; j < N; j++ )
N 1 N 1 {
1 ?
i 0 j i
/* 5*/
/* 6*/
/* 7*/
ThisSum += A[ j ];
if( ThisSum > MaxSum )
MaxSum = ThisSum;
}
}
/* 8*/ return MaxSum;
}
Copyright © Kashif Javed Algorithm Analysis 2-60
Running Time Calculations…
Algorithm 3
This algorithm uses a divide-and-conquer
strategy
4 -3 5 -2 -1 2 6 -2
Algorithm1
Scan through the list from left to right
Running time is linear
e.g. Gcd(50,15) = 5