The Pairwise Sorting Network



Similar documents
Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Fairness in Routing and Load Balancing

Math 55: Discrete Mathematics

IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS

Lecture 17 : Equivalence and Order Relations DRAFT

Closest Pair Problem

Lecture 1: Course overview, circuits, and formulas

Lecture 22: November 10

k, then n = p2α 1 1 pα k

Continued Fractions and the Euclidean Algorithm

8 Primes and Modular Arithmetic

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

Mathematical Induction. Lecture 10-11

MATH10040 Chapter 2: Prime and relatively prime numbers

Minimum cost maximum flow, Minimum cost circulation, Cost/Capacity scaling

Binary Heap Algorithms

INCIDENCE-BETWEENNESS GEOMETRY

Lecture 3: Finding integer solutions to systems of linear equations

3. Mathematical Induction

Converting a Number from Decimal to Binary

Analysis of Binary Search algorithm and Selection Sort algorithm

INDISTINGUISHABILITY OF ABSOLUTELY CONTINUOUS AND SINGULAR DISTRIBUTIONS

11 Multivariate Polynomials

Mathematical Induction

We can express this in decimal notation (in contrast to the underline notation we have been using) as follows: b + 90c = c + 10b

Computational Geometry. Lecture 1: Introduction and Convex Hulls

Math Workshop October 2010 Fractions and Repeating Decimals

Sample Induction Proofs

Reading 13 : Finite State Automata and Regular Expressions

On strong fairness in UNITY

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

A Brief Introduction to Property Testing

GRAPH THEORY LECTURE 4: TREES

MA4001 Engineering Mathematics 1 Lecture 10 Limits and Continuity

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Tolerating Multiple Faults in Multistage Interconnection Networks with Minimal Extra Stages

The Online Set Cover Problem

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Set theory as a foundation for mathematics

Regular Expressions with Nested Levels of Back Referencing Form a Hierarchy

Lecture 2: Universality

MATHEMATICAL INDUCTION. Mathematical Induction. This is a powerful method to prove properties of positive integers.

CS 598CSC: Combinatorial Optimization Lecture date: 2/4/2010

Stochastic Inventory Control

THE DEGREES OF BI-HYPERHYPERIMMUNE SETS

Victor Shoup Avi Rubin. Abstract

CS 103X: Discrete Structures Homework Assignment 3 Solutions

A Comparison of Parallel Sorting Algorithms on Different Architectures

Definition Given a graph G on n vertices, we define the following quantities:

the recursion-tree method

The LCA Problem Revisited


Midterm Practice Problems

Settling a Question about Pythagorean Triples

Graphs without proper subgraphs of minimum degree 3 and short cycles

Degrees that are not degrees of categoricity

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

CHAPTER 5 Round-off errors

Factoring & Primality

NP-Completeness and Cook s Theorem

Regular Expressions and Automata using Haskell

Termination Checking: Comparing Structural Recursion and Sized Types by Examples

8.1 Min Degree Spanning Tree

Today s Topics. Primes & Greatest Common Divisors

Automata and Formal Languages

Notes on Complexity Theory Last updated: August, Lecture 1

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

8 Divisibility and prime numbers

Real Roots of Univariate Polynomials with Real Coefficients

Data Streams A Tutorial

Ronald Graham: Laying the Foundations of Online Optimization

The Mixed Binary Euclid Algorithm

The Butterfly, Cube-Connected-Cycles and Benes Networks

Binary Search Trees CMPSC 122

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

The Relative Worst Order Ratio for On-Line Algorithms

Kevin James. MTHSC 412 Section 2.4 Prime Factors and Greatest Comm

Homework until Test #2

CONTINUED FRACTIONS AND PELL S EQUATION. Contents 1. Continued Fractions 1 2. Solution to Pell s Equation 9 References 12

Chapter 6. Cuboids. and. vol(conv(p ))

Competitive Analysis of On line Randomized Call Control in Cellular Networks

Regular Languages and Finite Automata

CMSC 858T: Randomized Algorithms Spring 2003 Handout 8: The Local Lemma

Connectivity and cuts

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Algorithms and Data Structures

Using Data-Oblivious Algorithms for Private Cloud Storage Access. Michael T. Goodrich Dept. of Computer Science

Transcription:

