diff --git a/src/main/java/com/thealgorithms/backtracking/Combination.java b/src/main/java/com/thealgorithms/backtracking/Combination.java new file mode 100644 index 000000000..1298621a1 --- /dev/null +++ b/src/main/java/com/thealgorithms/backtracking/Combination.java @@ -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 the type of elements in the array. + * @return a list of all combinations of length n. If n == 0, return null. + */ + public static List> combination(T[] arr, int n) { + if (n == 0) { + return null; + } + length = n; + T[] array = arr.clone(); + Arrays.sort(array); + List> result = new LinkedList<>(); + backtracking(array, 0, new TreeSet(), 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 the type of elements in the array. + */ + private static void backtracking(T[] arr, int index, TreeSet currSet, List> 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) 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]); + } + } +} diff --git a/src/main/java/com/thealgorithms/backtracking/Permutation.java b/src/main/java/com/thealgorithms/backtracking/Permutation.java new file mode 100644 index 000000000..58cc29918 --- /dev/null +++ b/src/main/java/com/thealgorithms/backtracking/Permutation.java @@ -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 the type of elements in the array. + * @return a list of all permutations. + */ + public static List permutation(T[] arr) { + T[] array = arr.clone(); + List 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 the type of elements in the array. + */ + private static void backtracking(T[] arr, int index, List 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 the type of elements in the array. + */ + private static void swap(int a, int b, T[] arr) { + T temp = arr[a]; + arr[a] = arr[b]; + arr[b] = temp; + } +} diff --git a/src/test/java/com/thealgorithms/backtracking/CombinationTest.java b/src/test/java/com/thealgorithms/backtracking/CombinationTest.java new file mode 100644 index 000000000..78e75b543 --- /dev/null +++ b/src/test/java/com/thealgorithms/backtracking/CombinationTest.java @@ -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> result = Combination.combination(new Integer[]{1, 2}, 0); + assertTrue(result == null); + } + @Test + void testLengthOne() + { + List> 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> 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); + } +} diff --git a/src/test/java/com/thealgorithms/backtracking/PermutationTest.java b/src/test/java/com/thealgorithms/backtracking/PermutationTest.java new file mode 100644 index 000000000..60916b3e6 --- /dev/null +++ b/src/test/java/com/thealgorithms/backtracking/PermutationTest.java @@ -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 result = Permutation.permutation(new Integer[]{}); + assertEquals(result.get(0).length, 0); + } + @Test + void testSingleElement() + { + List result = Permutation.permutation(new Integer[]{1}); + assertEquals(result.get(0)[0], 1); + } + @Test + void testMultipleElements() + { + List 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})); + } + + +}