Вы находитесь на странице: 1из 3

Q.1 (a) An algorithm takes 0.5ms for input size 100.

How long will it take for input size 500 if the running time is i. Quadratic ii. NlogN Solution : n 1 = 100 n 2 = 500 i. f(n1) = 0.5ms f(n2) = ? Quadratic :

If the algorithm is quadratic then f(n) is O(n2 ) Hence, f(n1) = c*n2, where c is constant. Hence, f(n1) = c*(n1)2. Hence, c = f(n1)/f(n1)2. Hence, c = 0.5/(100)2 = 5*10-5 Therefore, f(n2) = 5*10-5*(500)2 = 12.5ms. ii. NlogN

If the algorithm is linear logarithmic then, f(n) is O(nlog10n) Hence, f(n) = c*n log10 n, where c is constant. Hence, f(n1) = c*n1 log 10 n1. Hence, c = f(n1)/(n1 log10n1). Hence, c = 0.5/(100 * log 10 100) = 2.5*10-3 Therefore, f(n2) = 2.5 * 10-3*(500* log10500) = 3.374 ms. (b) Explain Strassens matrix multiplication. Strassen showed that 2*2 matrix multiplication can be accomplished in 7 multiplication and 18 addition or subtractions. The divide and conquer approach can be used for implementing Strassens matrix multiplication. Divide: Divides matrices into sub matrices : A0 , A1, A2 etc Conquer: Use a group of matrix multiply equations. Combine: Recursively multiply sub-matrices and get the final result of multiplication after performing required additions or subtractions.

(c) State advantages and disadvantages of recursion.

1. In general, the non-recursive version of a program is more efficient in terms of execution time and memory space than a recursive version. 2. In a recursive version, extra time is spent in switching over from one function to another. Moreover, every time a function is called time & memory is spent on memory allocation of local variables for that function. Time is also spent of activity of pushing whenever a function is called and popping whenever that function terminates. 3. In a non-recursive version, this needless stacking activity can be eliminated and moreover variables are allocated memory only once. This results in saving of memory & time. 4. However, we have also seen that sometimes recursion is most natural and logical way of solving certain problems. For example, technique like merge sort and quick sort become easier to code when implemented recursively although their non-recursive versions are also available. 5. Thus, there is a conflict between machine efficiency (saving execution time and saving space) and programmers efficiency (ease on programmers part). With the cost of programming increasing steadily & the cost of machine decreasing, we have reached a point where it is not worth the programmers time to construct a non-recursive solution to a problem that is most naturally solved recursively. (d) Prove that the worst case efficiency of quick sort is O(n2). 1. The best case assumes that the original array and all the resulting sub-arrays are unsorted, so that the pivot value(x[lb]) always finds its proper position at the middle of the sub-array. Suppose that this condition is not true and the original array is sorted or almost sorted in the required order. 2. Then x[lb] (the pivot) will be in its correct position and the original array will be split into two sub-files of size 0 and n-1. If this process continues, a total of n-1 sub arrays are sorted, the first of size n, the second of size n-1, the third of n-2 and so on. 3. Hence, we have the following recurrence relationship: T(n) = T(n-1)+cn T(n-1) = T(n-2) + c(n-1) T(n-2) = T(n-3) + c(n-2) T(2) = T(1) + c{n+(n-1)+(n-2)+.+2} Since, T(1) = 0 Therefore, T(n) = c{n+(n-1)+(n-2)+.+2+(1-1)} T(n) = c{n+n(n-1)/2-1} T(n) = cn + cn2/2 cn/2-c T(n) is O(n ) 4. Similarly, if the original array is sorted in descending order the final position of x[lb] is ub. The array is again split into two sub-arrays of sizes n-1 and 0 that are heavily unbalanced. Here also, a total of n-1 sub-files are sorted, the first of size n, the second of size n-1, the third of size n-2 and so on. Hence, the efficiency is again O(n2).
2

5. Thus, quick sort has an absurd property that it works completely unsorted files and worst for unsorted files. Practically, files which undergo sorting will be unsorted and hence quick sort will be better option than bubble sort.

Вам также может понравиться