The Pairwise Sorting Network Ian Parberry Center for Research in Parallel and Distributed Computing Department of Computer Sciences University of North Texas Abstract A new sorting network with exactly the same size and depth as the odd-even sorting network is presented. This sorting network is designed using the zero-one principle, and proceeds by first sorting pairs of bits and sorting the pairs into lexicographic order. 1 Introduction Sorting networks are a popular model of comparison-based parallel sorting that has been studied for over two decades (for example, see Knuth [5], or Parberry [7]). Sorting networks with optimal size are known for n 8 (Knuth [5]), and sorting networks with optimal depth are known for n 10 (Parberry [8]). For n 16, Knuth [5] contains the sorting networks with best known size and depth. For all practical values of n>16, the best known sorting network is the odd-even sorting network of Batcher [3], which is constructed recursively and has depth (log n)(log n +1)/2 andsizen(log n)(log n 1)/4+n 1. For extremely large n (currently approximately 2 6100, Paterson [11]), the asymptotically optimal sorting network of Ajtai, Komlós and Szemerédi [1, 2] has superior size and depth, but this is unlikely to be of practical interest. We will construct a new sorting network, called the pairwise sorting network, thathas exactly the same size and depth as the odd-even sorting network. Sorting networks with depth O(log 2 n) are no longer remarkable; for example, an infinite number of O(log 2 n)depth sorting networks can be constructed using the k-ary version (Parker and Parberry [10]) of Columnsort (Leighton [6]). Other O(log 2 n) depth sorting networks are known, for example, the periodic balanced sorting network of Dowd et al. [4]. However, the pairwise sorting network differs from these in that it is the first sorting network to be competitive with oddeven sort for all values of n. The value of the pairwise sorting network is not that it is Research supported by NSF Grant CCR 8801659. Author s address: Department of Computer Sciences, University of North Texas, P.O. Box 13886, Denton, TX 76203-3886, U.S.A. Electronic mail: ian@ponder.csci.unt.edu. 1

Figure 1: The 16-input sorting network with best known depth. superior to the odd-even sorting network in any sense, but that it is the first serious rival to appear in over 20 years. The pairwise sorting network has a somewhat interesting structure, with comparators in the first log n layers of an n-input network comparing values which are successively increasing powers of two apart. It shares this property with the 16-input sorting network with the smallest known depth, shown in Figure 1. In our diagrams the values being sorted travel downwards along vertical lines to which comparators, depicted as horizontal lines, are attached. Heavy dots are used to emphasize the end-points of comparators. Unsorted data is presented at the top of the network and emerges sorted in nondecreasing order from left to right at the bottom. The remainder of this paper consists of three short sections. The first contains a highlevel description of the algorithm, the second contains more details, and the final section contains the analysis. 2

Step 1: Sort pairs internally x 1 x x x x 2 3 x 4... n-1 n Step 2: Sort pairs into lexicographic order Sort Sort Step 3: Sort Sorted Pairs... Figure 2: Recursive construction of the pairwise sorting network. 2 The Pairwise Sorting Network The pairwise sorting network is constructed recursively as follows (see Figure 2). The construction will be guided by the zero-one principle (see Knuth [5] or Parberry [7]), which states that it is sufficient to sort sequences of bits. To sort n bits where n is a power of 2: 1. Divide the input into pairs of bits, and sort each pair internally. 2. Sort the pairs into lexicographic order (that is, all 00 pairs before all 01 pairs before all 11 pairs). 3. Sort the sorted pairs into nondecreasing order. Step 1 is easy to achieve. Suppose we are given a string of n zeros and ones, x = (x 1,x 2,...,x n ), where n is a power of 2. Simply compare x 2i 1 with x 2i andswapthemif the former is greater than the latter, for 1 i n/2. Step 2 is also relatively easy. Recursively sort (x 1,x 3,...,x n 1 )and(x 2,x 4,...,x n )to give y =(y 1,...,y n/2 )andz =(z 1,...,z n/2 ) respectively. Then z has at least as many ones as y, and hence the string (y 1,z 1,y 2,z 2,...,y n/2,z n/2 ) consists of sorted pairs. Step 3 is slightly more complicated, and is the subject of the next section. 3 Sorting Sorted Pairs If A {0, 1} 2k is a string of k sorted pairs of bits, that is, A =(a 1,b 1,a 2,b 2,...,a k,b k ), 3

where a i =0for1 i p + m and 1 otherwise, b i =0for1 i p and 1 otherwise, where m 0, then A said to have prefix p, amplitude m, andsuffix k m p. We will without loss of generality assume k is a power of two. Suppose A is a string of k sorted pairs of bits with prefix p, suffixr, and amplitude r p +1 m, wherem is a power of 2. For 1 i k m/2, compare b i with a i+m/2 and swap them if the former is larger than the latter. We claim that the resulting string has amplitude at most m/2. If r p < m/2 then the comparisons have no effect; in this case the claimed result holds, since A has amplitude at most m/2. Otherwise m/2 r p m 1andb p,...,b r m/2 are compared to a p+m/2,...,a r respectively, where the former are all one and the latter are all zero. Suppose that the result of the comparisons is A =(a 1,b 1,a 2,b 2,...,a k,b k ). Then a i,b i =0for1 i<p, a i,b i =1forr<i k, and b i =0 forp i r m/2 b i =1 forr m/2 <i r a i =0 forp i<p+ m/2 =1 forp + m/2 i r. a i Since r p m 1, we know that r 1 <p+ m/2 andp + m/2 >r m/2. The former implies that A has prefix (r p +1 m/2)andthelatterimpliesthatithassuffix (r p +1 m/2). Therefore A has amplitude (r p +1) 2(r p +1 m/2) = (m 1) (r p) <m/2. Therefore, a string of k sorted pairs of bits can be sorted in depth d(k) =logk and size s(k) =k log k k + 1 by repeating the above procedure on an arbitrary string of sorted pairs with the maximal amplitude descending from k/2 i 1 to k/2 i with the ith application, for 1 i log k. Theith application uses k(1 1/2 i ) comparisons, which implies that the size of the network is given by: log k i=1 (k k )=k log k k +1. 2i Figure 3 illustrates the construction on 16 inputs. This method for sorting sorted pairs of bits may be useful in other contexts; for example, it is used to reduce the depth of the reduction in Parberry [9]. 4 Analysis It is of some small interest to ask whether the comparator network for sorting sorted pairs (Step 3 of the algorithm) has optimal size and depth. Lower bounds can easily be obtained from the standard lower bounds on the size and depth of merging networks, since the network for sorting sorted pairs can be used to make a merging network, as follows. Suppose we are given two sorted strings of zeros and ones, x =(x 1,...,x n )andy =(y 1,...,y n ). Compare x i with y i and swap them if the former is larger than the latter, for 1 i n. If x has at most as many ones as y, then there is no change. If y has less ones than x, then the net 4

