lecture notes September 2, Batcher s Algorithm

Similar documents
NON-OVERLAPPING PERMUTATION PATTERNS. To Doron Zeilberger, for his Sixtieth Birthday

Tile Number and Space-Efficient Knot Mosaics

Chapter 7: Sorting 7.1. Original

Greedy Flipping of Pancakes and Burnt Pancakes

Non-overlapping permutation patterns

CS101 Lecture 28: Sorting Algorithms. What You ll Learn Today

Notes for Recitation 3

Olympiad Combinatorics. Pranav A. Sriram

arxiv: v2 [math.gt] 21 Mar 2018

Constructions of Coverings of the Integers: Exploring an Erdős Problem

Surreal Numbers and Games. February 2010

Cryptography. 2. decoding is extremely difficult (for protection against eavesdroppers);

A Lower Bound for Comparison Sort

4. Non Adaptive Sorting Batcher s Algorithm

ON THE EQUATION a x x (mod b) Jam Germain

Twenty-sixth Annual UNC Math Contest First Round Fall, 2017

Narrow misère Dots-and-Boxes

More Great Ideas in Theoretical Computer Science. Lecture 1: Sorting Pancakes

18.204: CHIP FIRING GAMES

Lower Bounds for the Number of Bends in Three-Dimensional Orthogonal Graph Drawings

Chameleon Coins arxiv: v1 [math.ho] 23 Dec 2015

A tournament problem

Asymptotic Results for the Queen Packing Problem

The Four Numbers Game

Problem Set 4 Due: Wednesday, November 12th, 2014

Math 255 Spring 2017 Solving x 2 a (mod n)

arxiv: v1 [math.gt] 21 Mar 2018

GENERALIZATION: RANK ORDER FILTERS

Kenken For Teachers. Tom Davis January 8, Abstract

TILING RECTANGLES AND HALF STRIPS WITH CONGRUENT POLYOMINOES. Michael Reid. Brown University. February 23, 1996

Whole Numbers. Predecessor and successor Given any natural number, you can add 1 to that number and get the next number i.e. you

Week 1. 1 What Is Combinatorics?

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 6 Lecture - 37 Divide and Conquer: Counting Inversions

and problem sheet 7

The number of mates of latin squares of sizes 7 and 8

Cards. There are many possibilities that arise with a deck of cards. S. Brent Morris

6.2 Modular Arithmetic

In 1974, Erno Rubik created the Rubik s Cube. It is the most popular puzzle

CS100: DISCRETE STRUCTURES. Lecture 8 Counting - CH6

On the Periodicity of Graph Games

Merge Sort. Note that the recursion bottoms out when the subarray has just one element, so that it is trivially sorted.

SMT 2014 Advanced Topics Test Solutions February 15, 2014

A NEW COMPUTATION OF THE CODIMENSION SEQUENCE OF THE GRASSMANN ALGEBRA

arxiv: v1 [math.co] 17 May 2016

Some forbidden rectangular chessboards with an (a, b)-knight s move

MATHEMATICS ON THE CHESSBOARD

Exploring Concepts with Cubes. A resource book

An Optimal Algorithm for a Strategy Game

Maxima and Minima. Terminology note: Do not confuse the maximum f(a, b) (a number) with the point (a, b) where the maximum occurs.

Collection of rules, techniques and theorems for solving polynomial congruences 11 April 2012 at 22:02

2009 Philippine Elementary Mathematics International Contest Page 1

GEOGRAPHY PLAYED ON AN N-CYCLE TIMES A 4-CYCLE

SOLITAIRE CLOBBER AS AN OPTIMIZATION PROBLEM ON WORDS

NANYANG TECHNOLOGICAL UNIVERSITY SEMESTER II EXAMINATION MH1301 DISCRETE MATHEMATICS. Time Allowed: 2 hours

Three Pile Nim with Move Blocking. Arthur Holshouser. Harold Reiter.

Integers. Chapter Introduction

Game Theory and Algorithms Lecture 19: Nim & Impartial Combinatorial Games

1111: Linear Algebra I

Graph Theory: The Four Color Theorem

CSE 573 Problem Set 1. Answers on 10/17/08

2. Nine points are distributed around a circle in such a way that when all ( )

SOLUTIONS TO PROBLEM SET 5. Section 9.1

Harmonic numbers, Catalan s triangle and mesh patterns

THE ASSOCIATION OF MATHEMATICS TEACHERS OF NEW JERSEY 2018 ANNUAL WINTER CONFERENCE FOSTERING GROWTH MINDSETS IN EVERY MATH CLASSROOM

The Pythagorean Theorem

A Memory-Efficient Method for Fast Computation of Short 15-Puzzle Solutions

Unique Sequences Containing No k-term Arithmetic Progressions

17. Symmetries. Thus, the example above corresponds to the matrix: We shall now look at how permutations relate to trees.

LECTURE 7: POLYNOMIAL CONGRUENCES TO PRIME POWER MODULI

3.5 Marginal Distributions

((( ))) CS 19: Discrete Mathematics. Please feel free to ask questions! Getting into the mood. Pancakes With A Problem!

Dyck paths, standard Young tableaux, and pattern avoiding permutations

MAT Modular arithmetic and number theory. Modular arithmetic

How Many Mates Can a Latin Square Have?

VARIATIONS ON NARROW DOTS-AND-BOXES AND DOTS-AND-TRIANGLES

Lectures: Feb 27 + Mar 1 + Mar 3, 2017

Games on graphs. Keywords: positional game, Maker-Breaker, Avoider-Enforcer, probabilistic

Sorting. Suppose behind each door (indicated below) there are numbers placed in a random order and I ask you to find the number 41.

Practice Midterm 2 Solutions

A STUDY OF EULERIAN NUMBERS FOR PERMUTATIONS IN THE ALTERNATING GROUP

Lecture 18 - Counting

EXPLAINING THE SHAPE OF RSK

NIM Games: Handout 1

arxiv: v1 [math.co] 24 Nov 2018

NOTES ON SEPT 13-18, 2012

Modular arithmetic Math 2320

Fast Sorting and Pattern-Avoiding Permutations

Exam 1 7 = = 49 2 ( ) = = 7 ( ) =

Mechanism Design without Money II: House Allocation, Kidney Exchange, Stable Matching

MITOCW ocw lec11

arxiv: v1 [math.co] 30 Nov 2017

Directed Towers of Hanoi

Permutation Groups. Definition and Notation

Modular Arithmetic. Kieran Cooney - February 18, 2016

THE ENUMERATION OF PERMUTATIONS SORTABLE BY POP STACKS IN PARALLEL

Solutions for the Practice Final

PROOFS OF SOME BINOMIAL IDENTITIES USING THE METHOD OF LAST SQUARES

Closed Almost Knight s Tours on 2D and 3D Chessboards

SMT 2013 Advanced Topics Test Solutions February 2, 2013

PATTERN AVOIDANCE IN PERMUTATIONS ON THE BOOLEAN LATTICE

Transcription:

18.310 lecture notes September 2, 2013 Batcher s Algorithm Lecturer: Michel Goemans Perhaps the most restrictive version of the sorting problem requires not only no motion of the keys beyond compare-and-switches, but also that the plan of comparison-and-switches be fixed in advance. In each of the methods mentioned so far, the comparison to be made at any time often depends upon the result of previous comparisons. For example, in HeapSort, it appears at first glance that we are making only compare-and-switches between pairs of keys, but the comparisons we perform are not fixed in advance. Indeed when fixing a headless heap, we move either to the left child or to the right child depending on which child had the largest element; this is not fixed in advance. A sorting network is a fixed collection of comparison-switches, so that all comparisons and switches are between keys at locations that have been specified from the beginning. These comparisons are not dependent on what has happened before. The corresponding sorting algorithm is said to be non-adaptive. We will describe a simple recursive non-adaptive sorting procedure, named Batcher s Algorithm after its discoverer. It is simple and elegant but has the disadvantage that it requires on the order of n(log n) 2 comparisons. which is larger by a factor of the order of log n than the theoretical lower bound for comparison sorting. For a long time (ten years is a long time in this subject!) nobody knew if one could find a sorting network better than this one. Then Ajtai, Komlós, and Szemerédi proved the existence of very complex networks that can sort with cn log n comparisons for a very large constant c. Their procedure is extremely complicated, and for most practical purposes Batcher s algorithm is more useful. The idea behind Batcher s algorithm is the following claim (which at first glance looks incredible): If you sort the first half of a list, and sort the second half separately, and then sort the odd-indexed entries (first, third, fifth,...) and the even-indexed entries (second, fourth, sixth,...) separately, then you need make only one more comparison-switch per key to completely sort the list. We will prove this below. For the remainder of this section, we will assume that the length of our list, n, is a power of 2. Let s see what happens for a particular list of length 8. Suppose we are given the following list of numbers: 2 7 6 3 9 4 1 8 We wish to sort it from least to greatest. If we sort the first and second halves separately we obtain: 2 3 6 7 1 4 8 9 Sorting the odd-indexed keys (2, 6, 1, 8) and then the even-indexed keys (3, 7, 4, 9) while leaving them in odd and even places respectively yields: 1 3 2 4 6 7 8 9 This list is now almost sorted: doing a comparison switch between the keys in positions (2 and 3), (4 and 5) and (6 and 7) will in fact finish the sort. Batcher-1

