我们绝对可以完全避免行或列违规,但同时避免两者取决于数据而不是算法。
这是我想出的。
让我们假设一个维度为 nX2n 的二维数组。
第 1 步:根据第一个元素对所有元组进行排序,我们称之为 TUP_1。
第 2 步:根据第二个元素对所有元组进行排序,我们称之为 TUP_2。
第 3 步:
i=0
while(i<n)
{
Pick first n tuples(unmarked) from TUP_2 which are at Positions a,b,c......
in TUP_1 such that a < b < c and so on.
Mark the picked tuples.
fill the ith row with:
first element from tuples in even positions.
second element from tuples in odd positions.
increment i.
}
注意:以上算法因条件而异
a < b < c would always avoid any violation in row.
但是,如果
the elements from TUP_2 are always picked in order without any skipping.
如果不是,则可能会发生列违规。
示例:让我们假设 3X4 矩阵
让输入包含以下元组。
(12,6) (12,1) (2,1) (11,1) (8,6) and (4,5).
排序,基于第一个元素的元组将给出 TUP_1
(2,1) (4,5) (8,6) (11,1) (12,1) and (12,6).
根据第二个元素对元组进行排序将得到 TUP_2
(2,1) (11,1) (12,1) (4,5) (8,6) (12,6).
现在执行第 3 步
(2,1) and (11,1) get picked up from TUP_2 because the tuples are at position
1 and 4 in TUP_1 and 1 < 4 and first row is filled as.
(2,1),(11,1)
(12,1) and (12,6) get picked up from TUP_2 because the tuples are at position
5 and 6 in TUP_1 and 5 < 6 and second row is filled as.
(12,1),(12,6)
(4,5) and (8,6) get picked up from TUP_2 because the tuples are at position
2 and 3 in TUP_1 and 2 < 3 and third row is filled as.
(4,5),(8,6)
因此,3X4 矩阵为:
(2,1),(11,1)
(12,1),(12,6)
(4,5),(8,6)