mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
Add GenerateSubsets
algorithm (#5867)
This commit is contained in:
@ -32,6 +32,7 @@
|
|||||||
* [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [HigherLowerPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java)
|
||||||
@ -738,6 +739,7 @@
|
|||||||
* [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [HigherLowerPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java)
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user