Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro. To Algorithms” book website (copyright McGraw Hill) adapted and supplemented CLRS “Intro. To Algorithms” Ch. 2: Getting Started Why? tj is the number of times the while loop test in line 5 is executed for that value of j. T(n) = c1n + c2(n-1) + c4(n-1) + c5 ∑j=2..n tj + c6 ∑j=2..n (tj-1) + c7 ∑j=2..n (tj-1) + c8(n-1) Ques: What are the best and worst-case running times of INSERTION-SORT? How about average-case? MERGE MERGE(A, p, q, r) merges subarrays A[p..q] and A[q+1..r], assuming that they are each already sorted. MERGE() requires extra space – arrays L and R – of the size of the input + 2. Ques: What is the time complexity of MERGE? (Linear) Ques: Could the merging be done in-place ? I.e., just using the input space (plus maybe a constant amount of extra space). E.g., like INSERTION-SORT. What is the run time of your algorithm? Ques: Ok, how about in-place and in linear time?! Divide-and-Conquer: MERGESORT Label1: Labe12: Label3: Calculating the Factorial: Use of the Stack in Recursion Consider computing the factorial recursively: int Fact(int n) { if (n < 1) return 1; Label1: else return n*Fact(n-1); } 1. 2. 3. 4. 5. How is the stack used? For a MIPS assembly language implementation of this routine see factorialRecursive.asm in http://www.cs.ait.ac.th/~guha/COA/Spim/ -> Examples Very useful !! Shows how recursion is actually implemented during run-time! What is the run time of Fact()? Can recursion be avoided? Replaced with iteration ? What’s the advantage/disadvantage? How is the stack used in MERGE-SORT ? The recurrence for the worst-case running time T(n) of MERGE-SORT: T(n) = (1) 2T(n/2) + (n) if n = 1 if n > 1 equivalently T(n) = c1 2T(n/2) + c2n if n = 1 if n > 1 Ques: Solve this recurrence by (1) using the recursion tree of Fig. 2.5 (2) iteratively expansion 1. 2. 3. Run BUBBLESORT on sample input. Is the algorithm correct? Why? Explain by saying what it accomplishes after each iteration of the for loop? What is the worst-case running time of BUBBLESORT? Horner’s Rule P(x) = ∑ akxk = a0 + x(a1 + x(a2 + … + x(an-1 + xan) … )) y ← 0 i ← n while i ≥ 0 do y ← ai + x*y i ← i-1 1. What is the running time of Horner’s rule for evaluating a polynomial? 2. How does it compare with the naïve method that computes each term separately from scratch and adds? Data Structures & Algorithms are Technology (not just Theory!) Why?! They are implemented in applications around us everywhere. Examples: … Problems Ex. 2.3-3 Use mathematical induction to show that when n is an exact power of 2, the solution of the recurrence T (n) = 2 if n = 2 , 2T (n/2) + n if n = 2k, for k > 1 is T (n) = n lg n. Ex. 2.3-4 Insertion sort can be expressed as a recursive procedure as follows. In order to sort A[1 . . n], we recursively sort A[1 . . n−1] and then insert A[n] into the sorted array A[1 . . n − 1]. Write a recurrence for the running time of this recursive version of insertion sort. Ex. 2.3-7 Describe a θ(n lg n)-time algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. Problems Prob. 2-4 Inversions Let A[1 . . n] be an array of n distinct numbers. If i < j and A[i ] > A[ j ], then the pair (i, j ) is called an inversion of A. a. List the five inversions of the array 2, 3, 8, 6, 1. b. What array with elements from the set {1, 2, . . . , n} has the most inversions? How many does it have? c. What is the relationship between the running time of insertion sort and the number of inversions in the input array? Justify your answer. d. Give an algorithm that determines the number of inversions in any permutation on n elements in (n lg n) worst-case time. (Hint: Modify merge sort.)