1

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.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
Harry
  • 11
  • 2

3 Answers3

1

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>}
Beta
  • 96,650
  • 16
  • 149
  • 150
0

I suppose algorythm is common, not C# algorythm. There are some approaches how to group all possible ways (with some restrictions) here

Community
  • 1
  • 1
Andrew Florko
  • 7,672
  • 10
  • 60
  • 107
0

This looks like a power set. There's some C# code in another SO answer over here.

Community
  • 1
  • 1
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345