Homework 1
Homework 1
Cautions: Each student has to do the homework on his/her own. Its forbidden to use internet resources. You can use books by giving necessary reference information (authors, book name, edition, page number). Do not directly give the results in your solutions. You are supposed to write all the operations in detail.
1. In each of the following situations, indicate whether f (n) = O( g (n)) , f (n) = ( g (n)) or f (n) = ( g (n)) . Give details of your work only for (j), (o), (p) and (q).
2. Consider the Fibonacci numbers. In this problem you will confirm that this sequence grows exponentially fast and obtain some bounds on its growth. a. Use induction to prove that F ( n) 2 0.5n for n>5. b. Find a constant c < 1 such that F (n) 2 cn for all n>0. Show that your answer is correct. c. What is the largest c you can find for which F ( n) 2 cn ?
3. The following algorithm calculates matrix multiplication by decomposing matrices into four block sub-matrices. Analyze this algorithm by calculating the number of scalar multiplications and scalar additions performed by the algorithm. a) Write a recurrence for the number of scalar additions. Solve the recurrence by using Master Theorem. b) Write a recurrence for the number of scalar multiplication. Solve the recurrence by using characteristic equation method. (Hint: You should perform a variable change to be able to apply the method.) c) What is the asymptotic running time of the algorithm?
PROCEDURE f(A[0..n-1,0..n-1], B[0..n-1,0..n-1], R[0..n-1,0..n-1], int n) { //Inputs: nxn-element arrays, A[0..n-1,0..n-1] and B[0..n-1,0n-1], n=2^k //Output: R [1..N, 1..N] if (n == 1) { R[0,0] A[0,0] * B[0,0]; } else { A00 A[0..(n/2)-1, 0..(n/2)-1]; A01 A[0..(n/2)-1, (n/2)..n-1]; A10 A[(n/2)..n-1, 0..(n/2)-1]; A11 A[(n/2)..n-1, (n/2)..n-1]; B00 B01 B10 B11 m1 m2 m3 m4 m5 m6 m7 f f f f f f f B[0..(n/2)-1, B[0..(n/2)-1, B[(n/2)..n-1, B[(n/2)..n-1, 0..(n/2)-1]; (n/2)..n-1]; 0..(n/2)-1]; (n/2)..n-1];
((A00+A11), (B00+B11), R, n/2); ((A10+A11), B00, R, n/2); (A00, (B01-B11), R, n/2); (A11, (B10-B00), R, n/2); ((A00+A01), B11, R, n/2); ((A10-A00), (B00+B11), R, n/2); ((A01-A11), (B10+B11), R, n/2); 0..(n/2)-1] (n/2)..n-1] 0..(n/2)-1] (n/2)..n-1] m1+m4-m5+m7; m3+m5; m2+m4; m1+m3-m2+m6;
} 4. By considering the algorithm below, answer the following questions. a) What are the algorithms basic operations? b) How many times are the basic operations executed? c) Determine the class O(?) the algorithm belongs to.
PROCEDURE q(X[0..n-1]) { //Input: an n-element array X[0..n-1] //Output: an n-element array A[0..n-1] such that for (i 0; i<=n-1, i++) { A[i] sumX(X, i)/(i + 1) } return A[0..n-1] } PROCEDURE sumX(X, int i) { int s=0; for (j 0; j<=i; j=j*2) { s s + X[j]; } return s; }
5. Recurrence relations: a) Solve the following recurrence equations with constant coefficients using characteristic equation method. T(n)=T(n-2)+4n if n>1 T(0)=3 , T(1)=2
b) Solve the following equations using master theorem method. S(n)=3S(n/4)+n logn c) Compare two functions asymptotically. 6. Solve the following recurrences.
n 1
b. T ( n ) = nT ( n ) + 3n and T (1) = 1 . c. T(n) = -5*T(n - l ) - 6*T(n -2) + 42*4n , T(1) = 56 , T(2) = 278 7. Given an unsorted sequence containing n elements x1 , x 2 ,..., x n . Let the difference d ij (1 i < j
n) be equal to x1 x 2 .
a. Give an (n log n) order algorithm to find the minimum of these (n 2 ) differences. (Hint: Consider sorting) b. Give 4 different algorithms to find the smallest n differences. Your algorithms should have different asymptotic complexity. There are algorithms with complexities (n 3 ) , (n 2 log n) , (n 2 ) and, (n log n) i. Write your algorithm. ii. Give detailed complexity analysis (both runtime and space complexities). iii. Implement your algorithms. iv. Validate your theoretical run time complexity analysis with experimental evaluation. v. Compare run time of your algorithms experimentally.