From d10dd2e08f1f5066de6ede5f3a776cd7ed0606c1 Mon Sep 17 00:00:00 2001 From: Min Date: Mon, 31 Jan 2022 14:29:08 -0500 Subject: [PATCH 1/3] add permutation --- .../backtracking/Permutation.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/main/java/com/thealgorithms/backtracking/Permutation.java 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 000000000000..680cab62e6d7 --- /dev/null +++ b/src/main/java/com/thealgorithms/backtracking/Permutation.java @@ -0,0 +1,67 @@ +package com.thealgorithms.backtracking; + +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; + } + /** + * Simply test + */ + public static void main(String[] args) { + Integer[] arr = new Integer[] {1, 2, 3 ,4}; + List list1 = Permutation.permutation(arr); + for (Integer[] ar : list1) { + System.out.print("["); + for (int i : ar) { + System.out.print(i); + if (i != ar[ar.length - 1]) System.out.print(" "); + } + System.out.print("] "); + } + } +} From 5acb409f8c9fd111b3d38a61566c0d6e11ae5c7d Mon Sep 17 00:00:00 2001 From: Min Date: Wed, 2 Feb 2022 22:37:55 -0500 Subject: [PATCH 2/3] add combination --- .../backtracking/Combination.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/main/java/com/thealgorithms/backtracking/Combination.java 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 000000000000..24fe35a638f8 --- /dev/null +++ b/src/main/java/com/thealgorithms/backtracking/Combination.java @@ -0,0 +1,70 @@ +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]); + } + } + /** + * Simply test + */ + public static void main(String[] args) { + Integer[] arr = new Integer[]{6,15,7,12,8,1,2,4}; + for (int n = 1; n <= arr.length; n++) { + List> result = Combination.combination(arr, n); + System.out.println("n = " + n + " size: " + result.size()); + for (TreeSet set : result) { + System.out.print("("); + for (int i : set) { + System.out.print(i + " "); + } + System.out.println(")"); + } + + } + } +} From 9125c21df1321d4144cb41408c50fd89c591e69d Mon Sep 17 00:00:00 2001 From: Min Date: Sun, 6 Feb 2022 13:26:49 -0500 Subject: [PATCH 3/3] add test --- .../backtracking/Combination.java | 18 ---------- .../backtracking/Permutation.java | 16 +-------- .../backtracking/CombinationTest.java | 33 +++++++++++++++++++ .../backtracking/PermutationTest.java | 32 ++++++++++++++++++ 4 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 src/test/java/com/thealgorithms/backtracking/CombinationTest.java create mode 100644 src/test/java/com/thealgorithms/backtracking/PermutationTest.java diff --git a/src/main/java/com/thealgorithms/backtracking/Combination.java b/src/main/java/com/thealgorithms/backtracking/Combination.java index 24fe35a638f8..1298621a179b 100644 --- a/src/main/java/com/thealgorithms/backtracking/Combination.java +++ b/src/main/java/com/thealgorithms/backtracking/Combination.java @@ -49,22 +49,4 @@ private static void backtracking(T[] arr, int index, TreeSet currSet, Lis currSet.remove(arr[i]); } } - /** - * Simply test - */ - public static void main(String[] args) { - Integer[] arr = new Integer[]{6,15,7,12,8,1,2,4}; - for (int n = 1; n <= arr.length; n++) { - List> result = Combination.combination(arr, n); - System.out.println("n = " + n + " size: " + result.size()); - for (TreeSet set : result) { - System.out.print("("); - for (int i : set) { - System.out.print(i + " "); - } - System.out.println(")"); - } - - } - } } diff --git a/src/main/java/com/thealgorithms/backtracking/Permutation.java b/src/main/java/com/thealgorithms/backtracking/Permutation.java index 680cab62e6d7..58cc2991882e 100644 --- a/src/main/java/com/thealgorithms/backtracking/Permutation.java +++ b/src/main/java/com/thealgorithms/backtracking/Permutation.java @@ -1,5 +1,6 @@ package com.thealgorithms.backtracking; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -49,19 +50,4 @@ private static void swap(int a, int b, T[] arr) { arr[a] = arr[b]; arr[b] = temp; } - /** - * Simply test - */ - public static void main(String[] args) { - Integer[] arr = new Integer[] {1, 2, 3 ,4}; - List list1 = Permutation.permutation(arr); - for (Integer[] ar : list1) { - System.out.print("["); - for (int i : ar) { - System.out.print(i); - if (i != ar[ar.length - 1]) System.out.print(" "); - } - System.out.print("] "); - } - } } 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 000000000000..78e75b5437b3 --- /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 000000000000..60916b3e6611 --- /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})); + } + + +}