Professional Documents
Culture Documents
HCM
Khoa Khoa học & Kỹ thuật Máy tính
COMPUTATIONAL COMPLEXITY
Question 1.
Solution:
Efficiency: a measure of amount of time for an algorithm to execute (Time Efficiency) or a
measure of amount of memory needed for an algorithm to execute (Space Efficiency).
Question 2.
Solution:
a. False.
Ex: f(n) = 2n, g(n) = 3n
b.False
Ex: f(n) = n, g(n) = n + 1.
Explanation:
First, we review the definition of Big-O:
f(n) = O(g(n)) there are constants C R+ and kN such that 0 ≤ f(n) ≤ Cg(n) where n > k
Question 3.
Solution:
a. O(n5)
b. O(n)
c. O(n)
d. O(2n)
e. O(n!)
f. O(nlog2(n))
Question 4.
Solution:
Assume that n/2 = [n/2] in the given program segment.
If n is even, the run-time efficiency is
n + (n – 2) + (n – 4) + … + 2 = n(n + 2)/4 = O(n2)
If n is odd, the run-time efficiency is
n + (n – 2) + (n – 4) + … + 1 = (n + 1)2/4 = O(n2)
Or generally, the run-time efficiency of the given program segment is O(n2).
Question 5.
Solution:
The iteration of variable i is executed N times. For each loop of variable i, the iteration of
variable j is executed N, N-1, N-2, …, 1 times. For each loop of variable j, the first iteration of
variable k is executed M times. For each loop of variable k, the iteration of variable x is
executed [(N+2)/3] times; the second iteration of variable k is executed 2M times. Therefore,
the run-time efficiency is
N*(M*[(N+2)/3] + 2*M) + (N-1)*(M*[(N+2)/3] + 2*M) + … + 1*(M*[(N+2)/3] + 2*M)
= N*(N+1)*(M*[(N+2)/3] + 2*M) / 2
= O(N3*M)
Question 6.
If the algorithm doIt has an efficiency factor of 7n, calculate the run time efficiency of the
following program segment:
1i = 1
2 loop (i <= n)
1j = 1
2 loop (j < n)
1 k = 1
2 loop (k <= n)
1 doIt(…)
2 k = k + 1
3 end loop
4 j = j + 1
3 end loop
4 i = i + 1
3 end loop
Solution:
There are 3 nested loops, the iteration of variable i is executed n times, j is executed n-1 times,
k is executed n times. Therefore, the run-time efficiency is n(n-1)n(7n) = O(n4).
Question 7.
Write a recurrence equation for the running time T(n) of f(n), and solve that recurrence.
Algorithm f (val n <integer>)
Pre n must be greater than 0
Return integer value of f corresponding to n
1 if (n = 1)
1 return 1
2 else
1 return f(n – 1) + f(n – 1)
End f
Solution:
T(1) = 1
T(n) = 1 + 2*T(n-1) = 1 + 2*(1 + 2*T(n-2)) = …
= 1 + 2 + 22 + … + 2n-1 = 2n - 1 = O(2n)
Question 8.
Write a recurrence equation for the running time T(n) of g(n), and solve that recurrence.
Algorithm g (val n <integer>)
Pre n must be greater than 0
Return integer value of g corresponding to n
1 if (n = 1)
1 return 1
2 else
1 return 2*g(n – 1)
End g
Solution:
T(1) = 1
T(n) = 1 + T(n-1) = 1 + 1 + T(n-2) = 1 + 1 + … + 1 + 1 + T(1) = n = O(n)
Question 9.
Given that the efficiency of an algorithm is 5nlog2(n), if a step in this algorithm takes 1
nanosecond (10−9), how long does it take the algorithm to process an input of size 7000?
Solution:
5 7000 log2(7000) 10−9 = 4.4706 10-4s
-- End --