Figure 3: A comparator network for sorting 8 sorted pairs of bits. effect is to swap x and y. Thus after the sequence of comparisons, x and y remain sorted and y must have at least as many ones as x. Therefore the string (x 1,y 1,x 2,y 2,...,x n,y n ) is an string of sorted pairs of bits. Since a network which merges two sorted sequences of n values requires at least 0.5n log n O(n) comparators (attributed to Floyd by Knuth [5]), we see that any network which sorts a sequence of sorted pairs of bits also requires at least 0.5n log n O(n) comparators. Thus the comparator network of the previous section has size larger than the optimum by at worst a factor of two. Since a standard argument based on the possible destinations of the smallest element in the right-most sorted subsequence will give a lower-bound of log n+1 on the depth of a network which merges two sorted sequences of n values, we see that any network which sorts a sequence of n sorted pairs of bits requires depth at least log n. Thus the comparator network of the previous section has optimal depth. By examination of the third layer one can show that the pairwise sorting network with 16 inputs (Figure 5) is not isomorphic to the 16 input odd-even sorting network (Figure 4), and hence one can prove by induction on n that the n input pairwise sorting network is not isomorphic to the odd-even sorting network. It is also easy to prove that the pairwise sorting network has the same size and depth bounds as Batcher s odd-even sorting network. Let D(n) be the depth of an n-input pairwise sorting network. Then D(2) = 1 and for n 2, D(n) = D(n/2) + d(n/2) + 1 = D(n/2) + log n. That is, D(n) =(log n)(log n + 1)/2 (proof by induction on n). Let S(n) be the size of an n-input pairwise sorting network. Then S(2) = 1 and for n>2, S(n) = 2S(n/2) + s(n/2) + n/2 = 2S(n/2) + n(log n 1)/2+1. That is, S(n) =n(log n)(log n 1)/4+n 1 (proof by induction on n). References [1] M. Ajtai, J. Komlós, and E. Szemerédi. An O(n log n) sorting network. Proc. 15th Ann. ACM Symp. on Theory of Computing, pages 1 9, April 1983. 5

Figure 4: The odd-even sorting network with 16 inputs. [2] M.Ajtai,J.Komlós, and E. Szemerédi. Sorting in c log n parallel steps. Combinatorica, 3:1 48, 1983. [3] K. E. Batcher. Sorting networks and their applications. In Proc. AFIPS Spring Joint Computer Conference, volume 32, pages 307 314, April 1968. [4] M. Dowd, Y. Perl, L. Rudolph, and M. Saks. The periodic balanced sorting network. J. Assoc. Comput. Mach., 36(4):738 757, October 1989. [5] D. E. Knuth. Sorting and Searching, volume 3 of The Art of Computer Programming. Addison-Wesley, 1973. [6] F. T. Leighton. Tight bounds on the complexity of parallel sorting. IEEE Transactions on Computers, C-34(4):344 354, April 1985. [7] I. Parberry. Parallel Complexity Theory. Research Notes in Theoretical Computer Science. Pitman Publishing, London, 1987. [8] I. Parberry. A computer-assisted optimal depth lower bound for nine-input sorting networks. Mathematical Systems Theory, 24:101 116, 1991. [9] I. Parberry. On the computational complexity of optimal sorting network verification. In Proceedings of The Conference on Parallel Architectures and Languages Europe, in 6

Figure 5: The pairwise sorting network with 16 inputs. Series Lecture Notes in Computer Science, volume 506, pages 252 269. Springer-Verlag, 1991. [10] B. Parker and I. Parberry. Constructing sorting networks from k-sorters. Information Processing Letters, 33(3):157 162, 1989. [11] M. S. Paterson. Improved sorting networks with O(log n) depth. Algorithmica, 5(4):75 92, 1990. 7