diff --git a/src/Assignments/A2/Partition.java b/src/Assignments/A2/Partition.java index 7e5d260..b9c4019 100644 --- a/src/Assignments/A2/Partition.java +++ b/src/Assignments/A2/Partition.java @@ -58,6 +58,36 @@ Algorithm Design Block */ package Assignments.A2; +import java.util.ArrayList; +import java.util.List; + public class Partition { + private int[] set; + private List> subsets; + + /** + * Generates the list of subsets using Bit Shift Counting by iterating over all combinations of 1 to n. + * After shifting the counter, the current subset is created initialized and if 1 shifted by j is logically + * ANDed with the i is not 0, it adds the value j represents to the subset. + * + * @precondition none + * @postcondition the subsets are generated and added to the field. + */ + public void generateSubsets() { + List> subsets = new ArrayList<>(); + int n = this.set.length; + + for (int i = 0; i < (1 << n); i++) { + List currentSubset = new ArrayList<>(); + for (int j = 0; j < n; j++) { + if ((i & (1 << j)) != 0) { + currentSubset.add(this.set[j]); + } + } + subsets.add(currentSubset); + } + this.subsets = subsets; + } + }