简体   繁体   English

将所有可能的点组合分组为m组

[英]Grouping of all possible combinations of points taken m at a time into r groups

I want to group all possible combinations of points taken m at a time into r groups. 我想组每次拍摄的M分割R基团点的所有可能的组合。

Points = [A,B,C,D........] Total n points Points = [A,B,C,D ........]总n

Combinations of these points taken m at a time will be a list l 一次取m的这些点的组合将是列表l

l = list(itertools.combinations(points,m))

How can I further group it into r groups such that i-th element of each group has no similar points. 如何进一步将其分组为r组,使得每组的第i个元素没有相似的点。

For example, 例如,

Points = [A,B,C,D] m = 2 and r = 2 点= [A,B,C,D] m = 2且r = 2

l = [[A,B],[A,C],[A,D],[B,C],[B,D],[C,D]] l = [[A,B],[A,C],[A,D],[B,C],[B,D],[C,D]]

So groups will be 所以小组会

Group 1 = [[A,B],[A,C],[A,D]] and corresponding to it Group 2 = [[C,D],[B,D],[B,C]] 第1组= [[A,B],[A,C],[A,D]]并对应于第2组= [[C,D],[B,D],[B,C]]

Note : The points in the i-th index of group 1 and group 2 have no similar points. 注意:组1和组2 的第i个索引中的点没有相似的点。

I want to do it for n number of points taken m at a time and grouping it into r groups. 我想这样做了一次取m个点的n个,并分组到R基团。

Please provide me the algorithm. 请提供我的算法。

Also note that when the number of points increases, the possible combinations also increase if we want more than 2 groups. 另请注意,当点数增加时,如果我们想要超过2组,可能的组合也会增加。

This is a tough problem! 这是一个棘手的问题! I've implemented a naive brute force solution. 我实施了一个天真的蛮力解决方案。 This is probably too slow but it's a starting point. 这可能太慢了,但这是一个起点。

from itertools import combinations, permutations

points = 'ABCDEF'
r = 3
m = len(points) // r
all_combinations = list(combinations(points, m))
group_length = len(all_combinations) // r
assert r * group_length == len(all_combinations)
found = set()

for combinations_permutation in permutations(all_combinations):
    groups = [combinations_permutation[group_length * i: group_length * (i + 1)]
              for i in range(r)]
    transpose = zip(*groups)

    # Avoid very similar-looking solutions
    canonical = frozenset(map(frozenset, transpose))

    if (canonical not in found and
            all(len(col) == len(set(col))
                for col in (sum(column, ()) for column in transpose))):
        found.add(canonical)
        for group in groups:
            print ', '.join(map(''.join, group))
        print '----'

Here's the start of the output: 这是输出的开始:

AB, AC, AD, AE, AF
CD, BE, BF, BD, BC
EF, DF, CE, CF, DE
----
AB, AC, AD, AE, AF
CD, BF, BE, BC, BD
EF, DE, CF, DF, CE
----
AB, AC, AD, AE, AF
CE, BD, BE, BF, BC
DF, EF, CF, CD, DE
----
AB, AC, AD, AE, AF
CE, BF, BC, BD, BE
DF, DE, EF, CF, CD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM