Stupid Columnsort Tricks Geeta Chaudhry Tom Cormen Dartmouth

  • Slides: 16
Download presentation
Stupid Columnsort Tricks Geeta Chaudhry Tom Cormen Dartmouth College Department of Computer Science

Stupid Columnsort Tricks Geeta Chaudhry Tom Cormen Dartmouth College Department of Computer Science

Columnsort § Sorts N numbers § Organized as r ´ s mesh § Divisibility

Columnsort § Sorts N numbers § Organized as r ´ s mesh § Divisibility restriction: s must divide r § Height restriction: r ≥ 2 s 2 § 8 steps 1. 2. 3. 4. 5. 6. 7. 8. Sort each column Transpose Sort each column Untranspose Sort each column Shift down 1/2 column Sort each column Shift up 1/2 column

Proof of Correctness § Columnsort is oblivious § Use 0 -1 Principle: If an

Proof of Correctness § Columnsort is oblivious § Use 0 -1 Principle: If an oblivious algorithm sorts all input sets consisting solely of 0 s and 1 s, then it sorts all input sets with arbitrary values. § After step 3, the mesh consists of § Clean rows of 0 s at the top § Clean rows of 1 s at the bottom § ≤ s dirty rows between the clean rows

Proof of Correctness (continued) § After step 4, the mesh consists of § Clean

Proof of Correctness (continued) § After step 4, the mesh consists of § Clean columns of 0 s on the left § Clean columns of 1 s on the right § A dirty area of size ≤ s 2 between the clean columns § r ≥ 2 s 2 ==> s 2 ≤ r/2 ==> the dirty area is at most 1/2 a column large

Proof of Correctness (continued) § If, entering step 5, the dirty area is at

Proof of Correctness (continued) § If, entering step 5, the dirty area is at most 1/2 a column large, then steps 5– 8 complete the sorting § If the dirty area fits in a single column, step 5 cleans it, and steps 6– 8 leave the mesh clean § If the dirty area spans two columns, then it’s in the bottom half of one column and the top half of the next column. § Step 5 does not change this § Step 6 gets the dirty area into one column § Step 7 cleans it § Step 8 moves all values back to where they belong

Removing the Divisibility Restriction § Step 1: Sort each column § Each column has

Removing the Divisibility Restriction § Step 1: Sort each column § Each column has ≤ 1 0 ->1 transition § ≤ s 0 ->1 transitions § There may be a 1 ->0 transition going from one column to the next § ≤ s– 1 1 ->0 transitions § Step 2: Transpose § Within rows, ≤ s 0 ->1 transitions, ≤ s– 1 1 ->0 transitions

Divisibility Restriction (continued) § After step 2, let § § § X = dirty

Divisibility Restriction (continued) § After step 2, let § § § X = dirty rows with one 0 ->1 transition, no 1 ->0 Y = dirty rows with one 1 ->0 transition, no 0 ->1 Z = all other dirty rows (≥ 1 0 ->1 and ≥ 1 1 ->0) Number of dirty rows = |X| + |Y| + |Z| All other rows are clean § Claim: max(|X|, |Y|) + |Z| ≤ s § § § Every row of X, Z contains ≥ 1 0 ->1 => |X| + |Z| ≤ s Every row of Y, Z contains ≥ 1 1 ->0 => |Y| + |Z| ≤ s– 1 max(|X|, |Y|) = |X| => max(|X|, |Y|) + |Z| ≤ s max(|X|, |Y|) = |Y| => max(|X|, |Y|) + |Z| ≤ s– 1 In either case, max(|X|, |Y|) + |Z| ≤ s

Divisibility Restriction (continued) § After step 3 § Clean rows of 0 s move

Divisibility Restriction (continued) § After step 3 § Clean rows of 0 s move to the top § Clean rows of 1 s move to the bottom § Pair up the min(|X|, |Y|) pairs of rows with one row in X and other row in Y from X: from Y: more 0 s than 1 s 0000111110000000 more 1 s than 0 s 000111110000 equal 0 s and 1 s 00001111000000 11111000111110000 111111 000000 111111

Divisibility Restriction (continued) § § § In all cases, ≥ 1 clean row is

Divisibility Restriction (continued) § § § In all cases, ≥ 1 clean row is formed ≥ min(|X|, |Y|) new clean rows are created Dirty rows remaining ≤ |X| + |Y| + |Z| – min(|X|, |Y|) |X| + |Y| – min(|X|, |Y|) = max(|X|, |Y|) ==> Dirty rows remaining ≤ max(|X|, |Y|) + |Z| ≤s From here, it’s the same as the original proof § Dirty area size ≤ s 2 ≤ r/2 (half a column) after step 4 § Steps 5– 8 clean up the dirty area

Subblock Distribution § Divide up the mesh into s 1/2 ´ s 1/2 subblocks

Subblock Distribution § Divide up the mesh into s 1/2 ´ s 1/2 subblocks § Each subblock contains s values § Add two steps between steps 3 and 4 § Step 3. 1: Perform any fixed permutation that moves all values in each subblock into all s columns § Step 3. 2: Sort each column § The resulting algorithm is subblock columnsort § Works with relaxed height restriction of r ≥ 4 s 3/2 (assuming the divisibility restriction)

Subblock Columnsort Correctness § After step 3, the line dividing 0 s and 1

Subblock Columnsort Correctness § After step 3, the line dividing 0 s and 1 s goes left to right and bottom to top (southwest to northeast) § § § Never turns back to the left Never turns back toward the bottom To show, suffices to show that after step 2, # of 0 s in each column ≥ # of 0 s in column to its right How could a column have # of 0 s < # of 0 s in column to its right? There would have to be a 1 ->0 transition in a row But divisibility restriction => there are no 1 ->0 transitions in rows after step 2

Subblock Columnsort Correctness (continued) § After step 3. 1, the number of 0 s

Subblock Columnsort Correctness (continued) § After step 3. 1, the number of 0 s in any two columns differs by ≤ 2 s 1/2 § The dirty area is confined to an area s rows high and s columns wide § In subblocks, s 1/2 ´ s 1/2 § Dividing line passes through ≤ s 1/2 + 1 subblocks vertically and ≤ s 1/2 subblocks horizontally ==> ≤ 2 s 1/2 subblocks total § Don’t double-count the 1 extra subblock § Step 3. 1 distributes each subblock to all s columns § All clean subblocks distribute their 0 s and 1 s uniformly to each column § Number of 0 s between any two columns differs by ≤ number of dirty subblocks = 2 s 1/2

Subblock Columnsort Correctness (continued) § After step 4, the mesh consists of § Clean

Subblock Columnsort Correctness (continued) § After step 4, the mesh consists of § Clean columns of 0 s on the left § Clean columns of 1 s on the right § A dirty area of size ≤ 2 s 3/2 between the clean columns § After sorting in step 3. 2, the dirty area is ≤ 2 s 1/2 rows high and ≤ s columns wide § Size of dirty area is ≤ 2 s 3/2

Subblock Columnsort Correctness (continued) § Finish up by observing that r ≥ 4 s

Subblock Columnsort Correctness (continued) § Finish up by observing that r ≥ 4 s 3/2 is equivalent to 2 s 3/2 ≤ r/2 § Now the dirty area is at most 1/2 a column § Steps 5– 8 clean up the dirty area § Can remove the divisibility restriction at the cost of tightening the height restriction to r ≥ 6 s 3/2

Slabpose Columnsort § Another variation on columnsort § Loosens height restriction to r ≥

Slabpose Columnsort § Another variation on columnsort § Loosens height restriction to r ≥ 4 s 3/2 § Has 10 steps § Partitions the mesh into vertical slabs

Conclusion § Removed and relaxed restrictions on columnsort § Divisibility restriction: s divides r

Conclusion § Removed and relaxed restrictions on columnsort § Divisibility restriction: s divides r § Height restriction: r ≥ 2 s 2 § Divisibility restriction is not necessary § Subblock columnsort § With divisibility restriction: r ≥ 4 s 3/2 § Without divisibility restriction: r ≥ 6 s 3/2 § Slabpose columnsort: r ≥ 4 s 3/2