python - Grouping of all possible combinations of points taken m at a time into r groups -


i want group possible combinations of points taken m @ time r groups.

points = [a,b,c,d........] total n points

combinations of these points taken m @ time list l

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

how can further group r groups such i-th element of each group has no similar points.

for example,

points = [a,b,c,d] m = 2 , r = 2

l = [[a,b],[a,c],[a,d],[b,c],[b,d],[c,d]]

so groups be

group 1 = [[a,b],[a,c],[a,d]] , corresponding group 2 = [[c,d],[b,d],[b,c]]

note : points in i-th index of group 1 , group 2 have no similar points.

i want n number of points taken m @ time , grouping r groups.

please provide me algorithm.

also note when number of points increases, possible combinations increase if want more 2 groups.

this tough problem! i've implemented naive brute force solution. slow it's 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()  combinations_permutation in permutations(all_combinations):     groups = [combinations_permutation[group_length * i: group_length * (i + 1)]               in range(r)]     transpose = zip(*groups)      # avoid similar-looking solutions     canonical = frozenset(map(frozenset, transpose))      if (canonical not in found ,             all(len(col) == len(set(col))                 col in (sum(column, ()) column in transpose))):         found.add(canonical)         group in groups:             print ', '.join(map(''.join, group))         print '----' 

here's start of 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, df, de, ef, cf, cd 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -