You are on page 1of 4

DATA STRUCTURES & ALGORITHMS

Tutorial 1 Questions COMPUTATIONAL COMPLEXITY

Required Questions Question 1. Reorder the following efficiencies from the smallest to the largest: 2n log(n2); 5n6; 22013; 2.5n; 2.2n; 2n6.5; log(n10); 4 log(n); 2100; n1.03; 70n; n log n; 8n6 + 5n2 Question 2. Determine the big-O notation for the following: a. 2n9 b. 100n + 10.5n2/3 + 5.2n7/5 c. 19log2(n) d. 5n10 + 2logn e. n! + k! (k is a predefined constant) f. 100log2(n) + 5(n+2n) Question 3. Calculate the run-time efficiency of the following program segment:
1 i = n-1 2 loop (i >= n/3) 1 j = 1 2 loop (j < n/3) 1 print(i, j) 2 j = j + 3

3 end loop 4 i = i - 3 3 end loop

Question 4. If the algorithm doIt has an efficiency factor of n2, calculate the run time efficiency of the following program segment: 1i=1 2 loop (i <= n) 1j=1 2 loop (j <= n) 1k=1 2 loop (k <= n) 1 doIt() 2k=k +2 3 end loop 4j=j*2 3 end loop 4i=i+1 3 end loop

Question 5. Given that the efficiency of an algorithm is 3nlog2(n3), if a step in this algorithm takes 1 nanosecond (10-9), how long does it take the algorithm to process an input of size 1024? Question 6. 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 2 2 else 1 return g(n 1)+ 2 End g

Advanced Questions Question 7. Prove that for any positive functions f and g, f(n) + g(n) and max(f(n), g(n)) are asymptotically equivalent (i.e. they yield the same big-O notations). Question 8.

Estimating the time complexity of the following program segment: 1i=0 2 loop (i < N) 1j=i 2 loop (j < N) 1k=0 2 loop (k < M) 1x=0 2 loop (x < K) 1 print(i, j, k) 2x=x+3 3 end loop 4k=k+1 3 end loop 4k=0 5 loop (k < 2*K) 1 print(k) 2k=k+1 6 end loop 7j=j+1 3 end loop 4i=i+1 3 end loop Question 9. Calculate the run-time efficiency of the following program segment: 1i=n 2 k = n/3 3 loop (i >= k) 1 j = n 3*k 2 loop (j < i) 1 print(i, j) 2j=j+3 3 end loop 4i=i-1 4 end loop Question 10. Give the best asymptotic (big-Oh) characterization of the best and worst case time complexity of the algorithm Count(A;B; n). Explain how you computed the complexity. Algorithm Count(A,B,n) Input: Arrays A and B of size n, where n is even. A, B store integers. i=0 sum = 0

while i <n/2 do if A[i + n/2 ] < 0 then for j = i+n/2 to n do sum = sum+B[j] i = i+1 return sum Question 11. Let A be an array storing positive integers. Write in pseudocode an algorithm that rearranges the elements of A so that the odd elements appear before the even elements. For example, if the input to the algorithm is an array A = {4; 5; 2; 9; 2}, and example of a valid output is A = {5; 9; 4; 2; 2}. Other outputs, for example A = {5; 9; 2; 2; 4}, are also valid, as long as the odd elements appear before the even ones. Compute the time complexity of your algorithm in the worst case. Explain how you computed complexity. Most likely, your complexity will be not linear. At the end of the course, we will learn a linear time algorithm for this problem. Question 12. 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 2) End

You might also like