CS502 Design & Analysis of Algorithms

Size: px
Start display at page:

Download "CS502 Design & Analysis of Algorithms"

Transcription

1 1. The word Algorithm comes from the name of the Muslim author Abu Ja far Mohammad ibn Musaal-Khowarizmi or Al-Khwarizmi Khwarizm Kheva Uzbekistan 2. In order to say anything meaningful about our algorithms, it will be important for us to settle on a. C++ program Java program Pseudo program Mathematical model of computation pg A RAM is an idealized machine with random-access memory. 256MB 512MB an infinitely large pg GB 4. Which formula is used for calculating worst case running time? Tworst n) = max T ( ) ( I = n I 1 Tworst( n) = max I = 1 T ( I) Tworst ( n) = max I = 1 T ( n) Tworst( n) = max I = n T ( n)

2 2 CS502 Design & Analysis of Algorithms 5. Divide-and-conquer involves breaking the problem into a small number of Sieve pivot Sub problems pg 34 Selection 6. In the analysis of Selection algorithm, we eliminate a constant fraction of the array with each phase; we get the convergent series in the analysis, linear arithmetic geometric pg 37 harmonic 7. One of the clever aspects of heaps is that they can be stored in arrays without using any. Pointers pg 40 constants variables functions 8. Quick sort can work well in virtual memory environment. It is true It is false Sorting is not performed in virtual memory environment Either true or false 9. What is common between Bubble sort, Insertion sort, Selection sort, Quick sort, and Heap sort? All are in-place algorithms pg 54 All are stable algorithms None of these All are unstable algorithms 10. Counting sort algorithm sorts in Θ(n + k) Θ(n - k) Θ(n) Θ(k) 11. If there are Θ (n 2 ) entries in edit distance matrix then the total running time is Θ (1) Θ (n 2 ) pg 84 Θ (n) Θ (n log n) 12. A product of matrices is if it is either single matrix or the product of two matrix products, surrounded by parentheses. Fully parenthesized Partially parenthesized Not parenthesized None of the options 13. Time complexity of chain matrix multiplication is Θ (n 3 ) and space complexity is Θ (n 2 )

3 3 CS502 Design & Analysis of Algorithms Θ (n 3 ) Θ (n log n) Θ (log n) 14. Suppose we have three items as shown in the following table, and suppose the capacity of the knapsack is 50 i.e. W = 50. Item Value Weight The optimal solution is to pick Items 1 and 2 Items 1 and 3 Items 2 and 3 None of these 15. Dynamic programming algorithms Store the results of end points Store the problems statment Do not store the subproblem results Do store the results of intermediate subproblems pg Algorithm s essential elements are Step wise solution Stepwise solution and finite time Step wise solution finite inputs Stepwise approach in which time and memory does not matter. 17. Suppose we have an algorithm that carries out N 2 operations for an input of size N. Let us say that a computer takes 1 microsecond (1/ second) to carry out one operation. How long does the algorithm run for an input of size 3000? 90 seconds 9 seconds 0.9 seconds 0.09 seconds 18. What is the solution to the recurrence T(n) = T(n/2)+n, T(1) = 1? O(logn) O(n) 37 O(nlogn) O(n 2 ) 19. Which of the following is true? The worst case time complexity of the quick sort is NlogN The best case time complexity of the merge sort is logn The worst case time complexity of the selection sort is N^2 The worst case time complexity of the merge sort is logn 20. In Quick Sort, partition algorithm partitions the array into sub arrays. 2 3 pg 46 1

4 4 Q # 01: In order to say anything meaningful about our algorithms, it will be important for us to settle on a. C++ program Java program Pseudo program Mathematical model of computation Q # 03: Divide-and-conquer involves breaking the problem into a small number of Pivot Sub problems Selection Sieve Q # 04: For the heap sort, access to nodes involves simple operations. Arithmetic 41 binary algebraic logarithmic Q # 05: If a sorting algorithm solely based on comparisons of keys in the array then it is impossible to sort more efficiently than Ω (n lg n) Θ (n lg n) Ο (n lg n) None of these Q # 06: The comparison based algorithm defines a(n). decision tree 54 array linked list stack 4 Q # 07: The main shortcoming of counting sort is that it is useful for Small Integers 71 Small characters Floats None of these Q # 08: Memoization is a process To avoid unnecessary repetitions by writing down the results of recursive calls and looking them again if needed later 74 To avoid repeated data by writing down the results of input array and looking them again if needed later. None of these All options are correct Q # 09: Matrix multiplication is An associative but not commutative operation 85

5 An associative and commutative operation Not associative but commutative operation Neither associative nor commutative operation Q # 10: We can find the product A x B of matrices A and B, only if they are compatible which means, No of Columns of A must be equal to No of Rows of B No of Columns of A must be equal to No of Columns of B No of Rows of A must be equal to No of Rows of B Order of A must be equal to order of B Q # 11: A product of matrices is if it is either single matrix or the product of two matrix products, surrounded by parentheses. Fully parenthesized Partially parenthesized Not parenthesized None of the options Q # 12: Merge sort requires Extra storage 54 No need of extra storage Sometimes requires extra storage Extra time than other sorting algorithms Q # 13: Heap sort is In place and stable Out place and stable Out place but not stable In place but not stable 54 Q # 14: Worst case running time of Quick Sort algorithm for an array with n elements is? 2 n n n 2 n 8 n 5 Q # 15: Which of the following is calculated with Big O notation? Medium bounds Lower bounds Upper bounds 25 Both upper and lower bound Q # 16: Which of the following is calculated with Big Theta notation? Lower bounds 25 Upper bounds Both upper and lower bound Medium bounds Q # 17: Identify the maximal points in given set, according to 2-D maxima (the points that are NOT dominated by other points).

6 {( 2,5 ),( 4, 4 ),( 4,11 ),( 5,1 ),( 7,7 ),( 7,13 ),( 9,10 ),( 11,5 ),( 12,12 ),( 13,3 ),( 14,10 ),( 15,7) } { } ( 7,13 ),( 12,12 ),( 14,10 ),( 15,7) { } ( 7,7 ),( 7,13 ),( 9,10 ),( 11,5 ),( 14,10) { } ( 2,5 ),( 4, 4 ),( 4,11 ),( 5,1 ),( 14,10) ( 4, 4 ),( 4,11 ),( 7,13 ),( 9,10)( 14,10) { } n i= 1 n Q # 18: is equal to, n(n+1) n(n) n(n+1)/3 n+1 20 Q # 19: What is the running time of the above sorting algorithm in worst case? 2 Θ( n ) 3 Θ( ) n 39 Θ( log ) n n Θ( log n) 6 Q # 20: The reason for introducing Sieve Technique algorithm is that it illustrates a very important special case of Divide-and-conquer 34 Decrease and conquer Greedy nature 2-dimension Maxima MIDTERM EXAMINATION

7 Q # 01: Random access machine or RAM is a/an Machine build by Al-Khwarizmi Mechanical machine Electronics machine Mathematical model 10 What is common between Bubble sort, Insertion sort, Selection sort, Quick sort, and Heap sort? All are in-place algorithms (pg54) All are stable algorithms None of these All are unstable algorithms 7 In order to say anything meaningful about our algorithms, it will be important for us to settle on a. C++ program Java program Pseudo program Mathematical model of computation (pg10) Q # 04: In which order we can sort? increasing order or decreasing order 39 both at the same time increasing order only decreasing order only Q # 05: For the heap sort we store the tree nodes in level-order traversal 40 in-order traversal pre-order traversal post-order traversal Q # 06: Quick sort procedure was invented by Hoare in 1960 Sedgewick Mellroy Coreman Q # 07: For sorting algorithms which one is the efficient algorithm having running time? O(n 2 ) O(n log n) not sure O(n 2 log n) O(n 3 ) Q # 08: When a recursive algorithm revisits the same problem over and over again, we say that the optimization problem has sub-problems. Overlapping Over costing Optimized None of these

8 Q # 09: A problem exhibits if an optimal solution to the problem contains within it optimal solution to sub-problems. Optimal structure 92 Efficient structure Inefficient structure Unknown behavior Q # 10: A p q matrix A can be multiplied with a q r matrix B. The result will be a p r matrix C. There are (p. r) total entries in C and each takes to compute. (q) 84 (1) (n2) (n3) Q # 11: Dynamic programming is Other name of rescursion Other name of divide and conquer Recursion without repetition 75 Recurrence development technique Q # 12: Dynamic programming algorithms Store the results of end points Store the problems statement Do not store the subproblem results Do store the results of intermediate subproblems 75 Q # 13: The worst case running time of the algorithm given below is, 8 6 Θ( ) n 2n 6 Θ n 2 Θ( n ) Θ( 2nlg 6) 14 page Q # 14: Due to left-complete nature of binary tree, heaps can be stored in Arrays 40 Structures

9 Link list Stack 9 QNo.1 What is heap and what is heap order? (Mark2) Answer:- The heap is the section of computer memory where all the variables created or initialized at runtime are stored. The heap order property: in a (min) heap, for every node X, the key in the parent is smaller than or equal to the key in X. QNo.2 Quick sort such that sort the array in to non-increasing order? (Mark2) Answer:- Quick sorting, an array A[1..n] of n numbers We are to reorder these elements into increasing (or decreasing) order. More generally, A is an array of objects and we sort them based on one of the attributes - the key value. The key value need not be a number. It can be any object from a totally ordered domain. Totally ordered domain means that for any two elements of the domain, x and y, either x < y, x = y or x > y. QNo.3 Draw the cost table for chain multiplication problem with initial states(mark3) Answer:- Cost table for chain multiplication problem in initial stage is the diagonal entries (all m [i, i]) filled with zeros and rest of the table entries are empty which are to be filled in next steps of the table calculation. (A1)(A2A3A4...An) or (A1A2)(A3A4...An) or (A1A2A3)(A4...An) or (A1A2A3A4...An 1)(An) QNo.4 we can avoid unnecessary repetitions for recursive calls? (Mark3) Answer:- We can avoid these unnecessary repetitions by writing down the results of recursive calls and looking them up again if we need them later. This process is called memorization. Worst case for edit distance algorithm? What is the simple change that can change the worst case time? Answer:- Analysis of DP edit distance There are (n2 ) entries in the matrix. Each entry E(i,j) takes (1) time to compute. The total running is 2 (n ) Recursion clearly leads to the same repetitive call pattern that we saw in Fibonnaci sequence. To avoid this, we will use the DP approach. We will build the solutionb bottom-up. We will use the base case E(0,j) to fill first row and the base case E(I,0) to fill first column. We will fill the remaining E matrix row by row. Q:Describe an efficient algorithm to find the median of a set of 106 integers; it is known that there are fewer than 100 distinct integers in the set. Solution:- Step1:Start Step2:Find the 100 distinct numbers among 10^6 integers. Step3:Sort the 100 distinct numbers Step4:Count the distinct numbers Step5: if count is odd,middle number is the median Step6:if count is even,add the middle two numbers then divide by 2,the result is the median Step5:End number. What is the formula for generating Catalan numbers?

10 10 CS502 Design & Analysis of Algorithms Solution Equation (22) is a recurrence relation. C_(n+1) = C_n * [2(2n+1)] / (n+2) we have the values of n in one column and the values of C_n in another, then to put this formula in Excel, on the (n+1)-th row just replace C_n and n with the appropriate cells from the previous row. What are Catalan numbers? Give the formula. Catalan numbers form a sequence of natural numbers that occur in various counting problems, often involving recursively defined objects Formula is C(n) = 2n Cn / (n+1) Q-Write a pseudo code Fibonacci With memorization? -- (3) Sol: MEMOFIB(n) 1 if (n < 2) 2 then return n 3 if (F[n] is undefined) 4 then F*n+ MEMOFIB(n 1) + MEMOFIB(n 2) 5 return F[n] Q Write Down the steps of Dynamic programming (5) Dynamic programming is essentially recursion without repetition. Developing a dynamic programming algorithm generally involves two separate steps: Formulate problem recursively. Write down a formula for the whole problem as a simple combination of answers to smaller sub problems. Build solution to recurrence from bottom up. Write an algorithm that starts with base cases and works its way up to the final solution. Dynamic programming algorithms need to store the results of intermediate sub problems. This is often but not always done with some kind of table. We will now cover a number of examples of problems in which the solution is based on dynamic programming strategy. Q: How we build heap Sol:We build a max heap out of the given array of numbers A[1..n]. We repeatedly extract the maximum item from the heap. Once the max item is removed, we are left with a hole at the root. Q:Write Pseudo code for KNAPSACK algorithm? 5 marks Solution: KNAPSACK (n, W) 1 for w=0,w 2 do V[0,w] 0 3 for i=0,n 4 do V[i,0] 0 5 for w=0,w 6 do if(wi w i v +V[i-1,w- i w ]>V[i-1,w]) 7 then V[I,w] i v +V[i-1,w] 8 else V[i,w] V[i-1,w] The time complexity is clearly O(n,W), it must be cautioned that as n and W get large, both time and space complexity become significant. Q:Spelling correction in edit distance? 3 marks Sol:A better way to display this editing process is to place the words with the other: S D I M D M, M A - T H S, A - R T - S THE FIRST WORD HAS AGAP FOR EVERY INSERTION (1)AND THE SECOND WORD HAS A GAP FOR EVERY DELETION (D). MATHES (M) DO NOT COUNT. THE EDIT TRANSCRIPT IS DEFINED AS A STRING OVER THE ALPHABETM,S,I,d THAT DESCRIBES A TRANSFORMATION OF ONE STRING INTO OTHER. FOR EXAMPLE S D I M D M =4 Q: Differentiate b/w Bubble sort, insertion sort and selection sort? 3 marks SOLUTION:Bubble sort: scan the array. Whenever two consecutive items are found that are out of order, swap them. Repeat until all consecutive items are in order. Insertion sort: assume that A*1 i-1] have already been sorted. Insert A[i] into its properposition in this sub array. Create this position by shifting all larger elements to the right. Selection sort: Assume that A[1..i-1] contain the i-1 smallest elements in sorted order. Find the smallest in A*i.n+ swap it with A*i+. Write down the steps of dynamic programming strategy. (2 marks)

