将载体压缩成笛卡尔积

时间:2015-02-15 23:52:56

标签: python algorithm cartesian-product

我有N个10维向量,其中每个元素的值可以是0,1或2。 例如,vector v=(0,1,1,2,0,1,2,0,1,1)是向量之一。 是否存在将这些向量压缩为最小数量的笛卡尔积的算法(最好是在python中)。如果不是完美的解决方案,是否存在至少提供良好压缩的算法。

示例:两个"笛卡尔向量" ([1,2], 0, 1, 0, 0, 0, 1, 1, [0,1], 0])(给出4个向量)和(0, 1, 0, 2, 0, 0, [0,2], 2, 0, 1)(给出2个向量)给出N = 6向量的最优解:

1,0,1,0,0,0,1,1,0,0
2,0,1,0,0,0,1,1,0,0
1,0,1,0,0,0,1,1,1,0
2,0,1,0,0,0,1,1,1,0
0,1,0,2,0,0,0,2,0,1
0,1,0,2,0,0,2,2,0,1

2 个答案:

答案 0 :(得分:0)

如果您的向量是(0,1) - 值,那么您将面临最小的DNF问题,which is(如果我错了,请纠正我)NP-hard。具有(0,1,2)值的向量不会使问题变得更简单。

答案 1 :(得分:0)

贪心算法可能很有用:

Let S = <V1, V2, ... Vn>

has_merge_ops = True

while has_merge_ops:
    has_merge_ops = False

    S' = []
    for v in S:
        if there exits v' which differs from v by just only ONE element:
            V = merge v and v' by merging the different values at index k
            S'.append(V)
            S.remove(v)
            S.remove(v')
            has_merge_ops = True

    S = S'

return S

所以它的工作原理如下:

after sorting, S = 
0,1,0,2,0,0,0,2,0,1
0,1,0,2,0,0,2,2,0,1
1,0,1,0,0,0,1,1,0,0
1,0,1,0,0,0,1,1,1,0
2,0,1,0,0,0,1,1,0,0
2,0,1,0,0,0,1,1,1,0

After first pass:
0,1,0,2,0,0,[0,2],2,0,1
1,0,1,0,0,0,1,1,[0,1],0
2,0,1,0,0,0,1,1,[0,1],0

After 2nd pass:
0,1,0,2,0,0,[0,2],2,0,1
[1,2],0,1,0,0,0,1,1,[0,1],0

Algorithm stops since no more merging is possible.