Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
48 views

Essential Exercises: Analysis and Design of Algorithms

This document provides a set of exercises related to analyzing algorithmic complexity and solving recurrences. The exercises cover topics like determining asymptotic complexity using Big O notation, solving recurrences using techniques like the Master Theorem, and finding recurrences to model the complexity of recursive algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Essential Exercises: Analysis and Design of Algorithms

This document provides a set of exercises related to analyzing algorithmic complexity and solving recurrences. The exercises cover topics like determining asymptotic complexity using Big O notation, solving recurrences using techniques like the Master Theorem, and finding recurrences to model the complexity of recursive algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ETSI Informatica Unit I: Complexity and Sorting

Analysis and Design of Algorithms


Complexity and Sorting
(2o Degree in Comp Sci, Comp Eng & Soft Eng)
E.T.S.I. INFORMATICA

Essential Exercises
1. Indicate which of the following statements are true:

a) n2 O(n3 )
b) n3 O(n2 )
c) 2n+1 O(2n )
d ) (n + 1)! O(n!)
e) f (n) O(n) 2f (n) O(2n )
f ) 3n O(2n )

g) log n O( n)

h) n O(log n)

Repeat the above changing O() by ().

2. Arrange the following functions in increasing order of growth.


2
(n 2)!, 4 ln(n + 100)10 , 22n , 106 n5 + 9n3 , (ln n) , 3
n, 5n , n + 1010

3. Solve the following recurrences and indicate their order of growth:

a) T (n) = 3T (n 1) + 4T (n 2) with T (0) = 0 and T (1) = 1


b) T (n) = 2T (n 1) (n + 5)3n with T (0) = 0.
c) T (n) = 4T (n/2) + n2 with T (1) = 1.
d ) T (n) = 2T (n/2) + n log n with T (1) = 1.
e) T (n) = 3T (n/2) + 5n + 3 with T (1) = 1.
f ) T (n) = 2T (n/2) + log n with T (1) = 1.

g) T (n) = 2T ( n) + log n with T (2) = 1.
h) T (n) = 5T (n/2) + (n log n)2 with T (1) = 1.
i ) T (n) = T (n 1) + 2T (n 2) 2T (n 3) with T (0) = 106, T (1) = 100, T (2) = 100.

j ) T (n) = 2T (n/4) + n with T (1) = 1.
k ) T (n) = 4T (n/3) + n2 with T (1) = 1.

4. For each of the following recurrences, determine their order of growth using the Master Theorem.
Then, find the exact solution for each of them (no need to compute values for the coefficients that
depend upon initial conditions).

a) t(n) = 4t(n/2) + n2
b) t(n) = 2t(n/2) + n log2 n
c) t(n) = 3t(n/2) + 5n + 3
d ) t(n) = 2t(n/2) + log2 n

e) t(n) = 2t( n) + log2 n
f ) t(n) = 5t(n/2) + (n log2 n)2

5. Find a recurrence (and solve it) for the complexity of the following algorithm:

1
ETSI Informatica Analysis and Design of Algorithms

public static int recursive (int n) {


if ( n <= 1 )
return 1;
else
return (recursive (n-1) + recursive (n-1));
}

6. Given an integer x and an ordered integer array A of length n whose elements are all different:

a) Design an algorithm to determine whether there are two elements in A whose sum is exactly
x.
b) Determine the complexity of this algorithm.
c) Is your solution linear (i.e. O(n))? If not, try to find such a solution.

Additional Exercises
7. Assuming T1 O(f ) and T2 O(f ), indicate which of the following statements are true:

a) T1 + T2 O(f ).
b) T1 T2 O(f ).
c) T1 /T2 O(1).
d ) T1 O(T2 )

8. Obtain an expression of the computational cost t(n) of the following piece of code, assuming
multiplication is the basic operation.

for i 1 to n do
for j 1 to i do
for k j to n do
rrk
endfor
endfor
endfor

9. Indicate for each of the following functions the class (g(n)) they belong to.

a) (n2 + 1)10

b) 10n2 + 7n + 3
c) 2n ln(n + 2)2 + (n + 2)2 ln(n/2)
d ) 2n+1 + 3n1
e) blog2 nc

10. Indicate for each of the following pairs of functions whether they have the same order of growth
or which of the functions grows faster.

a) n(n + 1) and 2000n2


b) log2 n and ln n
c) 100n2 and 0,001n3
2
d ) (log2 n) and log2 n2
e) 2n1 and 2n
f ) (n 1)! and n!

2
ETSI Informatica Unit I: Complexity and Sorting

11. Compute the best and worst case complexity of the following algorithm for determining whether
a matrix is symmetric or not.

public static boolean symmetric (int [] [] matrix, int n){


int i, j;
boolean b;

b = true;
i = 0;
while(i<n && b){
j = i + 1;
while(j<n && b){
b = (matrix [i][j] == matrix[j][i]);
j ++;
};
i ++;
};
return b;
}

12. Given the following recursive algorithm for computing Fibonacci numbers:

func Fib (n: N):N


variables f : N
begin
if n<3 then f 1
else f Fib(n1)+Fib(n2)
endif
return f
end

a) Find a recurrence for the computational cost of the algorithm, assuming the sum is the basic
operation.
b) Solve the recurrence to obtain the exact number of operations performed by the algorithm.

13. Solve the following linear recurrences:

a)

n n61
t(n) =
5t(n 1) 6t(n 2) n > 1

b)

n n61
t(n) =
3t(n 1) + 4t(n 2) n > 1

c)

0 n=0
t(n) =
2t(n 1) 3n (n + 5) n>0

14. Let f (n) (nk ), and let t(n) = at(n/b) + f (n). Express the Master Theorem in this particular
case.

15. Find a recurrence for the computational cost of the following algorithm (printing is assumed to be
the basic operation), and provide a tight bound for this cost using the Master Theorem.

3
ETSI Informatica Analysis and Design of Algorithms

proc A (n: N)
variables i, j: N
begin
for i 1 to n do
for j 1 to i do
print(i, j, n)
endfor
endfor
if n>0 then
for i 1 to 4 do
A(n/2)
endfor
endif
end

jun 16. Apply the Master Theorem to determine the exact order of growth of the following recurrence:
2008
t(n) = 3t(n/2) + n log n

jun 17. Solve the following recurrence:


2
2008 t(n) = 2n (t(n/2))
with the initial condition t(1) = 1.
sep 18. Given the recurrence:
2009 t(n) = 3t( 9 n) + log n
Find its order of growth using the Master Theorem (hint: a change of variable is required first).
jun 19. Apply the Master Theorem to determine the order of growth of the following recurrence:
2010 p
t(n) = 5t( 25 n) + log n
sep
2012 20. Solve the following recurrences:
a) Apply the Master Theorem to obtain the exact order of growth:

t(n) = 2t( n) + log n

b) Solve exactly, with the initial condition t(1) = 1.


t(n) = 2t(n 1) + 2

feb 2013 21. Find an exact solution to the following recurrence using the initial condition T (0) = 0.
T (n) = 2T (n 1) + 3n (n + 5)

feb 2014 22. Solve the following recurrence:



0 n=0
T (n) =
2T (n 1) + n + 2n n>0
feb 2016 23. Solve the following equation using the Master Theorem:
T (n) = 3T (n/4) + n log n

sep 24. Solve the following recurrence:


2016
T (n) = 4T (n/3) + n, n > 1

where T (n) = 1 for n 6 1. Use the method you find most appropriate.

You might also like