-
Notifications
You must be signed in to change notification settings - Fork 19.1k
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
Refactor: Combination #5225
Refactor: Combination #5225
Changes from 9 commits
0dd3755
f5d72aa
8ae600f
5f3fc44
d10c568
df5d877
66ba79b
fb5fab0
ab8c995
cb851ab
571ed90
950c8c6
a25969d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,58 @@ | ||
package com.thealgorithms.backtracking; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedList; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.TreeSet; | ||
|
||
/** | ||
* Finds all permutations of given array | ||
* @author Alan Piao (<a href="https://github.com/cpiao3">git-Alan Piao</a>) | ||
* Finds all combinations of a given array of unique elements. | ||
*/ | ||
public final class Combination { | ||
private 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. | ||
* Finds all combinations of a given array of length n. | ||
* If n is 0, an empty list is returned. | ||
* | ||
* @param data the array of elements. | ||
* @param combinationSize the desired size of combinations. | ||
* @param <T> the type of elements in the array (assumed to be unique). | ||
* @return a list of all combinations of size `combinationSize`. | ||
* @throws IllegalArgumentException if combinationSize is negative. | ||
*/ | ||
public static <T> List<TreeSet<T>> combination(T[] arr, int n) { | ||
if (n == 0) { | ||
public static <T> List<TreeSet<T>> combination(T[] data, int combinationSize) { | ||
if (combinationSize < 0) { | ||
throw new IllegalArgumentException("Combination size cannot be negative."); | ||
} else if (combinationSize == 0) { | ||
return null; | ||
} | ||
Comment on lines
+27
to
29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not see a reason to return
|
||
length = n; | ||
T[] array = arr.clone(); | ||
Arrays.sort(array); | ||
List<TreeSet<T>> result = new LinkedList<>(); | ||
backtracking(array, 0, new TreeSet<T>(), result); | ||
return result; | ||
|
||
List<TreeSet<T>> combinations = new ArrayList<>(); | ||
findCombinationsRecursive(data, 0, new ArrayList<>(), combinations, combinationSize); | ||
return combinations; | ||
} | ||
|
||
/** | ||
* 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. | ||
* Recursive helper function to find combinations using backtracking. | ||
* | ||
* @param data the array of elements. | ||
* @param startIndex the starting index for the current combination. | ||
* @param currentCombination the current combination being built. | ||
* @param combinations the list to store all found combinations. | ||
* @param combinationSize the desired size of combinations. | ||
* @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) { | ||
private static <T> void findCombinationsRecursive(T[] data, int startIndex, List<T> currentCombination, List<TreeSet<T>> combinations, int combinationSize) { | ||
if (currentCombination.size() == combinationSize) { | ||
combinations.add(new TreeSet<>(currentCombination)); // Deep copy to avoid modification | ||
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]); | ||
|
||
for (int i = startIndex; i < data.length; i++) { | ||
currentCombination.add(data[i]); | ||
findCombinationsRecursive(data, i + 1, currentCombination, combinations, combinationSize); | ||
currentCombination.removeLast(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not verified anywhere. Maybe the input type should be
Set<T>
? Or maybe this statement is not needed. I think what we are aiming for it to mimic theitertools.combinations
from python.