创建集的分区,使每个分区的长度相同

时间:2016-03-11 04:58:53

标签: python python-3.x set discrete-mathematics

根据标题,我试图编写一个函数,该函数返回长度为p的集合的元组列表,其中每个集合是集合S的分区。均分({0,1,2,3},2) - > [({0,1},{2,3}),({0,2},{1,3}),({0,3},{1,2})]。 但是由于itertools.products不会连接集合的元组,因此我很难让它在其他情况下正常工作。任何有关如何编写/修复功能的建议或帮助将不胜感激。以下是我到目前为止的尝试:

equipartitions_cache_d = {}
def equipartitions(S,p):
    global equipartitions_cache_d
    if len(S) % p != 0:
        raise ValueError("Set must be of a length which is a multiple of p")
    if equipartitions_cache_d.get((frozenset(S),p)):
        return equipartitions_cache_d[(frozenset(S),p)]
    else:
        if len(S) == p:
            equipartitions_cache_d[(frozenset(S),p)] = [S]
        else:
            gens = []
            combs = [set(s) for s in itertools.combinations(S, p)]
            for c in combs:
                gens += [s for s in itertools.product([c], equipartitions(S-c,p))]
            uniqgens = []
            for g in gens:
                if not any([all([x in q for x in g]) for q in uniqgens]):
                    uniqgens.append(g)
            equipartitions_cache_d[(frozenset(S),p)] = uniqgens
        return equipartitions_cache_d[(frozenset(S),p)]

1 个答案:

答案 0 :(得分:2)

效率不高(需要花费大量时间来处理更大的输入,例如equipart(set(range(100), 5)))但是有效。

def equipart(s, p):
    import itertools
    if len(s) % p != 0:
        raise ValueError("Set must be of a length which is a multiple of p")
    com = map(set, set(itertools.combinations(s, p)))
    res = list()
    for ia, a in enumerate(com):
        for il, l in enumerate(res):
            if not any([(a&x) for x in l]):
                res[il] = res[il] + (a, )
                break
        else:
            res.append((a, ))
    res = filter(lambda x: len(x) == len(s) / p, res)
    return res

输出:

In [94]: equipart({1,2,3,4,5,6}, 3)
Out[94]: 
[({3, 4, 6}, {1, 2, 5}),
 ({2, 3, 5}, {1, 4, 6}),
 ({1, 2, 6}, {3, 4, 5}),
 ({2, 3, 4}, {1, 5, 6}),
 ({4, 5, 6}, {1, 2, 3}),
 ({2, 3, 6}, {1, 4, 5}),
 ({1, 3, 6}, {2, 4, 5}),
 ({2, 4, 6}, {1, 3, 5}),
 ({2, 5, 6}, {1, 3, 4}),
 ({3, 5, 6}, {1, 2, 4})]

In [95]: equipart({1,2,3,4,5,6}, 2)
Out[95]: [({1, 2}, {5, 6}, {3, 4}), ({1, 3}, {4, 6}, {2, 5}), ({1, 6}, {2, 4}, {3, 5})]

修改

多个元组中具有单个子集的新方法。

def equipart(s, p):
    import itertools
    if len(s) % p != 0:
        raise ValueError("Set must be of a length which is a multiple of p")
    com = map(set, set(itertools.combinations(s, p)))
    res = [x for x in itertools.combinations(com, len(s)/p) if set().union(*x) == s]
    return res

输出:

In [37]: equipart({1,2,3,4,5,6}, 3)
Out[37]: 
[({3, 4, 6}, {1, 2, 5}),
 ({2, 3, 5}, {1, 4, 6}),
 ({1, 2, 6}, {3, 4, 5}),
 ({2, 3, 4}, {1, 5, 6}),
 ({4, 5, 6}, {1, 2, 3}),
 ({2, 3, 6}, {1, 4, 5}),
 ({1, 3, 6}, {2, 4, 5}),
 ({2, 4, 6}, {1, 3, 5}),
 ({2, 5, 6}, {1, 3, 4}),
 ({3, 5, 6}, {1, 2, 4})]

In [38]: equipart({1,2,3,4,5,6}, 2)
Out[38]: 
[({1, 2}, {5, 6}, {3, 4}),
 ({1, 2}, {4, 6}, {3, 5}),
 ({1, 2}, {4, 5}, {3, 6}),
 ({5, 6}, {1, 3}, {2, 4}),
 ({5, 6}, {1, 4}, {2, 3}),
 ({1, 3}, {4, 6}, {2, 5}),
 ({1, 3}, {4, 5}, {2, 6}),
 ({4, 6}, {1, 5}, {2, 3}),
 ({4, 5}, {1, 6}, {2, 3}),
 ({1, 4}, {2, 6}, {3, 5}),
 ({1, 4}, {3, 6}, {2, 5}),
 ({1, 5}, {2, 6}, {3, 4}),
 ({1, 5}, {3, 6}, {2, 4}),
 ({1, 6}, {2, 5}, {3, 4}),
 ({1, 6}, {2, 4}, {3, 5})]