Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.

Similar presentations


Presentation on theme: "Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro."— Presentation transcript:

1 Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro. To Algorithms” book website (copyright McGraw Hill) adapted and supplemented

2 CLRS “Intro. To Algorithms” Ch. 2: Getting Started

3

4

5 Why? t j is the number of times the while loop test in line 5 is executed for that value of j. T(n) = c 1 n + c 2 (n-1) + c 4 (n-1) + c 5 ∑ j=2..n t j + c 6 ∑ j=2..n (t j -1) + c 7 ∑ j=2..n (t j -1) + c 8 (n-1) Ques: What are the best and worst-case running times of INSERTION-SORT ? How about average-case?

6 MERGE MERGE (A, p, q, r) merges subarrays A[p..q] and A[q+1..r], assuming that they are each already sorted.

7 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?!

8 Divide-and-Conquer: MERGE- SORT Label1: Labe12: Label3:

9

10 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. How is the stack used? 2. 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! 3. What is the run time of Fact()? 4. Can recursion be avoided? Replaced with iteration ? What’s the advantage/disadvantage? 5. How is the stack used in MERGE-SORT ?

11 The recurrence for the worst-case running time T(n) of MERGE-SORT : T(n) =  (1) if n = 1 2T(n/2) +  (n) if n > 1 equivalently T(n) = c 1 if n = 1 2T(n/2) + c 2 n if n > 1 Ques: Solve this recurrence by (1) using the recursion tree of Fig. 2.5 (2) iteratively expansion

12 1.Run BUBBLESORT on sample input. 2.Is the algorithm correct? Why? Explain by saying what it accomplishes after each iteration of the for loop? 3.What is the worst-case running time of BUBBLESORT ?

13 Horner’s Rule P(x) = ∑ a k x k = a 0 + x(a 1 + x(a 2 + … + x(a n-1 + xa n ) … )) y ← 0 i ← n while i ≥ 0 do y ← a i + 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?

14 Data Structures & Algorithms are Technology (not just Theory!) Why?! They are implemented in applications around us everywhere. Examples: …

15 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.

16 Problems Prob. 2-4 Inversions Let A[1.. n] be an array of n distinct numbers. If 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.)


Download ppt "Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro."

Similar presentations


Ads by Google