Sorting & Lower Bounds Jeff Edmonds York University COSC 3101 Lecture 5.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Garfield AP Computer Science
Analysis of Algorithms
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Non-Comparison Based Sorting
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
MS 101: Algorithms Instructor Neelima Gupta
Mudasser Naseer 1 5/1/2015 CSC 201: Design and Analysis of Algorithms Lecture # 9 Linear-Time Sorting Continued.
Lower Bounds & Models of Computation Jeff Edmonds York University COSC 3101 Lecture 8.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Spring 2015 Lecture 5: QuickSort & Selection
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
CPSC 411, Fall 2008: Set 2 1 CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Fall 2008.
Lecture 5: Master Theorem and Linear Time Sorting
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
Analysis of Algorithms CS 477/677
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
1 CSE 326: Data Structures: Sorting Lecture 16: Friday, Feb 14, 2003.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
September 24, 2001 L5.1 Introduction to Algorithms Lecture 6 Prof. Shafi Goldwasser.
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?
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Analysis of Algorithms CS 477/677
Fall 2015 Lecture 4: Sorting in linear time
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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,
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
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.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
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.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
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.
Sorting.
Introduction to Algorithms
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Ch8: Sorting in Linear Time Ming-Te Chi
Ch 6: Heapsort Ming-Te Chi
Complexity Present sorting methods. Binary search. Other measures.
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
Lecture 3 / 4 Algorithm Analysis
Lower Bounds & Sorting in Linear Time
Linear-Time Sorting Algorithms
Sorting Popular algorithms:
Presentation transcript:

Sorting & Lower Bounds Jeff Edmonds York University COSC 3101 Lecture 5

Problem 1: Replace... while x!=0 AND y!=0 AND z!=0 do with... while x!=0 OR y!=0 OR z!=0 do Now available on website. DUE: February 13th CORRECTION: Assignment 2

Review of Sorting Algorithms

Minimal effort splitting Lots of effort recombining Lots of effort splitting Minimal effort recombining Merge SortInsertion Sort Quick SortSelection Sort Size of Sublists n /2,n /2 n-1,1 Four Recursive Sorts

Selection Sort 5, 9, 14, 31, 25, 8, 18 Swap with first item 25, 9, 14, 31, 5, 8, 18 Search array for minimum item 5, 9, 14, 31, 25, 8, 18 Search remaining array for min item 5, 8, 14, 31, 25, 9, 18 Swap with first item … Loop … 5, 8, 9, 14, 18, 25, 31

Insertion Sort 25, 9, 14, 31, 5, 8, 18 Insert 1 st item in correct position … Loop … 9, 25, 14, 31, 5, 8, 18 Insert 2 nd item in correct position 9, 14, 25, 31, 5, 8, 18 Insert 3 rd item in correct position 5, 8, 9, 14, 18, 25, 31

Selection Sort Average & Worst Time:  (n 2 ) Insertion Sort Average & Worst Time:  (n 2 )

Merge Sort Divide and Conquer

Merge Sort Split Set into Two (no real work) 25,31,52,88,98 Get one friend to sort the first half. 14,23,30,62,79 Get one friend to sort the second half.

Merge Sort Merge two sorted lists into one 25,31,52,88,9814,23,30,62,7914,23,25,30,31,52,62,79,88,98

Merge Sort Time: T(n) = =  (n log n) 2T(n/2) +  (n)

Quick Sort Divide and Conquer

Quick Sort Partition set into two using randomly chosen pivot ≤ 52 ≤

Quick Sort ≤ 52 ≤ 14,23,25,30,31 Get one friend to sort the first half. 62,79,98,88 Get one friend to sort the second half.

Quick Sort 14,23,25,30,31 62,79,98,8852 Glue pieces together. (No real work) 14,23,25,30,31,52,62,79,88,98

Quick Sort Let pivot be the first element in the list? ≤ 31 ≤ 52

Quick Sort ≤ 14 ≤ 14,23,25,30,31,52,62,79,88,98 23,25,30,31,52,62,79,88,98 If the list is already sorted, then the list is worst case unbalanced.

Quick Sort Best Time: Worst Time: Expected Time: T(n) = 2T(n/2) +  (n) =  (n log(n))

Quick Sort T(n) = 2T(n/2) +  (n) =  (n log(n)) Best Time: Worst Time: Expected Time: =  (n 2 ) T(n) = T(0) + T(n-1) +  (n)

Quick Sort T(n) = 2T(n/2) +  (n) =  (n log(n)) Best Time: T(n) = T(0) + T(n-1) +  (n) Worst Time: Expected Time: =  (n 2 ) T(n) = T( 1 / 3 n) + T( 2 / 3 n) +  (n) =  (n log(n))

Heaps, Heap Sort, & Priority Queues

Heap Definition Completely Balanced Binary Tree The value of each node  each of the node's children. Left or right child could be larger. Where can 1 go? Maximum is at root. Where can 8 go? Where can 9 go?

Heap Data Structure Completely Balanced Binary Tree Implemented by an Array

Make Heap Get help from friends

Heapify ? Maximum is at root. Where is the maximum?

Find the maximum. Put it in place ? Repeat Heapify

Heap Running Time: Heapify

Make Heap Get help from friends T(n) = 2T(n/2) + log(n) Running time: =  (n)

Heaps Heap ?

?

?

?

Running Time: i log(n) -i 2 log(n) -i

Insertion Sort Largest i values are sorted on side. Remaining values are off to side. 6,7,8,9 < Max is easier to find if a heap. Insertion

Heap Sort Largest i values are sorted on side. Remaining values are in a heap.

Heap Data Structure HeapArray Heap Array

Heap Sort Largest i values are sorted on side. Remaining values are in a heap Put next value where it belongs. Heap ?

Heap Sort ? ?? ?? ?? Heap

Heap Sort

Running Time:

Priority Queues Maintains dynamic set, S, of elements, each with a key. Max-priority queue supports: INSERT(S,x) MAXIMUM(S) EXTRACT-MAX(S) INCREASE-KEY(S,x,k) Application: Shedule jobs on a shared computer.

Priority Queues cont’d... MAXIMUM(S): EXTRACT-MAX(S):

Priority Queues cont’d... INSERT(S,x): INCREASE-KEY(S,x,k):

Other Sorting Algorithms: Counting Sort Radix Sort Bucket Sort

Counting Sort Input: Array A[1,…,n], with all elements in {1,..,k}. Output: Sorted array B[1,..,n]. Auxiliary Storage: C[1,..,k] A = _ _ _ B = _ _ _ _ _ C =

Counting Sort Example A = C = C = C =

Counting Sort Example cont’d A = _ 2 _ _ _ _ B = C = 1 _ 2 _ 3 _ _ B = C = 3 _ _ _ B = C = 2

Counting Sort Example cont’d A = _ _ 5 B = C = _ 5 B = C = B = C = 4

Counting Sort Analysis  (k)

Counting Sort Analysis  (k)  (n)

Counting Sort Analysis  (k)  (n)

Counting Sort Analysis  (k)  (n)

Counting Sort Analysis  (k)  (n)  (n + k) =  (n) if k = O(n)

Radix Sort Sort numbers by columns, starting with least significant digit (radix):

Radix Sort Analysis Assume use of Counting Sort (  (n)) in each pass. For constant d digits, overall  (dn). Counting sort not in place, therefore may prefer a comparison sort algorithm.

Bucket Sort Assumes input elements distributed uniformly on [0,1). List elements from buckets in order. Sort each bucket. Distribute n input values into the buckets. Divide [0,1) into equal sized buckets.

Bucket Sort Example

Bucket Sort Analysis Division into buckets & concatentation:  (n) Insertion sort:  (n 2 ) Define n i = number of elements in bucket B[i]. T(n) =  (n) +  i=1 O(n i 2 ) Take expectation of both sides: E[T(n)] =  (n) +  i=1 E[O(n i 2 )] =  (n) + n O(2 - 1/n) =  (n) n /n

Lower Bounds for Sorting using Information Theory

The Time Complexity of a Problem P Merge, Quick, and Heap Sort can sort N numbers using O(N log N) comparisons between the values. Theorem: No algorithm can sort faster. The Time Complexity of a Problem P: The minimum time needed by an algorithm to solve it.

The Time Complexity of a Problem P The minimum time needed by an algorithm to solve it. Problem P is computable in time T upper (n) if there is an algorithm A which outputs the correct answer in this much time Eg: Sorting computable in T upper (n) = O(n 2 ) time.  A,  I, A(I)=P(I) and Time(A,I)  T upper (|I|) Upper Bound:

Understand Quantifiers!!! One girl Could be a separate girl for each boy. SamMary BobBeth John Marilin Monro FredAnn SamMary BobBeth John Marilin Monro FredAnn  A,  I, A(I)=P(I) and Time(A,I)  T upper (|I|)

The Time Complexity of a Problem P The minimum time needed by an algorithm to solve it.  A,  I, A(I)=P(I) and Time(A,I)  T upper (|I|)  I,  A, A(I)=P(I) and Time(A,I)  T upper (|I|) What does this say? True for any problem P and time T upper. Given fixed I, its output is P(I).

The Time Complexity of a Problem P The minimum time needed by an algorithm to solve it. Time T lower (n) is a lower bound for problem p if no algorithm solves the problem faster. There may be algorithms that give the correct answer or run quickly on some inputs instance. Lower Bound:

The Time Complexity of a Problem P The minimum time needed by an algorithm to solve it. Lower Bound: Time T lower (n) is a lower bound for problem p if no algorithm solves the problem faster. eg: No algorithm can sort N values in T lower = sqrt(N) time. But for every algorithm, there is at least one instance I for which either the algorithm gives the wrong answer or it runs in too much time.  A,  I, A(I)  P(I) or Time(A,I)  T lower (|I|)

 A,  I, A(I)=P(I) and Time(A,I)  T upper (|I|) Lower Bound: Upper Bound: The Time Complexity of a Problem P The minimum time needed by an algorithm to solve it. “There is” and “there isn’t a faster algorithm” are almost negations of each other.

 A,  I, A(I)=P(I) and Time(A,I)  T upper (|I|) Upper Bound: Prover-Adversary Game I have an algorithm A that I claim works and is fast. I win if A on input I gives the correct output in the allotted time. Oh yeah, I have an input I for which it does not. What we have been doing all along.

Lower Bound: Prover-Adversary Game I win if A on input I gives the wrong output or runs slow.  A,  I, A(I)  P(I) or Time(A,I)  T lower (|I|) Proof by contradiction. I have an algorithm A that I claim works and is fast. Oh yeah, I have an input I for which it does not.

Lower Bound: Prover-Adversary Game  A,  I, A(I)  P(I) or Time(A,I)  T lower (|I|) I have an algorithm A that I claim works and is fast. Lower bounds are very hard to prove, because I must consider every algorithm no matter how strange.

The Yes/No Questions Game I choose a number  [1..N]. I ask you yes/no questions. Time is the number of questions asked in the worst case. I answer. I determine the number.

The Yes/No Questions Game Upper Bound 6 Great!

The Yes/No Questions Game Upper Bound Time N leaves = # of questions = height = log 2 (N)

The Yes/No Questions Game Lower Bound? Time N leaves Is there a faster algorithm? Is the third bit 1? If different questions? = # of questions = height = log 2 (N)

The Yes/No Questions Game Lower Bound? N leaves Is there a faster algorithm? If it has a different structure? Best case Worst case Time = # of questions = height = log 2 (N)

The Yes/No Questions Game Lower Bound Theorem: For every question strategy A, with the worst case object I to ask about, log 2 N questions need to be asked to determine one of N objects.  A,  I, A(I)  P(I) or Time(A,I)  log 2 N Proof: Prover/Adversary Game

Lower Bound Proof Oh yeah, I have an input I for which it does not. I have an algorithm A that I claim works and is fast. Two cases: # output leaves < N # output leaves  N

I win if A on input I gives the wrong output or requires log N questions. I have an algorithm A that I claim works and is fast. Man your algorithm does not have N output leaves. Lower Bound Proof: case 1 I give a input I = 4 with missing output.

I win if A on input I gives the wrong output or requires log N questions. Good now your algorithm has all N outputs as leaves. It must have height  log N. I have an algorithm A that I claim works and is fast. I give a input I = 5 at a deep leaf. Lower Bound Proof: case 2

The Yes/No Questions Game Lower Bound Theorem: For every question strategy A, with the worst case object I to ask about, log 2 N questions need to be asked to determine one of N objects.  A,  I, A(I)  P(I) or Time(A,I)  log 2 N Proof: Prover/Adversary Game End of Proof.

Communication Complexity Or a obj  a set of N objs. Time is the number of bits sent in the worst case. I send you a stream of bits. I determine the object. I choose a number  [1..N].

Communication Complexity Upper Bound 6 Great!

Communication Complexity Lower Bound Theorem: For every communication strategy A, with the worst case object I to communicate, log 2 N bits need to transmitted to communicate one of N objects.  A,  I, A(I)  P(I) or Time(A,I)  log 2 N

The Sorting Game I choose a permutation of {1,2,3,…,N}. I ask you yes/no questions. Time is the number of questions asked in the worst case. I answer. I determine the permuation.

Sorting Upper Bound b,c,a Great!

Time N! leaves = # of questions = height = log 2 (N!) Sorting Upper Bound

N! = 1 × 2 × 3 × … × N / 2 × … × N N factors each at most N. N / 2 factors each at least N / 2.  N N N / 2  N/2N/2 Bounding log(N!) N / 2 log( N / 2 )  log(N!)  N log(N) =  (N log(N)).

Time N! leaves = # of questions = height = log 2 (N!) =  (N log(N)). Is there a faster algorithm? Is the third bit of c 1? If different questions? Sorting Lower Bound

Is there a faster algorithm? What if a different model of computation? class InsertionSortAlgorithm { for (int i = 1; i < A.length; i++) { int j = i; int B = A[i]; while ((j > 0) && (A[j-1] > A[i])) { A[j] = A[j-1]; j--; } A[j] = B; }} Input: A = [a, b, c]Output in increasing order. Sorting - Lower Bound

I choose a permutation of {1,2,3,…,N}. I answer. I determine the permuation. class InsertionSortAlgorithm { for (int i = 1; i < A.length; i++) { int j = i; int B = A[i]; while ((j > 0) && (A[j-1] > A[i])) { A[j] = A[j-1]; j--; } A[j] = B; }} I ask you this yes/no question. I run my algorithm until I need to know something about the input permutation. Sorting with Alice & Bob

Theorem: For every sorting algorithm A, with the worst case input instance I,  (N log 2 N) comparisons (or other bit operations) need to be executed to sort N objects.  A,  I, A(I)  P(I) or Time(A,I)  N log 2 N Proof: Prover/Adversary Game Sorting - Lower Bound

Lower Bound Proof Oh yeah, I have an input I for which it does not. I have an algorithm A that I claim works and is fast. Two cases: # output leaves < N! # output leaves  N!

I win if A on input I gives the wrong output or requires log N questions. Man your algorithm does not have N! output leaves. Lower Bound Proof: case 1 I have an algorithm A that I claim works and is fast. I give a input I = with missing output. b,a,c

I win if A on input I gives the wrong output or requires log N! questions. Good now your algorithm has all N! outputs as leaves. It must have height  log N!. I have an algorithm A that I claim works and is fast. Lower Bound Proof: case 2 =  (N log(N)). I give a input I = at a deep leaf.

Sorting Lower Bound Theorem: For every sorting algorithm A, on the worst case input instance I,  (N log 2 N) comparisons (or other bit operations) need to be executed to sort N objects.  A,  I, A(I)  P(I) or Time(A,I)  N log 2 N Proof: Prover/Adversary Game End of Proof.

End

Problem 1: Replace... while x!=0 AND y!=0 AND z!=0 do with... while x!=0 OR y!=0 OR z!=0 do Now available on website. DUE: February 13th CORRECTION: Don’t forget.... Assignment 2