Lower Bound on Comparison-based Sorting

Similar documents
How fast can we sort? Sorting. Decision-tree example. Decision-tree example. CS 3343 Fall by Charles E. Leiserson; small changes by Carola

Lower bound for comparison-based sorting

Question 7.11 Show how heapsort processes the input:

Design and Analysis of Algorithms

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

COMP Analysis of Algorithms & Data Structures

Sorting. CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk

COMP Analysis of Algorithms & Data Structures

MA 252: Data Structures and Algorithms Lecture 9. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Data Structures and Algorithms Week 4

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Lecture 5: Sorting Part A

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Lecture: Analysis of Algorithms (CS )

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Properties of a heap (represented by an array A)

CSC Design and Analysis of Algorithms

lecture notes September 2, How to sort?

Data Structures and Algorithms. Roberto Sebastiani

Chapter 8 Sorting in Linear Time

Partha Sarathi Manal

Motivation for B-Trees

Data Structures and Algorithms Chapter 4

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Introduction to Algorithms

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Examination Questions Midterm 2

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

1 Lower bound on runtime of comparison sorts

Chapter 8 Sort in Linear Time

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Algorithms and Data Structures

Searching, Sorting. part 1

CSE 373 Lecture 19: Wrap-Up of Sorting

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Priority Queues and Heaps. Heaps of fun, for everyone!

Data Structures. Sorting. Haim Kaplan & Uri Zwick December 2013

Analysis of Algorithms

Chapter 6 Heapsort 1

A data structure and associated algorithms, NOT GARBAGE COLLECTION

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

13.4 Deletion in red-black trees

Lecture 7. Transform-and-Conquer

The best sorting algorithm that sorts by comparing the elements with each other is order Nlog(N). Why?

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

13.4 Deletion in red-black trees

Chapter 6 Heap and Its Application

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

COMP Data Structures

COMP Analysis of Algorithms & Data Structures

CIS265/ Trees Red-Black Trees. Some of the following material is from:

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

COMP Analysis of Algorithms & Data Structures

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

The complexity of Sorting and sorting in linear-time. Median and Order Statistics. Chapter 8 and Chapter 9

CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

CS Fall 2010 B-trees Carola Wenk

Sorting isn't only relevant to records and databases. It turns up as a component in algorithms in many other application areas surprisingly often.

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

CISC 235: Topic 4. Balanced Binary Search Trees

CISC 621 Algorithms, Midterm exam March 24, 2016

INF2220: algorithms and data structures Series 1

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

data structures and algorithms lecture 6

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

CPSC 320 Midterm 2 Thursday March 13th, 2014

CS24 Week 8 Lecture 1

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Sorting and Selection

Search Trees - 1 Venkatanatha Sarma Y

arxiv: v3 [cs.ds] 18 Apr 2011

Heap sort. Carlos Moreno uwaterloo.ca EIT

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

II (Sorting and) Order Statistics

CMPS 2200 Fall 2017 B-trees Carola Wenk

8 SortinginLinearTime

Transform & Conquer. Presorting

Friday Four Square! 4:15PM, Outside Gates

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Heaps. 2/13/2006 Heaps 1

Module 4: Dictionaries and Balanced Search Trees

EE 368. Weeks 5 (Notes)

Sorting is ordering a list of objects. Here are some sorting algorithms

Linked Structures Songs, Games, Movies Part III. Fall 2013 Carola Wenk

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Priority Queues. Chapter 9

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Basic Data Structures (Version 7) Name:

Transcription:

Lower Bound on Comparison-based Sorting Different sorting algorithms may have different time complexity, how to know whether the running time of an algorithm is best possible? We know of several sorting methods (merge-sort, heapsort) that take time O(n log n) in the worst case to sort a set of n elements. Can this be improved? For definiteness, we consider only comparison sorts, which work by comparing pairs of elements. All other logic and moving of elements are ok, but no other use of the elements values is allowed. The pairs to be compared can be chosen in any way at all. For comparison sorts, it makes sense to count the number of comparisons. COMP3600/6466: Lecture 8 2017 1

