forked from akgmage/data-structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowerset.java
More file actions
127 lines (94 loc) · 4.83 KB
/
Copy pathpowerset.java
File metadata and controls
127 lines (94 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Write a function that takes in an array of unique integers and returns its powerset.
Sample Input : [1, 2, 3]
Output: [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
Explanation:
The code snippet represents a function `Powerset` that takes an array of integers as input and returns the powerset
of the array. The powerset of a set is the set of all possible subsets of that set, including the empty set and the set itself.
The function `Powerset` calls a helper function `powerset` to perform the actual computation. Here's how the code works:
1. The `Powerset` function initializes the computation by calling the `powerset` function with the array and the index
of the last element in the array (`len(array) - 1`).
2. The `powerset` function is a recursive function that calculates the powerset. It takes the array and the current
index as input.
3. At each recursive call, the function checks if the index is less than 0. If so, it means all elements have been
processed, and it returns a 2D slice containing the empty set as the only subset.
4. If the index is not less than 0, the function retrieves the element at the current index from the array.
5. The function recursively calls itself with the array and the index decremented by 1 to generate the subsets without
the current element.
6. It then calculates the length of the subsets generated so far.
7. Using a loop, the function iterates over the existing subsets and creates new subsets by appending the current element
to each subset. The new subsets are added to the existing subset slice.
8. Finally, the function returns the updated subset slice, which contains all the subsets of the original array.
By recursively generating subsets while building upon the subsets generated at each step, the function constructs the
powerset of the given array.
O(n*2^n) time | O(n*2^n) space - where n is the length of the input array
*/
import java.util.ArrayList;
import java.util.List;
class Main {
public static List<List<Integer>> powerset(int[] array, int index) {
// Base case: when the index reaches -1, return a list with an empty subset
if (index < 0) {
List<List<Integer>> subsets = new ArrayList<>();
subsets.add(new ArrayList<>());
return subsets;
}
int element = array[index];
// Recursive call to generate the powerset for the elements up to index - 1
List<List<Integer>> subsets = powerset(array, index - 1);
// Length of the current subsets list
int length = subsets.size();
// Iterate through each subset and create new subsets by adding the current element
for (int i = 0; i < length; i++) {
List<Integer> currentSubset = subsets.get(i);
// Create a new subset by making a copy of the current subset
List<Integer> newSubset = new ArrayList<>(currentSubset);
// Add the current element to the new subset
newSubset.add(element);
// Add the new subset to the existing subsets list
subsets.add(newSubset);
}
// Return the resulting subsets list
return subsets;
}
public static List<List<Integer>> powerset(int[] array) {
// Call the helper function to generate the powerset starting from the last index
return powerset(array, array.length - 1);
}
public static void main(String[] args) {
int[] array = {1, 2, 3};
List<List<Integer>> subsets = powerset(array);
// Print the subsets
for (List<Integer> subset : subsets) {
System.out.println(subset);
}
}
}
// Iterative approach
public class Powerset {
public static List<List<Integer>> powerset(int[] array) {
List<List<Integer>> subset = new ArrayList<>();
subset.add(new ArrayList<>()); // Initialize the powerset with the empty subset
// Iterate over each element in the input array
for (int ele : array) {
int length = subset.size(); // Get the current length of the subset
// Iterate over each existing subset
for (int i = 0; i < length; i++) {
List<Integer> currentSubset = new ArrayList<>(subset.get(i)); // Get the current subset
// Create a new subset by making a copy of the current subset
List<Integer> newSubset = new ArrayList<>(currentSubset);
// Add the current element to the new subset
newSubset.add(ele);
// Append the new subset to the powerset
subset.add(newSubset);
}
}
// Return the powerset
return subset;
}
public static void main(String[] args) {
int[] array = {1, 2, 3};
List<List<Integer>> result = powerset(array);
System.out.println(result);
}
}