CombinatoricsSubsetsT Method (ISetT, Int32, Boolean) |
Namespace: Accord.Math
public static IEnumerable<SortedSet<T>> Subsets<T>( this ISet<T> set, int k, bool inPlace = false )
// Let's say we would like to compute all the // subsets of size 2 of the set { 1, 2, 3, 4 }: // ISet<int> set = new HashSet<int> { 1, 2, 3, 4 }; // The number of possible subsets might be too large // to fit on memory. For this reason, we can compute // values on-the-fly using foreach: foreach (SortedSet<int> subset in Combinatorics.Subsets(set, k: 2)) { // ... } // Or we could try to compute them all and store in an array: SortedSet<int>[] subsets = Combinatorics.Subsets(set, k: 2).ToArray(); // In either case, the result will be: int[][] expected = { new [] { 1, 2 }, new [] { 1, 3 }, new [] { 2, 3 }, new [] { 1, 4 }, new [] { 2, 4 }, new [] { 3, 4 }, };