Javatpoint Logo
Javatpoint Logo

Power set algorithm in C++

The power set is a collection of all subsets, as well as the empty set and the original set. A recursive method or an iterative approach involving bitwise manipulation might be used to build the power set of a set. A set is a group of unique elements. An empty set is a set with no elements; a power set is a set of every possible subset of a given set.

Understanding the Problem:

We have been given an array of 'N' elements, specifically [a, b, c]. We need to create a power set for this array, where each subset of the power set is sorted in ascending order. The array of subsets must be returned. The members of each subset should be arranged in ascending order. The order of the subsets in the answer array does not matter.

Example with [a, b, c]:

Suppose we start with the set [a, b, c]. Different subsets of this set are as follows:

[ ]

[a]

[b]

[a, b]

[c]

[a, c]

[b, c]

[a, b, c]

Now, the power set is simply the collection of all these subsets.

[ [], [a], [b], [a, b], [c], [a, c], [b, c], [a, b, c] ]

Method 1: Brute Force Approach

We'll start with empty subsets and loop through them in 'N' steps, where 'N' is the initial array size [a, b, c]. We have two possibilities at each step: include or exclude the currently selected element.

Let's use the example where 'N' is set to 3, and the contents of the input array are [a, b, c] to demonstrate this method.

  • Step 0: Begin with an empty set,[[]].
  • Step 1: Make duplicates of the previous subsets. In one copy, add 'a' to each subset; in another copy, leave the subsets alone. As a result, [[], [a]].
  • Step 2: Make two additional copies of the previous step's subsets. In one of the copies, add 'b' to each subset; in the other, leave the subsets alone. The subsets are now doubled: [[], [a], [b], [a, b]].
  • Step 3: Make two duplicates of the subsets from the previous step. In one copy, add 'c' to each subset; in the other, leave the subsets alone. It generates the following power set: [[], [a], [b], [a, b], [c], [a, c], [b, c], [a, b, c], [a, b, c]].

If we want to generate the power set, this method carefully examines every possible arrangement of including and excluding each element (a, b, and c) within the array [a, b, c]. Each step effectively doubles the total amount of subsets in the power set, producing the full set of all possible subsets.

Example:

Let's take an example to illustrate the power set algorithm in C++.

Input:

Output:

a 
b 
a b 
c 
a c 
b c 
a b c 

Complexity:

Time Complexity:

O(N * (2 N)), where 'N' is the number of elements of the array, 'ARRAY'.

The outside loop iterates 'N' times, and for each iteration 'i' of the outer loop, the inner loop iterates 2i times, within the scope of which we duplicate the list of length at most N at the end of each subset.

As a result, the entire time complexity will be N * (20 + 21 +. . . + 2N-2 + 2N-1) = O(N * 2N).

Space Complexity: O(1).

There is no extra space required.

Method 2: Recursive Approach

We'll write a recursive function (solve) to generate the subsets in this strategy.

The following four parameters will be provided to the recursive function:

  1. We must find the power set of an array called 'ARRAY'.
  2. The current element's index in the ARRAY array is 'IDX' (originally zero).
  3. The present array is designated as 'CURRENT' and will hold a subset.
  4. 'ANSWER' is an array of arrays storing our final response.

We have two options at each step: adding the element to the selected subset. Furthermore, if the index of the currently selected element is larger than or equal to the size of the supplied array, the recursive function will terminate.

Condition of Origin:

If IDX >=ANSWER.size(), then push 'CURRENT' to 'ANSWER' and return.

Recursive Calls:

  • Execute the recursive function by increasing the 'IDX' value by 1 and replacing the array 'ARRAY' with the excluding current element.
  • Push the 'ARRAY[IDX]' to 'CURRENT'.
  • Call the recursive function that increases the 'IDX' value by one and populates the array 'ARRAY' with the currently selected element.

Now, let's examine the algorithm's source code.

Filename: Power_set.c

Input:

Output:

3 
2 
2 3 
1 
1 3 
1 2 
1 2 3 

Complexity:

Time Complexity:

O(2^N), where 'N' is the array length, 'ARR'.

As with the recursion function, we make two calls at each stage for 2^N calls. As a result, the entire time complexity is O(2^N).

Space complexity:

O(N), where 'N' is the array's size and 'ARRAY' is the array length.

The Recursion tree is depth will be 'N' in the worst-case scenario. As a result, the total space complexity of the recursive call stack is O(N).

Method 3: The Bitmask Method

We can use a bit to indicate whether or not the associated element is a component of the current subset because it might be either 0 or 1. As a result, every bit of pattern represents a subset of the entire set. In this manner, we may obtain all subsets without taking up any extra space.

Assume we have the 'ARRAY' array.

Assume our response is 'ANSWER' = [[]].

Iterate 0<= I<= 2^N times and perform the following:

  • Consider the following array: TEMP = []
  • Iterate through ARRAY[i] for each 0 = j N and do the following:
  • Add the 'j'th value to 'TEMP' if the 'j'th bit of 'i' is set.
  • 'TEMP' should be added at the end of 'ANSWER'.

Filename: Powerset3.cpp

Input:

Output:

1 
2 
1 2 
3 
1 3 
2 3 
1 2 3 






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA