Thursday, January 28, 2010

combinatoricslib: permutations with repetitions

The permutation may have more elements than slots. For example, the three possible permutation of 12 in three slots are: 111, 211, 121, 221, 112, 212, 122, and 222.

Please read more here http://code.google.com/p/combinatoricslib/


This is a code:


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

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

// create permutation with repetition generator, second parameter is a number of slots
Generator<String> permutationWithRepetitionGenerator = new PermutationWithRepetitionGenerator<String>(corePermutation, 3);

// create iterator
Iterator<CombinatoricsVector<String>> permutationWithRepetitionIterator = permutationWithRepetitionGenerator.createIterator();

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

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

And the result

Number of permutationWithRepetition is: 8
PermutationWithRepetitionIterator=[#1, CombinatoricsVector=[[one, one, one]],size=3]]
PermutationWithRepetitionIterator=[#2, CombinatoricsVector=[[two, one, one]],size=3]]
PermutationWithRepetitionIterator=[#3, CombinatoricsVector=[[one, two, one]],size=3]]
PermutationWithRepetitionIterator=[#4, CombinatoricsVector=[[two, two, one]],size=3]]
PermutationWithRepetitionIterator=[#5, CombinatoricsVector=[[one, one, two]],size=3]]
PermutationWithRepetitionIterator=[#6, CombinatoricsVector=[[two, one, two]],size=3]]
PermutationWithRepetitionIterator=[#7, CombinatoricsVector=[[one, two, two]],size=3]]
PermutationWithRepetitionIterator=[#8, CombinatoricsVector=[[two, two, two]],size=3]]

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