Successfully reported this slideshow.

Sortingnetworks

1,459 views

Published on

Published in: Technology, Sports
  • Be the first to comment

Sortingnetworks

  1. 1. SORTING NETWORKS Presented by: Vikram S. Choudhary, JECRC, Jodhpur.
  2. 2. Overview <ul><li>Sorting Network Components </li></ul><ul><ul><li>Comparison Networks </li></ul></ul><ul><ul><ul><li>Comparator </li></ul></ul></ul><ul><ul><li>Zero-one Principle </li></ul></ul><ul><ul><li>Bitonic Sorting Network </li></ul></ul><ul><ul><ul><li>Half-Cleaner </li></ul></ul></ul><ul><ul><ul><li>Bitonic Sorter </li></ul></ul></ul><ul><ul><li>Merging Network </li></ul></ul><ul><li>Sorting Network </li></ul>
  3. 3. Comparison Networks <ul><li>Sorting networks are Comparison Networks that always sort their inputs </li></ul><ul><li>Comparison Network composed of: </li></ul><ul><ul><li>wires </li></ul></ul><ul><ul><li>comparators </li></ul></ul><ul><li>Wires: transmits values from place to place </li></ul><ul><li>Comparators: two inputs  two outputs </li></ul>
  4. 4. Comparison Networks <ul><li>Comparators: </li></ul><ul><ul><li>inputs: x and y </li></ul></ul><ul><ul><li>outputs: x’ and y’ </li></ul></ul><ul><ul><li>x’ = min(x, y) </li></ul></ul><ul><ul><li>y’ = max(x, y) </li></ul></ul><ul><ul><li>input output </li></ul></ul><ul><ul><li>x   x’ = min(x, y) </li></ul></ul><ul><ul><li>y   y’ = max(x, y) </li></ul></ul><ul><ul><li>x ------------------------------ x’ = min(x, y) </li></ul></ul><ul><ul><li>y ------------------------------ y’ = max(x, y) </li></ul></ul>comparator
  5. 5. Comparison Networks <ul><li>Comparators </li></ul><ul><li>n input wires: a 1 , a 2 , …, a n </li></ul><ul><ul><li>input sequence: < a 1 , a 2 , …, a n > </li></ul></ul><ul><li>n output wires: b 1 , b 2 , …, b n </li></ul><ul><ul><li>output sequence: <b 1 , b 2 , …, b n > </li></ul></ul><ul><li>Properties: </li></ul><ul><ul><li>requirement: graph is acyclic </li></ul></ul><ul><ul><li>output produced only when input is available </li></ul></ul><ul><ul><li>comparators process in parallel if input is available </li></ul></ul>
  6. 6. Comparison Networks <ul><li>comparators </li></ul><ul><li>a1 9 ----------------------------------------- b1 </li></ul><ul><li>a2 5 ----------------------------------------- b2 </li></ul><ul><li>a3 2 ----------------------------------------- b3 </li></ul><ul><li>a4 6 ----------------------------------------- b4 </li></ul><ul><li>values </li></ul>A B C D E
  7. 7. Comparison Networks <ul><li>outputs </li></ul><ul><li>a1 9 ----------------------------------------- 2 b1 </li></ul><ul><li>a2 5 ----------------------------------------- 5 b2 </li></ul><ul><li>a3 2 ----------------------------------------- 6 b3 </li></ul><ul><li>a4 6 ----------------------------------------- 9 b4 </li></ul><ul><li>depth: 1 1 2 2 3 </li></ul><ul><li>depth starts at 0 </li></ul><ul><li>input wires depth: d x and d y </li></ul><ul><li>output wires depth: max(d x , d y ) + 1 </li></ul>A B C D E 5 9 2 6 2 6 5 9
  8. 8. Comparison Networks <ul><li>Sorting Network is a Comparison Network where the output is sorted </li></ul><ul><ul><li>output sequence is monotonically increasing </li></ul></ul><ul><ul><li>(b 1 <= b 2 …<= b n ) for every input sequence </li></ul></ul><ul><li>Not all Comparison Networks are Sorting Networks </li></ul><ul><li>Comparison Network is like a procedure in that it specifies how comparisons are to occur </li></ul>
  9. 9. Zero-One Principle <ul><li>If a Sorting Network works correctly when each input is drawn from the set {0, 1}, then it works correctly on arbitrary input numbers </li></ul><ul><ul><li>(integers, reals, or values from any linearly sorted set) </li></ul></ul>
  10. 10. Zero-One Principle <ul><li>Lemma 27.1 </li></ul><ul><li>If a Comparison Network transforms the input sequence a = <a 1 , a 2 , …, a n > into the output sequence b = <b 1 , b 2 , …, b n >, then for any monotonically increasing function f, the network transforms the input sequence: </li></ul><ul><li>f(a) = <f(a 1 ), f(a 2 ), …, f(a n )> </li></ul><ul><li>into the output sequence: </li></ul><ul><li>f(b) = <f(b 1 ), f(b 2 ), …, f(b n )> </li></ul>
  11. 11. Zero-One Principle <ul><li>Proof: </li></ul><ul><ul><li>prove claim: if f is a monotonically increasing function, then a single comparator with inputs f(x) and f(y) produces output f(min(x, y)) and f(max(x, y)) </li></ul></ul><ul><ul><li>f(x) ------------------------------ min(f(x), f(y)) </li></ul></ul><ul><ul><li>f(y) ------------------------------ max(f(x), f(y)) </li></ul></ul><ul><ul><li>min(f(x), f(y)) = f(min(x, y)) </li></ul></ul><ul><ul><li>max(f(x), f(y)) = f(max(x, y)) </li></ul></ul>
  12. 12. Zero-One Principle <ul><li>outputs </li></ul><ul><li>f(x) = ceiling(x/2) </li></ul><ul><li>a1 9 ----------------------------------------- 1 b1 </li></ul><ul><li>a2 5 ----------------------------------------- 3 b2 </li></ul><ul><li>a3 2 ----------------------------------------- 3 b3 </li></ul><ul><li>a4 6 ----------------------------------------- 5 b4 </li></ul>A B C D E 3 5 1 3 1 3 3 5
  13. 13. Zero-One Principle <ul><li>Theorem 27.2 (Zero-One Principle) </li></ul><ul><li>If a Comparison Network with n inputs sorts all 2 n possible sequences of 0’s and 1’s correctly, then it sorts all sequences of arbitrary numbers correctly. </li></ul>
  14. 14. Zero-One Principle <ul><li>Proof: </li></ul><ul><li>By contradiction: </li></ul><ul><li>Suppose that the network sorts all zero-one sequences, but there exists a sequence of arbitrary numbers that the network does not sort: </li></ul><ul><li><a 1 , a 2 , …, a n > contains elements a i and a j </li></ul><ul><li>where a i < a j </li></ul><ul><li>but the network places a j before a i in the output sequence </li></ul>
  15. 15. Zero-One Principle <ul><li>Define monotonically increasing function f: </li></ul><ul><li>f(x) = { 0 if x <= a i , </li></ul><ul><li>1 if x > a i . </li></ul><ul><li>a j is placed before a i in the output sequence </li></ul><ul><li>f(a j ) placed before f(a i ) </li></ul><ul><li>when input is <a 1 , a 2 , …, a n > </li></ul><ul><li>since f(a j ) = 1 and f(a i ) = 0, from above we have a contradiction </li></ul>
  16. 16. Bitonic Sorting Network <ul><li>Bitonic Sequence: </li></ul><ul><li>a sequence that monotonically increases and then decreases, or can be circularly shifted to become monotonically increasing and then monotonically decreasing </li></ul><ul><li>examples: </li></ul><ul><li><1, 4, 6, 8, 3, 2>, < 6, 9, 4, 2, 3, 5>, and </li></ul><ul><li><9, 8, 3, 2, 4, 6> </li></ul><ul><li>zero-one: 0 i 1 j 0 k or 1 i 0 j 1 k when i,j,k >= 0 </li></ul>
  17. 17. Bitonic Sorting Network <ul><li>Half-Cleaner </li></ul><ul><ul><li>a bitonic sorter is composed of several stages, each of which is called a half-cleaner </li></ul></ul><ul><ul><li>half-cleaner is a Comparison Network of depth 1 </li></ul></ul><ul><ul><li>line i compared with i + n/2 for i = 1, 2,…, n/2 </li></ul></ul><ul><ul><li>assume that n is even </li></ul></ul>
  18. 18. Bitonic Sorting Network <ul><li>Example: Half-Cleaner[8] all 1’s or 0’s </li></ul><ul><li>0 ---------------------------------- 0 </li></ul><ul><li>0 ---------------------------------- 0 bitonic </li></ul><ul><li>1 ---------------------------------- 0 clean </li></ul><ul><li>bitonic 1 ---------------------------------- 0 ________ </li></ul><ul><li>1 ---------------------------------- 1 </li></ul><ul><li>0 ---------------------------------- 0 bitonic </li></ul><ul><li>0 ---------------------------------- 1 </li></ul><ul><li>0 ---------------------------------- 1 </li></ul>
  19. 19. Bitonic Sorting Network <ul><li>Lemma 27.3 </li></ul><ul><li>If the input to a half-cleaner is a bitonic sequence of 0’s and 1’s, then the output satisfies the following properties: </li></ul><ul><ul><li>both the top half and the bottom half are bitonic </li></ul></ul><ul><ul><li>every element in the top half is at least as small as every element in the bottom half </li></ul></ul><ul><ul><li>at least one half is clean (either all 0’s or 1’s) </li></ul></ul>
  20. 20. Bitonic Sorting Network <ul><li>Proof: </li></ul><ul><ul><li>the Comparison Network Half-Cleaner[n] compares inputs i and i + n/2 for i = 1, 2, … n/2 </li></ul></ul><ul><ul><li>suppose input is of the form: </li></ul></ul><ul><ul><li>00…011…100…0 </li></ul></ul><ul><ul><li>there are 3 possible cases for the n/2 midpoint to fall </li></ul></ul><ul><ul><li>and the situation where the midpoint falls in a block of 1’s is separated into 2 cases </li></ul></ul><ul><ul><li>So 4 cases in total </li></ul></ul>
  21. 21. Bitonic Sorting Network <ul><li>Cases: </li></ul><ul><li>bitonic </li></ul><ul><li>n/2 </li></ul><ul><li>bitonic </li></ul><ul><li>n/2 </li></ul>0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0 top top top top bottom bottom bottom bottom bitonic clean bitonic clean bitonic bitonic
  22. 22. Bitonic Sorting Network <ul><li>Cases: </li></ul><ul><li>bitonic </li></ul><ul><li>n/2 </li></ul><ul><li>bitonic </li></ul><ul><li>n/2 </li></ul>0 0 0 1 0 0 1 0 1 0 0 0 0 1 0 1 1 0 0 0 1 0 top top top top bottom bottom bottom bottom bitonic clean bitonic clean bitonic bitonic 0 1 1 1 1 1 1 1
  23. 23. Bitonic Sorting Network <ul><li>Bitonic Sorter </li></ul><ul><li>by recursively combining half-cleaners, we can build a bitonic sorter, a network that sorts bitonic sequences </li></ul><ul><ul><li>first stage: Half-Cleaner[n] </li></ul></ul><ul><ul><li>subsequent sorts with Bitonic-Sorter[n/2] </li></ul></ul><ul><li>Depth D(n) of Bitonic-Sorter[n]: </li></ul><ul><li>D(n) = { 0 if n =1, </li></ul><ul><li>D(n/2) + 1 if n = 2 k and k>= 1 </li></ul>
  24. 24. Bitonic Sorting Network <ul><li>Example: </li></ul>Half-Cleaner[n] Bitonic- Sorter[n/2] Bitonic- Sorter[n/2]
  25. 25. Bitonic Sorting Network <ul><li>Example: </li></ul>0 0 1 1 1 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 1 1 0 0 0 0 0 1 1 1 bitonic sorted
  26. 26. Bitonic Sorting Network <ul><li>Bitonic-Sorter can be used to sort a zero-one sequence </li></ul><ul><li>By the Zero-One principle it follows that any bitonic sequence of arbitrary numbers can be sorted using this network </li></ul>
  27. 27. A Merging Network <ul><li>Networks that can merge two sorted input sequences into one sorted output sequence </li></ul><ul><li>The merging network is based on the following </li></ul><ul><ul><li>Given two sorted sequences, if we reverse the second sequence and then concatenate the two, the resulting sequence is bitonic. </li></ul></ul><ul><ul><li>ie. </li></ul></ul><ul><ul><li>given : X = 00001111 and Y = 000011111 </li></ul></ul><ul><ul><li>Y R = 111110000 </li></ul></ul><ul><ul><li>X● Y R = 00001111111110000 </li></ul></ul>
  28. 28. A Merging Network <ul><li>We can construct M ERGER [n] by modifying the first half cleaner of B ITONIC -S ORTER [n]. </li></ul><ul><li>The key is to perform the reversal of the second half of the inputs implicitly. </li></ul><ul><li>Given two sorted sequences <a 1 , a 2 , …, a n/2 > and <a n/2+1 , a n/2+2 , …, a n > </li></ul><ul><li>Want the effect of bitonically sorting the sequence < a 1 , a 2 , …, a n/2, a n, a n-1, a n-2, ..., a n/2+1 > </li></ul>
  29. 29. A Merging Network <ul><li>B ITONIC -S ORTER [n] compares inputs i and n /2+ i for i = 1,2,3,…, n /2. </li></ul><ul><li>a 1 0 ---------------------------------- 0 b 1 </li></ul><ul><li>a 2 0 ---------------------------------- 0 b 2 bitonic </li></ul><ul><li>a 3 1 ---------------------------------- 0 b 3 clean </li></ul><ul><li>bitonic a 4 1 ---------------------------------- 0 b 4 </li></ul><ul><li>a 8 1 ---------------------------------- 1 b 8 </li></ul><ul><li>a 7 0 ---------------------------------- 0 b 7 bitonic </li></ul><ul><li>a 6 0 ---------------------------------- 1 b 6 </li></ul><ul><li>a 5 0 ---------------------------------- 1 b 5 </li></ul>
  30. 30. A Merging Network <ul><li>First stage of the merging network compares inputs i and n /2 + i for i = 1,2,3,…, n /2. </li></ul><ul><li>a 1 0 ---------------------------------- 0 b 1 </li></ul><ul><li>sorted a 2 0 ---------------------------------- 0 b 2 bitonic </li></ul><ul><li>a 3 1 ---------------------------------- 0 b 3 clean </li></ul><ul><li>a 4 1 ---------------------------------- 0 b 4 </li></ul><ul><li>a 5 0 ---------------------------------- 1 b 5 </li></ul><ul><li>sorted a 6 0 ---------------------------------- 1 b 6 bitonic </li></ul><ul><li>a 7 0 ---------------------------------- 0 b 7 </li></ul><ul><li>a 8 1 ---------------------------------- 1 b 8 </li></ul>
  31. 31. A Merging Network <ul><li>Since the reversal of a bitonic sequence is bitonic sequence. </li></ul><ul><li>Both top and bottom outputs from the first stage of the merging network satisfy the properties of Lemma 27.3 </li></ul><ul><ul><li>both the top half and the bottom half are bitonic </li></ul></ul><ul><ul><li>every element in the top half is at least as small as every element in the bottom half </li></ul></ul><ul><ul><li>at least one half is clean (either all 0’s or 1’s) </li></ul></ul>
  32. 32. A Merging Network <ul><li>The top and bottom outputs can be bitonically sorted in parallel to produce the sorted output of the merging network. </li></ul>
  33. 33. A Merging Network Bitonic- Sorter[n/2] Bitonic- Sorter[n/2]
  34. 34. A Merging Network 1 0 0 1 0 0 1 1 0 1 1 0 1 0 1 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 1 sorted sorted sorted
  35. 35. A Merging Network <ul><li>First stage is different </li></ul><ul><li>Consequently the depth is the same as B ITONIC -S ORTER [n] </li></ul><ul><li>Depth D(n) of M ERGER [n]: </li></ul><ul><li>D(n) = { 0 if n =1, </li></ul><ul><li>D(n/2) + 1 if n = 2 k and k>= 1 </li></ul><ul><li>D(n) = lg n </li></ul>
  36. 36. A Sorting Network <ul><li>Now we have all the necessary tools to construct a network that can sort any input sequence </li></ul><ul><li>We are going to use the a fore mentioned merging network to implement a parallel version of merge sort, called S ORTER [n] </li></ul>
  37. 37. A Sorting Network Sorter[n/2] Sorter[n/2] Merger[n]
  38. 38. A Sorting Network Merger[4] Merger[4] Merger[8] Merger[2] Merger[2] Merger[2] Merger[2]
  39. 39. A Sorting Network 1 0 1 0 1 0 0 0 0 0 1 1 0 0 0 1 0 1 0 0 0 0 1 1 0 1 0 1 0 1 0 0 1 depth 2 2 3 4 4 4 4 5 5 6
  40. 40. A Sorting Network
  41. 41. Conclusion <ul><li>Sorting Network Components </li></ul><ul><ul><li>Comparison Networks </li></ul></ul><ul><ul><li>Zero-one Principle </li></ul></ul><ul><ul><li>Bitonic Sorting Network </li></ul></ul><ul><ul><ul><li>Half-Cleaner </li></ul></ul></ul><ul><ul><ul><li>Bitonic Sorter </li></ul></ul></ul><ul><ul><li>Merging Network </li></ul></ul><ul><li>Sorting Networks </li></ul>

×