Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt

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.
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.
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?
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.
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.
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,
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.
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.
1 Chapter 8-1: Lower Bound of Comparison Sorts. 2 About this lecture Lower bound of any comparison sorting algorithm – applies to insertion sort, selection.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Advanced Sorting 7 2  9 4   2   4   7
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
Linear-Time Sorting Continued Medians and Order Statistics
Introduction to Algorithms
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
Lecture 5 Algorithm Analysis
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
Lecture 5 Algorithm Analysis
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Linear Sorting Sorting in O(n) Jeff Chastine.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
Data Structures Sorting Haim Kaplan & Uri Zwick December 2014.
Lower Bounds & Sorting in Linear Time
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Linear-Time Sorting Algorithms
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
Linear Time Sorting.
Sorting We have actually seen already two efficient ways to sort:
Lecture 5 Algorithm Analysis
Presentation transcript:

Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt 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 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(ni2) where ni 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(ni2) ] = (n) + ∑i=0..n-1 E[O(ni2)] = (n) + ∑i=0..n-1 O(E[ni2]) Claim : E[ni2] = 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[ni2] = 2 – 1/n, let the indicator variable Xij = I{A[j] falls in bucket i} for i = 0, …, n-1 and j = 1, 2, …, n. Therefore, ni = ∑j=1..n Xij and E[ni2] = E[ (∑j=1..n Xij)2 ] = ∑j=1..n E[Xij2] + ∑j=1..n ∑k=1..n & k≠j E[Xij Xik] Now, Xij is 1 with probability 1/n and 0 with probability 1 – 1/n. Therefore, E[Xij2] = 12 * 1/n + 02 * (1 – 1/n) = 1/n When k ≠ j, Xij and Xik and independent, so E[Xij Xik] = E[Xij] * E[Xik] = 1/n * 1/n = 1/n2 Substituting above: E[ni2] = ∑j=1..n 1/n + ∑j=1..n ∑k=1..n & k≠j 1/n2 = n * 1/n + n(n-1) 1/n2 = 1 + (n-1)/n = 2 – 1/n

Problems Ex. 8.1-4 Ex. 8.2-1 Ex. 8.2-2 Ex. 8.2-3 Ex. 8.2-4 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. 8.2-1 Ex. 8.2-2 Ex. 8.2-3 Ex. 8.2-4

Problems Ex. 8.3-1 Ex. 8.3-2 Ex. 8.4-1 Ex. 8.4-2 Ex. 8.4-3: 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 E2[X]? Prob. 8-4 (see next slide) Prob. 8-6

Solution to Ex. 8-4 c: 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 MATCH-JUGS(R<, B<) MATCH-JUGS(R>, B>)