Tuesday, 20 January 2015

Finding Subset


A subset is a set contained in another set. A set A is a subset of a set B if A is "contained" inside B.

Subsets of {a,b,c} are
[]
[a]
[b]
[a, b]
[c]
[a, c]
[b, c]
[a, b, c]

import org.paukov.combinatorics.Factory;
import org.paukov.combinatorics.Generator;
import org.paukov.combinatorics.ICombinatoricsVector;

public class SubsetEx {

    public static void main(String args[]) {
        // Create an initial vectorset
        ICombinatoricsVector<Character> initialSet = Factory.createVector(new Character[]{'a', 'b', 'c'});

        // Create an instance of the subset generator
        Generator<Character> gen = Factory.createSubSetGenerator(initialSet);

        // Print the subsets
        for (ICombinatoricsVector<Character> subSet : gen) {
            //System.out.println(subSet);
            System.out.println(subSet.getVector());
        }
    }
}


Output
[]
[a]
[b]
[a, b]
[c]
[a, c]
[b, c]
[a, b, c]

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment