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

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Lecture 4 Divide and Conquer for Nearest Neighbor Problem
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Spring 2015 Lecture 5: QuickSort & Selection
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
COMP 171 Data Structures and Algorithms Tutorial 4 In-Class Exercises: Algorithm Design.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
CS421 - Course Information Website Syllabus Schedule The Book:
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Merge sort, Insertion sort
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Introduction CIS 606 Spring The sorting problem Input: A sequence of n numbers 〈 a 1, a 2, …, a n 〉. Output: A permutation (reordering) 〈 a’ 1,
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
Algorithm Correctness A correct algorithm is one in which every valid input instance produces the correct output. The correctness must be proved mathematically.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Analysis of Algorithms
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture three Dr. Hamdy M. Mousa.
Getting Started Introduction to Algorithms Jeff Chastine.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Foundations II: Data Structures and Algorithms
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 3. Introduction to the Analysis of Algorithms.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Ch 7: Quicksort Ming-Te Chi
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Divide and Conquer (Merge Sort)
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Recurrences.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Presentation transcript:

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

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

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?

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: MERGE- SORT 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. How is the stack used? 2. For a MIPS assembly language implementation of this routine see factorialRecursive.asm in -> 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 ?

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

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 ?

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?

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

Problems Ex 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 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 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 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.)