I. Sorting Networks Thomas Sauerwald
Total Page:16
File Type:pdf, Size:1020Kb
I. Sorting Networks Thomas Sauerwald Easter 2015 Outline Introduction to Sorting Networks Batcher’s Sorting Network Counting Networks Load Balancing on Graphs I. Sorting Networks Introduction to Sorting Networks 2 Overview: Sorting Networks (Serial) Sorting Algorithms we already know several (comparison-based) sorting algorithms: Insertion sort, Bubble sort, Merge sort, Quick sort, Heap sort execute one operation at a time can handle arbitrarily large inputs sequence of comparisons is not set in advance Sorting Networks only perform comparisons can only handle inputs of a fixed size sequence of comparisons is set in advance Allows to sort n numbers Comparisons can be performed in parallel in sublinear time! Simple concept, but surprisingly deep and complex theory! I. Sorting Networks Introduction to Sorting Networks 3 Comparison Networks A sorting network is a comparison network which Comparison Network works correctly (that is, it sorts every input) A comparison network consists solely of wires and comparators: comparator is a device with, on given two inputs, x and y, returns two operates in O(1) outputs x0 and y 0 wire connect output of one comparator to the input of another special wires: n input wires a1; a2;:::; an and n output wires b1; b2;:::; bn Convention:27.1 Comparison use networks the same name for both a wire and its value. 705 7 3 x x! min(x, y) x x ! min(x, y) comparator = = 3 7 y y! max(x, y) y y! max(x, y) = = (a) (b) Figure 27.1 (a) Acomparatorwithinputsx and y and outputs x! and y!. (b) The same comparator, drawn as a single vertical line. Inputs x 7, y 3andoutputsx 3, y 7areshown. = = ! = ! = Acomparisonnetworkiscomposedsolelyofwiresandcomparators. A compara- tor,showninFigure27.1(a),isadevicewithtwoinputs,I. Sorting Networks Introduction to Sorting Networks x and y,andtwooutputs,4 x! and y!,thatperformsthefollowingfunction: x! min(x, y), = y! max(x, y). = Because the pictorial representation of a comparator in Figure 27.1(a) is too bulky for our purposes, we shall adopt the convention of drawing comparators as single vertical lines, as shown in Figure 27.1(b). Inputs appear on the left and outputs on the right, with the smaller input value appearing on the top output and the larger input value appearing on the bottom output. We can thus think of a comparator as sorting its two inputs. We shall assume that each comparator operates in O(1) time. In other words, we assume that the time between the appearance of the input values x and y and the production of the output values x! and y! is a constant. A wire transmits a value from place to place. Wires can connect the output of one comparator to the input of another, but otherwise they are either network input wires or network output wires. Throughout this chapter, we shall assume that a comparison network contains n input wires a1, a2,...,an,throughwhich the values to be sorted enter the network, and n output wires b1, b2,...,bn,which produce the results computed by the network. Also, we shall speak of the input sequence a1, a2,...,an and the output sequence b1, b2,...,bn ,referringto the values" on the input and# output wires. That is, we use" the same name# for both a wire and the value it carries. Our intention will always be clear from the context. Figure 27.2 shows a comparison network,whichisasetofcomparatorsinter- connected by wires. We draw a comparison network on n inputs as a collection of n horizontal lines with comparators stretched vertically. Note that a line does not represent a single wire, but rather a sequence of distinct wires connecting vari- ous comparators. The top line in Figure 27.2, for example, represents three wires: input wire a1,whichconnectstoaninputofcomparatorA;awireconnectingthe top output of comparator A to an input of comparator C;andoutputwireb1,which comes from the top output of comparator C.Eachcomparatorinputisconnected X Interconnections between comparators must be acyclic 9 5 2 2 5 9 6 5 F D F 2 2 F 5 6 D 6 6 9 9 depth 0 1 1 2 2 3 TracingThis backnetwork a path is in must fact a neverMaximum sorting cycle network! depth back ofon an output Depth of a wire: itself and go through the samewire comparator equals total twice. running time Input wire has Depth 0 If a comparator has two inputs of depths dx and dy , then outputs have depth maxfdx ; dy g + 1 Example of a Comparison Network (Figure 27.2) A horizontal line represents a sequence of distinct wires a1 b1 A C a2 b2 E a3 b3 B D a4 b4 I. Sorting Networks Introduction to Sorting Networks 5 A horizontal line represents a sequence of distinctX wires 9 5 2 2 5 9 6 5 D F 2 2 F 5 6 D 6 6 9 9 depth 0 1 1 2 2 3 TracingThis backnetwork a path is in must fact a neverMaximum sorting cycle network! depth back ofon an output Depth of a wire: itself and go through the samewire comparator equals total twice. running time Input wire has Depth 0 If a comparator has two inputs of depths dx and dy , then outputs have depth maxfdx ; dy g + 1 Example of a Comparison Network (Figure 27.2) Interconnections between comparators must be acyclic a1 b1 A C a2 b2 F E a3 b3 B D a4 b4 I. Sorting Networks Introduction to Sorting Networks 5 A horizontal line represents a sequence of distinct wires 9 5 2 2 5 9 6 5 F D 2 2 F 5 6 D 6 6 9 9 depth 0 1 1 2 2 3 TracingThis backnetwork a path is in must fact a neverMaximum sorting cycle network! depth back ofon an output Depth of a wire: itself and go through the samewire comparator equals total twice. running time Input wire has Depth 0 If a comparator has two inputs of depths dx and dy , then outputs have depth maxfdx ; dy g + 1 Example of a Comparison Network (Figure 27.2) Interconnections between comparators must be acyclic X a1 b1 A C a2 b2 D F E a3 b3 B a4 b4 I. Sorting Networks Introduction to Sorting Networks 5 A horizontal line represents a sequence of distinctX wires 9 5 2 2 5 9 6 5 F D F 2 2 5 6 D 6 6 9 9 depth 0 1 1 2 2 3 This network is in fact aMaximum sorting network! depth of an output Depth of a wire: wire equals total running time Input wire has Depth 0 If a comparator has two inputs of depths dx and dy , then outputs have depth maxfdx ; dy g + 1 Example of a Comparison Network (Figure 27.2) Interconnections between comparators must be acyclic a1 b1 A C a2 b2 E F a3 b3 B D a4 b4 Tracing back a path must never cycle back on itself and go through the same comparator twice. I. Sorting Networks Introduction to Sorting Networks 5 X InterconnectionsA horizontal between line represents comparators a sequencemust be of acyclic distinct wires F D F F D depth 0 1 1 2 2 3 Tracing back a path must neverMaximum cycle depth back ofon an output Depth of a wire: itself and go through the samewire comparator equals total twice. running time Input wire has Depth 0 If a comparator has two inputs of depths dx and dy , then outputs have depth maxfdx ; dy g + 1 Example of a Comparison Network (Figure 27.2) 9 5 2 2 a1 b1 A C 5 9 6 5 a2 b2 E 2 2 5 6 a3 b3 B D 6 6 9 9 a4 b4 This network is in fact a sorting network! I. Sorting Networks Introduction to Sorting Networks 5 X InterconnectionsA horizontal between line represents comparators a sequencemust be of acyclic distinct wires F D F F D TracingThis backnetwork a path is in must fact a never sorting cycle network! back on itself and go through the same comparator twice. Example of a Comparison Network (Figure 27.2) 9 5 2 2 a1 b1 A C 5 9 6 5 a2 b2 E 2 2 5 6 a3 b3 B D 6 6 9 9 a4 b4 depth 0 1 1 2 2 3 Maximum depth of an output Depth of a wire: wire equals total running time Input wire has Depth 0 If a comparator has two inputs of depths dx and dy , then outputs have depth maxfdx ; dy g + 1 I. Sorting Networks Introduction to Sorting Networks 5 Zero-One Principle Zero-One Principle: A sorting networks works correctly on arbitrary in- puts if it works correctly on binary inputs. Lemma 27.1 If a comparison network transforms the input a = ha1; a2;:::; ani into the output b = hb1; b2;:::; bni, then for any monotonically increasing function f , the network transforms f (a) = hf (a1); f (a2);:::; f (an)i into f (b) = hf (b1); f (b2);:::; f (bn)i. 710 Chapter 27 Sorting Networks f (x) min( f (x), f (y)) f (min(x, y)) = f (y) max( f (x), f (y)) f (max(x, y)) = Figure 27.4 The operation of the comparator in the proof of Lemma 27.1. Thefunction f is monotonically increasing. To prove the claim, consider a comparator whose input values are x and y.The upper output of the comparator is min(x, y) and the lower output is max(x, y). I. Sorting Networks Introduction to Sorting Networks 6 Suppose we now apply f (x) and f (y) to the inputs of the comparator, as is shown in Figure 27.4.