mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
Adding class for generating all subsequences from a given List (#5194)
* Adding class for generating all subsequences from a given List * Fix test data format * Fix braces wrong placement * Fix "Utility classes should not have a public or default constructor." * Fix checkstyle " Class Subsequence should be declared as final." * Renaming class Subsequence to SubsequenceFinder. Refactored test to Parametrized test. Fixed input parameter as final. * Fix formatting * Fix formatting * Fix formatting * Fix import ordering * Renaming method generate all. Renaming test method. Adding duplication test. Renaming TestData to TestCase. * Fix formatting * style: add assertion to avoid potential infinite loop --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -0,0 +1,54 @@
|
||||
package com.thealgorithms.backtracking;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Class generates all subsequences for a given list of elements using backtracking
|
||||
*/
|
||||
public final class SubsequenceFinder {
|
||||
private SubsequenceFinder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all subsequences of given list using backtracking
|
||||
*
|
||||
* @param sequence a list of items on the basis of which we need to generate all subsequences
|
||||
* @param <T> the type of elements in the array
|
||||
* @return a list of all subsequences
|
||||
*/
|
||||
public static <T> List<List<T>> generateAll(List<T> sequence) {
|
||||
List<List<T>> allSubSequences = new ArrayList<>();
|
||||
if (sequence.isEmpty()) {
|
||||
allSubSequences.add(new ArrayList<>());
|
||||
return allSubSequences;
|
||||
}
|
||||
List<T> currentSubsequence = new ArrayList<>();
|
||||
backtrack(sequence, currentSubsequence, 0, allSubSequences);
|
||||
return allSubSequences;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate through each branch of states
|
||||
* We know that each state has exactly two branching
|
||||
* It terminates when it reaches the end of the given sequence
|
||||
*
|
||||
* @param sequence all elements
|
||||
* @param currentSubsequence current subsequence
|
||||
* @param index current index
|
||||
* @param allSubSequences contains all sequences
|
||||
* @param <T> the type of elements which we generate
|
||||
*/
|
||||
private static <T> void backtrack(List<T> sequence, List<T> currentSubsequence, final int index, List<List<T>> allSubSequences) {
|
||||
assert index <= sequence.size();
|
||||
if (index == sequence.size()) {
|
||||
allSubSequences.add(new ArrayList<>(currentSubsequence));
|
||||
return;
|
||||
}
|
||||
|
||||
backtrack(sequence, currentSubsequence, index + 1, allSubSequences);
|
||||
currentSubsequence.add(sequence.get(index));
|
||||
backtrack(sequence, currentSubsequence, index + 1, allSubSequences);
|
||||
currentSubsequence.removeLast();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user