1 / 115

# Comparison Networks

Comparison Networks. Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions. Mergesort(array[1,…,n] of Integers): begin Mergesort(array[1,…,n/2]); Mergesort(array[n/2+1,…,n]); Merge(array[1,…,n/2], array[n/2+1,…,n]); end.

## Comparison Networks

E N D

### Presentation Transcript

1. Comparison Networks Sorting • Sorting binary values • Sorting arbitrary numbers • Implementing symmetric functions

2. Mergesort(array[1,…,n] of Integers): begin Mergesort(array[1,…,n/2]); Mergesort(array[n/2+1,…,n]); Merge(array[1,…,n/2], array[n/2+1,…,n]); end comparisons required to merge two arrays of size m/2 comparisons to sort n elements Sorting Algorithms Example Order of comparisons not fixed in advance. Not readily implementable in hardware.

3. C A D Sorting Network B B C A D Sorting Networks Order of comparisons fixed in advance. Readily implementable in hardware.

4. sorted Sorting Networks (binary values) inputs outputs 1 0 0 0 0 0 Sorting Network 1 0 0 1 0 1 1 1 1 1

5. Comparator (2-sorter) outputs inputs min(x, y) x C max(x, y) y

6. Comparator (2-sorter) outputs inputs min(x, y) x max(x, y) y

7. 1 0 0 0 0 1 0 0 width n 1 0 1 1 0 1 1 1 depth d Comparison Network

8. 1 0 0 0 0 1 0 0 1 0 1 1 0 1 1 1 d stages Comparison Network n / 2 comparisons per stage

9. Sorting Network Any ideas?

10. Sorting Network . . . n 1 . . . . . . n Sorting Network inputs outputs

11. Insertion Sort Network inputs outputs depth2n 3

12. Batcher Sorting Network Next Lecture

13. Sorting Arbitrary Numbers outputs inputs min(x, y) x max(x, y) y x, y can be values from any linearly ordered set, e.g., integers, reals, etc.

14. Comparison function: 1 if X > Y, C(X,Y) = 0 otherwise. Integer Comparator X, Y: integers represented as m-bit binary strings. Idea: use C(X,Y) to select the min and the max of X and Y.

15. 2 2 2 9 9 6 6 2 6 9 9 6 Sorting Arbitrary Numbers sorted

16. 1 1 1 1 5 4 4 5 4 5 5 4 Sorting Arbitrary Numbers sorted

17. 3 3 3 3 7 0 0 7 0 7 7 0 Sorting Arbitrary Numbers not sorted How can we verify if a network sorts all possible input sequences?

18. inputs outputs Sorting Arbitrary Numbers Try all possible 0/1 sequences.

19. inputs outputs 0 0 0 0 0 0 0 0 0 0 0 0 Sorting Arbitrary Numbers 000 000 Try all possible 0/1 sequences.

20. 0 0 0 0 0 0 0 0 1 1 1 1 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 Try all possible 0/1 sequences.

21. 0 0 0 0 1 0 0 1 0 1 1 0 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 Try all possible 0/1 sequences.

22. 0 0 0 0 1 1 1 1 1 1 1 1 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 Try all possible 0/1 sequences.

23. 0 0 0 1 1 0 0 0 0 1 1 0 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 100 001 Try all possible 0/1 sequences.

24. 0 0 0 1 1 1 1 0 1 1 1 1 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 100 001 101 011 Try all possible 0/1 sequences.

25. 1 1 1 1 1 0 0 1 0 1 1 0 not sorted! Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 100 001 101 011 110 101 Try all possible 0/1 sequences.

26. 1 1 1 1 1 1 1 1 1 1 1 1 not sorted! Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 100 001 101 011 110 101 111 111 Try all possible 0/1 sequences.

27. Sorting Arbitrary Numbers

28. inputs outputs Sorting Arbitrary Numbers Try all possible 0/1 sequences.

29. inputs outputs 0 0 0 0 0 0 0 0 0 0 0 0 Sorting Arbitrary Numbers 000 000 Try all possible 0/1 sequences.

30. 0 0 0 0 0 0 0 0 1 1 1 1 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 Try all possible 0/1 sequences.

31. 0 0 0 0 1 1 0 1 0 0 1 0 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 Try all possible 0/1 sequences.

32. 0 0 0 0 1 1 1 1 1 1 1 1 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 Try all possible 0/1 sequences.

33. 0 0 0 1 1 1 0 0 0 0 1 0 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 100 001 Try all possible 0/1 sequences.

34. 0 0 0 1 1 1 1 0 1 1 1 1 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 100 001 101 011 Try all possible 0/1 sequences.

35. 1 0 0 1 1 1 1 1 0 1 1 0 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 100 001 101 011 110 011 Try all possible 0/1 sequences.

36. 1 1 1 1 1 1 1 1 1 1 1 1 Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 100 001 101 011 110 011 111 111 Try all possible 0/1 sequences.

37. Sorting Arbitrary Numbers inputs outputs 000 000 001 001 010 001 011 011 100 001 101 011 110 011 111 111 all sorted! Try all possible 0/1 sequences.

38. Zero-One Principle If a comparison network sorts all possible sequences of 0’s and 1’s correctly, then it sorts all sequences ofarbitrary numbers correctly.

39. Lemma Given For a monotonically increasing function f,

40. Lemma Given For a monotonically increasing function f,

41. Proof: Lemma

42. Proof: Lemma

43. Proof: Lemma f is monotonically increasing:

44. Proof: Lemma f is monotonically increasing:

45. Proof: Lemma f is monotonically increasing:

46. Generalization Given

47. Generalization For a monotonically increasing function f, (by induction)

48. Suppose a) the network sorts all sequences of 0’s and 1’s, b) there exists a sequence that it doesn’t sort, i.e., such that but is placed before in the output. Define 0 if f (x) = 1 otherwise Proof: Zero-One Principle

49. . . . . . . Sorting Network . . . Proof: Zero-One Principle

50. . . . . . . Proof: Zero-One Principle Sorting Network . . .

More Related