11 11 CS502 Design & Analysis of Algorithms Solution: Developing a dynamic programming algorithm generally involves two separate steps: 1_formulate problem recursively. Write down a formula for the whole problem as a simple combination of answers to smaller sub problems. 2_ Build solution to recurrence from bottom up: Q: Solve the recursion problem. (5marks.) Solution: Recursion clearly leads to the same repetitive call pattern that we saw in Fibonnaci sequence. To avoid this, we will use the DP approach. We will build the solution bottom-up. We will use the base case E(0,j) to fill first row and the base case E(I,0) to fill first column. We will fill the remaining E matrix row by row.if we trace through the recursive calls to MemoFib, we find that array F[] gets filled from bottom up i.e., first F*2+, then F*3+, and so on, upto F[n]. we can replace recursion with a simple for-loop that just fills up the array F[] in that order. We are given an array of n elements of x1, x2,x3,,,,,xn, Q: Suggest best sorting algorithm of order On. (5 marks). Solution:The main shortcoming of counting sort is that it is useful for small integers, i.e., 1 k where k is small. If this were a million more, the size of the rank array would also be a million. Radix sort provides a nice work around this limitation by sorting numbers one digit at a time. Q: What are the two steps generally involved while developing dynamic programming algorithm. Solution: Developing a dynamic programming algorithm generally involves two separate steps: Q: How we build heap? (2) Solution: We build a max heap out of the given array of numbers A[1..n]. We repeatedly extract the maximum item from the heap. Once the max item is removed, we are left with a hole at the root. What are the applications of edit distance technique? Name any three (3) Solution: 1. Spelling Correction 2. Plagiarism Detection 3. Computational Molecular Biology 4. Speech recognition. What is the worst case running time for the bucket sort? What simple change is required in the algorithm to preserve its linear expected running time and makes it worst case time Θ(n log n) (5) Solution: The worst case for bucket sort occurs when the all inputs falls into single bucket, for example. Since we use insertion sort for sorting buckets and insertion sort as a worst case of O(n2). the worst case runtime for bucket sort is O(n2). By using an algorithm with worst case runtime of O(nlgn) instead of insertion sort for sorting buckets, we can ensure that worst case is O(nlgn) without affecting the average case behavior. Given an unordered list of n x0, x1, x2,, xn and elements is common, if there are atleast n/5 copies of it.we want to identify all the common numbers in our list. Give O(n log n) to solve the problem. (5) Solution:We define R n to be the set of ordered n-tuples of real numbers, Rn := {(x1, x2... xn) : x1,..., xn R}.The elements of Rn are called vectors. Given a vector x = (x1,..., xn), the numbers x1,..., xn are called the components of x. You are already quite familiar with Rnfor small values of n. Q: Find the out cost A1=5 x 4 A2= 4 x 6 A3= 6x2 (2 marks) Solution:-((A1A2)A3) = (5 4 6) + (5 6 2)= 180 (A1(A2A3)) = (4 6 2) + (5 4 2) = 88