This is no accident: given any list of length 8, if we sort the entries in the first half, then sort the entries in the second half, then sort the entries in odd positions, then sort the entries in even positions and lastly perform this same round of exchanges (second with third, fourth with fifth, and sixth with seventh), the list will end up sorted. Furthermore, and even more incredibly, the same fact holds for any list whose length is a multiple of 4 (as we shall see below); in that case, in the final step, we sort the 2l-th element with the (2l + 1)-st for l = 1, 2,, (n/2) 1. Theorem 1. For any list of length n, where n is a multiple of 4, first sorting separately the first and second halves, then sorting separately the odd-indexed keys and the even-indexed keys, and finally comparing-and-switching the keys indexed 2l and 2l + 1 for l = 1, 2,, (n/2) 1 results in a sorted list. Proof. After the two halves of the list have been sorted separately, it is obvious that for all i between 1 and n except for 1 and n 2 + 1, the (i 1)-st element of the list is less than the i-th. Call the (i 1)-st key the predecessor of the i-th. Note that 1 and n/2 + 1 are both odd. Every even-indexed key has as its predecessor a number smaller than itself (since any even-indexed key and its predecessor are in the same half), so the l-th smallest even-indexed key must be larger than at least l odd-indexed keys (look at its predecessor as well as the predecessors of the l 1 even-indexed keys that are smaller than it). Similarly, every odd-indexed key (with the possible exception of two keys, namely the 1st and ( n 2 + 1)-st) has as its predecessor a number smaller than itself, so the l + 1-st smallest odd-indexed key must be larger than at least l 1 even-indexed keys. If we denote by k i the i-th indexed key after sorting the first and second halves and the even and odd halves, we have just argued that and k 2l 1 k 2l k 2l 2 k 2l+1, for any appropriate l. Since we have sorted the even indexed keys and the odd indexed keys, we also know that k 2l 2 k 2l and that k 2l 1 k 2l+1. Thus, if we group the elements in pairs (k 2l, k 2l+1) for each appropriate l, we see that both elements of a pair are greater or equal to both elements of the previous pair. To finish the sort after sorting the odds and evens, it is therefore only necessary to compare the l + 1-st smallest odd-indexed key (k 2l+1 ) to the l-th smallest even-indexed key (k 2l ) for each appropriate l to determine its rank completely; this is precisely what the final step does. If we start with a list whose size is a power of two, we can use this idea to create longer and longer sorted lists by repeatedly sorting the first and second halves and then the odd and even entries followed by one additional round of comparison-switches. This task is slightly easier than it looks, as a result of a second observation: If you first sort the first and second halves, then the first and second halves of the odds and evens are already sorted. As a result, when sorting the odds and evens here, we only need to merge the two halves of the odd indexed keys and the two halves of the even indexed keys. We have shown how to merge 2 sorted lists of length p while performing only 2p 1 comparisons, but this requires an adaptive algorithm. Instead, Batcher s procedure proceeds recursively and has two different components: a sorting algorithm that sorts completely disordered lists and a merging algorithm that sorts lists whose first and second halves have already Batcher-2

