Tuesday, 20 January 2015

Get Permutations


permutation is all possible arrangements of a collection of things, where the order is important. If the order does not matter, it is a Combination.

for Example,
All the permutations for {a,b,c} are 6. Those are {a,b,c} {a,c,b} {b,a,c} {b,c,a} {c,a,b} {c,b,a}

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

public class PermutationEx {

    public static void main(String args[]) {
        // Create the initial vector of 3 elements (apple, orange, cherry)
        ICombinatoricsVector<String> originalVector = Factory.createVector(new String[]{"a", "b", "c"});

        // Create the permutation generator by calling the appropriate method in the Factory class
        Generator<String> gen = Factory.createPermutationGenerator(originalVector);

        // Print the result
        for (ICombinatoricsVector<String> perm : gen) {
            //System.out.println(perm);
            System.out.println(perm.getVector().toString());
        }
    }
}


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


Permutations with repetitions
import org.paukov.combinatorics.Factory;
import org.paukov.combinatorics.Generator;
import org.paukov.combinatorics.ICombinatoricsVector;

public class PermutationEx {

    public static void main(String args[]) {
        // Create the initial vector of 3 elements (apple, orange, cherry)
        ICombinatoricsVector<String> originalVector = Factory.createVector(new String[]{"a", "b", "c"});

        // Create the permutation generator by calling the appropriate method in the Factory class
        Generator<String> gen = Factory.createPermutationWithRepetitionGenerator(originalVector, 3);

        // Print the result
        for (ICombinatoricsVector<String> perm : gen) {
            //System.out.println(perm);
            System.out.println(perm.getVector().toString());
        }
    }
}


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

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment