Skip to content

feat: Add GenerateSubsets new algorithm with Junit tests #5867

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 9 commits into from
Oct 26, 2024
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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java)
* [FindNthBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/FindNthBit.java)
* [FirstDifferentBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/FirstDifferentBit.java)
* [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/GenerateSubsets.java)
* [GrayCodeConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java)
* [HammingDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HammingDistance.java)
* [HigherLowerPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java)
Expand Down Expand Up @@ -738,6 +739,7 @@
* [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java)
* [FindNthBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/FindNthBitTest.java)
* [FirstDifferentBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/FirstDifferentBitTest.java)
* [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/GenerateSubsetsTest.java)
* [GrayCodeConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/GrayCodeConversionTest.java)
* [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HammingDistanceTest.java)
* [HigherLowerPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.thealgorithms.bitmanipulation;

import java.util.ArrayList;
import java.util.List;

/**
* This class provides a method to generate all subsets (power set)
* of a given set using bit manipulation.
*
* @author Hardvan
*/
public final class GenerateSubsets {
private GenerateSubsets() {
}

/**
* Generates all subsets of a given set using bit manipulation.
* Steps:
* 1. Iterate over all numbers from 0 to 2^n - 1.
* 2. For each number, iterate over all bits from 0 to n - 1.
* 3. If the i-th bit of the number is set, add the i-th element of the set to the current subset.
* 4. Add the current subset to the list of subsets.
* 5. Return the list of subsets.
*
* @param set the input set of integers
* @return a list of all subsets represented as lists of integers
*/
public static List<List<Integer>> generateSubsets(int[] set) {
int n = set.length;
List<List<Integer>> subsets = new ArrayList<>();

for (int mask = 0; mask < (1 << n); mask++) {
List<Integer> subset = new ArrayList<>();
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) != 0) {
subset.add(set[i]);
}
}
subsets.add(subset);
}

return subsets;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.thealgorithms.bitmanipulation;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

class GenerateSubsetsTest {

@Test
void testGenerateSubsetsWithTwoElements() {
int[] set = {1, 2};
List<List<Integer>> expected = new ArrayList<>();
expected.add(new ArrayList<>());
expected.add(Arrays.asList(1));
expected.add(Arrays.asList(2));
expected.add(Arrays.asList(1, 2));

List<List<Integer>> result = GenerateSubsets.generateSubsets(set);
assertEquals(expected, result);
}

@Test
void testGenerateSubsetsWithOneElement() {
int[] set = {3};
List<List<Integer>> expected = new ArrayList<>();
expected.add(new ArrayList<>());
expected.add(Arrays.asList(3));

List<List<Integer>> result = GenerateSubsets.generateSubsets(set);
assertEquals(expected, result);
}

@Test
void testGenerateSubsetsWithThreeElements() {
int[] set = {4, 5, 6};
List<List<Integer>> expected = new ArrayList<>();
expected.add(new ArrayList<>());
expected.add(Arrays.asList(4));
expected.add(Arrays.asList(5));
expected.add(Arrays.asList(4, 5));
expected.add(Arrays.asList(6));
expected.add(Arrays.asList(4, 6));
expected.add(Arrays.asList(5, 6));
expected.add(Arrays.asList(4, 5, 6));

List<List<Integer>> result = GenerateSubsets.generateSubsets(set);
assertEquals(expected, result);
}
}