Repository: TheAlgorithms/Java
Pull request #5867 (2024-10-16 03:54:49)

feat: Add `GenerateSubsets` new algorithm with Junit tests
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5867?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 66.72%. Comparing base [(`2a518e3`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/2a518e3c9a44c4333c5ec9283895132264773afb?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`33fff1b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/33fff1b0a9ddc5439d2ffd4d72cd63c069f759b8?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5867      +/-   ##
============================================
+ Coverage     66.70%   66.72%   +0.02%     
- Complexity     4482     4487       +5     
============================================
  Files           608      609       +1     
  Lines         16913    16922       +9     
  Branches       3264     3267       +3     
============================================
+ Hits          11282    11292      +10     
  Misses         5182     5182              
+ Partials        449      448       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5867?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index bb952a26..cd511b4a 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -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)
@@ -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)
diff --git a/src/main/java/com/thealgorithms/bitmanipulation/GenerateSubsets.java b/src/main/java/com/thealgorithms/bitmanipulation/GenerateSubsets.java
new file mode 100644
index 00000000..f1b81249
--- /dev/null
+++ b/src/main/java/com/thealgorithms/bitmanipulation/GenerateSubsets.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;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/bitmanipulation/GenerateSubsetsTest.java b/src/test/java/com/thealgorithms/bitmanipulation/GenerateSubsetsTest.java
new file mode 100644
index 00000000..e3205d1d
--- /dev/null
+++ b/src/test/java/com/thealgorithms/bitmanipulation/GenerateSubsetsTest.java
@@ -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);
+    }
+}