├── src ├── main │ ├── java │ │ └── org │ │ │ └── paukov │ │ │ ├── tool │ │ │ ├── package.html │ │ │ └── IntExpressionParser.java │ │ │ └── combinatorics │ │ │ ├── package.html │ │ │ ├── IFilter.java │ │ │ ├── combination │ │ │ ├── multi │ │ │ │ ├── package.html │ │ │ │ ├── MultiCombinationGenerator.java │ │ │ │ └── MultiCombinationIterator.java │ │ │ └── simple │ │ │ │ ├── package.html │ │ │ │ ├── SimpleCombinationGenerator.java │ │ │ │ └── SimpleCombinationIterator.java │ │ │ ├── IGenerator.java │ │ │ ├── partition │ │ │ ├── package.html │ │ │ ├── PartitionGenerator.java │ │ │ └── PartitionIterator.java │ │ │ ├── cartesian │ │ │ ├── CartesianProductGenerator.java │ │ │ └── CartesianProductIterator.java │ │ │ ├── ICombinatoricsVector.java │ │ │ ├── composition │ │ │ ├── package.html │ │ │ ├── IntegerCompositionGenerator.java │ │ │ ├── IntegerCompositionIterator.java │ │ │ ├── CompositionIterator.java │ │ │ └── CompositionGenerator.java │ │ │ ├── util │ │ │ ├── package.html │ │ │ └── ComplexCombinationIterator.java │ │ │ ├── IntegerGenerator.java │ │ │ ├── subsets │ │ │ ├── package.html │ │ │ ├── IntegerSubListIterator.java │ │ │ ├── SubListIterator.java │ │ │ ├── SubSetIterator.java │ │ │ ├── IntegerSubSetIterator.java │ │ │ ├── IntegerSubSetGenerator.java │ │ │ └── SubSetGenerator.java │ │ │ ├── permutations │ │ │ ├── package.html │ │ │ ├── PermutationIterator.java │ │ │ ├── PermutationWithRepetitionIterator.java │ │ │ ├── PermutationWithRepetitionGenerator.java │ │ │ ├── PermutationGenerator.java │ │ │ └── DuplicatedPermutationIterator.java │ │ │ ├── Generator.java │ │ │ ├── IntegerFactory.java │ │ │ ├── IntegerVector.java │ │ │ └── CombinatoricsVector.java │ └── resources │ │ └── META-INF │ │ └── README.txt └── test │ └── java │ └── org │ └── paukov │ ├── tool │ └── IntExpressionPareserTest.java │ └── combinatorics │ ├── CombinatoricsFactoryTest.java │ ├── IntegerFactoryTest.java │ ├── cartesian │ └── CartesianProductTest.java │ ├── util │ └── UtilTest.java │ ├── IntegerVectorTest.java │ ├── CombinatoricsVectorTest.java │ ├── composition │ ├── IntegerCompositionTest.java │ └── CompositionTest.java │ └── partition │ └── PartitionsTest.java ├── .travis.yml ├── .gitignore ├── .project ├── .classpath ├── pom.xml └── LICENSE /src/main/java/org/paukov/tool/package.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | This package contains various tools. 6 | 7 | 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | dist: trusty 4 | 5 | jdk: 6 | - openjdk7 7 | - oraclejdk8 8 | 9 | script: mvn clean cobertura:cobertura coveralls:report 10 | -------------------------------------------------------------------------------- /src/main/java/org/paukov/combinatorics/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | This package contains common classes and interfaces of the library such as 6 |ICombinatoricsVector, Generator and CombinatoricsFactory.
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Eclipse
2 | .classpath
3 | .project
4 | .settings/
5 |
6 | # Intellij
7 | .idea/
8 | *.iml
9 | *.iws
10 |
11 | # Mac
12 | .DS_Store
13 |
14 | # Maven
15 | log/
16 | target/
17 |
18 | #Other files
19 | *.class
20 |
21 | # Mobile Tools for Java (J2ME)
22 | .mtj.tmp/
23 |
24 | # Package Files #
25 | *.jar
26 | *.war
27 | *.ear
28 |
29 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
30 | hs_err_pid*
--------------------------------------------------------------------------------
/src/test/java/org/paukov/tool/IntExpressionPareserTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Combinatorics Library
3 | * Copyright 2012 Dmytro Paukov d.paukov@gmail.com
4 | */
5 | package org.paukov.tool;
6 |
7 | import static org.junit.Assert.assertEquals;
8 |
9 | import org.junit.Test;
10 | import org.paukov.tool.IntExpressionParser;
11 |
12 | /**
13 | * @author Dmytro Paukov
14 | *
15 | */
16 | public class IntExpressionPareserTest {
17 |
18 | @Test
19 | public void simpleTest() {
20 | String expr = "1+2*3*4+3*(2+2)-10";
21 | int result = IntExpressionParser.eval(expr);
22 | assertEquals(27, result);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 | true if the value is accepted, otherwise false.
24 | */
25 | boolean accepted(long index, T value);
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 | 14 | As an example. Suppose there are 2 types of fruits (apple and orange) at a 15 | grocery store, and you want to buy 3 pieces of fruit. You could select 16 |
23 | Generate 3-combinations with repetitions of the set (apple, orange). 24 |
25 |
26 |
27 |
28 | // Create the initial vector of (apple, orange)
29 | ICombinatoricsVector<String> initialVector = CombinatoricsFactory.createVector(new String[] {
30 | "apple", "orange" });
31 |
32 | // Create a multi-combination generator to generate 3-combinations of
33 | // the initial vector
34 | Generator<String> gen = CombinatoricsFactory.createMultiCombinationGenerator(initialVector,3);
35 |
36 | // Print all possible combinations
37 | for (ICombinatoricsVector<String> combination : gen) {
38 | System.out.println(combination);
39 | }
40 |
41 |
42 | 43 | And the result of 4 multi-combinations 44 |
45 |
46 |53 |47 | CombinatoricsVector=([apple, apple, apple], size=3) 48 | CombinatoricsVector=([apple, apple, orange], size=3) 49 | CombinatoricsVector=([apple, orange, orange], size=3) 50 | CombinatoricsVector=([orange, orange, orange], size=3) 51 |52 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/src/main/java/org/paukov/combinatorics/IGenerator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Combinatorics Library
3 | * Copyright 2012 Dmytro Paukov d.paukov@gmail.com
4 | */
5 | package org.paukov.combinatorics;
6 |
7 | import java.util.Iterator;
8 | import java.util.List;
9 |
10 | /**
11 | * This interface is implemented by all generic generators in the library.
12 | *
13 | * @param
9 | A simple k-combination of a finite set S is a subset of k distinct elements
10 | of S. Specifying a subset does not arrange them in a particular order. As an
11 | example, a poker hand can be described as a 5-combination of cards from a
12 | 52-card deck: the 5 cards of the hand are all distinct, and the order of the
13 | cards in the hand does not matter.
14 |
15 | For example, let's generate 3-combination of the set (red, black, white, green, blue).
16 |
17 |
34 | And the result of 10 combinations
35 |
36 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/src/main/java/org/paukov/combinatorics/partition/package.html:
--------------------------------------------------------------------------------
1 |
2 |
8 | In number theory, a partition of a positive integer n is a way of writing n
9 | as a sum of positive integers. Two sums that differ only in the order of
10 | their summands are considered to be the same partition; if order matters then
11 | the sum becomes a composition. A summand in a partition is also called a
12 | part.
13 |
14 | WARNING! Be careful because number of all partitions can be very high even
15 | for not great given N.
16 |
17 | The partitions of 5 are listed below:
18 |
28 | The number of partitions of n is given by the partition function p(n). In
29 | number theory, the partition function p(n) represents the number of possible
30 | partitions of a natural number n, which is to say the number of distinct (and
31 | order independent) ways of representing n as a sum of natural numbers.
32 |
33 | Let's generate all possible partitions of 5:
34 |
35 |
50 | And the result of all 7 integer possible partitions
51 |
52 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/src/main/java/org/paukov/combinatorics/cartesian/CartesianProductGenerator.java:
--------------------------------------------------------------------------------
1 | package org.paukov.combinatorics.cartesian;
2 |
3 | import static org.paukov.combinatorics.CombinatoricsFactory.createVector;
4 |
5 | import java.util.Iterator;
6 | import org.paukov.combinatorics.Generator;
7 | import org.paukov.combinatorics.ICombinatoricsVector;
8 |
9 | /**
10 | * This generator generates Cartesian product from specified multiple lists. Set of lists is
11 | * specified in the constructor of generator to generate k-element Cartesian product, where k is the
12 | * size of the set of lists.
13 | *
14 | * A simple k-element Cartesian product of a finite sets S(1), S(2)...S(k) is a set of all ordered
15 | * pairs (x(1), x(2)...x(k), where x(1) ∈ S(1), x(2) ∈ S(2) ... x(k) ∈ S(k)
16 | *
17 | * Example. Generate 3-element Cartesian product from (1, 2, 3), (4, 5, 6), (7, 8, 9).
18 | *
19 | *
8 | A composition of an integer n is a way of writing n as the sum of a sequence
9 | of (strictly) positive integers. This class generates the composition if a
10 | positive integer value.
11 |
12 | A composition of an integer n is a way of writing n as the sum of a sequence
13 | of (strictly) positive integers. Two sequences that differ in the order of
14 | their terms define different compositions of their sum, while they are
15 | considered to define the same partition of that number.
16 |
17 | The sixteen compositions of 5 are:
18 |
37 | Compare this with the seven partitions of 5:
38 |
48 | Example. Generate compositions all possible integer compositions of 5.
49 |
50 |
64 | And the result
65 |
66 |
89 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/src/main/java/org/paukov/combinatorics/util/package.html:
--------------------------------------------------------------------------------
1 |
2 |
8 | It is possible to generate non-overlapping sublists of length n of a given list
9 |
10 | For example, if a list is (A, B, B, C), then the non-overlapping sublists of length 2 will be:
11 |
24 | To do that you should use an instance of the complex combination generator
25 |
26 |
42 | And the result
43 |
44 |
8 | A set A is a subset of a set B if A is "contained" inside B. A and B may
9 | coincide. The relationship of one set being a subset of another is called
10 | inclusion or sometimes containment.
11 |
12 | Examples:
13 |
19 | All subsets of (1, 2, 3) are:
20 |
31 | And code which generates all subsets of (one, two, three)
32 |
33 |
50 | And the result of all 8 possible subsets
51 |
52 |
67 | Version 2.0 of the combinatoricslib supports sets with duplicates. For
68 | example, if the original vector contains duplicates like (a, b, a, c), then
69 | the result will contain 14 subsets (instead of 16):
70 |
90 | If you still would like to treat the set with duplicates as not identical,
91 | you should create a generator and set the second parameter of the method
92 |
95 | Note. If the initial vector contains duplicates then the method
96 |
99 |
100 |
101 |
102 |
103 |
--------------------------------------------------------------------------------
/src/test/java/org/paukov/combinatorics/cartesian/CartesianProductTest.java:
--------------------------------------------------------------------------------
1 | package org.paukov.combinatorics.cartesian;
2 |
3 | import static org.junit.Assert.assertEquals;
4 | import static org.paukov.combinatorics.CombinatoricsFactory.createCartesianProductGenerator;
5 | import static org.paukov.combinatorics.CombinatoricsFactory.createVector;
6 |
7 | import java.util.Iterator;
8 | import java.util.List;
9 | import org.junit.Test;
10 | import org.paukov.combinatorics.CombinatoricsFactory;
11 | import org.paukov.combinatorics.Generator;
12 | import org.paukov.combinatorics.ICombinatoricsVector;
13 |
14 | /**
15 | * Created by dpaukov on 5/3/18.
16 | */
17 | public class CartesianProductTest {
18 |
19 | @Test
20 | public void test_cartesian_product() {
21 |
22 | ICombinatoricsVector
13 | This is an example of the permutations of 3 string items (apple, orange, cherry):
14 |
15 |
30 | And the result
31 |
32 |
43 |
44 |
46 | The permutation may have more elements than slots. For example, the three
47 | possible permutation of 12 in three slots are: 111, 211, 121, 221, 112, 212,
48 | 122, and 222.
49 |
50 | Let's generate all possible permutations with repetitions of 3 elements from
51 | the set of apple and orange:
52 |
53 |
68 | And the result
69 |
70 |
83 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/src/main/java/org/paukov/combinatorics/subsets/SubSetIterator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Combinatorics Library
3 | * Copyright 2012 Dmytro Paukov d.paukov@gmail.com
4 | */
5 | package org.paukov.combinatorics.subsets;
6 |
7 | import static org.paukov.combinatorics.CombinatoricsFactory.createVector;
8 |
9 | import java.util.Iterator;
10 | import org.paukov.combinatorics.Generator;
11 | import org.paukov.combinatorics.ICombinatoricsVector;
12 |
13 | /**
14 | * Iterator over the all subsets
15 | *
16 | * @param
19 | * A simple k-combination of a finite set S is a subset of k distinct elements
20 | * of S. Specifying a subset does not arrange them in a particular order. As an
21 | * example, a poker hand can be described as a 5-combination of cards from a
22 | * 52-card deck: the 5 cards of the hand are all distinct, and the order of the
23 | * cards in the hand does not matter.
24 | *
25 | * Example. Generate 3-combination of the set (red, black, white, green, blue).
26 | *
27 | *
49 | *
50 | * @param
19 | * A k-multicombination or k-combination with repetition of a finite set S is
20 | * given by a sequence of k not necessarily distinct elements of S, where order
21 | * is not taken into account.
22 | *
23 | * As an example. Suppose there are 2 types of fruits (apple and orange) at a
24 | * grocery store, and you want to buy 3 pieces of fruit. You could select
25 | *
32 | * Generate 3-combinations with repetitions of the set (apple, orange).
33 | *
34 | *
35 | *
55 | *
56 | * @param
20 | * WARNING. Be careful because number of all partitions can be very high even
21 | * for not great given N.
22 | *
23 | * The partitions of 5 are listed below:
24 | *
34 | * The number of partitions of n is given by the partition function p(n). In
35 | * number theory, the partition function p(n) represents the number of possible
36 | * partitions of a natural number n, which is to say the number of distinct (and
37 | * order independent) ways of representing n as a sum of natural numbers.
38 | *
39 | * Let's generate all possible partitions of 5:
40 | *
41 | *
57 | * And the result of all 7 integer possible partitions
58 | *
59 | *
73 | *
74 | * @author Dmytro.Paukov
75 | * @version 2.0
76 | * @see ICombinatoricsVector
77 | * @see PartitionIterator
78 | */
79 | public class PartitionGenerator extends Generator
114 | * Note.Exact value of number of partitions can be obtained from
115 | * actual generated list of partitions.
116 | *
117 | * WARNING! Be careful because number of all partitions can be very
118 | * high even for not great given value of N.
119 | */
120 | @Override
121 | public long getNumberOfGeneratedObjects() {
122 |
123 | if (_initialValue == 0) {
124 | return 0;
125 | }
126 |
127 | if (_initialValue > 0 && _initialValue <= MAXN) {
128 | double result = 2.0 * _initialValue / 3.0;
129 | result = Math.exp(Math.PI * Math.sqrt(result));
130 | result /= 4.0 * _initialValue * Math.sqrt(3);
131 | return (long) result;
132 | }
133 |
134 | throw new RuntimeException(
135 | "Unable to calculate the number of the partitions for the value N=" + _initialValue);
136 | }
137 |
138 | }
139 |
--------------------------------------------------------------------------------
/src/test/java/org/paukov/combinatorics/util/UtilTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Combinatorics Library
3 | * Copyright 2012 Dmytro Paukov d.paukov@gmail.com
4 | */
5 | package org.paukov.combinatorics.util;
6 |
7 | import static org.junit.Assert.assertEquals;
8 |
9 | import java.math.BigDecimal;
10 | import org.junit.Test;
11 |
12 | /**
13 | * @author Dmytro Paukov
14 | */
15 | public class UtilTest {
16 |
17 | private static BigDecimal[][] toBigDecimal(double[][] a) {
18 | int n = a.length;
19 | BigDecimal[][] b = new BigDecimal[n][n];
20 | for (int i = 0; i < n; i++) {
21 | for (int j = 0; j < n; j++) {
22 | b[i][j] = BigDecimal.valueOf(a[i][j]);
23 | }
24 | }
25 | return b;
26 | }
27 |
28 | @Test
29 | public void determinantCrout1Test() {
30 |
31 | BigDecimal result = Util.detCrout(toBigDecimal(new double[][]{
32 | {2, 4, 3, 5, 4}, {5, 4, 0, 2, 4}, {0, 5, 5, 2, 3},
33 | {1, 0, 4, 3, 0}, {0, 5, 1, 4, 4}}), 5);
34 |
35 | assertEquals(279.0, result.doubleValue(), 1e-10);
36 | }
37 |
38 | @Test
39 | public void determinantCrout2Test() {
40 |
41 | BigDecimal result = Util.detCrout(toBigDecimal(new double[][]{
42 | {3, 2, 2}, {0, 0, 5}, {4, 3, 1}}), 3);
43 |
44 | assertEquals(-5.0, result.doubleValue(), 1e-10);
45 | }
46 |
47 | @Test
48 | public void determinantCrout3Test() {
49 |
50 | BigDecimal result = Util.detCrout(toBigDecimal(new double[][]{
51 | {2, 2, 2}, {1, 2, 0}, {2, 2, 0}}), 3);
52 |
53 | assertEquals(-4.0, result.doubleValue(), 1e-10);
54 | }
55 |
56 |
57 | @Test
58 | public void test_pow2() {
59 | assertEquals(1L, Util.pow2(-1));
60 | assertEquals(1L, Util.pow2(0));
61 | assertEquals(2L, Util.pow2(1));
62 | assertEquals(4L, Util.pow2(2));
63 | assertEquals(8L, Util.pow2(3));
64 | assertEquals(16L, Util.pow2(4));
65 | assertEquals(32L, Util.pow2(5));
66 | assertEquals(64L, Util.pow2(6));
67 |
68 | }
69 |
70 | @Test
71 | public void test_factorial() {
72 | assertEquals(1L, Util.factorial(-1));
73 | assertEquals(1L, Util.factorial(0));
74 | assertEquals(1L, Util.factorial(1));
75 | assertEquals(2L, Util.factorial(2));
76 | assertEquals(6L, Util.factorial(3));
77 | assertEquals(24L, Util.factorial(4));
78 | assertEquals(3628800L, Util.factorial(10));
79 | assertEquals(2432902008176640000L, Util.factorial(20));
80 | }
81 |
82 | @Test
83 | public void test_combination() {
84 | assertEquals(1L, Util.combination(21, 0));
85 | assertEquals(21L, Util.combination(21, 1));
86 | assertEquals(210L, Util.combination(21, 2));
87 | assertEquals(1L, Util.combination(21, 21));
88 | }
89 |
90 | @Test
91 | public void test_big_factorial() {
92 | assertEquals(BigDecimal.ONE, Util.bigFactorial(-1));
93 | assertEquals(BigDecimal.ONE, Util.bigFactorial(0));
94 | assertEquals(BigDecimal.ONE, Util.bigFactorial(1));
95 | assertEquals(BigDecimal.valueOf(2), Util.bigFactorial(2));
96 | assertEquals(BigDecimal.valueOf(6), Util.bigFactorial(3));
97 | assertEquals(BigDecimal.valueOf(24), Util.bigFactorial(4));
98 | assertEquals(BigDecimal.valueOf(3628800), Util.bigFactorial(10));
99 | assertEquals(new BigDecimal("1307674368000"), Util.bigFactorial(15));
100 | assertEquals(new BigDecimal("2432902008176640000"), Util.bigFactorial(20));
101 | assertEquals(new BigDecimal("51090942171709440000"), Util.bigFactorial(21));
102 | }
103 |
104 | @Test
105 | public void test_minimum() {
106 | assertEquals(1, Util.minimum(1, 2, 3));
107 | assertEquals(1, Util.minimum(2, 1, 3));
108 | assertEquals(1, Util.minimum(2, 3, 1));
109 | }
110 |
111 | @Test
112 | public void test_gcd() {
113 | assertEquals(6, Util.gcd(12, 18));
114 | assertEquals(1, Util.gcd(103, 45));
115 | assertEquals(2, Util.gcd(64, 34));
116 |
117 | assertEquals(45, Util.gcd(0, 45));
118 | assertEquals(64, Util.gcd(64, 0));
119 | }
120 |
121 | @Test
122 | public void test_lcm() {
123 | assertEquals(36, Util.lcm(12, 18));
124 | assertEquals(2070, Util.lcm(46, 45));
125 | assertEquals(330, Util.lcm(66, 15));
126 | }
127 |
128 | @Test
129 | public void test_levenshteinDistance() {
130 | assertEquals(1, Util.levenshteinDistance("hello", "helo"));
131 | assertEquals(2, Util.levenshteinDistance("america", "amerigo"));
132 | assertEquals(4, Util.levenshteinDistance("abba", "abcadaba"));
133 |
134 | assertEquals(5, Util.levenshteinDistance("hello", ""));
135 | assertEquals(5, Util.levenshteinDistance("", "hello"));
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/src/main/java/org/paukov/combinatorics/partition/PartitionIterator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Combinatorics Library
3 | * Copyright 2012 Dmytro Paukov d.paukov@gmail.com
4 | */
5 | package org.paukov.combinatorics.partition;
6 |
7 | import java.util.ArrayList;
8 | import java.util.Iterator;
9 | import java.util.List;
10 | import org.paukov.combinatorics.CombinatoricsFactory;
11 | import org.paukov.combinatorics.ICombinatoricsVector;
12 |
13 | /**
14 | * Iterator for enumerating the all possible integer partitions
15 | *
16 | * @author Dmytro Paukov
17 | * @version 2.0
18 | * @see ICombinatoricsVector
19 | * @see PartitionGenerator
20 | */
21 | class PartitionIterator implements
22 | Iterator
19 | * A permutation is an ordering of a set in the context of all possible
20 | * orderings. For example, the set containing the first three digits, 123, has
21 | * six permutations: 123, 132, 213, 231, 312, and 321.
22 | *
23 | * This is an example of the permutations of 3 string items (apple, orange,
24 | * cherry):
25 | *
26 | *
46 | * And the result
47 | *
48 | *
61 | *
62 | * @param
17 | * A set A is a subset of a set B if A is "contained" inside B. A and B may
18 | * coincide. The relationship of one set being a subset of another is called
19 | * inclusion or sometimes containment.
20 | *
21 | * Examples:
22 | *
28 | * All subsets of (1, 2, 3) are:
29 | *
40 | * And code which generates all subsets of (1, 2, 3)
41 | *
42 | *
60 | * And the result of all 8 possible subsets
61 | *
62 | *
77 | * Version 2.0 of the combinatoricslib supports sets with duplicates. For
78 | * example, if the original vector contains duplicates like (1, 2, 1, 3), then
79 | * the result will contain 14 subsets (instead of 16):
100 | * If you still would like to treat the set with duplicates as not identical,
101 | * you should create a generator and set the second parameter of the method
102 | *
105 | * Note. If the initial vector contains duplicates then the method
106 | *
18 | * A set A is a subset of a set B if A is "contained" inside B. A and B may
19 | * coincide. The relationship of one set being a subset of another is called
20 | * inclusion or sometimes containment.
21 | *
22 | * Examples:
23 | *
29 | * All subsets of (1, 2, 3) are:
30 | *
41 | * And code which generates all subsets of (one, two, three)
42 | *
43 | *
61 | * And the result of all 8 possible subsets
62 | *
63 | *
78 | * Version 2.0 of the combinatoricslib supports sets with duplicates. For
79 | * example, if the original vector contains duplicates like (a, b, a, c), then
80 | * the result will contain 14 subsets (instead of 16):
101 | * If you still would like to treat the set with duplicates as not identical,
102 | * you should create a generator and set the second parameter of the method
103 | *
106 | *
107 | * Note. If the initial vector contains duplicates then the method
108 | * startIndex to stopIndex).
60 | *
61 | * @return List of the generated objects/vectors.
62 | */
63 | List Simple combinations
8 |
18 |
33 |
19 | // Create the initial vector
20 | ICombinatoricsVector<String> initialVector = CombinatoricsFactory.createVector(new String[] {
21 | "red", "black", "white", "green", "blue" });
22 |
23 | // Create a simple combination generator to generate 3-combinations of the
24 | // initial vector
25 | Generator<String> gen = CombinatoricsFactory.createSimpleCombinationGenerator(initialVector, 3);
26 |
27 | // Print all possible combinations
28 | for (ICombinatoricsVector<String> combination : gen) {
29 | System.out.println(combination);
30 | }
31 |
32 |
37 |
50 |
38 | CombinatoricsVector=([red, black, white], size=3)
39 | CombinatoricsVector=([red, black, green], size=3)
40 | CombinatoricsVector=([red, black, blue], size=3)
41 | CombinatoricsVector=([red, white, green], size=3)
42 | CombinatoricsVector=([red, white, blue], size=3)
43 | CombinatoricsVector=([red, green, blue], size=3)
44 | CombinatoricsVector=([black, white, green], size=3)
45 | CombinatoricsVector=([black, white, blue], size=3)
46 | CombinatoricsVector=([black, green, blue], size=3)
47 | CombinatoricsVector=([white, green, blue], size=3)
48 |
49 |
19 |
27 |
36 |
37 |
49 |
38 | // Create an instance of the partition generator to generate all
39 | // possible partitions of 5
40 | Generator<Integer> gen = CombinatoricsFactory.createPartitionGenerator(5);
41 |
42 | // Print the partitions
43 | for (ICombinatoricsVector<Integer> p : gen) {
44 | System.out.println(p);
45 | }
46 |
47 |
48 |
53 |
63 |
54 | CombinatoricsVector=([1, 1, 1, 1, 1], size=5)
55 | CombinatoricsVector=([2, 1, 1, 1], size=4)
56 | CombinatoricsVector=([2, 2, 1], size=3)
57 | CombinatoricsVector=([3, 1, 1], size=3)
58 | CombinatoricsVector=([3, 2], size=2)
59 | CombinatoricsVector=([4, 1], size=2)
60 | CombinatoricsVector=([5], size=1)
61 |
62 |
20 | * ICombinatoricsVector<Integer> set01 = createVector(1, 2, 3);
21 | * ICombinatoricsVector<Integer> set02 = createVector(4, 5, 6);
22 | * ICombinatoricsVector<Integer> set03 = createVector(7, 8, 9);
23 | *
24 | * Generator<Integer> cartesianProduct = createCartesianProductGenerator(set01, set02, set03);
25 | *
26 | *
27 | * @param index. If the index is out of bounds
22 | * the value will be added at the end of the vector
23 | *
24 | * @param index Position of element
25 | * @param value Value of the element to be set
26 | */
27 | void setValue(int index, T value);
28 |
29 | /**
30 | * Adds value to the vector.
31 | *
32 | * @param value Value of the element to be added
33 | */
34 | boolean addValue(T value);
35 |
36 | /**
37 | * Returns value of the index-element
38 | *
39 | * @param index The position of the element (Index of the element)
40 | * @return Value of the element
41 | */
42 | T getValue(int index);
43 |
44 | /**
45 | * Returns size of the vector
46 | *
47 | * @return Current size of the vector
48 | */
49 | int getSize();
50 |
51 | /**
52 | * This method detects duplicates in the vector
53 | *
54 | * @return true if the vector has duplicates, otherwise false
55 | */
56 | boolean hasDuplicates();
57 |
58 | /**
59 | * This method calculates the count of duplicates of a given value
60 | *
61 | * @return Number of the duplicates within the vector
62 | */
63 | int countElements(T value);
64 |
65 | /**
66 | * This method detects if the victor contains a given value
67 | *
68 | * @return true if the vector contains the value, otherwise false
69 | */
70 | boolean contains(T value);
71 |
72 | /**
73 | * This method detects if the vector's elements are equal. For example, this
74 | * method returns true for a vector (a, a, a) and false for a vector (a, b, a)
75 | *
76 | * @return True if the vector's elements are equal
77 | */
78 | boolean isAllElementsEqual();
79 |
80 | /**
81 | * Returns vector as a list of elements
82 | *
83 | * @return List of all elements
84 | */
85 | List
19 |
36 |
39 |
47 |
51 |
52 |
63 |
53 | // Create an instance of the integer composition generator to generate all possible compositions of 5
54 | Generator<Integer> gen = CombinatoricsFactory.createCompositionGenerator(5);
55 |
56 | // Print the compositions
57 | for (ICombinatoricsVector<Integer> p : gen) {
58 | System.out.println(p);
59 | }
60 |
61 |
62 |
67 |
68 |
88 |
69 | CombinatoricsVector=([5], size=1)
70 | CombinatoricsVector=([1, 4], size=2)
71 | CombinatoricsVector=([2, 3], size=2)
72 | CombinatoricsVector=([1, 1, 3], size=3)
73 | CombinatoricsVector=([3, 2], size=2)
74 | CombinatoricsVector=([1, 2, 2], size=3)
75 | CombinatoricsVector=([2, 1, 2], size=3)
76 | CombinatoricsVector=([1, 1, 1, 2], size=4)
77 | CombinatoricsVector=([4, 1], size=2)
78 | CombinatoricsVector=([1, 3, 1], size=3)
79 | CombinatoricsVector=([2, 2, 1], size=3)
80 | CombinatoricsVector=([1, 1, 2, 1], size=4)
81 | CombinatoricsVector=([3, 1, 1], size=3)
82 | CombinatoricsVector=([1, 2, 1, 1], size=4)
83 | CombinatoricsVector=([2, 1, 1, 1], size=4)
84 | CombinatoricsVector=([1, 1, 1, 1, 1], size=5)
85 |
86 |
87 |
12 |
23 |
27 |
41 |
28 | // create a vector (A, B, B, C)
29 | ICombinatoricsVector<String> vector = Factory.createVector(new String[] { "A", "B", "B", "C" });
30 |
31 | // Create a complex-combination generator
32 | Generator<ICombinatoricsVector<String>> gen = new ComplexCombinationGenerator<String>(vector, 2);
33 |
34 | // Iterate the combinations
35 | for (ICombinatoricsVector<ICombinatoricsVector<String>> comb : gen) {
36 | System.out.println(ComplexCombinationGenerator.convert2String(comb) + " - " + comb);
37 | }
38 |
39 |
40 |
45 |
46 |
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/src/main/java/org/paukov/combinatorics/IntegerGenerator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Combinatorics Library
3 | * Copyright 2012 Dmytro Paukov d.paukov@gmail.com
4 | */
5 | package org.paukov.combinatorics;
6 |
7 | import java.util.ArrayList;
8 | import java.util.Iterator;
9 | import java.util.List;
10 |
11 | /**
12 | * This is a special abstract class for all integer generators.
13 | *
14 | * There is a general pattern how to use the integer generators.
15 | *
16 | *
47 | ([A],[B, B, C]) - CombinatoricsVector=([CombinatoricsVector=([A], size=1), CombinatoricsVector=([B, B, C], size=3)], size=2)
48 | ([B, B, C],[A]) - CombinatoricsVector=([CombinatoricsVector=([B, B, C], size=3), CombinatoricsVector=([A], size=1)], size=2)
49 | ([B],[A, B, C]) - CombinatoricsVector=([CombinatoricsVector=([B], size=1), CombinatoricsVector=([A, B, C], size=3)], size=2)
50 | ([A, B, C],[B]) - CombinatoricsVector=([CombinatoricsVector=([A, B, C], size=3), CombinatoricsVector=([B], size=1)], size=2)
51 | ([A, B],[B, C]) - CombinatoricsVector=([CombinatoricsVector=([A, B], size=2), CombinatoricsVector=([B, C], size=2)], size=2)
52 | ([B, C],[A, B]) - CombinatoricsVector=([CombinatoricsVector=([B, C], size=2), CombinatoricsVector=([A, B], size=2)], size=2)
53 | ([B, B],[A, C]) - CombinatoricsVector=([CombinatoricsVector=([B, B], size=2), CombinatoricsVector=([A, C], size=2)], size=2)
54 | ([A, C],[B, B]) - CombinatoricsVector=([CombinatoricsVector=([A, C], size=2), CombinatoricsVector=([B, B], size=2)], size=2)
55 | ([A, B, B],[C]) - CombinatoricsVector=([CombinatoricsVector=([A, B, B], size=3), CombinatoricsVector=([C], size=1)], size=2)
56 | ([C],[A, B, B]) - CombinatoricsVector=([CombinatoricsVector=([C], size=1), CombinatoricsVector=([A, B, B], size=3)], size=2)
57 |
58 |
59 |
17 | * // create an integer vector
18 | * IntegerVector vector = new IntegerVector(new int[] { elements });
19 | *
20 | * // create a concrete integer generator
21 | * IntegerGenerator generator = IntegerFactory.create < Concrete > Generator(vector);
22 | *
23 | * // Iterate the generated objects
24 | * for (IntegerVector v : generator) {
25 | * System.out.println(v);
26 | * }
27 | *
28 | *
29 | * @author Dmytro Paukov
30 | * @version 2.0
31 | * @see IntegerVector
32 | * @see IntegerFactory
33 | * @see IGenerator
34 | */
35 | public abstract class IntegerGenerator implements IGeneratorstartIndex to stopIndex)
71 | *
72 | * @return List of the generated objects/vectors
73 | */
74 | @Override
75 | public List
14 |
18 |
21 |
30 |
34 |
49 |
35 | // Create an initial vector/set
36 | ICombinatoricsVector<String> initialSet = CombinatoricsFactory.createVector(new String[] {
37 | "one", "two", "three" });
38 |
39 | // Create an instance of the subset generator
40 | Generator<String> gen = CombinatoricsFactory.createSubSetGenerator(initialSet);
41 |
42 | // Print the subsets
43 | for (ICombinatoricsVector<String> subSet : gen) {
44 | System.out.println(subSet);
45 | }
46 |
47 |
48 |
53 |
54 |
66 |
55 | CombinatoricsVector=([], size=0)
56 | CombinatoricsVector=([one], size=1)
57 | CombinatoricsVector=([two], size=1)
58 | CombinatoricsVector=([one, two], size=2)
59 | CombinatoricsVector=([three], size=1)
60 | CombinatoricsVector=([one, three], size=2)
61 | CombinatoricsVector=([two, three], size=2)
62 | CombinatoricsVector=([one, two, three], size=3)
63 |
64 |
65 |
71 |
72 |
89 |
73 | ()
74 | (a)
75 | (b)
76 | (a, b)
77 | (a, a)
78 | (b, a)
79 | (a, b, a)
80 | (c)
81 | (a, c)
82 | (b, c)
83 | (a, b, c)
84 | (a, a, c)
85 | (b, a, c)
86 | (a, b, a, c)
87 |
88 | CombinatoricsFactory.createSubSetGenerator() as false. In this
93 | case all 16 subsets will be generated.
94 | getNumberOfGeneratedObjects won't be able to return the number
97 | of the sub sets/lists. It will throw a runtime exception
98 | Simple permutations
9 | A permutation is an ordering of a set in the context of all possible orderings. For example, the set
10 | containing the first three digits, 123, has
11 | six permutations: 123, 132, 213, 231, 312, and 321.
12 |
16 |
28 |
29 |
17 | // Create the initial vector of 3 elements (apple, orange, cherry)
18 | ICombinatoricsVector<String> originalVector = CombinatoricsFactory.createVector(new String[] { "apple", "orange", "cherry" });
19 |
20 | // Create the permutation generator by calling the appropriate method of the Factory class
21 | Generator<String> gen = CombinatoricsFactory.createPermutationGenerator(originalVector);
22 |
23 | // Print the result
24 | for (ICombinatoricsVector<String> perm : gen)
25 | System.out.println(perm);
26 |
27 |
33 |
42 |
34 | CombinatoricsVector=([apple, orange, cherry], size=3)
35 | CombinatoricsVector=([apple, cherry, orange], size=3)
36 | CombinatoricsVector=([cherry, apple, orange], size=3)
37 | CombinatoricsVector=([cherry, orange, apple], size=3)
38 | CombinatoricsVector=([orange, cherry, apple], size=3)
39 | CombinatoricsVector=([orange, apple, cherry], size=3)
40 |
41 | Permutations with repetitions
45 |
54 |
67 |
55 | // Create the initial vector of 2 elements (apple, orange)
56 | ICombinatoricsVector<String> originalVector = Factory.createVector(new String[] { "apple", "orange" });
57 |
58 | // Create the generator by calling the appropriate method in the Factory class.
59 | // Set the second parameter as 3, since we will generate 3-elemets permutations
60 | Generator<String> gen = Factory.createPermutationWithRepetitionGenerator(originalVector, 3);
61 |
62 | // Print the result
63 | for (ICombinatoricsVector<String> perm : gen)
64 | System.out.println( perm );
65 |
66 |
71 |
82 |
72 | CombinatoricsVector=([apple, apple, apple], size=3)
73 | CombinatoricsVector=([orange, apple, apple], size=3)
74 | CombinatoricsVector=([apple, orange, apple], size=3)
75 | CombinatoricsVector=([orange, orange, apple], size=3)
76 | CombinatoricsVector=([apple, apple, orange], size=3)
77 | CombinatoricsVector=([orange, apple, orange], size=3)
78 | CombinatoricsVector=([apple, orange, orange], size=3)
79 | CombinatoricsVector=([orange, orange, orange], size=3)
80 |
81 |
28 | *
29 | *
48 | *
30 | *
31 | * // Create the initial vector
32 | * ICombinatoricsVector<String> initialVector = CombinatoricsFactory.createVector(
33 | * new String[]{
34 | * "red", "black", "white", "green", "blue" });
35 | *
36 | * // Create a simple combination generator to generate 3-combinations of the
37 | * // initial vector
38 | * Generator<String> gen = CombinatoricsFactory.createSimpleCombinationGenerator(
39 | * initialVector, 3);
40 | *
41 | * // Print all possible combinations
42 | * for (ICombinatoricsVector<String> combination : gen) {
43 | * System.out.println(combination);
44 | * }
45 | *
46 | *
47 | *
31 | * // Create an instance of the integer composition generator to generate all possible
32 | * compositions of 5
33 | * IntegerGenerator gen = IntegerFactory.createIntegerCompositionGenerator(5);
34 | *
35 | * // Print the compositions
36 | * for (IntegerVector p : gen) {
37 | * System.out.println(p);
38 | * }
39 | *
40 | *
41 | * And the result
42 | *
43 | *
44 | * IntegerVector=([5], size=1)
45 | * IntegerVector=([1, 4], size=2)
46 | * IntegerVector=([2, 3], size=2)
47 | * IntegerVector=([1, 1, 3], size=3)
48 | * IntegerVector=([3, 2], size=2)
49 | * IntegerVector=([1, 2, 2], size=3)
50 | * IntegerVector=([2, 1, 2], size=3)
51 | * IntegerVector=([1, 1, 1, 2], size=4)
52 | * IntegerVector=([4, 1], size=2)
53 | * IntegerVector=([1, 3, 1], size=3)
54 | * IntegerVector=([2, 2, 1], size=3)
55 | * IntegerVector=([1, 1, 2, 1], size=4)
56 | * IntegerVector=([3, 1, 1], size=3)
57 | * IntegerVector=([1, 2, 1, 1], size=4)
58 | * IntegerVector=([2, 1, 1, 1], size=4)
59 | * IntegerVector=([1, 1, 1, 1, 1], size=5)
60 | *
61 | *
62 | * @author Dmytro Paukov
63 | * @version 2.0
64 | * @see IntegerCompositionIterator
65 | */
66 | public class IntegerCompositionGenerator extends IntegerGenerator {
67 |
68 | public static final int MAXN = 100;
69 |
70 | protected final Integer _initialValue;
71 |
72 | /**
73 | * Constructor
74 | *
75 | * @param n A positive integer value
76 | */
77 | public IntegerCompositionGenerator(Integer n) {
78 | super();
79 | this._initialValue = n;
80 | }
81 |
82 | /**
83 | * Returns the value which is used for generating the compositions. This value is
84 | * returned as an element of the single value vector.
85 | */
86 | @Override
87 | public IntegerVector getOriginalVector() {
88 | return createIntegerVector(new int[]{_initialValue});
89 | }
90 |
91 | /**
92 | * Returns number of the compositions
93 | */
94 | @Override
95 | public long getNumberOfGeneratedObjects() {
96 | return Util.pow2(_initialValue - 1);
97 | }
98 |
99 | /**
100 | * Creates the iterator over all compositions
101 | */
102 | @Override
103 | public Iterator
22 | * // create the initial vector or set
23 | * ICombinatoricsVector<T> vector = CombinatoricsFactory.createVector(new <T>[]{ elements } );
24 | *
25 | * // create a concrete generator
26 | * Generator<T> generator = CombinatoricsFactory.create<Concrete>Generator(vector);
27 | *
28 | * // iterate the generated objects
29 | * for (ICombinatoricsVector<T> v : generator) {
30 | * System.out.println( v );
31 | * }
32 | *
33 | *
34 | * @param iterator()
49 | * instead of this method
50 | */
51 | @Deprecated
52 | public IteratorstartIndex to stopIndex)
87 | *
88 | * @return List of the generated objects/vectors
89 | */
90 | public List
26 | *
31 | *
36 | *
37 | *
54 | *
38 | *
39 | * // Create the initial vector of (apple, orange)
40 | * ICombinatoricsVector<String> initialVector = CombinatoricsFactory.createVector(new String[] {
41 | * "apple", "orange" });
42 | *
43 | * // Create a multi-combination generator to generate 3-combinations of
44 | * // the initial vector
45 | * Generator<String> gen = CombinatoricsFactory.createMultiCombinationGenerator(initialVector, 3);
46 | *
47 | * // Print all possible combinations
48 | * for (ICombinatoricsVector<String> combination : gen) {
49 | * System.out.println(combination);
50 | * }
51 | *
52 | *
53 | *
25 | * 5
26 | * 4+1
27 | * 3+2
28 | * 3+1+1
29 | * 2+3
30 | * 2+2+1
31 | * 2+1+2
32 | * 2+1+1+1
33 | * 1+4
34 | * 1+3+1
35 | * 1+2+2
36 | * 1+2+1+1
37 | * 1+1+3
38 | * 1+1+2+1
39 | * 1+1+1+2
40 | * 1+1+1+1+1.
41 | *
42 | *
43 | * Compare this with the seven partitions of 5: 5, 4+1, 3+2, 3+1+1, 2+2+1, 2+1+1+1, 1+1+1+1+1.
44 | *
45 | *
46 | * Example. Generate compositions all possible integer compositions of 5.
47 | *
48 | *
49 | * // Create an instance of the integer composition generator to generate all possible
50 | * compositions of 5
51 | * Generator<Integer> gen = CombinatoricsFactory.createCompositionGenerator(5);
52 | *
53 | * // Print the compositions
54 | * for (ICombinatoricsVector<Integer> p : gen) {
55 | * System.out.println(p);
56 | * }
57 | *
58 | *
59 | * And the result
60 | *
61 | *
62 | * CombinatoricsVector=([5], size=1)
63 | * CombinatoricsVector=([1, 4], size=2)
64 | * CombinatoricsVector=([2, 3], size=2)
65 | * CombinatoricsVector=([1, 1, 3], size=3)
66 | * CombinatoricsVector=([3, 2], size=2)
67 | * CombinatoricsVector=([1, 2, 2], size=3)
68 | * CombinatoricsVector=([2, 1, 2], size=3)
69 | * CombinatoricsVector=([1, 1, 1, 2], size=4)
70 | * CombinatoricsVector=([4, 1], size=2)
71 | * CombinatoricsVector=([1, 3, 1], size=3)
72 | * CombinatoricsVector=([2, 2, 1], size=3)
73 | * CombinatoricsVector=([1, 1, 2, 1], size=4)
74 | * CombinatoricsVector=([3, 1, 1], size=3)
75 | * CombinatoricsVector=([1, 2, 1, 1], size=4)
76 | * CombinatoricsVector=([2, 1, 1, 1], size=4)
77 | * CombinatoricsVector=([1, 1, 1, 1, 1], size=5)
78 | *
79 | *
80 | * @author Dmytro Paukov
81 | * @version 2.0
82 | * @see CompositionIterator
83 | */
84 | public class CompositionGenerator extends Generatorlen
51 | *
52 | * @param array The array
53 | * @param len the length
54 | * @return The integer vector
55 | */
56 | public static IntegerVector createIntegerVector(int[] array, int len) {
57 | return new IntegerVector(array, len);
58 | }
59 |
60 | /**
61 | * Creates a sub-set generator
62 | *
63 | * @param originalVector The initial vector
64 | */
65 | public static IntegerGenerator createIntegerSubSetGenerator(
66 | IntegerVector originalVector) {
67 | return new IntegerSubSetGenerator(originalVector);
68 | }
69 |
70 | /**
71 | * Creates a sub-set generator
72 | *
73 | * @param originalVector The initial vector
74 | * @param treatAsIdentical true if the sub sets have to be treated as identical
75 | * @return An instance of the generator
76 | * @see IntegerSubSetGenerator
77 | */
78 | public static IntegerGenerator createIntegerSubSetGenerator(
79 | IntegerVector originalVector, boolean treatAsIdentical) {
80 | return new IntegerSubSetGenerator(originalVector, treatAsIdentical);
81 | }
82 |
83 | /**
84 | * Creates a composition generator
85 | *
86 | * @param n The initial value
87 | */
88 | public static IntegerGenerator createIntegerCompositionGenerator(Integer n) {
89 | return new IntegerCompositionGenerator(n);
90 | }
91 |
92 | /**
93 | * This method creates a combinatorics vector of (1, 2,.., n)
94 | *
95 | * @param n The value of the number of the elements
96 | * @return The vector
97 | */
98 | public static IntegerVector range(int n) {
99 | if (n < 0) {
100 | throw new IllegalArgumentException(
101 | "Range value must be more or equal to 0");
102 | }
103 |
104 | // If range is zero return an empty vector
105 | if (n == 0) {
106 | return new IntegerVector();
107 | }
108 |
109 | int[] array = new int[n];
110 | for (int i = 0; i < n; i++) {
111 | array[i] = i + 1;
112 | }
113 |
114 | return new IntegerVector(array);
115 | }
116 |
117 | /**
118 | * This method creates a combinatorics vector of (from, from + 1,.., to-1,
119 | * to)
120 | *
121 | * @param from The first value
122 | * @param to The second value
123 | * @return The vector
124 | */
125 | public static IntegerVector range(int from, int to) {
126 | if (from > to) {
127 | throw new IllegalArgumentException(
128 | "From parameter must be less then To parameter");
129 | }
130 |
131 | int[] array = new int[to - from + 1];
132 | for (int i = 0; i < to - from + 1; i++) {
133 | array[i] = i + from;
134 | }
135 |
136 | return new IntegerVector(array);
137 | }
138 | }
139 |
--------------------------------------------------------------------------------
/src/main/java/org/paukov/combinatorics/permutations/PermutationWithRepetitionGenerator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Combinatorics Library
3 | * Copyright 2012 Dmytro Paukov d.paukov@gmail.com
4 | */
5 | package org.paukov.combinatorics.permutations;
6 |
7 | import static org.paukov.combinatorics.CombinatoricsFactory.createVector;
8 |
9 | import java.util.Iterator;
10 | import org.paukov.combinatorics.CombinatoricsFactory;
11 | import org.paukov.combinatorics.Generator;
12 | import org.paukov.combinatorics.ICombinatoricsVector;
13 |
14 | /**
15 | * This generator generates permutations with repetitions from the specified vector by specified
16 | * length
17 | *
18 | * The permutation may have more elements than slots. For example, the three possible
19 | * permutation of 12 in three slots are: 111, 211, 121, 221, 112, 212, 122, and 222.
20 | *
21 | * Let's generate all possible permutations with repetitions of 3 elements from the set of apple and
22 | * orange:
23 | *
24 | *
25 | *
26 | * // Create the initial vector of 2 elements (apple, orange)
27 | * ICombinatoricsVector<String> originalVector = CombinatoricsFactory.createVector(new String[] {
28 | * "apple", "orange" });
29 | *
30 | * // Create the generator by calling the appropriate method in the Factory class.
31 | * // Set the second parameter as 3, since we will generate 3-elemets permutations
32 | * Generator<String> gen = CombinatoricsFactory.createPermutationWithRepetitionGenerator(originalVector,
33 | * 3);
34 | *
35 | * // Print the result
36 | * for (ICombinatoricsVector<String> perm : gen)
37 | * System.out.println( perm );
38 | *
39 | *
40 | * And the result
41 | *
42 | *
43 | * CombinatoricsVector=([apple, apple, apple], size=3)
44 | * CombinatoricsVector=([orange, apple, apple], size=3)
45 | * CombinatoricsVector=([apple, orange, apple], size=3)
46 | * CombinatoricsVector=([orange, orange, apple], size=3)
47 | * CombinatoricsVector=([apple, apple, orange], size=3)
48 | * CombinatoricsVector=([orange, apple, orange], size=3)
49 | * CombinatoricsVector=([apple, orange, orange], size=3)
50 | * CombinatoricsVector=([orange, orange, orange], size=3)
51 | *
52 | *
53 | * @param
25 | *
33 | *
42 | *
43 | *
56 | *
44 | * // Create an instance of the partition generator to generate all
45 | * // possible partitions of 5
46 | * Generator<Integer> gen = CombinatoricsFactory.createPartitionGenerator(5);
47 | *
48 | * // Print the partitions
49 | * for (ICombinatoricsVector<Integer> p : gen) {
50 | * System.out.println(p);
51 | * }
52 | *
53 | *
54 | *
55 | *
60 | *
61 | *
72 | *
62 | * CombinatoricsVector=([1, 1, 1, 1, 1], size=5)
63 | * CombinatoricsVector=([2, 1, 1, 1], size=4)
64 | * CombinatoricsVector=([2, 2, 1], size=3)
65 | * CombinatoricsVector=([3, 1, 1], size=3)
66 | * CombinatoricsVector=([3, 2], size=2)
67 | * CombinatoricsVector=([4, 1], size=2)
68 | * CombinatoricsVector=([5], size=1)
69 | *
70 | *
71 | *
27 | *
28 | *
45 | *
29 | *
30 | * // Create the initial vector of 3 elements (apple, orange, cherry)
31 | * ICombinatoricsVector<String> originalVector = CombinatoricsFactory
32 | * .createVector(new String[] { "apple", "orange", "cherry" });
33 | *
34 | * // Create the permutation generator by calling the appropriate method in the
35 | * // Factory class
36 | * Generator<String> gen = CombinatoricsFactory.createPermutationGenerator(originalVector);
37 | *
38 | * // Print the result
39 | * for (ICombinatoricsVector<String> perm : gen)
40 | * System.out.println(perm);
41 | *
42 | *
43 | *
44 | *
49 | *
50 | *
60 | *
51 | * CombinatoricsVector=([apple, orange, cherry], size=3)
52 | * CombinatoricsVector=([apple, cherry, orange], size=3)
53 | * CombinatoricsVector=([cherry, apple, orange], size=3)
54 | * CombinatoricsVector=([cherry, orange, apple], size=3)
55 | * CombinatoricsVector=([orange, cherry, apple], size=3)
56 | * CombinatoricsVector=([orange, apple, cherry], size=3)
57 | *
58 | *
59 | *
23 | *
27 | *
30 | *
39 | *
43 | *
44 | *
59 | *
45 | * // Create an initial vector/set
46 | * IntegerVector initialSet = IntegerFactory.createIntegerVector(new int[] { 1, 2,
47 | * 3 });
48 | *
49 | * // Create an instance of the integer subset generator
50 | * IntegerGenerator gen = IntegerFactory.createIntegerSubSetGenerator(initialSet);
51 | *
52 | * // Print the subsets
53 | * for (IntegerVector subSet : gen) {
54 | * System.out.println(subSet);
55 | * }
56 | *
57 | *
58 | *
63 | *
64 | *
76 | *
65 | * IntegerVector=([], size=0)
66 | * IntegerVector=([1], size=1)
67 | * IntegerVector=([2], size=1)
68 | * IntegerVector=([1, 2], size=2)
69 | * IntegerVector=([3], size=1)
70 | * IntegerVector=([1, 3], size=2)
71 | * IntegerVector=([2, 3], size=2)
72 | * IntegerVector=([1, 2, 3], size=3)
73 | *
74 | *
75 | *
80 | *
81 | *
99 | *
82 | * ()
83 | * (1)
84 | * (2)
85 | * (1, 2)
86 | * (1, 1)
87 | * (2, 1)
88 | * (1, 2, 1)
89 | * (3)
90 | * (1, 3)
91 | * (2, 3)
92 | * (1, 2, 3)
93 | * (1, 1, 3)
94 | * (2, 1, 3)
95 | * (1, 2, 1, 3)
96 | *
97 | *
98 | * IntegerFactory.createIntegerSubSetGenerator() as
103 | * false. In this case all 16 subsets will be generated.
104 | * getNumberOfGeneratedObjects won't be able to return the number
107 | * of the sub sets/lists. It will throw a runtime exception
108 | *
109 | * @author DmytroPaukov
110 | * @version 2.0
111 | * @see IntegerVector
112 | * @see IntegerSubSetIterator
113 | */
114 | public class IntegerSubSetGenerator extends IntegerGenerator {
115 |
116 | protected final boolean _hasDuplicates;
117 | protected final boolean _treatAsIdentical;
118 |
119 | /**
120 | * Core set
121 | */
122 | protected final IntegerVector _originalVector;
123 |
124 | /**
125 | * Constructor
126 | *
127 | * @param originalVector Original vector/set
128 | */
129 | public IntegerSubSetGenerator(IntegerVector originalVector) {
130 | _hasDuplicates = originalVector.hasDuplicates();
131 | _treatAsIdentical = true;
132 | _originalVector = IntegerFactory.createIntegerVector(originalVector
133 | .getVector());
134 | }
135 |
136 | /**
137 | * Constructor
138 | *
139 | * @param originalVector Original vector/set
140 | */
141 | public IntegerSubSetGenerator(IntegerVector originalVector,
142 | boolean treatAsIdentical) {
143 | _hasDuplicates = originalVector.hasDuplicates();
144 | _treatAsIdentical = treatAsIdentical;
145 | _originalVector = IntegerFactory.createIntegerVector(originalVector
146 | .getVector());
147 | }
148 |
149 | /**
150 | * Returns the core set
151 | */
152 | public IntegerVector getOriginalVector() {
153 | return _originalVector;
154 | }
155 |
156 | /**
157 | * Returns the number of the subsets. If the original set contains
158 | * duplicates this method will throw a runtime exception.
159 | */
160 | public long getNumberOfGeneratedObjects() {
161 |
162 | if (isSubList()) {
163 | throw new RuntimeException("The initial vector has duplicates: "
164 | + _originalVector);
165 | }
166 |
167 | return Util.pow2(_originalVector.getSize());
168 | }
169 |
170 | /**
171 | * Creates the iterator over the all subsets
172 | */
173 | public Iterator
24 | *
28 | *
31 | *
40 | *
44 | *
45 | *
60 | *
46 | * // Create an initial vector/set
47 | * ICombinatoricsVector<String> initialSet = CombinatoricsFactory.createVector(new String[] {
48 | * "one", "two", "three" });
49 | *
50 | * // Create an instance of the subset generator
51 | * Generator<String> gen = CombinatoricsFactory.createSubSetGenerator(initialSet);
52 | *
53 | * // Print the subsets
54 | * for (ICombinatoricsVector<String> subSet : gen) {
55 | * System.out.println(subSet);
56 | * }
57 | *
58 | *
59 | *
64 | *
65 | *
77 | *
66 | * CombinatoricsVector=([], size=0)
67 | * CombinatoricsVector=([one], size=1)
68 | * CombinatoricsVector=([two], size=1)
69 | * CombinatoricsVector=([one, two], size=2)
70 | * CombinatoricsVector=([three], size=1)
71 | * CombinatoricsVector=([one, three], size=2)
72 | * CombinatoricsVector=([two, three], size=2)
73 | * CombinatoricsVector=([one, two, three], size=3)
74 | *
75 | *
76 | *
81 | *
82 | *
100 | *
83 | * ()
84 | * (a)
85 | * (b)
86 | * (a, b)
87 | * (a, a)
88 | * (b, a)
89 | * (a, b, a)
90 | * (c)
91 | * (a, c)
92 | * (b, c)
93 | * (a, b, c)
94 | * (a, a, c)
95 | * (b, a, c)
96 | * (a, b, a, c)
97 | *
98 | *
99 | * Factory.createSubSetGenerator() as false. In this
104 | * case all 16 subsets will be generated.
105 | * getNumberOfGeneratedObjects won't be able to return the number
109 | * of the sub sets/lists. It will throw a runtime exception
110 | *
111 | * @param