Skip to content

Commit 12c67bc

Browse files
authored
Add permutations and combinations (TheAlgorithms#2932)
1 parent 101d08a commit 12c67bc

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Finds all permutations of given array
7+
* @author Alan Piao (https://github.com/cpiao3)
8+
*/
9+
public class Combination {
10+
private static int length;
11+
/**
12+
* Find all combinations of given array using backtracking
13+
* @param arr the array.
14+
* @param n length of combination
15+
* @param <T> the type of elements in the array.
16+
* @return a list of all combinations of length n. If n == 0, return null.
17+
*/
18+
public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
19+
if (n == 0) {
20+
return null;
21+
}
22+
length = n;
23+
T[] array = arr.clone();
24+
Arrays.sort(array);
25+
List<TreeSet<T>> result = new LinkedList<>();
26+
backtracking(array, 0, new TreeSet<T>(), result);
27+
return result;
28+
}
29+
/**
30+
* Backtrack all possible combinations of a given array
31+
* @param arr the array.
32+
* @param index the starting index.
33+
* @param currSet set that tracks current combination
34+
* @param result the list contains all combination.
35+
* @param <T> the type of elements in the array.
36+
*/
37+
private static <T> void backtracking(T[] arr, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
38+
if (index + length - currSet.size() > arr.length) return;
39+
if (length - 1 == currSet.size()) {
40+
for (int i = index; i < arr.length; i++) {
41+
currSet.add(arr[i]);
42+
result.add((TreeSet<T>) currSet.clone());
43+
currSet.remove(arr[i]);
44+
}
45+
}
46+
for (int i = index; i < arr.length; i++) {
47+
currSet.add(arr[i]);
48+
backtracking(arr, i + 1, currSet, result);
49+
currSet.remove(arr[i]);
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
7+
/**
8+
* Finds all permutations of given array
9+
* @author Alan Piao (https://github.com/cpiao3)
10+
*/
11+
public class Permutation {
12+
/**
13+
* Find all permutations of given array using backtracking
14+
* @param arr the array.
15+
* @param <T> the type of elements in the array.
16+
* @return a list of all permutations.
17+
*/
18+
public static <T> List<T[]> permutation(T[] arr) {
19+
T[] array = arr.clone();
20+
List<T[]> result = new LinkedList<>();
21+
backtracking(array, 0, result);
22+
return result;
23+
}
24+
/**
25+
* Backtrack all possible orders of a given array
26+
* @param arr the array.
27+
* @param index the starting index.
28+
* @param result the list contains all permutations.
29+
* @param <T> the type of elements in the array.
30+
*/
31+
private static <T> void backtracking(T[] arr, int index, List<T[]> result) {
32+
if (index == arr.length) {
33+
result.add(arr.clone());
34+
}
35+
for (int i = index; i < arr.length; i++) {
36+
swap(index, i, arr);
37+
backtracking(arr, index + 1, result);
38+
swap(index, i, arr);
39+
}
40+
}
41+
/**
42+
* Swap two element for a given array
43+
* @param a first index
44+
* @param b second index
45+
* @param arr the array.
46+
* @param <T> the type of elements in the array.
47+
*/
48+
private static <T> void swap(int a, int b, T[] arr) {
49+
T temp = arr[a];
50+
arr[a] = arr[b];
51+
arr[b] = temp;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.TreeSet;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
public class CombinationTest {
12+
@Test
13+
void testNoElement()
14+
{
15+
List<TreeSet<Integer>> result = Combination.combination(new Integer[]{1, 2}, 0);
16+
assertTrue(result == null);
17+
}
18+
@Test
19+
void testLengthOne()
20+
{
21+
List<TreeSet<Integer>> result = Combination.combination(new Integer[]{1, 2}, 1);
22+
assertTrue(result.get(0).iterator().next() == 1);
23+
assertTrue(result.get(1).iterator().next() == 2);
24+
}
25+
@Test
26+
void testLengthTwo()
27+
{
28+
List<TreeSet<Integer>> result = Combination.combination(new Integer[]{1, 2}, 2);
29+
Integer[] arr = result.get(0).toArray(new Integer[2]);
30+
assertTrue(arr[0] == 1);
31+
assertTrue(arr[1] == 2);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
public class PermutationTest {
11+
@Test
12+
void testNoElement()
13+
{
14+
List<Integer []> result = Permutation.permutation(new Integer[]{});
15+
assertEquals(result.get(0).length, 0);
16+
}
17+
@Test
18+
void testSingleElement()
19+
{
20+
List<Integer []> result = Permutation.permutation(new Integer[]{1});
21+
assertEquals(result.get(0)[0], 1);
22+
}
23+
@Test
24+
void testMultipleElements()
25+
{
26+
List<Integer []> result = Permutation.permutation(new Integer[]{1, 2});
27+
assertTrue(Arrays.equals(result.get(0), new Integer[]{1,2}));
28+
assertTrue(Arrays.equals(result.get(1), new Integer[]{2,1}));
29+
}
30+
31+
32+
}

0 commit comments

Comments
 (0)