Click or drag to resize
Accord.NET (logo)

CombinatoricsSubsetsT Method (ISetT, Boolean)

Generates all possibles subsets of the given set.

Namespace:  Accord.Math
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public static IEnumerable<SortedSet<T>> Subsets<T>(
	this ISet<T> set,
	bool inPlace = false
)
Request Example View Source

Parameters

set
Type: System.Collections.GenericISetT
inPlace (Optional)
Type: SystemBoolean

Type Parameters

T

Return Value

Type: IEnumerableSortedSetT

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type ISetT. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Examples
// Let's say we would like to compute all the
// possible subsets 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))
{
    // ...
}

// Or we could try to compute them all and store in an array:
SortedSet<int>[] subsets = Combinatorics.Subsets(set).ToArray();

// In either case, the result will be:

int[][] expected =
{
   new [] { 1 },
   new [] { 2 },
   new [] { 3 },
   new [] { 4 },
   new [] { 1, 2 },
   new [] { 1, 3 },
   new [] { 2, 3 },
   new [] { 1, 4 },
   new [] { 2, 4 },
   new [] { 3, 4 },
   new [] { 1, 2, 3 },
   new [] { 1, 2, 4 },
   new [] { 1, 3, 4 },
   new [] { 2, 3, 4 },
   new [] { 1, 2, 3, 4 }
};

// Note: although the empty set is technically a subset
// of all sets, it won't be included in the enumeration
See Also