Tuesday, January 26, 2010

combinatoricslib: permutations without repetitions

I would like to introduce combinatoricslib. This is a library written on Java that contains various stuff to resolve different issues connected to combinatorics such as generating combinatorial objects and etc.

Today I am presenting permutations without repetitions. A permutation is an ordering of a set in the context of all possible orderings. For example, the set containing the first three digits, 123, has six permutations: 123, 132, 213, 231, 312, and 321.

This is a example of permutation of 3 string items: 

// create array of initial items
ArrayList<string> array = new ArrayList<string>();
array.add("one");
array.add("two");
array.add("three");

// create combinatorics vector
CombinatoricsVector<string> corePermutation = new CombinatoricsVector<string>(array);

// create permutation generator
Generator<string> generator = new PermutationGenerator<string>(corePermutation);

// print the number of generated permutations
System.out.println("Number of permutations is: " + gnerator.getNumberOfGeneratedObjects());

// create iterator
Iterator<combinatoricsvector<string>> iterator = generator.createIterator();
iterator.first();

// go through the iterator
while (!iterator.isDone()) {
iterator.next();
CombinatoricsVector<string> permutation = iterator.getCurrentItem();
System.out.println(permutation);
}


Output result is

Number of permutations is: 6
CombinatoricsVector=[[one, two, three]], size=3]
CombinatoricsVector=[[one, three, two]], size=3]
CombinatoricsVector=[[three, one, two]], size=3]
CombinatoricsVector=[[three, two, one]], size=3]
CombinatoricsVector=[[two, three, one]], size=3]
CombinatoricsVector=[[two, one, three]], size=3]

You can read more and download the library from here  

No comments: