Skip to content

Commit 1e8dabe

Browse files
maciegKevinGilmore
authored andcommitted
BAEL-3479 | combinatorics (eugenp#8233)
* BAEL-3479 | combinatorics * BAEL-3479 | style fix
1 parent 9d545c2 commit 1e8dabe

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.algorithms.combinatorics;
2+
3+
import java.util.*;
4+
5+
import static java.util.Collections.swap;
6+
7+
public class Combinatorics {
8+
9+
public static List<List<Integer>> permutations(List<Integer> sequence) {
10+
List<List<Integer>> results = new ArrayList<>();
11+
permutationsInternal(sequence, results, 0);
12+
return results;
13+
}
14+
15+
private static void permutationsInternal(List<Integer> sequence, List<List<Integer>> results, int index) {
16+
if (index == sequence.size() - 1) {
17+
results.add(new ArrayList<>(sequence));
18+
}
19+
20+
for (int i = index; i < sequence.size(); i++) {
21+
swap(sequence, i, index);
22+
permutationsInternal(sequence, results, index + 1);
23+
swap(sequence, i, index);
24+
}
25+
}
26+
27+
public static List<List<Integer>> combinations(List<Integer> inputSet, int k) {
28+
List<List<Integer>> results = new ArrayList<>();
29+
combinationsInternal(inputSet, k, results, new ArrayList<>(), 0);
30+
return results;
31+
}
32+
33+
private static void combinationsInternal(
34+
List<Integer> inputSet, int k, List<List<Integer>> results, ArrayList<Integer> accumulator, int index) {
35+
int leftToAccumulate = k - accumulator.size();
36+
int possibleToAcculumate = inputSet.size() - index;
37+
38+
if (accumulator.size() == k) {
39+
results.add(new ArrayList<>(accumulator));
40+
} else if (leftToAccumulate <= possibleToAcculumate) {
41+
combinationsInternal(inputSet, k, results, accumulator, index + 1);
42+
43+
accumulator.add(inputSet.get(index));
44+
combinationsInternal(inputSet, k, results, accumulator, index + 1);
45+
accumulator.remove(accumulator.size() - 1);
46+
}
47+
}
48+
49+
public static List<List<Character>> powerSet(List<Character> sequence) {
50+
List<List<Character>> results = new ArrayList<>();
51+
powerSetInternal(sequence, results, new ArrayList<>(), 0);
52+
return results;
53+
}
54+
55+
private static void powerSetInternal(
56+
List<Character> set, List<List<Character>> powerSet, List<Character> accumulator, int index) {
57+
if (index == set.size()) {
58+
powerSet.add(new ArrayList<>(accumulator));
59+
} else {
60+
accumulator.add(set.get(index));
61+
62+
powerSetInternal(set, powerSet, accumulator, index + 1);
63+
accumulator.remove(accumulator.size() - 1);
64+
powerSetInternal(set, powerSet, accumulator, index + 1);
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.algorithms.combinatorics;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
7+
import org.junit.Test;
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertSame;
10+
11+
public class CombinatoricsUnitTest {
12+
13+
@Test
14+
public void givenEmptySequence_whenCallingPermutations_ShouldReturnEmptyList() {
15+
List<Integer> sequence = Arrays.asList();
16+
17+
List<List<Integer>> permutations = Combinatorics.permutations(sequence);
18+
19+
assertEquals(0, permutations.size());
20+
}
21+
22+
@Test
23+
public void givenOneElementSequence_whenCallingPermutations_ShouldReturnPermutations() {
24+
List<Integer> sequence = Arrays.asList(1);
25+
26+
List<List<Integer>> permutations = Combinatorics.permutations(sequence);
27+
28+
assertEquals(1, permutations.size());
29+
assertEquals(1, permutations.get(0).size());
30+
assertSame(1, permutations.get(0).get(0));
31+
}
32+
33+
@Test
34+
public void givenFourElementsSequence_whenCallingPermutations_ShouldReturnPermutations() {
35+
List<Integer> sequence = Arrays.asList(1, 2, 3, 4);
36+
37+
List<List<Integer>> permutations = Combinatorics.permutations(sequence);
38+
39+
assertEquals(24, permutations.size());
40+
assertEquals(24, new HashSet<>(permutations).size());
41+
}
42+
43+
@Test
44+
public void givenTwoElements_whenCalling3Combinations_ShouldReturnEmptyList() {
45+
List<Integer> set = Arrays.asList(1, 2);
46+
47+
List<List<Integer>> combinations = Combinatorics.combinations(set, 3);
48+
49+
assertEquals(0, combinations.size());
50+
}
51+
52+
@Test
53+
public void givenThreeElements_whenCalling3Combinations_ShouldReturnOneCombination() {
54+
List<Integer> set = Arrays.asList(1, 2, 3);
55+
56+
List<List<Integer>> combinations = Combinatorics.combinations(set, 3);
57+
58+
assertEquals(1, combinations.size());
59+
assertEquals(combinations.get(0), Arrays.asList(1, 2, 3));
60+
}
61+
62+
@Test
63+
public void givenFourElements_whenCalling2Combinations_ShouldReturnCombinations() {
64+
List<Integer> set = Arrays.asList(1, 2, 3, 4);
65+
66+
List<List<Integer>> combinations = Combinatorics.combinations(set, 2);
67+
68+
assertEquals(6, combinations.size());
69+
assertEquals(6, new HashSet<>(combinations).size());
70+
}
71+
72+
@Test
73+
public void givenFourElements_whenCallingPowerSet_ShouldReturn15Sets() {
74+
List<Character> sequence = Arrays.asList('a', 'b', 'c', 'd');
75+
76+
List<List<Character>> combinations = Combinatorics.powerSet(sequence);
77+
78+
assertEquals(16, combinations.size());
79+
}
80+
}

0 commit comments

Comments
 (0)