Deterministic Selection and Sorting Prepared by John Reif, Ph.D. Analysis of Algorithms.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Randomized Algorithms for Selection and Sorting Prepared by John Reif, Ph.D. Analysis of Algorithms.
MS 101: Algorithms Instructor Neelima Gupta
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Lower bound: Decision tree and adversary argument
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
MS 101: Algorithms Instructor Neelima Gupta
Prune-and-Search Method
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
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 Lower Bounds & Sorting in Linear Time.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CPSC 668Set 10: Consensus with Byzantine Failures1 CPSC 668 Distributed Algorithms and Systems Fall 2006 Prof. Jennifer Welch.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
Lecture 5: Master Theorem and Linear Time Sorting
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Analysis of Algorithms CS 477/677
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
The Complexity of Algorithms and the Lower Bounds of Problems
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
The Lower Bounds of Problems
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
1 Prune-and-Search Method 2012/10/30. A simple example: Binary search sorted sequence : (search 9) step 1  step 2  step 3  Binary search.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Asymptotics and Recurrence Equations Prepared by John Reif, Ph.D. Analysis of Algorithms.
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,
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
The Selection Algorithm : Design & Analysis [10].
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.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
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.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
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.
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.
Graph Algorithms Using Depth First Search
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
CS200: Algorithm Analysis
Linear Sorting Sorting in O(n) Jeff Chastine.
Chapter 11 Limitations of Algorithm Power
Lower Bounds & Sorting in Linear Time
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Lecture 5 Algorithm Analysis
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
The Selection Problem.
Presentation transcript:

Deterministic Selection and Sorting Prepared by John Reif, Ph.D. Analysis of Algorithms

Readings Main Reading Selections: –CLR, Chapters 2, 8, 9

Deterministic Selection and Sorting: Selection Algorithms and Lower Bounds Sorting Algorithms and Lower Bounds

Problem P size n Divide into sub problems size n 1, …, n k solve these and “glue” together solutions Time to combine solutions

Examples 1st lecture’s mult Fast fourier transform Binary search Merge sorting

Selection, and Sorting on Decision Tree Model Input a, b, c

Time Time = (# comparisons on longest path) Binary tree with L Leaves Facts: (1) has = L – 1 internal nodes (2) max height

Merging 2 lists with total of n keys Input X 1 < X 2 < … < X k and Y 1 < Y 2 < … < Y n-k Output Ordered merge of two key lists

Goal Provably asymptotically optimal algorithm in Decision Tree Model Use this Model because it −Allows simple proofs of lower bounds −time = # comparisons so easy to bound time costs

Algorithm Insert Input (X 1 < X 2 < … < X k ), (Y 1 ) Case k = n – 1 Algorithm: Binary Search by Divide- and-Conquer −(1) Compare −(2) If insert Y 1 into −elseand insert Y 1 into

Total Comparison Cost: Since a binary tree with n = k + 1 leaves has depth >, this is optimal!

Case: Merging equal length lists Input −(X 1 < X 2 < … < X k ) −(Y 1 < Y 2 < … < Y n-k ) Where

Algorithm 1)i  1, j  1 2)while i < k and j < k do if X i < Y j then output (X i ) and set i  i + 1 else output (Y j ) and set j  j + 1 3)output remaining elements Algorithm clearly uses 2k – 1 = n – 1 comparisons

Lower Bound: Consider case X 1 < Y 1 < X 2 < Y 2 < …< X k < Y k any merge algorithm must compare Claim: X i with Y i for i = 1,…, k Y i with X i+1 for j = 1,…, k-1 Otherwise we could flip Y i < X i with no change)  so requires > 2k-1 = n-1 comparisons!

Sorting by Divide-and-Conquer AlgorithmMerge Sort Inputset S of n keys 1)Partition S into set x of keys and set Y of keys

Sorting by Divide-and-Conquer (cont’d) 2)Recursively compute Merge Sort 3)Merge above sequences using n-1 comparisons 4)Output merged sequence

Time Analysis

Lower Bounds on Sorting (on decision tree model) n! distinct leaves

Easy Approximation (via Integration)

A Better Bound Using Sterling Approximation

Selection Problems Input X 1, X 2, …, X n and index k  {1, …, n} Output X (k) = the k’th best

History Rev C. L. Dodge (Lewis Carol) wrote article on lawn tennis tournament in James Gazett, 1883 Felt prizes unjust because –although winner X (1) always gets 1st prize –second X (2) may not get 2nd prize

History (cont’d) Carol proposed his own (nonoptimal) tournament…

Selection of the Champion X (1) X (1) is easily determined in n-1 comparison X (1) requires n-1 comparisons x1x1

Selection of the Champion X (1) (cont’d) Proof –Everyone except the champion X (1) must lose at least once!

Selection of the Second Best X (2) Using comparisons Algorithm 1)form a balanced binary tree for tournament to find X (1) using n-1 comparisons

Selection of the Second Best X (2) (cont’d)

1)Let Y be the set of players knocked out by champion X (1) 2)Play a tournament among the Y’s 3)output X (2) = champion of the Y’s using more comparisons

Lower Bounds on Finding X (2) Requires comparisons Proof −# comparisons > m 1 + m 2 + … −Where m i = # players who lost i or more matches

Lower Bounds on Finding X (2) (cont’d) Claim m 1 > n -1, since at end we must know X (1) as well as X (2) Claim m 2 > (# who lost to X (1) ) -1, since everyone (except X (2) ) who lost to X (1) must also have lost one more time.

Lower Bounds on Finding X (2) (cont’d) lemma (# who lost to X (1) ) > in worst case proof Use oracle who “fixes” results of games so that champion X (1) plays > matches

Lower Bounds on Finding X (2) (cont’d) Declare X i > X j if a)X i previously undefeated and X j lost at least once b)Both undefeated but X i played more matches c)Otherwise, decide consistently with previous decisions

Selection by Divide-and-Conquer Algorithm − Select k (X) Input − set X of n keys and index k

Deriving Recurrence for Time Bounds Use a sufficiently large constant c 1 (assuming d is constant) If say d = 5, T(n) < 20 c 1 n = O(n)

Deriving Recurrence for Time Bounds

Lower Bounds for Selecting X (k) Input −X = {x 1, …, x n }, index k Theorem −Every leaf of Decision Tree has depth > n - 1

Proof of Lower Bound for Selecting X (k) Fix a path p from root to leaf The comparisons done on p define a relation R p Let R p + = transitive closure of R p

Lemma If path p determines X m = X (k) then for all im either x i R p + x m or x m R p + x i Proof − Suppose x i is unrelated to x m by R p + − They can replace x i in linear order either before or after x m to violate x m =x (k)

Let the “key” comparison for x i be when x i is compared with x j where either 1) j=m 2) x i R p x j and x j R p + x m, or 3) x j R p x i and x m R p + x j Fact –X i has unique “key” comparison determining either x i R p + x m or x m R p + x i  So there are n-1 “key” comparisons, each distinct!

RADIXSORT: Linear Time Sort for RAM Assume keys over integers 1,…,n Costs O(n) time on unit cost RAM Avoids (n log n) lower bound on SORT by avoiding comparisons instead uses indexing of RAM Generalizes (in c passes) to key domains {1, …, n c }

Procedure RADIXSORT Input 1)for j = 1, …, n do initialize B[j] to be the empty list 2)for I=1, …, n do add I to B[x i ] 3)Let L = (i 1, i 2, …, i n ) be the concatenation of B[1],…,B[n] 4)output

Deterministic Selection and Sorting Prepared by John Reif, Ph.D. Analysis of Algorithms