Data Structures and Algorithms
Data Structures and Algorithms
Efficiency of Algorithms
Definition :
T(N) = O (f(N)) if there are positive constants
c and n0 such that
T(N) ≤ c f(N) when N ≥ n0
– T(N) grows no faster than f(N)
– growth rate of T(N) is less than or equal to growth
rate of f(N) for large N
– f(N) is an upper bound on T(N)
• not fully correct !
What is performance?
Execution Time (most important !)
Required memory space
How?
– Emprical after implementation
– Theoritical before implementation
int i, a = 0;
for (i=0; i<n; i++)
{
print i; T(n) = θ(n)
a=a+i;
}
return i;
Example :
T(n) = θ(r*q)
GIT – Computer Engineering Department 17
GENERAL RULES
RULE 3 : Consequtive Statements
Add the running times
for … θ(N)
…;
for … θ(N2)
for … θ(N2)
…;
if (condition) T3(n)
S1; T1(n) T(n)
else
S2; T2(n)
T best ( N ) = min {T ( I )}
|I |= N
T worst ( N ) ≥ T av ( N ) ≥ T best ( N )
T ( n ) = O (T worst ( n )) = Ω (T best ( n ))
if p(T) = p(F) = ½
Tav (n) = p(T)T1 (n) + p(F)T2 (n) + T3 (n) = θ(n2)
-2 11 -4 13 -5 -2
A[2,4] sum = 20
A[2,5] sum = 15
T(N) = O(N3)
GIT – Computer Engineering Department 30
Algorithm 1:
j j −1
∑A
k =i
k = A j + ∑ Ak
k =i
T(N) = O(N2)
compute
Te (N) Te (N)
=
Tt (N) f (N)
– if converges to a constant
f(N) is a tight bound
– if converges to zero
not tight bound (overestimate)
– if diverges
underestimate
N Tt Te Te /Tt