Skip to content
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

Adding class for generating all subsequences from a given List #5194

Merged
merged 16 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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) {
if (index == sequence.size()) {
vil02 marked this conversation as resolved.
Show resolved Hide resolved
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.thealgorithms.backtracking;

import static org.junit.jupiter.api.Assertions.assertIterableEquals;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class SubsequenceFinderTest {

@ParameterizedTest
@MethodSource("getTestCases")
void testGenerateAll(TestCase testData) {
final var actual = SubsequenceFinder.generateAll(testData.input());
assertIterableEquals(testData.expected(), actual);
}

static Stream<TestCase> getTestCases() {
return Stream.of(new TestCase(new ArrayList<>(), List.of(List.of())), new TestCase(List.of(1, 2), List.of(List.of(), List.of(2), List.of(1), List.of(1, 2))),
new TestCase(List.of("A", "B", "C"), List.of(List.of(), List.of("C"), List.of("B"), List.of("B", "C"), List.of("A"), List.of("A", "C"), List.of("A", "B"), List.of("A", "B", "C"))),
new TestCase(List.of(1, 2, 3), List.of(List.of(), List.of(3), List.of(2), List.of(2, 3), List.of(1), List.of(1, 3), List.of(1, 2), List.of(1, 2, 3))), new TestCase(List.of(2, 2), List.of(List.of(), List.of(2), List.of(2), List.of(2, 2))));
}

record TestCase(List<Object> input, List<List<Object>> expected) {
}
}