Essential Exercises: Analysis and Design of Algorithms
Essential Exercises: Analysis and Design of Algorithms
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)
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
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.
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.
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:
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.
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
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)
where T (n) = 1 for n 6 1. Use the method you find most appropriate.