Combinatorics grouping
I'm looking for an algorithm in c# that solves a combinatorics problem:
Assume i have the objects 1,2,3,4
I want to get all possible ways to group these object in multiple groups, that each time contain all objects. Order is not important. Example:
<1,2,3,4> <1,2 / 3,4> <1,3 / 2,4> <1,4 / 3,2> <1,2,3 / 4> <1,2,4 / 3> <1,3,4 / 2> <2,3,4 / 1> <1,2 / 3 / 4 > <1,3 / 2 / 4> <1,4 / 3 / 2> <2,3 / 1 / 4> <4,3 / 1 / 2> <1 / 2 / 3 / 4>
In the first case there is one group that contain all 4 objects. Next are cases with开发者_运维百科 2 groups that contain all objects in many different ways. The last case is 4 groups, that each one contains only one object.
Start with <1>. With the addition of each new object, for each of the previous solutions, it can go into any of the groups or a new group of its own.
1: <1>
12: <1> => {<1,2> <1|2>}
123: <1,2> => {<1,2,3> <1,2|3>}, <1|2> => {<1,3|2> <1|2,3> <1|2|3>}
1234: <1,2,3> => {<1,2,3,4> <1,2,3|4>},
<1,2|3> => {<1,2,4|3> <1,2|3,4> <1,2|3|4>},
<1,3|2> => {<1,3,4|2> <1,3|2,4> <1,3|2|4>}
I suppose algorythm is common, not C# algorythm. There are some approaches how to group all possible ways (with some restrictions) here
This looks like a power set. There's some C# code in another SO answer over here.
精彩评论