我有24个项目,我需要分成6组4,可以使用哪些算法来找出所有可能的组合?组合、我有、可以使用、算法

2023-09-11 23:07:59 作者:相关有意义的>>

我有24个独特的项目。

I have 24 unique items.

我需要将它们分成6组,每组由4个项目每。

I need to separate them into 6 groups comprised of 4 items each.

我可以使用哪些算法遍历这24项考虑到订单的所有可能的组合/分离是不相关的。

What algorithm can I use to iterate over all possible combinations / separations of these 24 items considering order is not relevant

例如,一种这样的组合将是:

For example, one such combination would be:

ABCD-EFGH,IJKL-MNOP-QRST-UVWX

ABCD-EFGH-IJKL-MNOP-QRST-UVWX

^由于顺序并不重要,这将是一样的:

^ Because order is not important, this would be the same as:

DCBA-HGFE-LKJI-POMN-TSRQ-XWVU

DCBA-HGFE-LKJI-POMN-TSRQ-XWVU

^但是他们不同于:

渔护署-EBGH-IJKL-MNOP-QRST-UVWX

AFCD-EBGH-IJKL-MNOP-QRST-UVWX

...这是一个独特的组合

...which is a unique combination.

所以只是重申:我试图找出如何得到所有可能的组合出24个项目考虑到我需要将它们分成6组,每组4

So just to reiterate: I'm trying to figure out how to get all possible combinations out of 24 items considering I need to separate them into 6 groups of 4.

另外,6组的排序也并不重要。含义:ABCD-EFGH,IJKL-MNOP-QRST-UVWX是一样的EFGH,IJKL-MNOP-QRST-UVWX-ABCD

Also, the ordering of the 6 groups is also not important. Meaning: "ABCD-EFGH-IJKL-MNOP-QRST-UVWX" is the same as "EFGH-IJKL-MNOP-QRST-UVWX-ABCD"

请注意:帮助实现这一点的Java将是巨大的。

NOTE: Help for implementing this in Java would be great.

谢谢!

编辑:

怎么样的解决方案,其中:

What about a solution where:

(1)我第一次尝试找出四个给定的24个项目的所有可能的群体。 (2)然后,我发现六给出#1产生的组中的所有可能的组

(1) I first try to find all possible groups of four given the 24 items. (2) Then I find all possible groups of six given the groups yielded by #1

思考?

推荐答案

攻击对这类问题的一个常见的​​角度来定义规范presentation对于要列举每一个对象,然后修剪使用递归。为了您的具体问题,我们规定,规范presentation都有每个组的排序,以及该群体本身的排序顺序字母。使用递归,我们在所有可能的方式如下。把在第一组。把乙在第一或第二组。如果A和B是在一起的,放下在第一或第二组中。如果A和B是分开,放下在第一,第二,或第三组。一般情况下,把每一个连续的字母在一组字母已经在它还是第一个空组。

A common angle of attack for this kind of problem is to define a canonical presentation for each object to be enumerated and then use recursion with pruning. For your particular problem, let's stipulate that the canonical presentation has the letters in each group sorted as well as the groups themselves in sorted order. Using recursion, we do the following in all possible ways. Put A in the first group. Put B in the first or second group. If A and B are together, put C in the first or second group. If A and B are apart, put C in the first, second, or third group. In general, put each successive letter in a group with letters already in it or in the first empty group.

Python的:

def combinations(unassigned, groupcountmax, groupsizemax, groups=()):
    if not unassigned:
        yield groups
    else:
        x, unassigned1 = unassigned[0], unassigned[1:]
        for i, group in enumerate(groups):
            if len(group) < groupsizemax:
                yield from combinations(unassigned1, groupcountmax, groupsizemax,
                                        groups[:i] + (group + x,) + groups[i+1:])
        if len(groups) < groupcountmax:
            yield from combinations(unassigned1, groupcountmax, groupsizemax,
                                    groups + (x,))

这将需要相当长的时间来列举的24项部门到6套4,因为有24!/(6!(4!)^ 6)〜他们4.5e12。

It will take quite a while to enumerate divisions of 24 items into 6 sets of 4, since there are 24!/(6! (4!)^6) ~ 4.5e12 of them.