Add permutations and combinations (#2932)
This commit is contained in:
parent
101d08ae24
commit
12c67bc501
@ -0,0 +1,52 @@
|
||||
package com.thealgorithms.backtracking;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Finds all permutations of given array
|
||||
* @author Alan Piao (https://github.com/cpiao3)
|
||||
*/
|
||||
public class Combination {
|
||||
private static int length;
|
||||
/**
|
||||
* Find all combinations of given array using backtracking
|
||||
* @param arr the array.
|
||||
* @param n length of combination
|
||||
* @param <T> the type of elements in the array.
|
||||
* @return a list of all combinations of length n. If n == 0, return null.
|
||||
*/
|
||||
public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
|
||||
if (n == 0) {
|
||||
return null;
|
||||
}
|
||||
length = n;
|
||||
T[] array = arr.clone();
|
||||
Arrays.sort(array);
|
||||
List<TreeSet<T>> result = new LinkedList<>();
|
||||
backtracking(array, 0, new TreeSet<T>(), result);
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Backtrack all possible combinations of a given array
|
||||
* @param arr the array.
|
||||
* @param index the starting index.
|
||||
* @param currSet set that tracks current combination
|
||||
* @param result the list contains all combination.
|
||||
* @param <T> the type of elements in the array.
|
||||
*/
|
||||
private static <T> void backtracking(T[] arr, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
|
||||
if (index + length - currSet.size() > arr.length) return;
|
||||
if (length - 1 == currSet.size()) {
|
||||
for (int i = index; i < arr.length; i++) {
|
||||
currSet.add(arr[i]);
|
||||
result.add((TreeSet<T>) currSet.clone());
|
||||
currSet.remove(arr[i]);
|
||||
}
|
||||
}
|
||||
for (int i = index; i < arr.length; i++) {
|
||||
currSet.add(arr[i]);
|
||||
backtracking(arr, i + 1, currSet, result);
|
||||
currSet.remove(arr[i]);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.thealgorithms.backtracking;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Finds all permutations of given array
|
||||
* @author Alan Piao (https://github.com/cpiao3)
|
||||
*/
|
||||
public class Permutation {
|
||||
/**
|
||||
* Find all permutations of given array using backtracking
|
||||
* @param arr the array.
|
||||
* @param <T> the type of elements in the array.
|
||||
* @return a list of all permutations.
|
||||
*/
|
||||
public static <T> List<T[]> permutation(T[] arr) {
|
||||
T[] array = arr.clone();
|
||||
List<T[]> result = new LinkedList<>();
|
||||
backtracking(array, 0, result);
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Backtrack all possible orders of a given array
|
||||
* @param arr the array.
|
||||
* @param index the starting index.
|
||||
* @param result the list contains all permutations.
|
||||
* @param <T> the type of elements in the array.
|
||||
*/
|
||||
private static <T> void backtracking(T[] arr, int index, List<T[]> result) {
|
||||
if (index == arr.length) {
|
||||
result.add(arr.clone());
|
||||
}
|
||||
for (int i = index; i < arr.length; i++) {
|
||||
swap(index, i, arr);
|
||||
backtracking(arr, index + 1, result);
|
||||
swap(index, i, arr);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Swap two element for a given array
|
||||
* @param a first index
|
||||
* @param b second index
|
||||
* @param arr the array.
|
||||
* @param <T> the type of elements in the array.
|
||||
*/
|
||||
private static <T> void swap(int a, int b, T[] arr) {
|
||||
T temp = arr[a];
|
||||
arr[a] = arr[b];
|
||||
arr[b] = temp;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.thealgorithms.backtracking;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class CombinationTest {
|
||||
@Test
|
||||
void testNoElement()
|
||||
{
|
||||
List<TreeSet<Integer>> result = Combination.combination(new Integer[]{1, 2}, 0);
|
||||
assertTrue(result == null);
|
||||
}
|
||||
@Test
|
||||
void testLengthOne()
|
||||
{
|
||||
List<TreeSet<Integer>> result = Combination.combination(new Integer[]{1, 2}, 1);
|
||||
assertTrue(result.get(0).iterator().next() == 1);
|
||||
assertTrue(result.get(1).iterator().next() == 2);
|
||||
}
|
||||
@Test
|
||||
void testLengthTwo()
|
||||
{
|
||||
List<TreeSet<Integer>> result = Combination.combination(new Integer[]{1, 2}, 2);
|
||||
Integer[] arr = result.get(0).toArray(new Integer[2]);
|
||||
assertTrue(arr[0] == 1);
|
||||
assertTrue(arr[1] == 2);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.thealgorithms.backtracking;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class PermutationTest {
|
||||
@Test
|
||||
void testNoElement()
|
||||
{
|
||||
List<Integer []> result = Permutation.permutation(new Integer[]{});
|
||||
assertEquals(result.get(0).length, 0);
|
||||
}
|
||||
@Test
|
||||
void testSingleElement()
|
||||
{
|
||||
List<Integer []> result = Permutation.permutation(new Integer[]{1});
|
||||
assertEquals(result.get(0)[0], 1);
|
||||
}
|
||||
@Test
|
||||
void testMultipleElements()
|
||||
{
|
||||
List<Integer []> result = Permutation.permutation(new Integer[]{1, 2});
|
||||
assertTrue(Arrays.equals(result.get(0), new Integer[]{1,2}));
|
||||
assertTrue(Arrays.equals(result.get(1), new Integer[]{2,1}));
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user