12 Q: How to construct an optimal solution for o/1 knapsack problem? Sol: Construction of optimal solution: Construct an optimal solution from computed information. Let us try this: If items are labelled 1, 2,..., n, then a subproblem would be to find an optimal solution for S k = items labelled 1, 2,..., k This is a valid subproblem definition. The question is: can we describe the final solution S n in terms of subproblems S k? Unfortunately, we cannot do that. Here is why. Consider the optimal solution if we can choose items 1 through 4 only. Solution Items chosen are 1, 2, 3, 4 Total weight: = 14 Total value: = 20 Now consider the optimal solution when items 1 through 5 are available Q: Effect of calling max heap Solution:-The smallest key is in the root in a min heap; in the max heap, the largest is in the root. Q: Suggest the criteria for measuring algorithms. Also discuss the issues need to be discussed in the algorithm design. Solutions:- In order to design good algorithms, we must first agree the criteria for measuring algorithms. The emphasis in this course will be on the design of efficient algorithm, and hence we will measure algorithms in terms of the amount of computational resources that the algorithm requires. These resources include mostly running time and memory. Depending on the application, there may be other elements that are taken into account, such as the number disk accesses in a database program or the communication bandwidth in a networking application Q: Write the STABLE OR IN-PLACE of QUICK, HEAP, COUNTING, MERGE Sorts. [5 MARKS] Answer: In-place Stable In-place Stable Merge sort Bubble sort Bubble sort Quick sort Insertion sort Insertion sort Heap sort Selection sort Counting sort 12 Q: Define MEMOIZATION? [2 MARKS] Answer: We can avoid unnecessary repetition by write down the recursive calls and look them when we need. Define worst case and average case of Quick sort? Answer: Worst case: We maximize over all possible values of q, means the selection of pivot q which gives the maximum (worst) time for sorting. The worst case running time is O (n2). Average case: Average case analysis of quick sort, the average is computed over all possible random choices of the pivot index q. The average case running time for quick sort is. (n log n). Q-We can avoid unnecessary repetitions for recursive calls?( I thnk how can we...) [PAGE 74] Answer: By using Fibonacci Sequence We can avoid this unnecessary repetition by writing down the results of recursive calls and looking them up again if we need them later. This process is called memoization. Spelling correction in edit distance? 3 marks

13 13 CS502 Design & Analysis of Algorithms Answer: Spelling Correction: If a text contains a word that is not in the dictionary, a 'close' word, i.e. one with a small edit distance, may be suggested as a correction. Most word processing applications, such as Microsoft Word, have spelling checking and correction facility. When Word, for example, finds an incorrectly spelled word, it makes suggestions of possible replacements. Q-What is Bubble sort? Answer: Bubble sort: Scan the array. Whenever two consecutive items are found that are out of order, swap them. Repeat until all consecutive items are in order. Q-What is the worst case running time for the Quick sort? What simple change is required in the algorithm to preserve its linear expected running time and makes it worst case time.(n log n). Answer Worst case running time is O (n). The simple change which can change the running time of the edit distance Algorithm is the number of entries n2. Q: In edit distance what is the simple change that can change the worst case time? Ans: The simple change which can change the running time of edit distance algorithm is the number of entries n2,or you can say the term n. In other words you can say the length of the strings determine the running time of the computation. Q:Define the worst case of edit distance algorithm? Answer: The worst case running time of edit distance DP algorithm is O (n2), which is the number of entries in the table n2 as each entry need constant time to compute. Q:Average-case Analysis of Quicksort Answer: Average case analysis of Quick sort: The running time of Quick sort depends on the selection of pivot q which is done randomly. For average case analysis of quick sort, average is computed over all possible random choices of the pivot index q. The average case running time for quick sort is. (N log n). Q: Worst case Analysis of Quick sort? Answer: Worst case analysis of Quick sort: For worst case we maximize over all possible values of q, means the selection of pivot q which gives the maximum (worst) time for sorting. The chances are that the pivot values q=1 (start) or n (end) or n/2 (middle) happens to give maximum time values. The worst case running time is O (n2). Q: What is radix sort and explain it with examples. Answer: Radix sort: In linear time sorting, counting sort is used for the numbers in the range 1 to k where k is small and it is based on determining the rank of each number in final sorted array. But it is useful only for small integers i.e., 1...k where k is small. But if k were very large, then the size of the rank array formed would also be very large which is not efficient. So solution for such cases is the Radix sort which works by sorting one digit at a time. Example: [1] 8[4]1 [1] [3] 3[7]3 [3] [5] 1[8]5 [8]41 Q: Essential constraint for the counting sort. Answer: Essential constraint for the counting sort is that numbers to be sorted must be small integers i.e., 1...k where k is small. Q: Better appropriate to cope with chain matrix multiplication.

14 Answer: Better appropriate to cope with chain matrix multiplication is to do its Dynamic Programming formulation. It is breaking up the problem into sub problems, solving and string and then combining solutions to those sub problems to solve the global problem. Q: Heapify procedure and Build Heap explain in simple words with example. Answer: If an element in the Heap is not at its proper place means it is violating the Heap Order, the Heapify procedure is used to fix it and place it at its proper position. In Heapify, we recursively swap the element with its larger one child and stop at a stage when this element is larger than both of its children or it becomes the leaf node. Build Heap procedure is used for building Heap from any list and it is done by applying the Heapify procedure on each element starting from bottom and going upward to the root. Starting is done at second last level as the leaf nodes have no children so already in heap order. Q: Difference between worst case \Average cases Analysis of Quicksort explain in simple words with example. Answer: The running time of Quick sort depends on the selection of pivot q which is done randomly. For worst case we maximize over all possible values of q, means the selection of pivot q which gives the maximum (worst) time for Sorting. The chances are that the pivot values q=1 (start) or n (end) or n/2 (middle) happen to give maximum time values. The worst case running time is O (n2). For average case analysis of quick sort, the average is computed over all possible random choices of the pivot index q. The average case running time for quick sort is. (n log n). Q: Edit Distance in Speech Recognition. Answer: Algorithm is similar to those for the edit-distance problem is used in some speech recognition systems. Find a close match between a new utterance and one in library of classified utterance. Q: What is sorting? Describe slow running sorting algorithms. 5 marks Answer: Sorting is the process of arranging items in sequence. Slow running sorting algorithms: [PAGE 39] There are a number of well-known slow O(n2) sorting algorithms. 1. Bubble sort: Scan the array. Whenever two consective item are found that are out of order, swap them. Repeat until all consecutive items are in order. 2. Insertion sort: Assume that A[1..i-1] have already been sorted. Insert A[i] into its proper position in this sub array. Create this position by shifting all larger elements to the right. 3. Selection sort: Assume that A[1.i-1] contain the i-1 smallest element in A[i.n] swap it with A[i]. Q: What is Catalan Numbers and write their formula. 3 marks Answer: The Catalan numbers are famous functions in combinatrics. Formula: 14 Slow Bubble sort: Insertion sort Selection sort: O(n2) Fast Quicksort Merge sort Heap sort O(n log n) Fastest Count Radix Bucket Ω(n log n) Lower Bounds for Sorting: The best we have seen so far is O(n log n) algorithms for sorting. Is it possible to do better than O(n log n)? If a sorting algorithm is solely based on

15 comparison of keys in the array then it is impossible to sort more efficiently than (n log n) time. All algorithms we have seen so far are comparison-based sorting algorithms. Consider sorting three numbers a1, a2, and a3. There are 3! = 6 possible combinations: (a1, a2, a3), (a1, a3, a2), (a3, a2, a1) (a3, a1, a2), (a2, a1, a3), (a2, a3, a1) One of these permutations leads to the numbers in sorted order. The comparison based algorithm defines a decision tree. Here is the tree for the three numbers. Q: Following is the cost table for chain matrix multiplication problem with initial state. Mark the cells with question mark? which are updated after first iteration Ans: m[1, 2] = m[1, 1] +m[2, 2] + p0 p1 p2 = = 120 m[2, 3] = m[2, 2] +m[3, 3] + p1 p2 p3 = = 48 m[3, 4] = m[3, 3] +m[4, 4] + p2 p3 p4 = = 84 m[4, 5] = m[4, 4] +m[5, 5] + p3 p4 p5 = = 42 Q: Why do we analyze the average case performance of a randomized algorithm and not its worst case performance? Average case performance :The analysis of the algorithm s performance is the same for all inputs.in this case the average is computed over all possible random choices that the algorithm might make for the choice of the pivot index in the second step of the QuickSort procedure. worst case performance is a recursive program, it is natural to use a recurrence to describe its running time. But unlike MergeSort, where we had control over the sizes of the recursive calls, here we do not. It depends on how the pivot is chosen. Suppose that we are sorting an array of size n, A[1 : n], and further suppose that the pivot that we select is of rank q, for some q in the range 1 to n. It takes _(n) time to do the partitioning and other overhead, and we make two recursive calls. The first is to the subarray A[1 : q 1] which has q 1 elements, and the other is to the subarray A[q + 1 : n] which has n q elements. So if we ignore the _(n) (as usual) we get the recurrence: T(n) = T(q 1) + T(n q) + n Q: Suggest and describe modifications of the implementation of QuickSort that will improve its performance. Ans:The running time of quicksort depends heavily on the selection of the pivot. If the rank of the pivot is very large or very small then the partition (BST) will be unbalanced. Since the pivot is chosen randomly in our algorithm, the expected running time is O(n log n). The worst case time, however, is O(n2). Luckily, this happens rarely. 15

16 16 Algorithm: Informal Definition: An algorithm is any well-defined computational procedure that takes some values, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into output. Algorithms, Programming: A good understanding of algorithms is essential for a good understanding of the most basic element of computer science: programming. Unlike a program, an algorithm is a mathematical entity, which is independent of a specific programming language, machine, or compiler. A RAM is an idealized machine with an infinitely large random-access memory. Instructions are executed one-by-one (there is no parallelism). 2-dimension maxima: Let us do an example that illustrates how we analyze algorithms. Suppose you want to buy a car. You want the pick the fastest car. But fast cars are expensive; you want the cheapest. You cannot decide which is more important: speed or price. Definitely do not want a car if there is another that is both faster and cheaper. We say that the fast, cheap car dominates the slow, expensive car relative to your selection criteria. So, given a collection of cars, we want to list those cars that are not dominated by any other. Here is how we might model this as a formal problem. Let a point p in 2-dimensional space be given by its integer coordinates, p = (p.x, p.y). A point p is said to be dominated by point q if p.x _ q.x and p.y _ q.y. Given a set of n points, P = {p1, p2,..., pn} in 2-space a point is said to be maximal if it is not dominated by any other point in P. Running Time Analysis: The main purpose of our mathematical analysis will be measuring the execution time. We will also be concerned about the space (memory) required by the algorithm. The running time of an implementation of the algorithm would depend upon the speed of the computer, programming language, optimization by the compiler etc. Although important, we will ignore these technological issues in our analysis. To measure the running time of the brute-force 2-d maxima algorithm, we could count the number of steps of the pseudo code that are executed or, count the number of times an element of P is accessed or, the number of comparisons that are performed. Worst-case time is the maximum running time over all (legal) inputs of size n. Let I denote an input instance, let I denote its length, and let T(I) denote the running time of the algorithm on input I. Then Tworst(n) = max I =n T(I)

17 17 CS502 Design & Analysis of Algorithms Average-case time is the average running time over all inputs of size n. Let p(i) denote the probability of seeing this input. The average-case time is the weighted sum of running times with weights being the probabilities: Tavg(n) =X I =n p(i)t(i) We will almost always work with worst-case time. Average-case time is more difficult to compute; it is difficult to specify probability distribution on inputs. Worst-case time will specify an upper limit on the running time. Worst-case running time is _(n2). This is called the symptotic growth rate of the function. Sorting takes _(n log n); we will show this later when we discuss sorting. The for loop executes ntimes. The inner loop (seemingly) could be iterated (n 1) times. It seems we still have an n(n סּ 1) or ( n2 )סּ algorithm. Asymptotic Notation: You may be asking that we continue to use the notation _() but have never defined it. Let s remedy this now. Given any function g(n), we define _(g(n)) to be a set of functions that asymptotically equivalent to g(n). Formally: _(g(n)) = {f(n) there exist positive constants c1, c2 and n0 such that 0 _ c1g(n) _ f(n) _ c2g(n) for all n _ n0} Lower bound: f(n) grows asymptotically at least as fast as n2. For this, need to show that there exist positive constants c1 and n0, such that f(n) _ c1n2 for all n _ n0. Consider the reasoning f(n) = 8n2 + 2n 3 _ 8n2 3 = 7n2 + (n2 3) _ 7n2 Thus c1 = 7. We implicitly assumed that 2n _ 0 and n2 3 _ 0 These are not true for all n but if n _ p3, then both are true. So select n0 _ p3. We then have f(n) _ c1n2 for all n _ n0. Upper bound: f(n) grows asymptotically no faster than n2. For this, we need to show that there exist positive constants c2 and n0, such that f(n) _ c2n2 for all n _ n0. Consider the reasoning f(n) = 8n2 + 2n 3 _ 8n2 + 2n _ 8n2 + 2n2 = 10n2 The O-notation is used to state only the asymptotic upper bounds. The Ω-notation allows us to state only the asymptotic lower bounds. Divide: the problem into a small number of pieces Conquer: solve each piece by applying divide and conquer to it recursively Combine: the pieces together into a global solution. (Divide:) split A down the middle into two subsequences, each of size roughly n/2 (Conquer:) sort each subsequence by calling merge sort recursively on each. (Combine:) merge the two sorted subsequences into a single sorted list. Sieve Technique The reason for introducing this algorithm is that it illustrates a very important special case of divide-and-conquer, which I call the sieve technique. We think of divide-and-conquer as reaking the problem into a small number of smaller subproblems, which are then solved recursively. The sieve technique is a special case, where the number of subproblems is just 1. Selection Algorithm:It is easy to see that the rank of the The rank of the pivot x is q p + 1 in A[p..r]. Let rank x = q p + 1. If k = rank x then the pivot is kth smallest. If k < rank x then search A[p..q 1] recursively. If k > rank x then search A[q + 1..r] recursively. Find element of rank (k q) because we eliminated q smaller elements in A. A heap is a left-complete binary tree that conforms to the heap order. The heap order property: in a (min) heap, for every node X, the key in the parent is smaller than or equal to the key in X. In other words, the parent node has key smaller than or equal to both of its

18 children nodes. Similarly, in a max heap, the parent has a key larger than or equal both of its children Thus the smallest key is in the root in a min heap; in the max heap, the largest is in the root. An in-place sorting algorithm is one that uses no additional array for storage. A sorting algorithm is stable if duplicate elements remain in the same relative position after sorting. Theorem 1: Any comparison-based sorting algorithm has worst-case running time (n log n). Edit Distance The words computer and commuter are very similar, and a change of just one letter, p- m, will change the first word into the second. The word sport can be changed into sort by the deletion of the p, or equivalently, sort can be changed into sport by the insertion of p. The edit distance of two strings, s1 and s2, is defined as the minimum number of point mutations required to change s1 into s2, where a point mutation is one of: change a letter, insert a letter or delete a letter For example, the edit distance between FOOD and MONEY is at most four: FOOD! MOOD! MONfD! MONED! MONEY Constructing the Optimal Solution The algorithm for computing V[i, j] does not keep record of which subset of items gives the optimal solution. To compute the actual subset, we can add an auxiliary boolean array keep[i, j] which is 1 if we decide to take the ith item and 0 otherwise. We will use all the values keep[i, j] to determine the optimal subset T of items to put in the knapsack as follows: If keep[n,w] is 1, then n 2 T. We can now repeat this argument for keep[n 1,W wn]. If kee[n,w] is 0, the n 62 T and we repeat the argument for keep[n 1,W]. 18

CS502 Midterm Spring 2013 (26 May-05 June)

CS502 Midterm Spring 2013 (26 May-05 June) How would you modify QUICKSORT to sort into non-increasing order? Answer: - Click here for detail To make QUICKSORT sort in non-increasing order we must modify PARTITION. Q- We can avoid unnecessary repetitions

More information

Pseudo code of algorithms are to be read by.

Pseudo code of algorithms are to be read by. Cs502 Quiz No1 Complete Solved File Pseudo code of algorithms are to be read by. People RAM Computer Compiler Approach of solving geometric problems by sweeping a line across the plane is called sweep.

More information

MIDTERM EXAMINATION Fall 2011 CS502- Fundamentals of Algorithms

MIDTERM EXAMINATION Fall 2011 CS502- Fundamentals of Algorithms MIDTERM EXAMINATION Fall 2011 CS502- Fundamentals of Algorithms Question No: 1 ( Marks: 1 ) - Please choose one Due to left complete nature of binary tree, the heap can be stored in 1. Arrays (Page 40)

More information

Divide and Conquer Strategy. (Page#27)

Divide and Conquer Strategy. (Page#27) MUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala faisalgrw123@gmail.com Reference Short Questions for MID TERM EXAMS CS502 Design and Analysis of Algorithms Divide and Conquer Strategy

More information

MUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala

MUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala MUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala faisalgrw123@gmail.com Reference MCQ s For MIDTERM EXAMS CS502- Design and Analysis of Algorithms 1. For the sieve technique we solve

More information

Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem

Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem (Thursday, Jan 29, 1998) Read: Chapter 1 in CLR. Analyzing Algorithms: In order to design good algorithms, we must first agree the criteria for measuring

More information

1. For the sieve technique we solve the problem, recursively mathematically precisely accurately 2. We do sorting to, keep elements in random positions keep the algorithm run in linear order keep the algorithm

More information

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are

More information

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment Class: V - CE Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology Sub: Design and Analysis of Algorithms Analysis of Algorithm: Assignment

More information

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements. Sorting Sorting is ordering a list of elements. Types of sorting: There are many types of algorithms exist based on the following criteria: Based on Complexity Based on Memory usage (Internal & External

More information

O(n): printing a list of n items to the screen, looking at each item once.

O(n): printing a list of n items to the screen, looking at each item once. UNIT IV Sorting: O notation efficiency of sorting bubble sort quick sort selection sort heap sort insertion sort shell sort merge sort radix sort. O NOTATION BIG OH (O) NOTATION Big oh : the function f(n)=o(g(n))

More information

COMP Data Structures

COMP Data Structures COMP 2140 - Data Structures Shahin Kamali Topic 5 - Sorting University of Manitoba Based on notes by S. Durocher. COMP 2140 - Data Structures 1 / 55 Overview Review: Insertion Sort Merge Sort Quicksort

More information

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to

More information

Cs502 Solved MCQS MidTerm

Cs502 Solved MCQS MidTerm Cs502 Solved MCQS MidTerm Question # 1 Word Algorithm comes from the name of the Muslim author: Abu Ja far Mohammad ibn Musaal-Khowarizmi. (p7) May 2013 Question # 2 Al-Khwarizmi s work was written in

More information

Deliverables. Quick Sort. Randomized Quick Sort. Median Order statistics. Heap Sort. External Merge Sort

Deliverables. Quick Sort. Randomized Quick Sort. Median Order statistics. Heap Sort. External Merge Sort More Sorting Deliverables Quick Sort Randomized Quick Sort Median Order statistics Heap Sort External Merge Sort Copyright @ gdeepak.com 2 Quick Sort Divide and conquer algorithm which relies on a partition

More information

EECS 2011M: Fundamentals of Data Structures

EECS 2011M: Fundamentals of Data Structures M: Fundamentals of Data Structures Instructor: Suprakash Datta Office : LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle Note: Some slides in this lecture are adopted from James

More information

Lecture 2: Getting Started

Lecture 2: Getting Started Lecture 2: Getting Started Insertion Sort Our first algorithm is Insertion Sort Solves the sorting problem Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,...,

More information

Lecture 5: Sorting Part A

Lecture 5: Sorting Part A Lecture 5: Sorting Part A Heapsort Running time O(n lg n), like merge sort Sorts in place (as insertion sort), only constant number of array elements are stored outside the input array at any time Combines

More information

Sorting and Selection

Sorting and Selection Sorting and Selection Introduction Divide and Conquer Merge-Sort Quick-Sort Radix-Sort Bucket-Sort 10-1 Introduction Assuming we have a sequence S storing a list of keyelement entries. The key of the element

More information

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides Jana Kosecka Linear Time Sorting, Median, Order Statistics Many slides here are based on E. Demaine, D. Luebke slides Insertion sort: Easy to code Fast on small inputs (less than ~50 elements) Fast on

More information

The divide and conquer strategy has three basic parts. For a given problem of size n,

The divide and conquer strategy has three basic parts. For a given problem of size n, 1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often

More information

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place? Sorting Binary search works great, but how do we create a sorted array in the first place? Sorting in Arrays Sorting algorithms: Selection sort: O(n 2 ) time Merge sort: O(nlog 2 (n)) time Quicksort: O(n

More information

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

The Limits of Sorting Divide-and-Conquer Comparison Sorts II The Limits of Sorting Divide-and-Conquer Comparison Sorts II CS 311 Data Structures and Algorithms Lecture Slides Monday, October 12, 2009 Glenn G. Chappell Department of Computer Science University of

More information

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1 DIVIDE & CONQUER Definition: Divide & conquer is a general algorithm design strategy with a general plan as follows: 1. DIVIDE: A problem s instance is divided into several smaller instances of the same

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms CSE 5311 Lecture 8 Sorting in Linear Time Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1 Sorting So Far

More information

Module 2: Classical Algorithm Design Techniques

Module 2: Classical Algorithm Design Techniques Module 2: Classical Algorithm Design Techniques Dr. Natarajan Meghanathan Associate Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Module

More information

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6 6.0 Introduction Sorting algorithms used in computer science are often classified by: Computational complexity (worst, average and best behavior) of element

More information

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK Page 1 UNIT I INTRODUCTION 2 marks 1. Why is the need of studying algorithms? From a practical standpoint, a standard set of algorithms from different

More information

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

Sorting. There exist sorting algorithms which have shown to be more efficient in practice. Sorting Next to storing and retrieving data, sorting of data is one of the more common algorithmic tasks, with many different ways to perform it. Whenever we perform a web search and/or view statistics

More information

Midterm solutions. n f 3 (n) = 3

Midterm solutions. n f 3 (n) = 3 Introduction to Computer Science 1, SE361 DGIST April 20, 2016 Professors Min-Soo Kim and Taesup Moon Midterm solutions Midterm solutions The midterm is a 1.5 hour exam (4:30pm 6:00pm). This is a closed

More information

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist. Sorting Popular algorithms: Selection sort* Insertion sort* Bubble sort* Quick sort* Comb-sort Shell-sort Heap sort* Merge sort* Counting-sort Radix-sort Bucket-sort Tim-sort Many algorithms for sorting

More information

Selection (deterministic & randomized): finding the median in linear time

Selection (deterministic & randomized): finding the median in linear time Lecture 4 Selection (deterministic & randomized): finding the median in linear time 4.1 Overview Given an unsorted array, how quickly can one find the median element? Can one do it more quickly than bysorting?

More information

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms Analysis of Algorithms Unit 4 - Analysis of well known Algorithms 1 Analysis of well known Algorithms Brute Force Algorithms Greedy Algorithms Divide and Conquer Algorithms Decrease and Conquer Algorithms

More information

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer Computer Science 385 Analysis of Algorithms Siena College Spring 2011 Topic Notes: Divide and Conquer Divide and-conquer is a very common and very powerful algorithm design technique. The general idea:

More information

SORTING AND SELECTION

SORTING AND SELECTION 2 < > 1 4 8 6 = 9 CHAPTER 12 SORTING AND SELECTION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016)

More information

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq

More information

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation Comp2711 S1 2006 Correctness Oheads 1 Efficiency Issues Comp2711 S1 2006 Correctness Oheads 2 Run Times An implementation may be correct with respect to the Specification Pre- and Post-condition, but nevertheless

More information

Principles of Algorithm Design

Principles of Algorithm Design Principles of Algorithm Design When you are trying to design an algorithm or a data structure, it s often hard to see how to accomplish the task. The following techniques can often be useful: 1. Experiment

More information

Lecture Notes on Quicksort

Lecture Notes on Quicksort Lecture Notes on Quicksort 15-122: Principles of Imperative Computation Frank Pfenning Lecture 8 February 5, 2015 1 Introduction In this lecture we consider two related algorithms for sorting that achieve

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 12: Sorting Algorithms MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Outline 2 Last week Implementation of the three tree depth-traversal algorithms Implementation of the BinarySearchTree

More information

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 2. O(n) 2. [1 pt] What is the solution to the recurrence T(n) = T(n/2) + n, T(1)

More information

Basic Data Structures (Version 7) Name:

Basic Data Structures (Version 7) Name: Prerequisite Concepts for Analysis of Algorithms Basic Data Structures (Version 7) Name: Email: Concept: mathematics notation 1. log 2 n is: Code: 21481 (A) o(log 10 n) (B) ω(log 10 n) (C) Θ(log 10 n)

More information

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1 CS241 - week 1 review Special classes of algorithms: logarithmic: O(log n) linear: O(n) quadratic: O(n 2 ) polynomial: O(n k ), k 1 exponential: O(a n ), a > 1 Classifying algorithms is generally done

More information

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014 CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting Aaron Bauer Winter 2014 The main problem, stated carefully For now, assume we have n comparable elements in an array and we want

More information

CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION DESIGN AND ANALYSIS OF ALGORITHMS Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION http://milanvachhani.blogspot.in EXAMPLES FROM THE SORTING WORLD Sorting provides a good set of examples for analyzing

More information

4. Sorting and Order-Statistics

4. Sorting and Order-Statistics 4. Sorting and Order-Statistics 4. Sorting and Order-Statistics The sorting problem consists in the following : Input : a sequence of n elements (a 1, a 2,..., a n ). Output : a permutation (a 1, a 2,...,

More information

Classic Data Structures Introduction UNIT I

Classic Data Structures Introduction UNIT I ALGORITHM SPECIFICATION An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. All algorithms must satisfy the following criteria: Input. An algorithm has zero

More information

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information

( D. Θ n. ( ) f n ( ) D. Ο%

( D. Θ n. ( ) f n ( ) D. Ο% CSE 0 Name Test Spring 0 Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to run the code below is in: for i=n; i>=; i--) for j=; j

More information

( ) n 3. n 2 ( ) D. Ο

( ) n 3. n 2 ( ) D. Ο CSE 0 Name Test Summer 0 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n n matrices is: A. Θ( n) B. Θ( max( m,n, p) ) C.

More information

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9) CPSC 311 Lecture Notes Sorting and Order Statistics (Chapters 6-9) Acknowledgement: These notes are compiled by Nancy Amato at Texas A&M University. Parts of these course notes are based on notes from

More information

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs Algorithms in Systems Engineering IE172 Midterm Review Dr. Ted Ralphs IE172 Midterm Review 1 Textbook Sections Covered on Midterm Chapters 1-5 IE172 Review: Algorithms and Programming 2 Introduction to

More information

Algorithm Design Techniques part I

Algorithm Design Techniques part I Algorithm Design Techniques part I Divide-and-Conquer. Dynamic Programming DSA - lecture 8 - T.U.Cluj-Napoca - M. Joldos 1 Some Algorithm Design Techniques Top-Down Algorithms: Divide-and-Conquer Bottom-Up

More information

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn) CSE 0 Name Test Fall 0 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to find the maximum of the n elements of an integer array is in: A.

More information

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Algorithm Efficiency & Sorting Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Overview Writing programs to solve problem consists of a large number of decisions how to represent

More information

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems. 2.3 Designing algorithms There are many ways to design algorithms. Insertion sort uses an incremental approach: having sorted the subarray A[1 j - 1], we insert the single element A[j] into its proper

More information

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting) IS 709/809: Computational Methods in IS Research Algorithm Analysis (Sorting) Nirmalya Roy Department of Information Systems University of Maryland Baltimore County www.umbc.edu Sorting Problem Given an

More information

CSci 231 Final Review

CSci 231 Final Review CSci 231 Final Review Here is a list of topics for the final. Generally you are responsible for anything discussed in class (except topics that appear italicized), and anything appearing on the homeworks.

More information

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort Principles of Imperative Computation V. Adamchik CS 15-1 Lecture Carnegie Mellon University Sorting Sorting Sorting is ordering a list of objects. comparison non-comparison Hoare Knuth Bubble (Shell, Gnome)

More information

Lecture Notes on Quicksort

Lecture Notes on Quicksort Lecture Notes on Quicksort 15-122: Principles of Imperative Computation Frank Pfenning Lecture 8 September 20, 2012 1 Introduction In this lecture we first sketch two related algorithms for sorting that

More information

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48 Algorithm Analysis (Algorithm Analysis ) Data Structures and Programming Spring 2018 1 / 48 What is an Algorithm? An algorithm is a clearly specified set of instructions to be followed to solve a problem

More information

Chapter 7 Sorting. Terminology. Selection Sort

Chapter 7 Sorting. Terminology. Selection Sort Chapter 7 Sorting Terminology Internal done totally in main memory. External uses auxiliary storage (disk). Stable retains original order if keys are the same. Oblivious performs the same amount of work

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 5.1 Introduction You should all know a few ways of sorting in O(n log n)

More information

Chapter 3 Dynamic programming

Chapter 3 Dynamic programming Chapter 3 Dynamic programming 1 Dynamic programming also solve a problem by combining the solutions to subproblems. But dynamic programming considers the situation that some subproblems will be called

More information

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning Lecture 7 Quicksort 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In this lecture we consider two related algorithms for sorting that achieve a much better running time than

More information

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS CHAPTER 11 SORTING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO AND

More information

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n) CSE 0 Test Your name as it appears on your UTA ID Card Fall 0 Multiple Choice:. Write the letter of your answer on the line ) to the LEFT of each problem.. CIRCLED ANSWERS DO NOT COUNT.. points each. The

More information

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS Lecture 03-04 PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS By: Dr. Zahoor Jan 1 ALGORITHM DEFINITION A finite set of statements that guarantees an optimal solution in finite interval of time 2 GOOD ALGORITHMS?

More information

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints. 02/10/2006 01:42 AM Class 7 From Wiki6962 Table of contents 1 Basic definitions 2 Bubble Sort 2.1 Observations 3 Quick Sort 3.1 The Partition Algorithm 3.2 Duplicate Keys 3.3 The Pivot element 3.4 Size

More information

CS240 Fall Mike Lam, Professor. Quick Sort

CS240 Fall Mike Lam, Professor. Quick Sort ??!!!!! CS240 Fall 2015 Mike Lam, Professor Quick Sort Merge Sort Merge sort Sort sublists (divide & conquer) Merge sorted sublists (combine) All the "hard work" is done after recursing Hard to do "in-place"

More information

Lecture: Analysis of Algorithms (CS )

Lecture: Analysis of Algorithms (CS ) Lecture: Analysis of Algorithms (CS583-002) Amarda Shehu Fall 2017 Amarda Shehu Lecture: Analysis of Algorithms (CS583-002) Sorting in O(n lg n) Time: Heapsort 1 Outline of Today s Class Sorting in O(n

More information

( ) 1 B. 1. Suppose f x

( ) 1 B. 1. Suppose f x CSE Name Test Spring Last Digits of Student ID Multiple Choice. Write your answer to the LEFT of each problem. points each is a monotonically increasing function. Which of the following approximates the

More information

n 2 ( ) ( ) + n is in Θ n logn

n 2 ( ) ( ) + n is in Θ n logn CSE Test Spring Name Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply an m n matrix and a n p matrix is in: A. Θ( n) B. Θ( max(

More information

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1 Sorting Riley Porter 1 Introduction to Sorting Why study sorting? Good algorithm practice! Different sorting algorithms have different trade-offs No single best sort for all scenarios Knowing one way to

More information

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1 CSE 0 Name Test Fall 00 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each t. What is the value of k? k=0 A. k B. t C. t+ D. t+ +. Suppose that you have

More information

SORTING, SETS, AND SELECTION

SORTING, SETS, AND SELECTION CHAPTER 11 SORTING, SETS, AND SELECTION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING ASSORTED MINUTIAE HW6 Out Due next Wednesday ASSORTED MINUTIAE HW6 Out Due next Wednesday Only two late days allowed ASSORTED MINUTIAE HW6 Out Due

More information

CS 303 Design and Analysis of Algorithms

CS 303 Design and Analysis of Algorithms Mid-term CS 303 Design and Analysis of Algorithms Review For Midterm Dong Xu (Based on class note of David Luebke) 12:55-1:55pm, Friday, March 19 Close book Bring your calculator 30% of your final score

More information

Merge Sort

Merge Sort Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Divide-and-Conuer Divide-and conuer is a general algorithm design paradigm: n Divide: divide the input data S in two disjoint subsets S 1 and

More information

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT. Week 10 1 2 3 4 5 6 Sorting 7 8 General remarks We return to sorting, considering and. Reading from CLRS for week 7 1 Chapter 6, Sections 6.1-6.5. 2 Chapter 7, Sections 7.1, 7.2. Discover the properties

More information

CPSC 320 Midterm 2 Thursday March 13th, 2014

CPSC 320 Midterm 2 Thursday March 13th, 2014 CPSC 320 Midterm 2 Thursday March 13th, 2014 [12] 1. Answer each question with True or False, and then justify your answer briefly. [2] (a) The Master theorem can be applied to the recurrence relation

More information

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Comparison Sorts. Chapter 9.4, 12.1, 12.2 Comparison Sorts Chapter 9.4, 12.1, 12.2 Sorting We have seen the advantage of sorted data representations for a number of applications Sparse vectors Maps Dictionaries Here we consider the problem of

More information

Sorting & Growth of Functions

Sorting & Growth of Functions Sorting & Growth of Functions CSci 588: Data Structures, Algorithms and Software Design Introduction to Algorithms, Cormen et al., Chapter 3 All material not from online sources or text copyright Travis

More information

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Quicksort. Repeat the process recursively for the left- and rightsub-blocks. Quicksort As the name implies, this is the fastest known sorting algorithm in practice. It is excellent for average input but bad for the worst-case input. (you will see later). Basic idea: (another divide-and-conquer

More information

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22 CS 234 Module 8 November 15, 2018 CS 234 Module 8 ADT Priority Queue 1 / 22 ADT Priority Queue Data: (key, element pairs) where keys are orderable but not necessarily distinct, and elements are any data.

More information

Multiple-choice (35 pt.)

Multiple-choice (35 pt.) CS 161 Practice Midterm I Summer 2018 Released: 7/21/18 Multiple-choice (35 pt.) 1. (2 pt.) Which of the following asymptotic bounds describe the function f(n) = n 3? The bounds do not necessarily need

More information

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once. Chapter 8. Sorting in Linear Time Types of Sort Algorithms The only operation that may be used to gain order information about a sequence is comparison of pairs of elements. Quick Sort -- comparison-based

More information

CPSC W2 Midterm #2 Sample Solutions

CPSC W2 Midterm #2 Sample Solutions CPSC 320 2014W2 Midterm #2 Sample Solutions March 13, 2015 1 Canopticon [8 marks] Classify each of the following recurrences (assumed to have base cases of T (1) = T (0) = 1) into one of the three cases

More information

L.J. Institute of Engineering & Technology Semester: VIII (2016)

L.J. Institute of Engineering & Technology Semester: VIII (2016) Subject Name: Design & Analysis of Algorithm Subject Code:1810 Faculties: Mitesh Thakkar Sr. UNIT-1 Basics of Algorithms and Mathematics No 1 What is an algorithm? What do you mean by correct algorithm?

More information

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs Algorithms in Systems Engineering ISE 172 Lecture 12 Dr. Ted Ralphs ISE 172 Lecture 12 1 References for Today s Lecture Required reading Chapter 6 References CLRS Chapter 7 D.E. Knuth, The Art of Computer

More information

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 1. O(logn) 2. O(n) 3. O(nlogn) 4. O(n 2 ) 5. O(2 n ) 2. [1 pt] What is the solution

More information

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

( ) + n. ( ) = n 1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1 CSE 0 Name Test Summer 00 Last Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. Suppose you are sorting millions of keys that consist of three decimal

More information

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES DESIGN AND ANALYSIS OF ALGORITHMS Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES http://milanvachhani.blogspot.in USE OF LOOPS As we break down algorithm into sub-algorithms, sooner or later we shall

More information

UNIT 1 BASICS OF AN ALGORITHM

UNIT 1 BASICS OF AN ALGORITHM UNIT 1 BASICS OF AN ALGORITHM Basics of an Algorithm Structure Page Nos. 1.0 Introduction 5 1.1 Objectives 6 1.2. Analysis and Complexity of Algorithms 6 1.3 Basic Technique for Design of Efficient Algorithms

More information

Data Structures and Algorithms Key to Homework 1

Data Structures and Algorithms Key to Homework 1 Data Structures and Algorithms Key to Homework 1 January 31, 2005 15 Define an ADT for a set of integers (remember that a set may not contain duplicates) Your ADT should consist of the functions that can

More information

CS1 Lecture 30 Apr. 2, 2018

CS1 Lecture 30 Apr. 2, 2018 CS1 Lecture 30 Apr. 2, 2018 HW 7 available very different than others you need to produce a written document based on experiments comparing sorting methods If you are not using a Python (like Anaconda)

More information

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case: CS245-2015S-11 Sorting in Θ(nlgn) 1 11-0: Merge Sort Recursive Sorting Base Case: A list of length 1 or length 0 is already sorted Recursive Case: Split the list in half Recursively sort two halves Merge

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information