Sridhar: Design and Analysis of Algorithms
Chapter 3
3.1 An algorithm of order Ο(n2) takes 5 seconds to compute answers for an input instance
size n = 10. If the algorithm size is increased to 50, how much time will it take?
Solution:
C(102) = 5
5
C
102
If the algorithm size is increased to 50, then, C(502) = x
5 5
x 2
502 2500 125 Seconds
10 100
3.2 Write algorithm for the following problems. Use step count and operation count for
finding run time.
a) Matrix addition
The base code is given as follows: step frequency step X frequency
for i = 1 to n do 1 n+1 n+1
for j = 1 to n do 1 n+1 n(n+1)
c[i,j] = a[i,j] + b[i,j] 1 n(n+1) nn
end for - - ---
end for -- --- ----
Step Count = (n+1)+n(n+1)+ n 2
= n+1+ n 2 +n+ n 2 = 2 n 2 +2n+1
= O(n).
One can analyze this segment using operation count also. The
n n
n(n 1)
Operation Count= 1 .
i 1 i 1 2
Therefore, the asymptotic complexity analysis is O(n2 ).
b) Matrix multiplication
The base code is given as follows: step frequency step X frequency
for i = 1 to n do 1 n+1 n+1
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
for j = 1 to n do 1 n+1 n(n+1)
for k = 1 to n do 1 n+1 n(n+1)(n+1)
c[i,j] = c[i,j]+ a[i,k] * b[k,j] 1 n(n+1) nXnXn
end for - - ---
end for -- --- ----
∴Step count = (n+1)+ n(n+1) + n(n+1)(n+1)+ n × n × n= 2n3 3n2 3n 1 = O(n3 )
One can analyze this segment using operation count also. The
n n n
Operation Count= 1 O(n3 ) .
i 1 i 1 i 1
Therefore, the asymptotic complexity analysis is O(n3 ).
c) Matrix norm
Hint: It can be observed that matrix norm is O( n 2 )
d) Summation of an array
Steps
Algorithm sum(n) –
Sum = 0 1
for i = 1 to n do n+1
Sum = sum + n(i) n
end for –
End –
∴ Step count = 2n+2 = O(n).
One can analyze this segment using operation count also. The
n
Operation Count= i n . Therefore, the asymptotic complexity analysis is O(n2 ).
i 1
3.3 Let us say that an algorithm takes 6 seconds for an input size n =10. How much time will
it take when n = 100, if the algorithm run-time is as follows:
a) n3
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
C (103) = 6
6
C
103
C 1003 x
6
x 1003
1000
600 Seconds
b) log n
C log 2 10 6
6
C
log 2 10
C log 2 100 x
6
x log 2 100
log 2 10
12 Seconds
3.4 Let two algorithms take the following values :
A n n3
B n 2n 2
What is the cut-off or break point where algorithms deviate?
n n3 2n2
1 1 2
2 8 8
3 27 18
4 64 32
∴ Cut-off point is after 2.
3.5 Use the limit rule to check that n2n is in Ο(4n).
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
t n n 2n
lim lim 2 n 0
g n 2
∴n2n is in Ο(4n).
3.6 Prove that n n
t n n
∴ lim 1 0
g n n
∴n∈θ(n).
3.7 Prove that all logarithmic functions grow at slow rate.
The slowest growing function is logarithmic function. Therefore, all logarithmic
functions grow at equal rates.
3.8 Differentiate between the following notations.
a) θ and Ο
θ: average case
Ο: worst case (asymptotically bounded by a function from above)
b) Ο and Ω
Ω is asymptotically bounded by a function from below.
3.9 What is the asymptotic notation for the following polynomial?
t(n) = 6n2 + 100
Solution:
Ο(n2)
3.10 Prove the following;
a) n3 log n is in n3
1
n3 log n
lim 3
lim n 0
n n
∴ n3 log n is in n3
b) 2n-4 is in 2n
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
2n 4 1
lim n
lim 4
2 2
1 1
lim
16 16
∴ 2n-4 is in 2n
3.11 Prove the following
a) n4 log n 17 is n 4
By adding rule
max(n4 , log n,17)
n4
b) T n n3 1
By adding rule
max(n3 ,1)
n3 n 2
c) T n 5n2 15n
t n 5n 2 15n
lim
g n n2
5n 2
5
n2
∴ Ω(n2).
3.12 Find suitable notations for the following sequence.
n
a) i
i 1
Solution:
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
n n 1
n
i 2
n2
i 1 =
n
b) i2
i 1
Solution:
n
n n 1 2n 1
i2 2
i 1 =
≈ Ο(n3)
n
c) n3
i 1
Solution:
n 2 n 1
n 2
n 4 3
i 1
n 2 n 2 2n 1
4
n 4 2n 3 n 2
4
≈ n4
n
1
d) i
i 1
Solution:
Ο(log n)
n 1
e) i
i2
Solution:
θ(n)
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
n
f) 7i
i1
Solution:
7 n1 1
7
7 n1 7
7
7n1 7n
n
g) i i 7
i 1
Solution:
= Ο(n2).
n n n
h) ijk
i 1 j 1 k 1
Solution:
n3 n 1
3
8
n3 n3 1 3n 2 3n
8
n6 3n5 3n4 n3
8
Ο(n6).
i) n
1
i i2
i 1
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
Solution:
log n
3.13 What is the time complexity of the following algorithm segments using step count and
operation count?
a)
Frequency C Cx Frequency
k=1 1 1 1
while k≤n n 1 n
k=k+1 n 1 n
End while 0 0 0
2n+1
∴ Step count = 2n+ 1= Ο(n).
One can analyze this segment using operation count also. The
n
Operation Count= i = O(n).
i 1
Therefore, the asymptotic complexity analysis is O(n).
b)
Frequency C Cx frequency
for i=1 to n-1 do N 1 N
for j=i+1 to n do n(n+1) 1 n(n+1)
swap n(n+1) 1 n(n+1)
End for – – –
End for – – –
n(n+1)+ n(n+1)+n
∴Step count= 2n2 3n = Ο(n2).
One can analyze this segment using operation count also. The
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
1 n2
n n
Operation Count=
1 1
Therefore, the asymptotic complexity analysis is n 2 .
3.14 Show that the running time of an quadratic algorithm nearly doubles as input size
becomes larger.
Solution:
Let ‘n’ be the input and doubles it to 2n.
2n
∴ =2
n
This is true for doubling of the input size.
3.15 Let two algorithms be A and B. The complexity functions of algorithms A and B are as
follows.
TA 100n
TB n 4
Which function grows faster as n→∞? What is the relationship between these functions?
Solution:
100n grows faster
TA is exponential and
TB is polynomial algorithm.
3.16
Show that n3 n2 n log n
Solution:
Using additive low
max n3 , n 2 , n log n
n3
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
3.17 Show that n log n log n!
Solution:
log n n
¦ log n
3.18 Given that
T1(n) = Ο(f(n))
T2(n) = Ο(g(n))
Solution :
T1(n)T2(n)
= Ο(f(n),g(n))
= Ο(max(f(n),g(n)))
3.19 Find the θ rotation for each of the following functions.
a) 2n1 4n1
Solution:
2n
2
4 4n 2n 4n
4
n
b) (n2+6)8
8
Solution: n2 ¦ n16
c) 7logn + 10n3
Solution: θ(n3)
3.20 Order the following rotations:
logn, n2n, n!, n
Solution: logn, n , n2n, n!
3.21 Consider the following segment. Apply step count method and analyze the algorithm
segment.
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
Step frequency Stepfrequency
Algorithm sum()
Begin –
sum=0.0 1 1 1
For i=1 to n do 1 n+1 n+1
val=2i 1 n n
sum=sum+i 1 n n
End for
return sum 1 1 1
End
3n+3
∴ step count = 3n + 3 = O(n).
One can analyze this segment using operation count also. The
n
Operation Count= 1 n
1
Therefore, the asymptotic complexity analysis is O(n).
3.22 Perform the complexity analysis of the following algorithm segment that simply returns a
value using step count.
Solution:
Step frequency Stepfrequency
Algorithm sample() –
Begin –
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
for i=1 to n 1 n+1 n+1
return i 1 n n
endfor – –
end – –
2n+1
∴ step count = 2n+1 = O(n).
One can analyze this segment using operation count also. The
n
Operation Count= 1 n . Therefore, the asymptotic complexity analysis is
1
O(n).
3.23 Perform the complexity analysis of the following segment that initialized a matrix to
unity. Use step count.
Solution:
Step frequency Stepfrequency
Algorithm sample()
Begin
for i=1 to n do 1 n+1 n+1
for j=1 to n do 1 (n+1)n n2+n
A(i,j)=0 1 (n+1)n n2+n
End for – – –
End for – – –
end
2n2+3n+1
∴ Step count = 2n2 3n 1 O(n2 ) .
One can analyze this segment using operation count also. The
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
1 n2 . Therefore, the asymptotic complexity analysis is
n n
Operation Count=
i 1 i 1
Ο(n2).
3.24 Consider the following algorithm segment and perform complexity analysis using
operation count. Consider all the operations of the algorithm segment.
Algorithm sample()
Begin
for k = 1 to n-1
for m = k+1 to n
return m+1
endfor
endfor
End
Solution:
This problem is about applying operation count. The operation count for this segment
would be equal to
n 1 1 n 1
1 n k 1 1
k 1 k 1 k 1
n n k 1 1
n2
3.25 Consider the following segment, how many multiplication operations need to be done?
for i= 1 to 50 do
for j = 51 to 100 do
A = BC
end for
end for
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
Solution:
100
The number of multiplication required is equal to 1 100 .
1
3.26 Let us assume that the algorithm analysis of an algorithm yields the summation as
follows.
n 1 n 1 n
T n 3 6
i 1 i 1 i 1
What is the resultant sum?
Solution:
It can be observed that
n
33 3 6n 6n 6n ( 6 6 n)
n 1times n 1times i 1
=(n-1)3-(n-1)(6n)
=3n-3-6n2+6n
=-6n2+3n-3
3.27 Suppose an algorithm takes 8 seconds to run an algorithm on an input size n=12.
Estimate the largest size that could be compute in 56 seconds. Assume the algorithm
complexity is
a) θ(n)
Solution:
cn = 8
12c = 8
8 2
c
12 3
2
x 56 Seconds
3
3
x 56 28 3 84
2
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
n = 84 is the largest we can run.
b) θ(n2)
Solution:
cn2 = 8 Seconds
224c = 8
8
c
224
8
x 56 Seconds
224
224
x 56
8
= 1568 Seconds.
3.28 Let us assume that the given two algorithms are A and B. The complexity functions are
given as follows:
TA = n2
TA = n + 2
What is the breaking point?
Solution: n n2 n+2
2
n –n–2 =0 0 0 0
(n–2)(n+1) =0 1 1 3
n = 2, –1 2 4 6
The cutoff value is 2
3.29 Let t(n) = 3n3+2n2+3. Prove that this is Ο(n3)
Solution:
The highest degree of the polynomial is 3.
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
Ο(n3)
3.30 Let t(n) = 9n2. Prove that the order is Ο(n2)
9n 2 ≤cn2
When c≥ 9, this conditions holds good.
9n2∈ Ο(n2)
3.31 Find the smallest threshold number for the polynomial
3n2 + 255 ≤ 4n2
Solution:
4n3-3n2-255 = 0
n2 = 255
n 255 15.96 ¦ 15
Approximately the cut-off threshold is 16.
or
255 ≤ n2
n 255 15.96 ¦ 16
3.32 Prove that
n∈ Ο(n2)
Solution:
n≤cn2
This condition holds good for all c≥ 1.
This is in Ο(n2).
3.33 Let t(n) = 6n 2 + 7n + 8. Prove that this is of the order of Ω(n2).
Solution:
6n 2 + 7n + 8 ≥cn2
This holds good for c≥ 7
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
This is in Ω(n2) .
3.34 Prove that 2n-1 + n2n∈ Ο(4n)
Proof:
2n 1 n 2n 2n1
2n 1
2n 1 n 22 n
0.
2n 1
4n This holds good for all c This is Ο(4n).
3.35 Consider the following segment. Apply step count method and analyze the algorithm
segment.
Step 1: Algorithm evencount()
Step 2: Begin
Step 3: sum = 0.0
Step 4: for i = 1 to n do
Step 5: if (i mod 2 == 0) then
Step 6: sum = sum + i
Step 7: End if
Step 8: End for
Step 9: return(sum)
Step 10: End.
Solution:
Step 1: Algorithm evencount() Step Freq Total
Step 2: Begin - - -
Step 3: sum = 0.0 1 1 1
Step 4: for i = 1 to n do 1 n+1 n+1
Step 5: if (i mod 2 == 0) then 1 n n
Step 6: sum = sum + i
Step 7: End if - - -
Step 8: End for - - -
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
Step 9: return(sum) 1 1 1
Step 10: End. - - -
Therefore, the step count = 2n+3 = O(n)
3.36 Perform the complexity analysis of the following algorithm segment that simply
returns a value using step count.
Step 1: Algorithm countodd()
Step 2: Begin
Step 3: count = 0
Step 4: for i = 1 to n do
Step 5: if (i mod 2 != 0) then
Step 6: count = count + 1
Step 7 : End if
Step 8 : End for
Step 9: return(count)
Step 10: End.
Solution
Hint: Same as before O(n).
3.37 Perform the complexity analysis of the following segment that initialize the diagonal
of a matrix to unity. Use step count.
Step 1: Algorithm sample()
Step 2: Begin
Step 3: for i = 1 to n do
Step 4: for j = 1 to n do
Step 5: if (i==j) then
Step 6: A(i,j) = 1
Step 7: End if
Step 8: End for
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
Step 9: End for
Step 10: End.
Solution
Step 1: Algorithm sample() Step Count Total
Step 2: Begin - - -
Step 3: for i = 1 to n do 1 n+1 n+1
Step 4: for j = 1 to n do 1 (n+1)n (n+1)n
Step 5: if (i==j) then 1 nxn nxn
Step 6: A(i,j) = 1
Step 7: End if - - -
Step 8: End for - - -
Step 9: End for - - -
Step 10: End. - - -
Therefore, the step count = (n 1) n2 n n2 2n2 2n 1 O(n2 )
Therefore, the asymptotic complexity is O( n 2 ).
3.38 Consider the following segment and perform complexity analysis using operation
count. Consider the dominant operations of the algorithm segment.
Step 1: Algorithm sample() step count Total
Step 2: Begin - - -
Step 3: for k = 1 to n do 1 n+1 n+1
Step 4: for m = 1 to n do 1 n(n+1) n(n+1)
Step 5: return (k*m) 1 nxn nxn
Step 6: End for -- --- ---
Step 7: End for -- --- ---
Step 8: End. -- ---- ---
Therefore, step count = n+1+ n2 n + n 2 = O(n2 ) .
© Oxford University Press, All rights reserved.
Sridhar: Design and Analysis of Algorithms
Therefore, the asymptotic complexity is O( n 2 ).
© Oxford University Press, All rights reserved.