sorting networks
play

Sorting networks Lecture 24 November 19, 2015 Sariel (UIUC) New - PowerPoint PPT Presentation

NEW CS 473: Theory II, Fall 2015 Sorting networks Lecture 24 November 19, 2015 Sariel (UIUC) New CS473 1 Fall 2015 1 / 35 24.1: Model of Computation Sariel (UIUC) New CS473 2 Fall 2015 2 / 35 Model of Computation Q: Perform a


  1. What is going on? Faster algorithm achieved by changing the computation model. 1 allowed new “strange” operations 2 (cutting a piece of pasta into a certain length, picking the longest one in constant time, and measuring the length of a pasta piece in constant time) Using these operations we can sort in linear time. 3 So, are there other useful computation models? 4 Sariel (UIUC) New CS473 6 Fall 2015 6 / 35

  2. What is going on? Faster algorithm achieved by changing the computation model. 1 allowed new “strange” operations 2 (cutting a piece of pasta into a certain length, picking the longest one in constant time, and measuring the length of a pasta piece in constant time) Using these operations we can sort in linear time. 3 So, are there other useful computation models? 4 Sariel (UIUC) New CS473 6 Fall 2015 6 / 35

  3. Circuits are fast... Computing the following circuit naively takes 1 8 units of time. Use parallelism! 2 Circuits are really parallel... 3 Sorting numbers with circuits? 4 Q: Can sort in sublinear time by allowing parallel comparisons? 5 Sariel (UIUC) New CS473 7 Fall 2015 7 / 35

  4. Circuits are fast... Computing the following circuit naively takes 1 8 units of time. Use parallelism! 2 Circuits are really parallel... 3 Sorting numbers with circuits? 4 Q: Can sort in sublinear time by allowing parallel comparisons? 5 Sariel (UIUC) New CS473 7 Fall 2015 7 / 35

  5. Circuits are fast... Computing the following circuit naively takes 1 8 units of time. Use parallelism! 2 Circuits are really parallel... 3 Sorting numbers with circuits? 4 Q: Can sort in sublinear time by allowing parallel comparisons? 5 Sariel (UIUC) New CS473 7 Fall 2015 7 / 35

  6. Circuits are fast... Computing the following circuit naively takes 1 8 units of time. Use parallelism! 2 4 time units! Circuits are really parallel... 3 Sorting numbers with circuits? 4 Q: Can sort in sublinear time by allowing parallel comparisons? 5 Sariel (UIUC) New CS473 7 Fall 2015 7 / 35

  7. Circuits are fast... Computing the following circuit naively takes 1 8 units of time. Use parallelism! 2 4 time units! Circuits are really parallel... 3 Sorting numbers with circuits? 4 Q: Can sort in sublinear time by allowing parallel comparisons? 5 Sariel (UIUC) New CS473 7 Fall 2015 7 / 35

  8. Circuits are fast... Computing the following circuit naively takes 1 8 units of time. Use parallelism! 2 4 time units! Circuits are really parallel... 3 Sorting numbers with circuits? 4 Q: Can sort in sublinear time by allowing parallel comparisons? 5 Sariel (UIUC) New CS473 7 Fall 2015 7 / 35

  9. 24.2: Sorting with a circuit – a naive solution Sariel (UIUC) New CS473 8 Fall 2015 8 / 35

  10. Sorting with a circuit – a naive solution comparator gate: 1 x ′ = min( x, y ) x Comparator y y ′ = max( x, y ) Draw it as: 2 Sariel (UIUC) New CS473 9 Fall 2015 9 / 35

  11. Sorting with a circuit – a naive solution comparator gate: 1 x ′ = min( x, y ) x Comparator y y ′ = max( x, y ) Draw it as: 2 Sariel (UIUC) New CS473 9 Fall 2015 9 / 35

  12. Sorting with a circuit – a naive solution comparator gate: 1 x ′ = min( x, y ) x Comparator y y ′ = max( x, y ) Draw it as: 2 x ′ = min( x, y ) x y ′ = max( x, y ) y Sariel (UIUC) New CS473 9 Fall 2015 9 / 35

  13. Sorting network - an example Sariel (UIUC) New CS473 10 Fall 2015 10 / 35

  14. How to draw a circuit... 1 wires : horizontal lines 2 gates : vertical segments (i.e., gates) connecting lines. Inputs arrive the wires from left. 3 Output on the right side of wires. 4 largest number is output on the 5 bottom line. Sorting algorithms = ⇒ sorting 6 circuits. Sariel (UIUC) New CS473 11 Fall 2015 11 / 35

  15. How to draw a circuit... 1 wires : horizontal lines 2 gates : vertical segments (i.e., gates) connecting lines. Inputs arrive the wires from left. 3 Output on the right side of wires. 4 largest number is output on the 5 bottom line. Sorting algorithms = ⇒ sorting 6 circuits. Sariel (UIUC) New CS473 11 Fall 2015 11 / 35

  16. How to draw a circuit... 1 wires : horizontal lines 2 gates : vertical segments (i.e., gates) connecting lines. Inputs arrive the wires from left. 3 Output on the right side of wires. 4 largest number is output on the 5 bottom line. Sorting algorithms = ⇒ sorting 6 circuits. Sariel (UIUC) New CS473 11 Fall 2015 11 / 35

  17. How to draw a circuit... 1 wires : horizontal lines 2 gates : vertical segments (i.e., gates) connecting lines. Inputs arrive the wires from left. 3 Output on the right side of wires. 4 largest number is output on the 5 bottom line. Sorting algorithms = ⇒ sorting 6 circuits. Sariel (UIUC) New CS473 11 Fall 2015 11 / 35

  18. How to draw a circuit... 1 wires : horizontal lines 2 gates : vertical segments (i.e., gates) connecting lines. Inputs arrive the wires from left. 3 Output on the right side of wires. 4 largest number is output on the 5 bottom line. Sorting algorithms = ⇒ sorting 6 circuits. Sariel (UIUC) New CS473 11 Fall 2015 11 / 35

  19. How to draw a circuit... 1 wires : horizontal lines 2 gates : vertical segments (i.e., gates) connecting lines. Inputs arrive the wires from left. 3 Output on the right side of wires. 4 largest number is output on the 5 bottom line. Sorting algorithms = ⇒ sorting 6 circuits. Sariel (UIUC) New CS473 11 Fall 2015 11 / 35

  20. Definitions Definition A comparison network is a DAG , with n inputs and n outputs, where each gate has two inputs and two outputs. Definition depth of a wire is 0 at input. For gate with two inputs of depth d 1 and d 2 the depth on the output wire is 1 + max( d 1 , d 2 ) . depth of comparison network is maximum depth of an output wire. Definition sorting network : comparison network such that for any input, the output is monotonically sorted. size : sorting network is number of gates. running time of sorting network is its depth. Sariel (UIUC) New CS473 12 Fall 2015 12 / 35

  21. Definitions Definition A comparison network is a DAG , with n inputs and n outputs, where each gate has two inputs and two outputs. Definition depth of a wire is 0 at input. For gate with two inputs of depth d 1 and d 2 the depth on the output wire is 1 + max( d 1 , d 2 ) . depth of comparison network is maximum depth of an output wire. Definition sorting network : comparison network such that for any input, the output is monotonically sorted. size : sorting network is number of gates. running time of sorting network is its depth. Sariel (UIUC) New CS473 12 Fall 2015 12 / 35

  22. Definitions Definition A comparison network is a DAG , with n inputs and n outputs, where each gate has two inputs and two outputs. Definition depth of a wire is 0 at input. For gate with two inputs of depth d 1 and d 2 the depth on the output wire is 1 + max( d 1 , d 2 ) . depth of comparison network is maximum depth of an output wire. Definition sorting network : comparison network such that for any input, the output is monotonically sorted. size : sorting network is number of gates. running time of sorting network is its depth. Sariel (UIUC) New CS473 12 Fall 2015 12 / 35

  23. Sorting network based on insertion sort Inner loop of insertion sort is: 1 Insertion sort as a network: 2 Sariel (UIUC) New CS473 13 Fall 2015 13 / 35

  24. Sorting network based on insertion sort Inner loop of insertion sort is: 1 Insertion sort as a network: 2 Sariel (UIUC) New CS473 13 Fall 2015 13 / 35

  25. Sorting network based on insertion sort 5 3 7 4 6 9 1 2 8 (i) (ii) Lemma The sorting network based on insertion sort has O ( n 2 ) gates, and requires 2 n − 1 time units to sort n numbers. Sariel (UIUC) New CS473 14 Fall 2015 14 / 35

  26. Sorting network based on insertion sort 5 3 7 4 6 9 1 2 8 (i) (ii) Lemma The sorting network based on insertion sort has O ( n 2 ) gates, and requires 2 n − 1 time units to sort n numbers. Sariel (UIUC) New CS473 14 Fall 2015 14 / 35

  27. 24.3: The Zero-One Principle Sariel (UIUC) New CS473 15 Fall 2015 15 / 35

  28. Converting a sequence into a binary sequence 10 8 6 4 2 0 A B C D E F G H I x Sariel (UIUC) New CS473 16 Fall 2015 16 / 35

  29. Converting a sequence into a binary sequence 10 8 6 4 2 0 A B C D E F G H I x Sariel (UIUC) New CS473 16 Fall 2015 16 / 35

  30. Converting a sequence into a binary sequence 10 8 6 4 2 0 A B C D E F G H I x Sariel (UIUC) New CS473 16 Fall 2015 16 / 35

  31. Converting a sequence into a binary sequence Sariel (UIUC) New CS473 16 Fall 2015 16 / 35

  32. Converting a sequence into a binary sequence 0 0 0 0 0 1 1 1 1 Sariel (UIUC) New CS473 16 Fall 2015 16 / 35

  33. 24.3.1: The zero-one principle Sariel (UIUC) New CS473 17 Fall 2015 17 / 35

  34. The Zero-One Principle Definition zero-one principle states that if a comparison network sort correctly all binary inputs ( ∀ input is 0 or 1) then it sorts correctly all inputs (input is real number). Need to prove the zero-one principle. Lemma A comparison network transforms input sequence a = � a 1 , a 2 , . . . , a n � = ⇒ b = � b 1 , b 2 , . . . , b n � Then for any monotonically increasing function f , the network transforms � � � � f ( a ) = f ( a 1 ) , . . . , f ( a n ) = ⇒ f ( b ) = f ( b 1 ) , . . . , f ( b n ) Sariel (UIUC) New CS473 18 Fall 2015 18 / 35

  35. The Zero-One Principle Definition zero-one principle states that if a comparison network sort correctly all binary inputs ( ∀ input is 0 or 1) then it sorts correctly all inputs (input is real number). Need to prove the zero-one principle. Lemma A comparison network transforms input sequence a = � a 1 , a 2 , . . . , a n � = ⇒ b = � b 1 , b 2 , . . . , b n � Then for any monotonically increasing function f , the network transforms � � � � f ( a ) = f ( a 1 ) , . . . , f ( a n ) = ⇒ f ( b ) = f ( b 1 ) , . . . , f ( b n ) Sariel (UIUC) New CS473 18 Fall 2015 18 / 35

  36. The Zero-One Principle Definition zero-one principle states that if a comparison network sort correctly all binary inputs ( ∀ input is 0 or 1) then it sorts correctly all inputs (input is real number). Need to prove the zero-one principle. Lemma A comparison network transforms input sequence a = � a 1 , a 2 , . . . , a n � = ⇒ b = � b 1 , b 2 , . . . , b n � Then for any monotonically increasing function f , the network transforms � � � � f ( a ) = f ( a 1 ) , . . . , f ( a n ) = ⇒ f ( b ) = f ( b 1 ) , . . . , f ( b n ) Sariel (UIUC) New CS473 18 Fall 2015 18 / 35

  37. Proof Induction on number of comparators. 1 Consider a comparator with inputs x and y , and outputs 2 x ′ = min( x, y ) and y ′ = max( x, y ) . If f ( x ) = f ( y ) then the claim trivially holds. 3 If f ( x ) < f ( y ) then clearly 4 max( f ( x ) , f ( y )) = f (max( x, y )) and min( f ( x ) , f ( y )) = f (min( x, y )) , since f ( · ) is monotonically increasing. � x, y � , for x < y , we have output � x, y � . 5 Input: � f ( x ) , f ( y ) � = ⇒ output is � f ( x ) , f ( y ) � . 6 Similarly, if x > y , the output is � y, x � . In this case, for the 7 input � f ( x ) , f ( y ) � the output is � f ( y ) , f ( x ) � . This establish the claim for a single comparator. Sariel (UIUC) New CS473 19 Fall 2015 19 / 35

  38. Proof Induction on number of comparators. 1 Consider a comparator with inputs x and y , and outputs 2 x ′ = min( x, y ) and y ′ = max( x, y ) . If f ( x ) = f ( y ) then the claim trivially holds. 3 If f ( x ) < f ( y ) then clearly 4 max( f ( x ) , f ( y )) = f (max( x, y )) and min( f ( x ) , f ( y )) = f (min( x, y )) , since f ( · ) is monotonically increasing. � x, y � , for x < y , we have output � x, y � . 5 Input: � f ( x ) , f ( y ) � = ⇒ output is � f ( x ) , f ( y ) � . 6 Similarly, if x > y , the output is � y, x � . In this case, for the 7 input � f ( x ) , f ( y ) � the output is � f ( y ) , f ( x ) � . This establish the claim for a single comparator. Sariel (UIUC) New CS473 19 Fall 2015 19 / 35

  39. Proof Induction on number of comparators. 1 Consider a comparator with inputs x and y , and outputs 2 x ′ = min( x, y ) and y ′ = max( x, y ) . If f ( x ) = f ( y ) then the claim trivially holds. 3 If f ( x ) < f ( y ) then clearly 4 max( f ( x ) , f ( y )) = f (max( x, y )) and min( f ( x ) , f ( y )) = f (min( x, y )) , since f ( · ) is monotonically increasing. � x, y � , for x < y , we have output � x, y � . 5 Input: � f ( x ) , f ( y ) � = ⇒ output is � f ( x ) , f ( y ) � . 6 Similarly, if x > y , the output is � y, x � . In this case, for the 7 input � f ( x ) , f ( y ) � the output is � f ( y ) , f ( x ) � . This establish the claim for a single comparator. Sariel (UIUC) New CS473 19 Fall 2015 19 / 35

  40. Proof Induction on number of comparators. 1 Consider a comparator with inputs x and y , and outputs 2 x ′ = min( x, y ) and y ′ = max( x, y ) . If f ( x ) = f ( y ) then the claim trivially holds. 3 If f ( x ) < f ( y ) then clearly 4 max( f ( x ) , f ( y )) = f (max( x, y )) and min( f ( x ) , f ( y )) = f (min( x, y )) , since f ( · ) is monotonically increasing. � x, y � , for x < y , we have output � x, y � . 5 Input: � f ( x ) , f ( y ) � = ⇒ output is � f ( x ) , f ( y ) � . 6 Similarly, if x > y , the output is � y, x � . In this case, for the 7 input � f ( x ) , f ( y ) � the output is � f ( y ) , f ( x ) � . This establish the claim for a single comparator. Sariel (UIUC) New CS473 19 Fall 2015 19 / 35

  41. Proof Induction on number of comparators. 1 Consider a comparator with inputs x and y , and outputs 2 x ′ = min( x, y ) and y ′ = max( x, y ) . If f ( x ) = f ( y ) then the claim trivially holds. 3 If f ( x ) < f ( y ) then clearly 4 max( f ( x ) , f ( y )) = f (max( x, y )) and min( f ( x ) , f ( y )) = f (min( x, y )) , since f ( · ) is monotonically increasing. � x, y � , for x < y , we have output � x, y � . 5 Input: � f ( x ) , f ( y ) � = ⇒ output is � f ( x ) , f ( y ) � . 6 Similarly, if x > y , the output is � y, x � . In this case, for the 7 input � f ( x ) , f ( y ) � the output is � f ( y ) , f ( x ) � . This establish the claim for a single comparator. Sariel (UIUC) New CS473 19 Fall 2015 19 / 35

  42. Proof Induction on number of comparators. 1 Consider a comparator with inputs x and y , and outputs 2 x ′ = min( x, y ) and y ′ = max( x, y ) . If f ( x ) = f ( y ) then the claim trivially holds. 3 If f ( x ) < f ( y ) then clearly 4 max( f ( x ) , f ( y )) = f (max( x, y )) and min( f ( x ) , f ( y )) = f (min( x, y )) , since f ( · ) is monotonically increasing. � x, y � , for x < y , we have output � x, y � . 5 Input: � f ( x ) , f ( y ) � = ⇒ output is � f ( x ) , f ( y ) � . 6 Similarly, if x > y , the output is � y, x � . In this case, for the 7 input � f ( x ) , f ( y ) � the output is � f ( y ) , f ( x ) � . This establish the claim for a single comparator. Sariel (UIUC) New CS473 19 Fall 2015 19 / 35

  43. Proof continued Claim: if a wire carry a value a i , when the sorting network get 1 input a 1 , . . . , a n , then for input f ( a 1 ) , . . . , f ( a n ) this wire would carry the value f ( a i ) . Proof by induction on the depth on the wire at each point. 2 If point has depth 0 , then its input and claim trivially hold. 3 Assume holds for all points in circuit of depth ≤ qi , and 4 consider a point p on a wire of depth i + 1 . G : gate which this wire is an output of. 5 By induction, claim holds for inputs of G . 6 Now, the claim holds for the gate G itself. Apply above single gate proof for G . = ⇒ claim holds at p . Sariel (UIUC) New CS473 20 Fall 2015 20 / 35

  44. Proof continued Claim: if a wire carry a value a i , when the sorting network get 1 input a 1 , . . . , a n , then for input f ( a 1 ) , . . . , f ( a n ) this wire would carry the value f ( a i ) . Proof by induction on the depth on the wire at each point. 2 If point has depth 0 , then its input and claim trivially hold. 3 Assume holds for all points in circuit of depth ≤ qi , and 4 consider a point p on a wire of depth i + 1 . G : gate which this wire is an output of. 5 By induction, claim holds for inputs of G . 6 Now, the claim holds for the gate G itself. Apply above single gate proof for G . = ⇒ claim holds at p . Sariel (UIUC) New CS473 20 Fall 2015 20 / 35

  45. Proof continued Claim: if a wire carry a value a i , when the sorting network get 1 input a 1 , . . . , a n , then for input f ( a 1 ) , . . . , f ( a n ) this wire would carry the value f ( a i ) . Proof by induction on the depth on the wire at each point. 2 If point has depth 0 , then its input and claim trivially hold. 3 Assume holds for all points in circuit of depth ≤ qi , and 4 consider a point p on a wire of depth i + 1 . G : gate which this wire is an output of. 5 By induction, claim holds for inputs of G . 6 Now, the claim holds for the gate G itself. Apply above single gate proof for G . = ⇒ claim holds at p . Sariel (UIUC) New CS473 20 Fall 2015 20 / 35

  46. Proof continued Claim: if a wire carry a value a i , when the sorting network get 1 input a 1 , . . . , a n , then for input f ( a 1 ) , . . . , f ( a n ) this wire would carry the value f ( a i ) . Proof by induction on the depth on the wire at each point. 2 If point has depth 0 , then its input and claim trivially hold. 3 Assume holds for all points in circuit of depth ≤ qi , and 4 consider a point p on a wire of depth i + 1 . G : gate which this wire is an output of. 5 By induction, claim holds for inputs of G . 6 Now, the claim holds for the gate G itself. Apply above single gate proof for G . = ⇒ claim holds at p . Sariel (UIUC) New CS473 20 Fall 2015 20 / 35

  47. Proof continued Claim: if a wire carry a value a i , when the sorting network get 1 input a 1 , . . . , a n , then for input f ( a 1 ) , . . . , f ( a n ) this wire would carry the value f ( a i ) . Proof by induction on the depth on the wire at each point. 2 If point has depth 0 , then its input and claim trivially hold. 3 Assume holds for all points in circuit of depth ≤ qi , and 4 consider a point p on a wire of depth i + 1 . G : gate which this wire is an output of. 5 By induction, claim holds for inputs of G . 6 Now, the claim holds for the gate G itself. Apply above single gate proof for G . = ⇒ claim holds at p . Sariel (UIUC) New CS473 20 Fall 2015 20 / 35

  48. 24.3.1.1: Sorting correctly binary sequences implies real sorting Sariel (UIUC) New CS473 21 Fall 2015 21 / 35

  49. 0 / 1 sorting implies real sorting Theorem If a comparison network with n inputs sorts all 2 n binary strings of length n correctly, then it sorts all sequences correctly. Sariel (UIUC) New CS473 22 Fall 2015 22 / 35

  50. Proof: 0 / 1 sorting implies real sorting Assume for contradiction that fails for input a 1 , . . . , a n . Let 1 b 1 , . . . b n be the output sequence for this input. Let a i < a k be the two numbers that are output in incorrect 2 order (i.e. a k appears before a i in output). � 0 x ≤ a i f ( x ) = 3 1 x > a i . By lemma for input � f ( a 1 ) , . . . , f ( a n ) � , 4 circuit would output � f ( b 1 ) , . . . , f ( b n ) � . This sequence looks like: 000 .. 0???? f ( a k )???? f ( a i )??1111 5 but f ( a i ) = 0 and f ( a j ) = 1 . Namely, the output is a 6 sequence of the form ????1????0???? , which is not sorted. bin. input � f ( b 1 ) , . . . , f ( b n ) � sorting net’ fails. A 7 contradiction. Sariel (UIUC) New CS473 23 Fall 2015 23 / 35

  51. Proof: 0 / 1 sorting implies real sorting Assume for contradiction that fails for input a 1 , . . . , a n . Let 1 b 1 , . . . b n be the output sequence for this input. Let a i < a k be the two numbers that are output in incorrect 2 order (i.e. a k appears before a i in output). � 0 x ≤ a i f ( x ) = 3 1 x > a i . By lemma for input � f ( a 1 ) , . . . , f ( a n ) � , 4 circuit would output � f ( b 1 ) , . . . , f ( b n ) � . This sequence looks like: 000 .. 0???? f ( a k )???? f ( a i )??1111 5 but f ( a i ) = 0 and f ( a j ) = 1 . Namely, the output is a 6 sequence of the form ????1????0???? , which is not sorted. bin. input � f ( b 1 ) , . . . , f ( b n ) � sorting net’ fails. A 7 contradiction. Sariel (UIUC) New CS473 23 Fall 2015 23 / 35

  52. Proof: 0 / 1 sorting implies real sorting Assume for contradiction that fails for input a 1 , . . . , a n . Let 1 b 1 , . . . b n be the output sequence for this input. Let a i < a k be the two numbers that are output in incorrect 2 order (i.e. a k appears before a i in output). � 0 x ≤ a i f ( x ) = 3 1 x > a i . By lemma for input � f ( a 1 ) , . . . , f ( a n ) � , 4 circuit would output � f ( b 1 ) , . . . , f ( b n ) � . This sequence looks like: 000 .. 0???? f ( a k )???? f ( a i )??1111 5 but f ( a i ) = 0 and f ( a j ) = 1 . Namely, the output is a 6 sequence of the form ????1????0???? , which is not sorted. bin. input � f ( b 1 ) , . . . , f ( b n ) � sorting net’ fails. A 7 contradiction. Sariel (UIUC) New CS473 23 Fall 2015 23 / 35

  53. Proof: 0 / 1 sorting implies real sorting Assume for contradiction that fails for input a 1 , . . . , a n . Let 1 b 1 , . . . b n be the output sequence for this input. Let a i < a k be the two numbers that are output in incorrect 2 order (i.e. a k appears before a i in output). � 0 x ≤ a i f ( x ) = 3 1 x > a i . By lemma for input � f ( a 1 ) , . . . , f ( a n ) � , 4 circuit would output � f ( b 1 ) , . . . , f ( b n ) � . This sequence looks like: 000 .. 0???? f ( a k )???? f ( a i )??1111 5 but f ( a i ) = 0 and f ( a j ) = 1 . Namely, the output is a 6 sequence of the form ????1????0???? , which is not sorted. bin. input � f ( b 1 ) , . . . , f ( b n ) � sorting net’ fails. A 7 contradiction. Sariel (UIUC) New CS473 23 Fall 2015 23 / 35

  54. Proof: 0 / 1 sorting implies real sorting Assume for contradiction that fails for input a 1 , . . . , a n . Let 1 b 1 , . . . b n be the output sequence for this input. Let a i < a k be the two numbers that are output in incorrect 2 order (i.e. a k appears before a i in output). � 0 x ≤ a i f ( x ) = 3 1 x > a i . By lemma for input � f ( a 1 ) , . . . , f ( a n ) � , 4 circuit would output � f ( b 1 ) , . . . , f ( b n ) � . This sequence looks like: 000 .. 0???? f ( a k )???? f ( a i )??1111 5 but f ( a i ) = 0 and f ( a j ) = 1 . Namely, the output is a 6 sequence of the form ????1????0???? , which is not sorted. bin. input � f ( b 1 ) , . . . , f ( b n ) � sorting net’ fails. A 7 contradiction. Sariel (UIUC) New CS473 23 Fall 2015 23 / 35

  55. Proof: 0 / 1 sorting implies real sorting Assume for contradiction that fails for input a 1 , . . . , a n . Let 1 b 1 , . . . b n be the output sequence for this input. Let a i < a k be the two numbers that are output in incorrect 2 order (i.e. a k appears before a i in output). � 0 x ≤ a i f ( x ) = 3 1 x > a i . By lemma for input � f ( a 1 ) , . . . , f ( a n ) � , 4 circuit would output � f ( b 1 ) , . . . , f ( b n ) � . This sequence looks like: 000 .. 0???? f ( a k )???? f ( a i )??1111 5 but f ( a i ) = 0 and f ( a j ) = 1 . Namely, the output is a 6 sequence of the form ????1????0???? , which is not sorted. bin. input � f ( b 1 ) , . . . , f ( b n ) � sorting net’ fails. A 7 contradiction. Sariel (UIUC) New CS473 23 Fall 2015 23 / 35

  56. Proof: 0 / 1 sorting implies real sorting Assume for contradiction that fails for input a 1 , . . . , a n . Let 1 b 1 , . . . b n be the output sequence for this input. Let a i < a k be the two numbers that are output in incorrect 2 order (i.e. a k appears before a i in output). � 0 x ≤ a i f ( x ) = 3 1 x > a i . By lemma for input � f ( a 1 ) , . . . , f ( a n ) � , 4 circuit would output � f ( b 1 ) , . . . , f ( b n ) � . This sequence looks like: 000 .. 0???? f ( a k )???? f ( a i )??1111 5 but f ( a i ) = 0 and f ( a j ) = 1 . Namely, the output is a 6 sequence of the form ????1????0???? , which is not sorted. bin. input � f ( b 1 ) , . . . , f ( b n ) � sorting net’ fails. A 7 contradiction. Sariel (UIUC) New CS473 23 Fall 2015 23 / 35

  57. Proof: 0 / 1 sorting implies real sorting Assume for contradiction that fails for input a 1 , . . . , a n . Let 1 b 1 , . . . b n be the output sequence for this input. Let a i < a k be the two numbers that are output in incorrect 2 order (i.e. a k appears before a i in output). � 0 x ≤ a i f ( x ) = 3 1 x > a i . By lemma for input � f ( a 1 ) , . . . , f ( a n ) � , 4 circuit would output � f ( b 1 ) , . . . , f ( b n ) � . This sequence looks like: 000 .. 0???? f ( a k )???? f ( a i )??1111 5 but f ( a i ) = 0 and f ( a j ) = 1 . Namely, the output is a 6 sequence of the form ????1????0???? , which is not sorted. bin. input � f ( b 1 ) , . . . , f ( b n ) � sorting net’ fails. A 7 contradiction. Sariel (UIUC) New CS473 23 Fall 2015 23 / 35

  58. 24.4: A bitonic sorting network Sariel (UIUC) New CS473 24 Fall 2015 24 / 35

  59. Bitonic sorting network Definition A bitonic sequence is a sequence which is first increasing and then decreasing, or can be circularly shifted to become so. example The sequences (1 , 2 , 3 , π, 4 , 5 , 4 , 3 , 2 , 1) and (4 , 5 , 4 , 3 , 2 , 1 , 1 , 2 , 3) are bitonic, while the sequence (1 , 2 , 1 , 2) is not bitonic. Sariel (UIUC) New CS473 25 Fall 2015 25 / 35

  60. Bitonic sorting network Definition A bitonic sequence is a sequence which is first increasing and then decreasing, or can be circularly shifted to become so. example The sequences (1 , 2 , 3 , π, 4 , 5 , 4 , 3 , 2 , 1) and (4 , 5 , 4 , 3 , 2 , 1 , 1 , 2 , 3) are bitonic, while the sequence (1 , 2 , 1 , 2) is not bitonic. Sariel (UIUC) New CS473 25 Fall 2015 25 / 35

  61. Binary bitonic sequences Observation binary bitonic sequence is either of the form 0 i 1 j 0 k or of the form 1 i 0 j 1 k , where 0 i (resp, 1 i ) denote a sequence of i zeros (resp., ones). Sariel (UIUC) New CS473 26 Fall 2015 26 / 35

  62. Bitonic sorting network Definition A bitonic sorter is a comparison network that sorts all bitonic sequences correctly. Sariel (UIUC) New CS473 27 Fall 2015 27 / 35

  63. Half cleaner... Definition half-cleaner : a comparison network, connecting line i with line i + n/ 2 . Sariel (UIUC) New CS473 28 Fall 2015 28 / 35

  64. Half cleaner... Definition half-cleaner : a comparison network, connecting line i with line i + n/ 2 . Sariel (UIUC) New CS473 28 Fall 2015 28 / 35

  65. Half cleaner... Definition half-cleaner : a comparison network, connecting line i with line i + n/ 2 . Half-Cleaner [ n ] denote half-cleaner with n inputs. Sariel (UIUC) New CS473 28 Fall 2015 28 / 35

  66. Half cleaner... Definition half-cleaner : a comparison network, connecting line i with line i + n/ 2 . Half-Cleaner [ n ] denote half-cleaner with n inputs. Depth of Half-Cleaner [ n ] is one. Sariel (UIUC) New CS473 28 Fall 2015 28 / 35

  67. Half cleaner on bitonic sequence... 000..000 111..111 000..000 000..000 111 half− 000..000 cleaner 111..111 000..000 111..111 000..000 111 000..000 111..111 000..000 111 What a half-cleaner do to an input which is a (binary) bitonic 1 sequence? In example... left half size is clean and all equal to 0 . 2 Right side of the output is bitonic. 3 Specifically, one can prove by simple (but tedious) case analysis 4 that the following lemma holds. Sariel (UIUC) New CS473 29 Fall 2015 29 / 35

  68. Half cleaner on bitonic sequence... 000..000 111..111 000..000 000..000 111 half− 000..000 cleaner 111..111 000..000 111..111 000..000 111 000..000 111..111 000..000 111 What a half-cleaner do to an input which is a (binary) bitonic 1 sequence? In example... left half size is clean and all equal to 0 . 2 Right side of the output is bitonic. 3 Specifically, one can prove by simple (but tedious) case analysis 4 that the following lemma holds. Sariel (UIUC) New CS473 29 Fall 2015 29 / 35

  69. Half cleaner on bitonic sequence... 000..000 111..111 000..000 000..000 111 half− 000..000 cleaner 111..111 000..000 111..111 000..000 111 000..000 111..111 000..000 111 What a half-cleaner do to an input which is a (binary) bitonic 1 sequence? In example... left half size is clean and all equal to 0 . 2 Right side of the output is bitonic. 3 Specifically, one can prove by simple (but tedious) case analysis 4 that the following lemma holds. Sariel (UIUC) New CS473 29 Fall 2015 29 / 35

  70. Half cleaner on bitonic sequence... 000..000 111..111 000..000 000..000 111 half− 000..000 cleaner 111..111 000..000 111..111 000..000 111 000..000 111..111 000..000 111 What a half-cleaner do to an input which is a (binary) bitonic 1 sequence? In example... left half size is clean and all equal to 0 . 2 Right side of the output is bitonic. 3 Specifically, one can prove by simple (but tedious) case analysis 4 that the following lemma holds. Sariel (UIUC) New CS473 29 Fall 2015 29 / 35

  71. Half cleaner half sorts a bitonic sequence... Lemma If the input to a half-cleaner (of size n ) is a binary bitonic sequence then for the output sequence we have that (i) the elements in the top half are smaller than the elements in bottom half, and (ii) one of the halves is clean, and the other is bitonic. Sariel (UIUC) New CS473 30 Fall 2015 30 / 35

  72. Proof Proof. If the sequence is of the form 0 i 1 j 0 k and the block of ones is completely on the left side (i.e., its part of the first n/ 2 bits) or the right side, the claim trivially holds. So, assume that the block of ones starts at position n/ 2 − β and ends at n/ 2 + α . α � ������������� �� ������������� � 00 . . . 00 111 . . . 111 00 . . . 00 11 00 . . . 00 000 . . . 000 11 . . . 11 111 . . . 111 � ����������� �� ����������� � H alf C leaner β If n/ 2 − α ≥ β then this is exactly the case depicted above and claim holds. If n/ 2 − α < β then the second half is going to be all ones, as depicted on the right. Implying the claim for this case. A similar analysis holds if the sequence is of the form 1 i 0 j 1 k . Sariel (UIUC) New CS473 31 Fall 2015 31 / 35

  73. ✒ ☞ ☞ ✌ � ✌ ☞ ☞ ✌ Bitonic sorter - sorts bitonic sequences... �✂✁☎✄✝✆✟✞✠✁☛✡ ✆✎✍✎✄✝✏✑✍✓✒✕✔ ✖✘✗ ✫✬✜✭✙✯✮ ✡✚✙✛✏✢✜✣✞✤✏✥✍✧✦★✞✪✩ ✝✓✂✕✔ �✂✁☎✄✝✆✟✞✠✁☛✡ ✆✎✍✎✄✝✏✑✍ ✒✕✔ ✖✘✗ ✁✄✂✆☎✞✝✠✟✡☎☞☛✍✌✎✟✑✏ �✂✁☎✄✰✆✎✞✠✁☛✡ ✆✎✍✎✄✝✏✥✍✱✦★✞✲✩ (i) (ii) (iii) (i) recursive construction of BitonicSorter [ n ] , (ii) opening up the recursive construction, and (iii) the resulting comparison network. Sariel (UIUC) New CS473 32 Fall 2015 32 / 35

  74. Bitonic sorter... the result Lemma BitonicSorter [ n ] sorts bitonic sequences of length n = 2 k , it uses ( n/ 2) k = ( n/ 2) lg n gates, and it is of depth k = lg n . Sariel (UIUC) New CS473 33 Fall 2015 33 / 35

  75. Merging sequence Merging question: Given two sorted sequences of length n/ 2 , 1 how do we merge them into a single sorted sequence? Concatenate the two sequences... 2 ... second sequence is being flipped (i.e., reversed). 3 Easy to verify that the resulting sequence is bitonic, and as such 4 we can sort it using the BitonicSorter [ n ] . Given two sorted sequences a 1 ≤ a 2 ≤ . . . ≤ a n and 5 b 1 ≤ b 2 ≤ . . . ≤ b n , observe that the sequence a 1 , a 2 , . . . , a n , b n , b n − 1 , b n − 2 , . . . , b 2 , b 1 is bitonic. Sariel (UIUC) New CS473 34 Fall 2015 34 / 35

  76. ☞ ✌ ✘ Merger[n] : Using a bitonic sorter Merging two sorted sequences into a sorted sequence �✂✁☎✄✝✆✟✞✠✁☛✡ ✆✎✍✏✄✝✑✒✍✔✓✕✞✗✖ ✑✒✍✎✙✚✑✒✍✛✓✜✞✗✖ (i) (ii) (iii) (iv) (i) Merger via flipping the lines of bitonic sorter. (ii) BitonicSorter . (iii) Merger after we “physically” flip the lines. (iv) Equivalent drawing of the resulting Merger . Sariel (UIUC) New CS473 35 Fall 2015 35 / 35

  77. � ✗ ✒ � ✗ � ✤ ✤ Merger[n] described using FlipCleaner ✓✙✘✛✚✜✟✢✓✣✁ ✚✥☛✥✘✛☎✦☛ ✧✪✩ ✫✭✬ ✂✔✓✖✕ ✁✄✂✆☎✞✝✠✟✡☎☞☛✍✌✎✟✑✏ ✓✙✘✛✚✜✟✢✓✣✁ ✚✥☛✥✘✛☎✦☛★✧✪✩ ✫✭✬ (i) (ii) (i) FlipCleaner [ n ] , and (ii) Merger [ n ] described using FlipCleaner . Sariel (UIUC) New CS473 36 Fall 2015 36 / 35

Recommend


More recommend