Merge pull request #6460 from chandra1123/BAEL-2504-Combinations
Bael 2504 combinations
This commit is contained in:
commit
6658e92a87
|
@ -39,6 +39,11 @@
|
||||||
<version>${org.assertj.core.version}</version>
|
<version>${org.assertj.core.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.dpaukov</groupId>
|
||||||
|
<artifactId>combinatoricslib3</artifactId>
|
||||||
|
<version>3.3.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -77,7 +82,7 @@
|
||||||
<commons-math3.version>3.6.1</commons-math3.version>
|
<commons-math3.version>3.6.1</commons-math3.version>
|
||||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||||
<commons-codec.version>1.11</commons-codec.version>
|
<commons-codec.version>1.11</commons-codec.version>
|
||||||
<guava.version>25.1-jre</guava.version>
|
<guava.version>27.0.1-jre</guava.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import org.apache.commons.math3.util.CombinatoricsUtils;
|
||||||
|
|
||||||
|
public class ApacheCommonsCombinationGenerator {
|
||||||
|
|
||||||
|
private static final int N = 6;
|
||||||
|
private static final int R = 3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print all combinations of r elements from a set
|
||||||
|
* @param n - number of elements in set
|
||||||
|
* @param r - number of elements in selection
|
||||||
|
*/
|
||||||
|
public static void generate(int n, int r) {
|
||||||
|
Iterator<int[]> iterator = CombinatoricsUtils.combinationsIterator(n, r);
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
final int[] combination = iterator.next();
|
||||||
|
System.out.println(Arrays.toString(combination));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
generate(N, R);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import org.paukov.combinatorics3.Generator;
|
||||||
|
|
||||||
|
public class CombinatoricsLibCombinationGenerator {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Generator.combination(0, 1, 2, 3, 4, 5)
|
||||||
|
.simple(3)
|
||||||
|
.stream()
|
||||||
|
.forEach(System.out::println);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
|
public class GuavaCombinationsGenerator {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Set<Set<Integer>> combinations = Sets.combinations(ImmutableSet.of(0, 1, 2, 3, 4, 5), 3);
|
||||||
|
System.out.println(combinations.size());
|
||||||
|
System.out.println(Arrays.toString(combinations.toArray()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class IterativeCombinationGenerator {
|
||||||
|
|
||||||
|
private static final int N = 5;
|
||||||
|
private static final int R = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate all combinations of r elements from a set
|
||||||
|
* @param n the number of elements in input set
|
||||||
|
* @param r the number of elements in a combination
|
||||||
|
* @return the list containing all combinations
|
||||||
|
*/
|
||||||
|
public List<int[]> generate(int n, int r) {
|
||||||
|
List<int[]> combinations = new ArrayList<>();
|
||||||
|
int[] combination = new int[r];
|
||||||
|
|
||||||
|
// initialize with lowest lexicographic combination
|
||||||
|
for (int i = 0; i < r; i++) {
|
||||||
|
combination[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (combination[r - 1] < n) {
|
||||||
|
combinations.add(combination.clone());
|
||||||
|
|
||||||
|
// generate next combination in lexicographic order
|
||||||
|
int t = r - 1;
|
||||||
|
while (t != 0 && combination[t] == n - r + t) {
|
||||||
|
t--;
|
||||||
|
}
|
||||||
|
combination[t]++;
|
||||||
|
for (int i = t + 1; i < r; i++) {
|
||||||
|
combination[i] = combination[i - 1] + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return combinations;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
IterativeCombinationGenerator generator = new IterativeCombinationGenerator();
|
||||||
|
List<int[]> combinations = generator.generate(N, R);
|
||||||
|
System.out.println(combinations.size());
|
||||||
|
for (int[] combination : combinations) {
|
||||||
|
System.out.println(Arrays.toString(combination));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SelectionRecursiveCombinationGenerator {
|
||||||
|
|
||||||
|
private static final int N = 6;
|
||||||
|
private static final int R = 3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate all combinations of r elements from a set
|
||||||
|
* @param n - number of elements in input set
|
||||||
|
* @param r - number of elements to be chosen
|
||||||
|
* @return the list containing all combinations
|
||||||
|
*/
|
||||||
|
public List<int[]> generate(int n, int r) {
|
||||||
|
List<int[]> combinations = new ArrayList<>();
|
||||||
|
helper(combinations, new int[r], 0, n - 1, 0);
|
||||||
|
return combinations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Choose elements from set by recursing over elements selected
|
||||||
|
* @param combinations - List to store generated combinations
|
||||||
|
* @param data - current combination
|
||||||
|
* @param start - starting element of remaining set
|
||||||
|
* @param end - last element of remaining set
|
||||||
|
* @param index - number of elements chosen so far.
|
||||||
|
*/
|
||||||
|
private void helper(List<int[]> combinations, int data[], int start, int end, int index) {
|
||||||
|
if (index == data.length) {
|
||||||
|
int[] combination = data.clone();
|
||||||
|
combinations.add(combination);
|
||||||
|
} else {
|
||||||
|
int max = Math.min(end, end + 1 - data.length + index);
|
||||||
|
for (int i = start; i <= max; i++) {
|
||||||
|
data[index] = i;
|
||||||
|
helper(combinations, data, i + 1, end, index + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SelectionRecursiveCombinationGenerator generator = new SelectionRecursiveCombinationGenerator();
|
||||||
|
List<int[]> combinations = generator.generate(N, R);
|
||||||
|
for (int[] combination : combinations) {
|
||||||
|
System.out.println(Arrays.toString(combination));
|
||||||
|
}
|
||||||
|
System.out.printf("generated %d combinations of %d items from %d ", combinations.size(), R, N);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SetRecursiveCombinationGenerator {
|
||||||
|
|
||||||
|
private static final int N = 5;
|
||||||
|
private static final int R = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate all combinations of r elements from a set
|
||||||
|
* @param n - number of elements in set
|
||||||
|
* @param r - number of elements in selection
|
||||||
|
* @return the list containing all combinations
|
||||||
|
*/
|
||||||
|
public List<int[]> generate(int n, int r) {
|
||||||
|
List<int[]> combinations = new ArrayList<>();
|
||||||
|
helper(combinations, new int[r], 0, n-1, 0);
|
||||||
|
return combinations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param combinations - List to contain the generated combinations
|
||||||
|
* @param data - List of elements in the selection
|
||||||
|
* @param start - index of the starting element in the remaining set
|
||||||
|
* @param end - index of the last element in the set
|
||||||
|
* @param index - number of elements selected so far
|
||||||
|
*/
|
||||||
|
private void helper(List<int[]> combinations, int data[], int start, int end, int index) {
|
||||||
|
if (index == data.length) {
|
||||||
|
int[] combination = data.clone();
|
||||||
|
combinations.add(combination);
|
||||||
|
} else if (start <= end) {
|
||||||
|
data[index] = start;
|
||||||
|
helper(combinations, data, start + 1, end, index + 1);
|
||||||
|
helper(combinations, data, start + 1, end, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SetRecursiveCombinationGenerator generator = new SetRecursiveCombinationGenerator();
|
||||||
|
List<int[]> combinations = generator.generate(N, R);
|
||||||
|
for (int[] combination : combinations) {
|
||||||
|
System.out.println(Arrays.toString(combination));
|
||||||
|
}
|
||||||
|
System.out.printf("generated %d combinations of %d items from %d ", combinations.size(), R, N);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.algorithms.combination;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class CombinationUnitTest {
|
||||||
|
|
||||||
|
private static final int N = 5;
|
||||||
|
private static final int R = 3;
|
||||||
|
private static final int nCr = 10;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSetAndSelectionSize_whenCalculatedUsingSetRecursiveAlgorithm_thenExpectedCount() {
|
||||||
|
SetRecursiveCombinationGenerator generator = new SetRecursiveCombinationGenerator();
|
||||||
|
List<int[]> selection = generator.generate(N, R);
|
||||||
|
assertEquals(nCr, selection.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSetAndSelectionSize_whenCalculatedUsingSelectionRecursiveAlgorithm_thenExpectedCount() {
|
||||||
|
SelectionRecursiveCombinationGenerator generator = new SelectionRecursiveCombinationGenerator();
|
||||||
|
List<int[]> selection = generator.generate(N, R);
|
||||||
|
assertEquals(nCr, selection.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenSetAndSelectionSize_whenCalculatedUsingIterativeAlgorithm_thenExpectedCount() {
|
||||||
|
IterativeCombinationGenerator generator = new IterativeCombinationGenerator();
|
||||||
|
List<int[]> selection = generator.generate(N, R);
|
||||||
|
assertEquals(nCr, selection.size());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue