我需要增加长度的一组组合的所有组合。第一组创建如下:
combinations = list(itertools.combinations(5, 2))
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]然后,匹配特定条件的所有组合都存储在匹配中:
[(0, 2), (0, 3), (0, 4), (3, 4)]其中,我想找到长度为3的所有相交组合:
[(0, 3, 4)]我只需要找到彼此相交的组合。(0,3),(0,4),(3,4)是相交的,所以(0,3,4)是一个组合。(0,2,4)不是,因为(2,4)不是相交的
我现在用的是:
combinations = []
for y in matches:
for x in matches:
if y != x:
el = qsort(unique(y[:] + x[:]))
if el not in combinations and len(el) == it:
combinations.append(el)但是,使用超过200个数字的组合需要太长时间。有办法更快地做到这一点吗?
发布于 2015-06-02 15:54:50
正如切普纳所建议的,我正在寻找这张图中的所有循环或团。Networkx的enumerate_all_cliques()是查找它们的简单方法。
https://stackoverflow.com/questions/30035458
复制相似问题