been sorted. We will call these algorithms Sort and Merge. Let us emphasize that Merge only sorts if the first half and second half are already sorted. If not, it is unclear what Merge does. Our plan when executing Batcher s procedure is to successively merge smaller lists to get larger ones, but we use Batcher s merging algorithm rather than the simple merge, to make it non-adaptive. It follows that Batcher s Algorithm can be written in the following recursive form (provided n is a power of 2 larger than 2): Sort(x 1,..., x n ) calls: Sort(x 1,..., x n/2 ), then Sort(x n/2+1,..., x n ), and then Merge(x 1,..., x n ). Merge(x 1,..., x n ) calls: Merge(x i, for i odd), then Merge(x i for i even), and then Comp(x 2, x 3 ), Comp(x 4, x 5 ), Comp(x n 2, x n 1 ). Comp(x i, x j ) means: compare the key in the position i with the one in position j and put the larger one in position j, the smaller one in position i. In the definition of Merge, notice that if the first and second halves of (x 1,..., x n ) are already sorted then the first and second halves of both (x i, for i odd) and (x i for i even) are already sorted as well, and therefore will become sorted after their Merge calls. For the base case, we should say that Sort(x 1, x 2 ) = Merge(x 1, x 2 ) = Comp(x 1, x 2 ). We now turn to the question: how many steps does this algorithm take? Let S(n) denote the number of comparisons needed to sort n items, and let M(n) denote the number of comparisons needed to merge two sorted lists of n/2 items each. Then we have S(n) = 2S(n/2) + M(n), M(n) = 2M(n/2) + n/2 1 with the initial conditions S(2) = M(2) = 1. If we ignore the -1 term (which is certainly fine if we re aiming for an upper bound on S(n)), the second recurrence relation has the solution n M (n) = log n. 2 You can verify this by induction. Therefore M(n) n 2 log n. We can use this to show that S(n) n (log n) 2 2. We will see another way to count the number of comparisons below. We now examine the Batcher procedure from the bottom up rather from the top down as we have done so far. We have seen that the procedure for sorting n items reduces to two applications of the procedure for sorting n/2 items followed by two applications of the procedure for merging n/2 items followed by a single round of comparison-switches. The procedures for sorting-merging n/2 items similarly involve procedures for sorting-merging n/4 items, which involve procedure for sorting n/8 items, and so on, until we get down to sorting single pairs. From the opposite point of view, we start from an unsorted list, sort into pairs, then apply several rounds of these sorts to create sorted groups of size four, then sorted groups of size eight, and so on until we have sorted all Batcher-3

n keys. We can use this description of Batcher s Algorithm to deduce what comparison switches are involved in sorting 2 j keys in this way. We will describe rounds of comparisons; a round will be a set of comparison-switches which use each key at most once and hence can be performed simultaneously; this is for example useful for a parallel implementation of a sorting algorithm. The first step is to arrange odd-even adjacent pairs of keys into ordered pairs. This can be done in one round. (1, 2), (3, 4), (5, 6),... For brevity, we write (1, 2) for Comp(x 1, x 2 ). To sort into groups of four we then want to sort the odd and even-indexed keys into pairs by applying a similar round to the odd and even-indexed keys separately and in parallel; we then perform the final step for sorting into groups of four, by doing the comparisons (a, a + 1) for a 2 (mod 4). (1.3), (2, 4), (5, 7), (6, 8),... (2, 3), (6, 7),... Producing sorted groups of size eight from what we now have then involves repeating these last two rounds on odds and evens, and then doing the final round for sorting into groups of eight (see upcoming figure). (1, 5), (2, 6), (3, 7), (4, 8), (9, 13)... (3, 5), (4, 6), (11, 13), (12, 14),... (2, 3), (4, 5), (6, 7), (10, 11),... Each merging step here involves one more round of comparisons than the previous one, because we must repeat the previous round on odds and evens and add a final round. Producing sorted 8 s from sorted 4 s takes 3 rounds of comparisons (see upcoming figure); in general, getting sorted 2 k s from sorted 2 k 1 s takes k rounds of comparisons. To actually sort n = 2 k keys in this way involves making them into sorted 2 s, then sorted 4 s, then sorted 8 s, etc,. until one has sorted 2 k s. This takes 1 + 2 + 3 +... + k rounds of comparisons, which is k(k+1)/2 rounds. Since each round takes no more than n/2 comparisons and k = log n, we need approximately n(log n) 2 /4 comparisons in this algorithm. In fact, when n is sufficiently large, it can be shown that the number of comparisons required becomes strictly less than n(log n) 2 /4. We schematically represent Batcher s algorithm as a sorting network in the following way. We draw an horizontal line (wire) for each key to be sorted and the convention is that processing goes from left to right. A compare-and-switch is represented by a vertical line segment between the corresponding wires; the larger key is output on the top wire (to the right of the comparator, as processing goes from left to right) and the smaller key on the bottom wire. Inputs enter the sorting network on the left (with the ith key on the ith wire from the top) and outputs can be read on the right side. The comparisons made in each round of Batcher s algorithm are drawn closely to each other. Here is such a representation for Batcher s algorithm on 8 inputs; the color coding refers to sorting in groups of size 8 from groups of size 4. Batcher-4

3, 4. 5. 1. 2. Key: 1. Sort on first half. 2. Sort on second half. 3. Merge on odd keys. 4. Merge on even keys. 5. Final compare and switch of adjacent keys. Batcher s algorithm is not difficult to implement. If one can write recursive code, the initial description we gave can be coded directly. Otherwise, one can explicitly construct the rounds of comparisons that are to be made as described above. Batcher-5

MIT OpenCourseWare http://ocw.mit.edu 18.310 Principles of Discrete Applied Mathematics Fall 2013 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.