Skip to content

add permutation #2932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main/java/com/thealgorithms/backtracking/Combination.java
Original file line number Diff line number Diff line change
@@ -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]);
}
}
}
53 changes: 53 additions & 0 deletions src/main/java/com/thealgorithms/backtracking/Permutation.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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}));
}


}