The document contains questions for a mid-term exam on design and analysis of algorithms. It includes questions about analyzing time complexities of algorithms like merge sort, quicksort, and matrix multiplication. It also includes questions about designing algorithms for problems like finding maximum/minimum elements, merging sorted lists, and identifying duplicate elements.
The document contains questions for a mid-term exam on design and analysis of algorithms. It includes questions about analyzing time complexities of algorithms like merge sort, quicksort, and matrix multiplication. It also includes questions about designing algorithms for problems like finding maximum/minimum elements, merging sorted lists, and identifying duplicate elements.
ANSWER ANY TEN QUESTIONS Name of the algorithm mentioned in “ “ are as per the convention of the text book. All Parts of a question should be answered at one place. SUBJECT: Design and Analysis of Algorithm Subject Code: CS2006 FULL MARKS : 30 Duration of Examination: 2 Hours [01] Solve the following recurrences exactly or asymptotically. You can assume any convenient form for n. [a] T(1) = 1, T(n) = T(n)+1, n>1 [b] T(n) = 1, T(n) = 7T(n/2) + n ; n > 1 [c] T(0) = 0, 2 T(n)=4T(n/2)+n , n>1 [3] 02[a] The worst-case time of Merge Sort in O(nlogn). What is its best-case time? Can we say that the time for Merge Sort is (n log n)? 02[b] Show that the following equation is correct: 13n 4 + 4log n = (n4)? 02[c] Show that two (large) n-digit integers can be multiplied in O ( n1.585 )? [3] 03[a] Write the recurrence relation to compute the time taken by a Divide and Conquer algorithm to solve defective chessboard problem with size [2K 2k, with k 0]? Solve the recurrence relation using substitution method. 03[b] State Master’s Theorem? Apply the State Master’s Theorem to solve the recurrence relations: T(n) = 3 T(n/2) + n2 , T(1) = 1 03[c] Let T(n) = the number of moves to move a Tower of n disks from the source peg to the target peg. Write the recurrence relation for T(n) based upon the recursive Towers of Hanoi algorithm. Solve this recurrence relation. [3] [04] Write a recursive algorithm “BinSrch” to perform binary search on a list of elements that are sorted in non-decreasing order. Modify Algorithm “BinSrch” so that it searches for two keys. In other words, given an array A[1..n] of n elements and two elements x 1 and x2, the algorithm should return two integer k1 and k2 representing the position of x 1 and x2 respectively, in A. [3] [05] Design a search algorithm that divides a sorted array into one third and two thirds instead of two halves as in binary search algorithm “BinSrch”. Analyze the time complexity of the algorithm. Modify the algorithm “BinSrch” so that it divides the sorted array into three equal parts instead of two as in algorithm “BinSrch”. In each iteration, the algorithm should test element x to be searched for against two entries in the array. Analyze the time complexity of the algorithm. [3] [06] Write a divide conquer recursive algorithm “MaxMin” to find maximum and minimum from a set of n elements? Modify Algorithm “MaxMin” so that it works when n is not a power of 2. Is the number of comparisons performed by the new algorithm 3n/2-2 even if n is not a power of 2? [3] [07] Consider the following modification of algorithm “MergeSort”. The algorithm first divides the input array A[low..high] into four parts A 1, A2, A3 and A4 instead of two. It then sorts each part recursively, and finally merges the four sorted parts to obtain the original array in sorted order. Assume for simplicity that n is a power of 4. Write out the modified algorithm. Analyze its running time. [3] [08] Suppose we have an unsorted array A of n elements, and we want to know if the array contains any duplicate elements. Clearly write an algorithm for solving this problem. By efficient, I mean your method should use O(n log n) key comparisons in the worst case. What is the asymptotic order of the running time of your method in the worst case? Clearly explain how you obtain your result. [3] Page 1 of 2 [09] Suppose a[1:m] and b[1:n] both contain sorted elements in non-decreasing order. Write an algorithm “Merge” that merges these items into c[1:m+n] single sorted sequences in O (n) time. Is it possible to design an efficient algorithm in comparison to “Merge”? [3] [10] Show that average time complexity of “QuickSort” is (n log n)? Show that work space needed by Algorithm “QuickSort” varies between (log n) and (n)? What is its average space complexity? [3] [11] Define Selection Problem? How quick sort is used to find the solution to selection problem [Find the kth LARGEST element from an array of n elements] with the help of an algorithm (Quickselect)? Show that the average time complexity of the algorithms is O (n)? [3] [12] Suppose we have an unsorted array A of n elements, and we want to know if the array contains any duplicate elements. Write an efficient algorithm for solving this problem. By efficient, I mean your method should use O (n log n) key comparisons in the worst case. What is the asymptotic order of the running time of your method in the worst case? Clearly explain how you obtain your result. [3] [13] What do you mean by greedy choice property? Suggest a greedy algorithm to solve 0/1 knapsack problem. Comment on data structure to be used for implementation. What is average case time complexity of your greedy algorithm? [3] [14] Explain the method by Strassen’s for matrix multiplication and prove that the time complexity is (n2.81)? Suppose that someone found a way to multiply two n × n matrices using only 148 multiplications of n/6 × n/6 matrices and (n2) additional work. Assume we used this to produce a recursive divide-and-conquer algorithm for matrix multiplication, similar to Strassen’s algorithm. Write a recurrence for the number of operations required to multiply two n × n matrices by this method. Give -notation for the number of operations required by this algorithm. Your formula may involve expressions like log35 (but of course the numbers might be different from 3 and 5) without giving a numerical value for the logarithm. [3] 15[a] Consider the following variation on “MergeSort” for large values of n. Instead of recusing until n is sufficiently small, recur at most a constant r times, and then use insertion sort to solve the 2 r resulting sub problems. What is the (asymptotic) running time of this variation as a function of n? 15[b] Explain the behavior of Algorithm “QuickSort”, when the input array A[1..n] consists of n identical element? 15[c] Explain the behavior of Algorithm “QuickSort”, when the distinct inputs are already sorted in decreasing order? [3] [16] You have been commissioned to write a program for the next version of electronic voting software. The input will be the number of candidates, d, and an array votes of size v holding the votes in the order they were cast where each vote is an integer from 1 to d. (You can assume that d is typically much smaller than v.) The goal is to determine if there is a candidate with a majority of the votes. If there is a candidate with the majority of the votes, you are to output the indices in votes for the elements that hold a vote for the winning candidate. Describe the algorithm that you would recommend to solve this problem. Analyze the time complexity of your algorithm (as a function of d and v) and very briefly argue why it was the best choice. [3] --------------------------------------- Good luck ---------------------------------------