CS 583 Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Lower bound: Decision tree and adversary argument
Advertisements

Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Introduction to Algorithms
Spring 2015 Lecture 5: QuickSort & Selection
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Lecture 5: Master Theorem and Linear Time Sorting
Analysis of Algorithms CS 477/677
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
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.
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.
Analysis of Algorithms CS 477/677
Fall 2015 Lecture 4: Sorting in linear time
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
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,
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
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.
Lecture 5 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Lower Bounds & Sorting in Linear Time
Sorting.
David Kauchak cs062 Spring 2010
Order Statistics Comp 122, Spring 2004.
Decision trees Polynomial-Time
CPSC 411 Design and Analysis of Algorithms
Linear-Time Sorting Continued Medians and Order Statistics
CPSC 411 Design and Analysis of Algorithms
Lecture 5 Algorithm Analysis
Linear Sorting Sections 10.4
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
Ch8: Sorting in Linear Time Ming-Te Chi
CS 583 Analysis of Algorithms
Lecture 5 Algorithm Analysis
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
Medians and Order Statistics
CS200: Algorithm Analysis
Linear Sorting Sorting in O(n) Jeff Chastine.
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
Lower Bounds & Sorting in Linear Time
CS 583 Analysis of Algorithms
Linear Sorting Section 10.4
Linear-Time Sorting Algorithms
Lecture 5 Algorithm Analysis
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
Topic 5: Heap data structure heap sort Priority queue
Order Statistics Comp 122, Spring 2004.
CPSC 411 Design and Analysis of Algorithms
Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to.
David Kauchak cs302 Spring 2012
The Selection Problem.
Algorithms CSCI 235, Spring 2019 Lecture 19 Order Statistics
Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II
CS200: Algorithm Analysis
Sorting We have actually seen already two efficient ways to sort:
Lecture 5 Algorithm Analysis
Presentation transcript:

CS 583 Analysis of Algorithms Sorting in Linear Time CS 583 Analysis of Algorithms 5/5/2019 CS583 Fall'06: Sorting in Linear Time

CS583 Fall'06: Sorting in Linear Time Outline Comparison Sort Algorithms Lower Bounds for Sorting Counting Sort Order Statistics Minimum and Maximum Selection 5/5/2019 CS583 Fall'06: Sorting in Linear Time

CS583 Fall'06: Sorting in Linear Time Comparison Sorts We have seen several algorithms that can sort n numbers in O(n lg n) time. Merge sort and heapsort achieve this upper bound in the worst case; quicksort achieves it on average. These algorithms have one common property: the sorted order they determine is based only on comparisons between the input elements. Such sorting algorithms are called comparison sorts.  We prove that any comparison sort must make (n lg n) comparisons in the worst case to sort n elements. 5/5/2019 CS583 Fall'06: Sorting in Linear Time

Comparison Sort: Decision Tree We assume without loss of generality that all input elements are distinct. In this case, we can simply make comparisons of one form, for example, ai <= aj.   Comparison sorts can be viewed in terms of decision tree. For example, sorting three elements using insertion sort will look as follows: 1:2 <= > 2:3 1:3 <= > <1,2,3> 1:3 <= > <1,3,2> <3,1,2> ... 5/5/2019 CS583 Fall'06: Sorting in Linear Time

Comparison Sort: Decision Tree (cont.) In a decision tree each internal node is annotated by i:j for some i and j in the range 1 <= i,j <= n. Each leaf is annotated by a permutation <(1), ... , (n)>. The execution of the sorting algorithm corresponds to tracing a path from the root to a leaf. Any correct sorting algorithm must be able to produce each permutation of its input, hence all n! leaves of the decision tree must be "reachable". 5/5/2019 CS583 Fall'06: Sorting in Linear Time

Lower Bound for Comparison Sorts Theorem 8.1 Any comparison sort algorithm requires (n lg n) comparisons in the worst case.   Proof. The length of the longest path from the root of a decision tree to any of its reachable trees represents the worst-case number of comparisons that a sorting algorithm performs. Hence, we need to determine the height of the decision tree, where each permutation is a reachable leaf. 5/5/2019 CS583 Fall'06: Sorting in Linear Time

Lower Bound for Comparison Sorts (cont.) Consider a tree of height h and l leaves. Since each permutation appears as a leaf, we have n! <= l. A binary tree of height h has no more than 2h leaves:   n! <= l <= 2h => h >= lg(n!) lg(n!) = (n lg n) (see 3.18) => h >= (n lg n) => h = (n lg n)   5/5/2019 CS583 Fall'06: Sorting in Linear Time

CS583 Fall'06: Sorting in Linear Time Counting Sort This algorithm assumes that each of the n input elements is an integer in the range 0 to k. When k = O(n), the sort runs in (n) time.  The basic idea is to determine for each input element x, the number of elements less than x. This information can be used to place x directly into its position in the output array. The algorithm requires an input array A, the output array B, and an intermediate (“counting”) array C. 5/5/2019 CS583 Fall'06: Sorting in Linear Time

Counting Sort: Example n=5, k=2: 2 1 0 2 2   C after steps 1-4: 0 1 2 1 1 3 C after loop 6: 1 2 5 B in loop 9: 1 2 3 4 5 index 2 1 5/5/2019 CS583 Fall'06: Sorting in Linear Time

Counting Sort: Pseudocode Counting-Sort (A,B,n,k) 1 for i = 0 to k 2 C[i] = 0 3 for i = 0 to n 4 C[A[i]]++ 5 // C[i] contains the number of elements = i 6 for i = 1 to k 7 C[i] = C[i] + C[i-1] 8 // C[i] now contains number of elements <= i 9 for i = n to 1 10 B[C[A[i]]] = A[i] 11 C[A[i]]-- 12 return 5/5/2019 CS583 Fall'06: Sorting in Linear Time

Counting Sort: Performance After loop 6, the array C contains the first position of an element with value i, which is the same as the number of elements <= i. At each iteration, when the i element is placed into the output array, the position of the next element i will be before the current one. To calculate the running time, observe that the number of operations is k+1(loop 1) + n+k+n = (k+n). When using k=O(n), we have the running time (n).  An important quality of the counting sort is that it is stable, numbers with the same value appear in the output array in the same order as they do in the input array. The property of stability is important when satellite date are carried around with the key. 5/5/2019 CS583 Fall'06: Sorting in Linear Time

CS583 Fall'06: Sorting in Linear Time Order Statistics The ith order statistics of a set of n elements is the ith smallest element. The minimum element is the first order statistics (i=1). The maximum element is the last order statistics (i=n). A median is a the “half point” of the set (i=(n+1)/2). The selection problem is finding the ith order statistics from a set of n distinct numbers. It can be solved in O(n lg n) time by sorting elements, and then selecting the ith element from the sorted array. The fastest algorithm runs in O(n) time in the worst case. 5/5/2019 CS583 Fall'06: Sorting in Linear Time

Minimum/Maximum: Pseudocode MINIMUM(A) 1 min = A[1] 2 for i = 2 to length[A] 3 if A[i] < min min = A[i] 5 return min The above algorithm makes (n-1) comparisons. Finding the maximum can be accomplished with (n-1) comparisons as well: MAXIMUM(A) 1 max = A[1] 3 if A[i] > max max = A[i] 5 return max 5/5/2019 CS583 Fall'06: Sorting in Linear Time

Simultaneous Minimum and Maximum MINMAX(A) 1 min = A[1] 2 max = A[1] 3 i = 2 4 while (i <= length[A]) 5 if (i+1) > length[A] 6 x_min = A[i]; x_max = A[i] 7 else 8 if A[i] < A[i+1] 9 x_min = A[i]; x_max = A[i+1] 10 else 11 x_min = A[i+1]; x_max = A[i] 12 if x_max > max 13 max = x_max 14 if x_min < min 15 min = x_min 16 i += 2 17 return (min, max) The above algorithm performs at most 5n/2 comparisons to find both minimum and maximum, and hence runs in (n) time. 5/5/2019 CS583 Fall'06: Sorting in Linear Time

CS583 Fall'06: Sorting in Linear Time General Selection The general selection algorithm finds an ith order statistics. The algorithm below is modeled after a quicksort algorithm with expected running time O(n). RANDOMIZED-SELECT (A,p,r,i) 1 if p=r 2 return A[p] 3 q = RANDOMIZED-PARTITION(A,p,r) 4 k = q-p+1 5 if i = k // the pivot element is the answer 6 return A[q] 7 else 8 if i<k 9 return RANDOMIZED-SELECT(A,p,q-1,i) 10 else 11 return RANDOMIZED-SELECT(A,q+1,r,i-k) 5/5/2019 CS583 Fall'06: Sorting in Linear Time