Skip to content

Commit 82b0ba1

Browse files
author
alxkm
committed
Adding class for generating all subsequences from a given List
1 parent 8be8b95 commit 82b0ba1

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Class generates all subsequences for a given list of elements using backtracking
8+
*/
9+
public class Subsequence {
10+
/**
11+
* Find all subsequences of given list using backtracking
12+
*
13+
* @param sequence a list of items on the basis of which we need to generate all subsequences
14+
* @param <T> the type of elements in the array
15+
* @return a list of all subsequences
16+
*/
17+
public static <T> List<List<T>> generateAllSubsequences(List<T> sequence) {
18+
List<List<T>> allSubSequences = new ArrayList<>();
19+
if (sequence.isEmpty()) {
20+
allSubSequences.add(new ArrayList<>());
21+
return allSubSequences;
22+
}
23+
List<T> currentSubsequence = new ArrayList<>();
24+
backtrack(sequence, currentSubsequence, 0, allSubSequences);
25+
return allSubSequences;
26+
}
27+
28+
/**
29+
* Iterate through each branch of states
30+
* We know that each state has exactly two branching
31+
* It terminates when it reaches the end of the given sequence
32+
*
33+
* @param sequence all elements
34+
* @param currentSubsequence current subsequence
35+
* @param index current index
36+
* @param allSubSequences contains all sequences
37+
* @param <T> the type of elements which we generate
38+
*/
39+
private static <T> void backtrack(List<T> sequence, List<T> currentSubsequence, int index, List<List<T>> allSubSequences) {
40+
if (index == sequence.size()) {
41+
allSubSequences.add(new ArrayList<>(currentSubsequence));
42+
return;
43+
}
44+
45+
backtrack(sequence, currentSubsequence, index + 1, allSubSequences);
46+
currentSubsequence.add(sequence.get(index));
47+
backtrack(sequence, currentSubsequence, index + 1, allSubSequences);
48+
currentSubsequence.removeLast();
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
9+
10+
public class SubsequenceTest {
11+
12+
@Test
13+
void testNoElement() {
14+
List<List<Object>> emptyResult = Subsequence.generateAllSubsequences(new ArrayList<>());
15+
assertEquals(1, emptyResult.size());
16+
assertEquals(0, emptyResult.get(0).size());
17+
}
18+
19+
@Test
20+
void testLengthOfTwo() {
21+
List<List<Integer>> expected = List.of(
22+
List.of(),
23+
List.of(2),
24+
List.of(1),
25+
List.of(1, 2)
26+
);
27+
List<List<Object>> actual = Subsequence.generateAllSubsequences(List.of(1, 2));
28+
assertIterableEquals(expected, actual);
29+
}
30+
31+
@Test
32+
void testLengthOfThree() {
33+
List<List<String>> expected = List.of(
34+
List.of(),
35+
List.of("C"),
36+
List.of("B"),
37+
List.of("B", "C"),
38+
List.of("A"),
39+
List.of("A", "C"),
40+
List.of("A", "B"),
41+
List.of("A", "B", "C")
42+
);
43+
List<List<Object>> actual = Subsequence.generateAllSubsequences(List.of("A", "B", "C"));
44+
assertIterableEquals(expected, actual);
45+
}
46+
47+
@Test
48+
void testLengthOfThreeInteger() {
49+
List<List<Integer>> expected = List.of(
50+
List.of(),
51+
List.of(3),
52+
List.of(2),
53+
List.of(2, 3),
54+
List.of(1),
55+
List.of(1, 3),
56+
List.of(1, 2),
57+
List.of(1, 2, 3)
58+
);
59+
List<List<Object>> actual = Subsequence.generateAllSubsequences(List.of(1, 2, 3));
60+
assertIterableEquals(expected, actual);
61+
}
62+
}

0 commit comments

Comments
 (0)