BAEL-3479 | combinatorics (#8233)
* BAEL-3479 | combinatorics * BAEL-3479 | style fix
This commit is contained in:
parent
9cae3c7c86
commit
38a42d8329
@ -0,0 +1,67 @@
|
|||||||
|
package com.baeldung.algorithms.combinatorics;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static java.util.Collections.swap;
|
||||||
|
|
||||||
|
public class Combinatorics {
|
||||||
|
|
||||||
|
public static List<List<Integer>> permutations(List<Integer> sequence) {
|
||||||
|
List<List<Integer>> results = new ArrayList<>();
|
||||||
|
permutationsInternal(sequence, results, 0);
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void permutationsInternal(List<Integer> sequence, List<List<Integer>> results, int index) {
|
||||||
|
if (index == sequence.size() - 1) {
|
||||||
|
results.add(new ArrayList<>(sequence));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = index; i < sequence.size(); i++) {
|
||||||
|
swap(sequence, i, index);
|
||||||
|
permutationsInternal(sequence, results, index + 1);
|
||||||
|
swap(sequence, i, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<List<Integer>> combinations(List<Integer> inputSet, int k) {
|
||||||
|
List<List<Integer>> results = new ArrayList<>();
|
||||||
|
combinationsInternal(inputSet, k, results, new ArrayList<>(), 0);
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void combinationsInternal(
|
||||||
|
List<Integer> inputSet, int k, List<List<Integer>> results, ArrayList<Integer> accumulator, int index) {
|
||||||
|
int leftToAccumulate = k - accumulator.size();
|
||||||
|
int possibleToAcculumate = inputSet.size() - index;
|
||||||
|
|
||||||
|
if (accumulator.size() == k) {
|
||||||
|
results.add(new ArrayList<>(accumulator));
|
||||||
|
} else if (leftToAccumulate <= possibleToAcculumate) {
|
||||||
|
combinationsInternal(inputSet, k, results, accumulator, index + 1);
|
||||||
|
|
||||||
|
accumulator.add(inputSet.get(index));
|
||||||
|
combinationsInternal(inputSet, k, results, accumulator, index + 1);
|
||||||
|
accumulator.remove(accumulator.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<List<Character>> powerSet(List<Character> sequence) {
|
||||||
|
List<List<Character>> results = new ArrayList<>();
|
||||||
|
powerSetInternal(sequence, results, new ArrayList<>(), 0);
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void powerSetInternal(
|
||||||
|
List<Character> set, List<List<Character>> powerSet, List<Character> accumulator, int index) {
|
||||||
|
if (index == set.size()) {
|
||||||
|
powerSet.add(new ArrayList<>(accumulator));
|
||||||
|
} else {
|
||||||
|
accumulator.add(set.get(index));
|
||||||
|
|
||||||
|
powerSetInternal(set, powerSet, accumulator, index + 1);
|
||||||
|
accumulator.remove(accumulator.size() - 1);
|
||||||
|
powerSetInternal(set, powerSet, accumulator, index + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
package com.baeldung.algorithms.combinatorics;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
|
|
||||||
|
public class CombinatoricsUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEmptySequence_whenCallingPermutations_ShouldReturnEmptyList() {
|
||||||
|
List<Integer> sequence = Arrays.asList();
|
||||||
|
|
||||||
|
List<List<Integer>> permutations = Combinatorics.permutations(sequence);
|
||||||
|
|
||||||
|
assertEquals(0, permutations.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenOneElementSequence_whenCallingPermutations_ShouldReturnPermutations() {
|
||||||
|
List<Integer> sequence = Arrays.asList(1);
|
||||||
|
|
||||||
|
List<List<Integer>> permutations = Combinatorics.permutations(sequence);
|
||||||
|
|
||||||
|
assertEquals(1, permutations.size());
|
||||||
|
assertEquals(1, permutations.get(0).size());
|
||||||
|
assertSame(1, permutations.get(0).get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFourElementsSequence_whenCallingPermutations_ShouldReturnPermutations() {
|
||||||
|
List<Integer> sequence = Arrays.asList(1, 2, 3, 4);
|
||||||
|
|
||||||
|
List<List<Integer>> permutations = Combinatorics.permutations(sequence);
|
||||||
|
|
||||||
|
assertEquals(24, permutations.size());
|
||||||
|
assertEquals(24, new HashSet<>(permutations).size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoElements_whenCalling3Combinations_ShouldReturnEmptyList() {
|
||||||
|
List<Integer> set = Arrays.asList(1, 2);
|
||||||
|
|
||||||
|
List<List<Integer>> combinations = Combinatorics.combinations(set, 3);
|
||||||
|
|
||||||
|
assertEquals(0, combinations.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenThreeElements_whenCalling3Combinations_ShouldReturnOneCombination() {
|
||||||
|
List<Integer> set = Arrays.asList(1, 2, 3);
|
||||||
|
|
||||||
|
List<List<Integer>> combinations = Combinatorics.combinations(set, 3);
|
||||||
|
|
||||||
|
assertEquals(1, combinations.size());
|
||||||
|
assertEquals(combinations.get(0), Arrays.asList(1, 2, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFourElements_whenCalling2Combinations_ShouldReturnCombinations() {
|
||||||
|
List<Integer> set = Arrays.asList(1, 2, 3, 4);
|
||||||
|
|
||||||
|
List<List<Integer>> combinations = Combinatorics.combinations(set, 2);
|
||||||
|
|
||||||
|
assertEquals(6, combinations.size());
|
||||||
|
assertEquals(6, new HashSet<>(combinations).size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFourElements_whenCallingPowerSet_ShouldReturn15Sets() {
|
||||||
|
List<Character> sequence = Arrays.asList('a', 'b', 'c', 'd');
|
||||||
|
|
||||||
|
List<List<Character>> combinations = Combinatorics.powerSet(sequence);
|
||||||
|
|
||||||
|
assertEquals(16, combinations.size());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user