How long does it take to sort? (continued) It is easier to analyze a slightly different problem. Consider a list A = (a 1,a 2,...,a n ) of distinct values. Problem 1. Sort A in increasing order. Problem 2. Determine which order the elements of A are in. For example, the order of A = (20,40,10,30,50) is (2,4,1,3,5). There are n! possible answers given a n-element list. It is easy to see that these two problems require the same number of comparisons: Given a solution to one of the two problems, it can be modified to solve the other problem without using more comparisons. COMP3600/6466: Lecture 8 2017 2

Lower Bound on Sorting An example: Given a 3-element sequence a 1, a 2, and a 3, sort the sequence in increasing order, there are 3!=6 possible sorted sequences are as follows. 1. a 1 a 2 a 3, or its corresponding indices 1,2,3 2. a 1 a 3 a 2, or its corresponding indices 1,3,2 3. a 2 a 1 a 3, or its corresponding indices 2,1,3 4. a 2 a 3 a 1, or its corresponding indices 2,3,1 5. a 3 a 1 a 2, or its corresponding indices 3,1,2 6. a 3 a 2 a 1, or its corresponding indices 3,2,1 COMP3600/6466: Lecture 8 2017 3

Lower Bound on Sorting (count.) Let a 1,a 2,...,a n be n elements to be sorted. At each time, a pair of elements is compared. We can use a binary comparison tree to represent the sort procedure, where each node in the tree represents one comparison each node is labelled by the indices of the pair comparison elements For example, a pair of elements a 1 and a 3 to be compared, we label its corresponding node in the comparison tree as (1: 3). COMP3600/6466: Lecture 8 2017 4

Lower bound on sorting The input is a sequence of 3 elements: a 1,a 2,a 3. COMP3600/6466: Lecture 8 2017 5

Lower bound on sorting (examples) a1=15, a2=19, a3=14 < = 1:2 > < = a1=17, a2=12, a3=25 1:2 > 2:3 > < = 1:3 > 1:3 <2,1,3> 2:3 < = > 12, 17, 25 <3,1,2> 14, 15, 19 COMP3600/6466: Lecture 8 2017 6

Lower Bound on Sorting Important observations on the binary comparison tree of sorting: The length of a path from the tree root to a tree leaf corresponds to the number of comparisons to sort a sequence. The maximum length of any such a path is also referred to as the the depth of the comparison tree. There are n! tree leaves in the binary comparison tree, as there are n! = n (n 1) (n 2)... 2 1 different sorted sequences for n elements to be sorted, depending on the n input element sequence. Minimizing the number of comparisons of sorting n elements is equivalent to minimizing the depth (or the height) of the binary comparison tree. COMP3600/6466: Lecture 8 2017 7

Lower Bound on Sorting In other words, the problem becomes to construct a binary comparison tree that has n! leaves such that the longest path from the tree root to a tree leaf is minimized. We observe that each internal node (except the tree root) in the binary comparison tree has a parent node and two children nodes, then, the depth of any binary tree containing no less than 2 h leaves is at least h, assuming that the depth of the tree root is 0. Thus, if a binary comparison tree contains N = n! leaves, then its minimum depth h is logn, due to the fact that 2 h N and N = n!. COMP3600/6466: Lecture 8 2017 8

How long does it take to sort? (continued) Alternatively, we show the lower bound on sorting by another method. Suppose we have an algorithm which can always solve Problem 2 in K comparisons. Each comparison has two possible values < and >, assuming that all elements are distinct. Therefore, the sequence of K comparisons has at most 2 K possible values. Such as <,>,<,<,>,>,<,<,>,<. Refer to the previous example: a 1 = 15,a 2 = 19,a 3 = 14, its corresponding comparison sequence is a 1 :a 2, a 2 :a 3, a 1 :a 3, i.e., <,>,>. Since Problem 2 has n! possible answers, it must be that 2 K n!. Otherwise, two different answers would have the same sequence of comparison values. Thus, K log(n!), and log(n!) = Θ(n log n) by Stirlings formula. Stirling s Approximation n! = 2πn( n e )n (1 + Θ( 1 n )). COMP3600/6466: Lecture 8 2017 9

The Lower bound of the median of n-element sequence Exercise: Given n distinct elements, show a non-trival lower bound for finding the median of the n elements under the comparison model. Or Show the following theorem. Theorem: Any algorithm to find the median of n-elements (for odd n) by comparison of elements must do at least 3n/2 3/2 comparisons in the worst case. COMP3600/6466: Lecture 8 2017 10