How can I maximally partition a set?

后端 未结 6 1240
既然无缘
既然无缘 2021-01-03 14:17

I\'m trying to solve one of the Project Euler problems. As a consequence, I need an algorithm that will help me find all possible partitions of a set, in any order.

6条回答
  •  失恋的感觉
    2021-01-03 14:55

    Well, the problem has two aspects.

    Firsty, the items can be arranged in any order. So for N items, there are N! permutations (assuming the items are treated as unique).
    Secondly, you can envision the grouping as a bit flag between each item indicating a divide. There would be N-1 of these flags, so for a given permutation there would be 2^(N-1) possible groupings.
    This means that for N items, there would be a total of N!*(2^(N-1)) groupings/permutations, which gets big very very fast.

    In your example, the top four items are groupings of one permutation. The last item is a grouping of another permutation. Your items can be viewed as :

    2 on 3 off 3 off 5
    2 on 3 on 3 off 5
    2 on 3 off 3 on 5
    2 on 3 on 3 on 5
    2 off 5 on 3 off 3

    The permutations (the order of display) can be derived by looking at them like a tree, as mentioned by the other two. This would almost certainly involve recursion, such as here. The grouping is independent of them in many ways. Once you have all the permutations, you can link them with the groupings if needed.

提交回复
热议问题