sanjeeb-talukdar / combinatoricslib

Automatically exported from code.google.com/p/combinatoricslib
0 stars 0 forks source link

Add permutationLength parameter to createPermutationGenerator #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Problem: Generate all permutations of a given length where repetition is NOT 
allowed, from a larger set of elements.

Example: 

   // Create the initial vector of 3 elements (apple, orange, cherry)
   ICombinatoricsVector<String> originalVector = Factory.createVector(new String[] { "apple", "orange", "cherry", "raspberry" });

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

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

The result of X permutations, expected output:

   CombinatoricsVector=([apple, orange, cherry], size=3)
   CombinatoricsVector=([apple, cherry, orange], size=3)
   CombinatoricsVector=([cherry, apple, orange], size=3)
   CombinatoricsVector=([cherry, orange, apple], size=3)
   CombinatoricsVector=([orange, cherry, apple], size=3)
   CombinatoricsVector=([orange, apple, cherry], size=3)

   CombinatoricsVector=([raspberry, orange, cherry], size=3)
   CombinatoricsVector=([raspberry, cherry, orange], size=3)
   CombinatoricsVector=([cherry, raspberry, orange], size=3)
   CombinatoricsVector=([cherry, orange, raspberry], size=3)
   CombinatoricsVector=([orange, cherry, raspberry], size=3)
   CombinatoricsVector=([orange, raspberry, cherry], size=3)

   CombinatoricsVector=([apple, raspberry, cherry], size=3)
   CombinatoricsVector=([apple, cherry, raspberry], size=3)
   CombinatoricsVector=([cherry, apple, raspberry], size=3)
   CombinatoricsVector=([cherry, raspberry, apple], size=3)
   CombinatoricsVector=([raspberry, cherry, apple], size=3)
   CombinatoricsVector=([raspberry, apple, cherry], size=3)

   CombinatoricsVector=([apple, orange, raspberry], size=3)
   CombinatoricsVector=([apple, raspberry, orange], size=3)
   CombinatoricsVector=([raspberry, apple, orange], size=3)
   CombinatoricsVector=([raspberry, orange, apple], size=3)
   CombinatoricsVector=([orange, raspberry, apple], size=3)
   CombinatoricsVector=([orange, apple, raspberry], size=3)

Original issue reported on code.google.com by k...@kra.lc on 24 Apr 2015 at 8:23

GoogleCodeExporter commented 9 years ago
Why don't you use all permutations for all 3-combinations generated from the 
initial vector of 4 items?

Here is an example:

        // Create the initial vector of 4 elements (apple, orange, cherry, raspberry)
        ICombinatoricsVector<String> originalVector = Factory.createVector(
                new String[] { "apple", "orange", "cherry", "raspberry" }
        );

        // Create the combination generator by calling the appropriate method in the Factory class
        Generator<String> combinations = Factory.createSimpleCombinationGenerator(originalVector, 3);

        // Print all permutations for all simple 3-combinations
        for (ICombinatoricsVector<String> comb : combinations){
            Generator<String> permutations = Factory.createPermutationGenerator(comb);
            for(ICombinatoricsVector<String> perm : permutations){
                System.out.println(perm);
            }
        }

You will see the following result:
CombinatoricsVector=([apple, orange, cherry], size=3)
CombinatoricsVector=([apple, cherry, orange], size=3)
CombinatoricsVector=([cherry, apple, orange], size=3)
CombinatoricsVector=([cherry, orange, apple], size=3)
CombinatoricsVector=([orange, cherry, apple], size=3)
CombinatoricsVector=([orange, apple, cherry], size=3)
CombinatoricsVector=([apple, orange, raspberry], size=3)
CombinatoricsVector=([apple, raspberry, orange], size=3)
CombinatoricsVector=([raspberry, apple, orange], size=3)
CombinatoricsVector=([raspberry, orange, apple], size=3)
CombinatoricsVector=([orange, raspberry, apple], size=3)
CombinatoricsVector=([orange, apple, raspberry], size=3)
CombinatoricsVector=([apple, cherry, raspberry], size=3)
CombinatoricsVector=([apple, raspberry, cherry], size=3)
CombinatoricsVector=([raspberry, apple, cherry], size=3)
CombinatoricsVector=([raspberry, cherry, apple], size=3)
CombinatoricsVector=([cherry, raspberry, apple], size=3)
CombinatoricsVector=([cherry, apple, raspberry], size=3)
CombinatoricsVector=([orange, cherry, raspberry], size=3)
CombinatoricsVector=([orange, raspberry, cherry], size=3)
CombinatoricsVector=([raspberry, orange, cherry], size=3)
CombinatoricsVector=([raspberry, cherry, orange], size=3)
CombinatoricsVector=([cherry, raspberry, orange], size=3)
CombinatoricsVector=([cherry, orange, raspberry], size=3)

Original comment by d.pau...@gmail.com on 24 Apr 2015 at 9:07

GoogleCodeExporter commented 9 years ago
True. Thanks a lot! :-)

Original comment by k...@kra.lc on 24 Apr 2015 at 9:36

GoogleCodeExporter commented 9 years ago

Original comment by d.pau...@gmail.com on 24 Apr 2015 at 9:38