[This is a solution to the sort each column, algorithm puzzle posted earlier]
A brief description of the problem:
Given a matrix of integers, by permuting each row separately (with possibly different permutations), can each column of the matrix be sorted (in ascending order, with smallest in row one)? Give a polynomial time algorithm for it.
Solution
Consider the case of a 2×n matrix, which has two rows and n columns.
Suppose it was possible to sort the columns by permuting the rows.
The claim is that, the columns remain sorted, even after we sort the rows!
For example, this is a matrix in which the columns are sorted:
[4786510020010]
Now, if we sort each row individually we get
[4678510100200]
The columns are still sorted!
The proof is actually not difficult:
Suppose a≤b and c≤d, then we have that:
min{a,c}≤min{b,d} and max{a,c}≤max{b,d}.
Thus
[…a…c……b…d…]
can be replaced by
[…min{a,c}…max{a,c}……min{b,d}…max{b,d}…]
and still have the columns sorted.
Thus if the columns are sortable, they are sortable by sorting the individual rows (and vice-versa).
So the algorithm is simple: sort each row, and then check if the columns are sorted.
A brief description of the problem:
Given a matrix of integers, by permuting each row separately (with possibly different permutations), can each column of the matrix be sorted (in ascending order, with smallest in row one)? Give a polynomial time algorithm for it.
Solution
Consider the case of a 2×n matrix, which has two rows and n columns.
Suppose it was possible to sort the columns by permuting the rows.
The claim is that, the columns remain sorted, even after we sort the rows!
For example, this is a matrix in which the columns are sorted:
[4786510020010]
Now, if we sort each row individually we get
[4678510100200]
The columns are still sorted!
The proof is actually not difficult:
Suppose a≤b and c≤d, then we have that:
min{a,c}≤min{b,d} and max{a,c}≤max{b,d}.
Thus
[…a…c……b…d…]
can be replaced by
[…min{a,c}…max{a,c}……min{b,d}…max{b,d}…]
and still have the columns sorted.
Thus if the columns are sortable, they are sortable by sorting the individual rows (and vice-versa).
So the algorithm is simple: sort each row, and then check if the columns are sorted.
No comments:
Post a Comment