Algorithms 1
Algorithms 1
Contents
2. Machine Reason :
Time , Space.
Cost of algorithm :
** How do you compare the efficiency of two algorithms ( for one problem ) ?
1. Compare the execution time.
2. Compare the size of program or (algorithm)
Size of program or algorithm is dependent on :
- The number of lines.
- The number of instructions.
** It’s better \
to measure the complexity of algorithm, that means (count number of basic
operations).
3. Average complexity :
A(n) = ∑ ( ) ( )
Where P(I) is the probability that the instance I will occur and
T(I) the number of basic operations for instance I.
Examples :
Suppose we have a one dim array length 10 containing different int keys
19 22 13 45 34 31 100 90 75 60
1 2 3 4 5 6 7 8 9 10
t=1;
while ( t <= n ) // n is the input size
{
for ( int i = 1 ; i < n ; ++i )
{
Add += i % t ;
for ( int j = n ; j > 1 ; --j )
P = P *3 ;
}
if ( X > 2)
S.O.P(Y - 1);
t = t + 1;
Basic Op Count
<= N
< n2
++ n2
+= n2
% n2
> n3
-- n3
* n3
> N
- <= n
+ N
Given following :
solved
Two algorithms P1, P2 for solving the same problem with W1 , W2 as worst case
complexities of both algorithms :
P1 : W1(n) = 100n
P2 : W2(n) = 4n 2
1. suppose the input size : n < 25
……..
…….
In general :
n : P1 better than P2 , ( using the same computer )
Definitions :
That means an algorithm with worst case complexity g(n) runs faster than one with
worst case complexity f(n)
an algorithm is efficient W(n) is O(nk) , where k N\{0}.
Examples :
Ex1 :
Given two positive real functions W1(n) and W2(n) , where
W1(n) is O(W2(n)) ?
Solution :
Suppose k = 1 and n0 = 25 , g(n) = W1(n) and f(n) = W2(n)
g(n) ≤ k*f(n) ? n ≥ n0
W1(n) ≤ 1*W2(n) ? n ≥ 25
100n ≤ 1*4n2 ? n ≥ 25
25 ≤ n ? Yes n ≥ 25
W1(n) is O(W2(n))
W2(n) is O(W1(n)) ?
Solution :
Suppose k = 1 and n0 = 25 , g(n) = W2(n) and f(n) = W1(n)
g(n) ≤ k*f(n) ? n ≥ n0
W2(n) ≤ 1*W1(n) ? n ≥ 25
4n2 ≤ 100n ? n ≥ 25
n ≤ 25 ? NO n ≥ 25
W2(n) is NOT O(W1(n)) W2(n) is Ω (W1(n))
Ex2 :
W2(n) is Ω (W1(n)) ?
Solution :
Suppose k = 1 and n0 = 25 , g(n) = W2(n) and f(n) = W1(n)
g(n) ≥ k*f(n) ? n ≥ n0
W2(n) ≥ 1*W1(n) ? n ≥ 25
4n2 ≥ 100n ? n ≥ 25
n ≥ 25 ? YES n ≥ 25 W2(n) is Ω (W1(n))
1. Infinite constant functions (like g(n) = 1/2 , = 1/5 , = 7.5 , = 10000 , 1020 , ….)
2. Infinite log functions ( Like g(n) = log2n , 3.5*log2n , 10000*log2n , 5*log2n+1000 -9.5 , … )
3. Infinite linear functions (Like g(n) = 100*n , ….. )
4. Infinite linear log functions ( Like g(n) = 7*n*log2n , …. )
5. Infinite polynomial functions ( like g(n) = n2 , n3 , n5 , 15*n4 – n*log2n , …..)
6. Infinite expontial functions ( Like g(n) = 2n , 3n , ….)
1. lim ( any constant function) / log2(n) = 0 , likes g(n) = 1/2 ,g(n) = 1000 , g(n) = 10200
2. lim ( log2 (n) / n ) =0
3. lim (n / (n*log2 (n)) ) = 0
4. lim ((n*log2 (n)) / n2) = 0
5. lim ( np / nq ) =0 … If ( p < q ) and p , q >= 3
6. p n
lim ( n / 2 ) =0 … positive integer indices p
Efficiency of algorithms :
1. O(1) [ constant functions ] is better than O(log2n)
2. O( log2 n) [log functions ] is better than O(n)
3. O( n) [ linear functions ] is better than O(n log2n)
4. O( n log2n) [ log linear functions ]is better than O(n2)
5. O( np) [ polynomial functions ] is better than O(nq) … if ( p < q) and p,q >= 2
6. O( np) [Expontial functions ] is better than O(2n) … positive integer indices p
OR : \
O(1) < O( log2n) < O(n) < O( n log2n) < O(n2) < O(np) (p > 2) < O(2n) .
Given following :
solved
Two algorithms P1, P2 for solving the same problem with W1 , W2 as worst case
complexities of both algorithms :
P1 : W1(n) = 100n
P2 : W2(n) = 4n 2
Which algorithm is better to use than the other ?
P1 : W1(n) = 100n
lim W1(n)/n = lim 100n/n = 100 ≠ ∞ W1(n) is O(n)
P2 : W2(n) = 4n 2
lim W2(n)/n2 = lim 4n2/n2 = 4 ≠ ∞ W2(n) is O(n2)
solved
T = W(n)*
when we solve the equation , we can know the maximum size , which can be handled
in T .
Examples :
Ex1 :
Suppose τ
= 1 ms ,
W(n) = n2 ,
T = 1 hour
T = W(n)*
60*60 = n2 * 10-3
n2 = 6* 105
n = 600 * √ 10 ≈ 1897 input size
Ex2 :
Given an algorithm with W(n) = 2n runs on two different machines so that the time
for execution a basic operation for the first machine equal to and for the other one
equal to /k , where k >= 2.
Calculate n1 , n2 maximum input size for two machines which can be handled in T time
(Same time interval)
Problem?
Solved
Algorithm
W(n) = 2n
Make a program
Runs on two different machines (Computers) using the same time interval
calculate n1 claculate n2
Solution :
Using the equation T = W(n)*
T = W(n1)*
T = /k*W(n2)
W(n1)* = T = /k*W(n2)
W(n2) = k*W(n1)