five lectures on ca 4 sorting
play

Five Lectures on CA 4. Sorting Thomas Worsch Department of - PowerPoint PPT Presentation

Five Lectures on CA 4. Sorting Thomas Worsch Department of Informatics Karlsruhe Institute of Technology http://liinwww.ira.uka.de/~thw/vl-hiroshima/ at Hiroshima University, January 2012 Outline Sorting in one-dimensional CA Odd-even


  1. Five Lectures on CA 4. Sorting Thomas Worsch Department of Informatics Karlsruhe Institute of Technology http://liinwww.ira.uka.de/~thw/vl-hiroshima/ at Hiroshima University, January 2012

  2. Outline Sorting in one-dimensional CA Odd-even transposition sort Knuth’s 0-1 principle Sorting in two-dimensional CA Sorting order Shearsort Algorithm by Schnorr and Shamir

  3. Sorting in one-dimensional CA Odd-even transposition sort Knuth’s 0-1 principle Sorting in two-dimensional CA Sorting order Shearsort Algorithm by Schnorr and Shamir

  4. Sorting in one-dimensional CA Odd-even transposition sort Knuth’s 0-1 principle Sorting in two-dimensional CA Sorting order Shearsort Algorithm by Schnorr and Shamir

  5. Problem Notation for x ∈ A , w ∈ A ∗ denote by N x ( w ) the number of occurences of symbol x in w

  6. Problem Notation for x ∈ A , w ∈ A ∗ denote by N x ( w ) the number of occurences of symbol x in w Problem: sorting of bits given: A = { 0 , 1 } R = Z N = H 1 wanted: CA with Q ⊇ A ∪ { # } and f , such that each pattern w ∈ A + 0 N 0 ( w ) 1 N 1 ( w ) is transformed into

  7. Algorithm ◮ Q = A ∪ { # } ◮ f given by ℓ ( − 1) ℓ (0) ℓ (1) f ( ℓ ) × 1 0 1 × 1 0 0 otherwise: × × s s ◮ Note: f is well defined

  8. Example # 1 1 0 1 1 1 0 1 0 # # 1 0 1 1 1 0 1 0 1 # # 0 1 1 1 0 1 0 1 1 # # 0 1 1 0 1 0 1 1 1 # # 0 1 0 1 0 1 1 1 1 # # 0 0 1 0 1 1 1 1 1 # # 0 0 0 1 1 1 1 1 1 #

  9. Example 2 # 1 1 1 1 1 1 0 0 0 # # 1 1 1 1 1 0 1 0 0 # # 1 1 1 1 0 1 0 1 0 # # 1 1 1 0 1 0 1 0 1 # # 1 1 0 1 0 1 0 1 1 # # 1 0 1 0 1 0 1 1 1 # # 0 1 0 1 0 1 1 1 1 # # 0 0 1 0 1 1 1 1 1 # # 0 0 0 1 1 1 1 1 1 #

  10. Problem: Sorting of “trits” given: A = { 0 , 1 , 2 } R = Z N = H 1 wanted: CA with Q ⊇ A ∪ { # } and f , such that each pattern w ∈ A + 0 N 0 ( w ) 1 N 1 ( w ) 2 N 2 ( w ) is transformed into Let’s try . . .

  11. Problem: Sorting of “trits” Problem: ℓ ( − 1) ℓ (0) ℓ (1) f ( ℓ ) ??? 2 1 0

  12. Problem: Sorting of “trits” Problem: ℓ ( − 1) ℓ (0) ℓ (1) f ( ℓ ) ??? 2 1 0 Solution even cells: look to the left and to the right alternately odd cells: look to the right and to the left alternately

  13. Algorithm (Odd-even transposition sort) ◮ arbitrary input alphabet A ◮ Q = A × { L , R , } ∪ { # } (identify s ∈ A with ( s , ) ) ◮ f given by the following table: ℓ ( − 1) ℓ (0) ℓ (1) f ( ℓ ) real sorting: ( s , R ) ( t , L ) × (max( s , t ) , R ) × ( s , R ) ( t , L ) (min( s , t ) , L ) ( t , L ) × ( t , R ) # × ( s , R ) ( s , L ) # initialisation: ( s , ) × ( s , L ) # ( s , L ) ( t , ) × ( t , L ) otherwise: × × z z

  14. Example 1 0 2 1 0 0 1 1 2 0 # # # # L R L R L 1 0 2 1 0 0 1 1 0 2 # # # # L R L R L R 1 0 2 1 0 0 1 0 1 2 # # # # R L L R L R L 0 1 2 1 0 0 0 1 1 2 # # # # L R L R L R L R 0 1 2 1 0 0 0 1 1 2 # # # # R L R L L R L R L 0 1 1 2 0 0 0 1 1 2 # # # # L R L R L R L R L R

  15. Lemma Odd-even transposition sort is correct. How does one prove that?

  16. Sorting in one-dimensional CA Odd-even transposition sort Knuth’s 0-1 principle Sorting in two-dimensional CA Sorting order Shearsort Algorithm by Schnorr and Shamir

  17. Lemma ( 0 - 1 principle) ◮ If a sorting algorithm only uses “compare-and-swap-if-greater” operations

  18. Lemma ( 0 - 1 principle) ◮ If a sorting algorithm only uses “compare-and-swap-if-greater” operations ◮ and if it is independent of the concrete values to be sorted which elements are compared and when,

  19. Lemma ( 0 - 1 principle) ◮ If a sorting algorithm only uses “compare-and-swap-if-greater” operations ◮ and if it is independent of the concrete values to be sorted which elements are compared and when, ◮ then the algorithm works correctly for all input sequences

  20. Lemma ( 0 - 1 principle) ◮ If a sorting algorithm only uses “compare-and-swap-if-greater” operations ◮ and if it is independent of the concrete values to be sorted which elements are compared and when, ◮ then the algorithm works correctly for all input sequences ◮ if and only if it works correctly for input sequences which are bits. correctness of shearsort

  21. Proof (1) Assume that x 1 , . . . , x n is an input sequence which is not sorted correctly. Will show: There is also an input sequence x ∗ 1 , . . . , x ∗ n of bits which is not sorted correctly.

  22. Proof (1) Assume that x 1 , . . . , x n is an input sequence which is not sorted correctly. Will show: There is also an input sequence x ∗ 1 , . . . , x ∗ n of bits which is not sorted correctly. Assume: correct order would be x π (1) ≤ x π (2) ≤ · · · ≤ x π ( n ) the algorithm produces , , · · · , x σ (1) x σ (2) x σ ( n )

  23. Proof (1) Assume that x 1 , . . . , x n is an input sequence which is not sorted correctly. Will show: There is also an input sequence x ∗ 1 , . . . , x ∗ n of bits which is not sorted correctly. Assume: correct order would be x π (1) ≤ x π (2) ≤ · · · ≤ x π ( n ) the algorithm produces , , · · · , x σ (1) x σ (2) x σ ( n ) k : first wrong position x σ ( i ) = x π ( i ) for 1 ≤ i < k x σ ( k ) > x π ( k ) r : where x π ( k ) ended up instead of k x σ ( r ) = x π ( k ) where r > k

  24. Proof (2) Define a bit sequence x ∗ 1 , . . . , x ∗ n as follows � 1 if x i > x π ( k ) � � x ∗ i = x i > x π ( k ) i. e. x ∗ i = 0 if x i ≤ x π ( k ) � � for example: σ ( k ) = x σ ( k ) > x π ( k ) = 1 x ∗ � � x ∗ σ ( r ) = x σ ( r ) > x π ( k ) = 0

  25. Proof (2) Define a bit sequence x ∗ 1 , . . . , x ∗ n as follows � 1 if x i > x π ( k ) � � x ∗ i = x i > x π ( k ) i. e. x ∗ i = 0 if x i ≤ x π ( k ) � � for example: σ ( k ) = x σ ( k ) > x π ( k ) = 1 x ∗ � � x ∗ σ ( r ) = x σ ( r ) > x π ( k ) = 0 � � Nun gilt: x i > x j = ⇒ x j > x π ( k ) = ⇒ x i > x π ( k ) � � = ⇒ x ∗ j = 1 = ⇒ x ∗ i = 1 = ⇒ x ∗ i ≥ x ∗ j und analog x i ≤ x j = ⇒ x ∗ i ≤ x ∗ j .

  26. Proof (3) Hence for input sequence f¨ ur die Eingabe x ∗ 1 , . . . , x ∗ n the algorithm produces the same result which one gets

  27. Proof (3) Hence for input sequence f¨ ur die Eingabe x ∗ 1 , . . . , x ∗ n the algorithm produces the same result which one gets if one makes the same swaps for the x ∗ 1 , . . . , x ∗ n bits, which the algorithm makes for the x 1 , . . . , x n . The algorithm permutes the x ∗ i according to σ .

  28. Proof (3) Hence for input sequence f¨ ur die Eingabe x ∗ 1 , . . . , x ∗ n the algorithm produces the same result which one gets if one makes the same swaps for the x ∗ 1 , . . . , x ∗ n bits, which the algorithm makes for the x 1 , . . . , x n . The algorithm permutes the x ∗ i according to σ . When the algorithm stops, the result is · · · · · · · · · x ∗ x ∗ x ∗ x ∗ σ (1) σ ( k ) σ ( r ) σ ( n ) � � � � i.e. · · · x σ ( k ) > x π ( k ) · · · x σ ( r ) > x π ( k ) · · · i.e. · · · 1 · · · 0 · · · and that ist a non sorted sequence.

  29. Correctness of odd-even transposition sort ◮ 0-1 principle is applicable ◮ consider a bit sequence with e ones.

  30. Correctness of odd-even transposition sort ◮ 0-1 principle is applicable ◮ consider a bit sequence with e ones. ◮ look at the algorithm as sending R -signals from left to right:

  31. Correctness of odd-even transposition sort ◮ 0-1 principle is applicable ◮ consider a bit sequence with e ones. ◮ look at the algorithm as sending R -signals from left to right: 1. The first signal moves the rightmost 1 to its final position (arriving at the latest at time 2 + ( n − 1) = n + 1)

  32. Correctness of odd-even transposition sort ◮ 0-1 principle is applicable ◮ consider a bit sequence with e ones. ◮ look at the algorithm as sending R -signals from left to right: 1. The first signal moves the rightmost 1 to its final position (arriving at the latest at time 2 + ( n − 1) = n + 1) 2. The second signal moves the second 1 (counted from the right) to its final position (arriving at the latest at time 4 + ( n − 2) = n + 2)

  33. Correctness of odd-even transposition sort ◮ 0-1 principle is applicable ◮ consider a bit sequence with e ones. ◮ look at the algorithm as sending R -signals from left to right: 1. The first signal moves the rightmost 1 to its final position (arriving at the latest at time 2 + ( n − 1) = n + 1) 2. The second signal moves the second 1 (counted from the right) to its final position (arriving at the latest at time 4 + ( n − 2) = n + 2) . . . and so on

Recommend


More recommend