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
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Advertisements

Sorting in Linear Time Comp 550, Spring Linear-time Sorting Depends on a key assumption: numbers to be sorted are integers in {0, 1, 2, …, k}. Input:
MS 101: Algorithms Instructor Neelima Gupta
1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
Lower bound for sorting, radix sort COMP171 Fall 2005.
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.
Lower bound for sorting, radix sort COMP171 Fall 2006.
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Comp 122, Spring 2004 Keys into Buckets: Lower bounds, Linear-time sort, & Hashing.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
Lecture 5: Master Theorem and Linear Time Sorting
8.Sorting in linear time Hsu, Lih-Hsing. Computer Theory Lab. Chapter 8P Lower bound for sorting The decision tree model.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Analysis of Algorithms CS 477/677
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time sorting.
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
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.
Sorting Fun1 Chapter 4: Sorting     29  9.
Analysis of Algorithms CS 477/677
Fall 2015 Lecture 4: Sorting in linear time
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
1 Algorithms CSCI 235, Fall 2015 Lecture 17 Linear Sorting.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
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.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Lower Bounds & Sorting in Linear Time
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
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
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
Ch8: Sorting in Linear Time 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
Lower Bounds & Sorting in Linear Time
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Lecture 5 Algorithm Analysis
Lower bound for sorting, radix sort
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to.
CS 583 Analysis of Algorithms
The Selection Problem.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Sorting We have actually seen already two efficient ways to sort:
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. 8: Sorting in Linear Time

Theorem 8.1: Any comparison sort algorithm requires  (n log n) comparisons in the worst case. Proof: The decision tree must have n! leaves, one corresponding to each possible output. Moreover, the branching-factor of the tree is two, i.e., the tree is binary, because each comparison gives one of two results. The height of the decision tree is the number of comparisons made in the worst case. Therefore, the minimum possible height of a binary tree with n! leaves is the number of comparisons in the worst case. Min. possible ht. = log (n!) =  (n logn) (using Stirling’s approximation).

COUNTING-SORT assumes that each of the input elements is an integer in the range 0 to k, inclusive. A [1..n] is the input array, B [1..n] is the output array, C [0..k] is a temporary working array.

The running time of COUNTING-SORT is  (k+n) (why?), which is  (n) if k ≤ n. Intuitively, explain why COUNTING-SORT can beat the lower bound of  (n logn) for comparison sorts. Doesn’t it do comparisons as well…!?

A stable sort is one where items with the same value appear in the same order in the output as they appeared in the input. Lemma 8.3: Given n d-digit numbers in which each digit can take up to k possible values, RADIX-SORT correctly sorts these numbers in  (d (n +k)) time. Proof: COUNTING-SORT on each column * no. of columns Is COUNTING-SORT stable?!!

BUCKET-SORT assumes each input element A [i ] lies in 0 ≤ A [i ] < 1 (i.e., normalized). A [1..n] is the input array, B [0..n-1] is an auxiliary array of linked lists (=buckets).

Analysis of BUCKET-SORT The running time is T(n) =  (n) + ∑ i=0..n-1 O(n i 2 ) where n i is the random variable representing the number of elements in bucket B[i], because insertion sort is quadratic-time. Therefore, E[T(n)] = E[  (n) + ∑ i=0..n-1 O(n i 2 ) ] =  (n) + ∑ i=0..n-1 E[O(n i 2 )] =  (n) + ∑ i=0..n-1 O(E[n i 2 ]) Claim : E[n i 2 ] = 2 – 1/n If the claim is proved: E[T(n)] =  (n) + ∑ i=0..n-1 O(2 – 1/n) =  (n) + n * O(2 – 1/n) =  (n)

To prove the claim E[n i 2 ] = 2 – 1/n, let the indicator variable X ij = I{A[j] falls in bucket i} for i = 0, …, n-1 and j = 1, 2, …, n. Therefore, n i = ∑ j=1..n X ij and E[n i 2 ] = E[ (∑ j=1..n X ij ) 2 ] = ∑ j=1..n E[X ij 2 ] + ∑ j=1..n ∑ k=1..n & k≠j E[X ij X ik ] Now, X ij is 1 with probability 1/n and 0 with probability 1 – 1/n. Therefore, E[X ij 2 ] = 1 2 * 1/n * (1 – 1/n) = 1/n When k ≠ j, X ij and X ik and independent, so E[X ij X ik ] = E[X ij ] * E[X ik ] = 1/n * 1/n = 1/n 2 Substituting above: E[n i 2 ] = ∑ j=1..n 1/n + ∑ j=1..n ∑ k=1..n & k≠j 1/n 2 = n * 1/n + n(n-1) 1/n 2 = 1 + (n-1)/n = 2 – 1/n

Problems Ex You are given a sequence of n elements to sort. The input sequence consists of n/k subsequences, each containing k elements. The elements in a given subsequence are all smaller than the elements in the succeeding subsequence and larger than the elements in the preceding subsequence. Thus, all that is needed to sort the whole sequence of length n is to sort the k elements in each of the n/k subsequences. Show an (n lg k) lower bound on the number of comparisons needed to solve this variant of the sorting problem. (Hint: It is not rigorous to simply combine the lower bounds for the individual subsequences.) Ex Ex Ex Ex

Problems Ex Ex Ex Ex Ex Let X be a random variable that is equal to the number of heads in two flips of a fair coin. What is E [X 2 ]? What is E 2 [X]? Prob. 8-4 (see next slide) Prob. 8-6

MATCH-JUGS(R, B) if |R| = 0 // Sets are empty then return if |R| = 1 // Sets contain just one jug each then let R = {r} and B = {b} output “(r, b)” return else r ← a randomly chosen jug in R compare r to every jug of B B < ← the set of jugs in B that are smaller than r B > ← the set of jugs in B that are larger than r b ← the one jug in B with the same size as r compare b to every jug of R − {r} R < ← the set of jugs in R that are smaller than b R > ← the set of jugs in R that are larger than b output “(r, b)” MATCH-JUGS(R <, B < ) MATCH-JUGS(R >, B > ) Solution to Ex. 8-4 c: