VOOZH about

URL: https://www.geeksforgeeks.org/dsa/recursive-program-to-generate-power-set/

⇱ Recursive program to generate power set - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Recursive program to generate power set

Last Updated : 11 Jul, 2025

Given a set represented as a string, write a recursive code to print all subsets of it. The subsets can be printed in any order. 

Examples:

Input :  set = "abc"
Output : { "", "a", "b", "c", "ab", "ac", "bc", "abc"}

Input : set = "abcd"
Output : { "", "a" ,"ab" ,"abc" ,"abcd", "abd" ,"ac" ,"acd", "ad" ,"b", "bc" ,"bcd" ,"bd" ,"c" ,"cd" ,"d" }

Using Pick and Do Not Pick for Every Element:

The idea is to consider two cases for every character.
(i) Consider current character as part of current subset
(ii) Do not consider current character as part of the current subset.  

Follow the approach to implement the above idea:

  • The base condition of the recursive approach is when the current index reaches the size of the given string (i.e, index == n). then print the current string say, curr.
  • Make two recursive calls for the two cases for every character
    • We consider the character as part of the current subset
    • We do not consider current character as part of the current subset

Output
abc
ab
ac
a
bc
b
c

Time Complexity: O(2n)
Auxiliary Space: O(n), For recursive call stack

Fixing Prefixes One by One

The idea is to fix a prefix, and generate all subsets beginning with the current prefix. After all subsets with a prefix are generated, replace the last character with one of the remaining characters.

Follow the approach to implement the above idea:

  • The base condition of the recursive approach is when the current index reaches the size of the given string (i.e, index == n), then return
  • First, print the current subset
  • Iterate over the given string from the current index (i.e, index) to less than the size of the string
    • Appending the remaining characters to the current subset
    • Make the recursive call for the next index.
    • Once all subsets beginning with the initial "curr" are printed, remove the last character to consider a different prefix of subsets.

Follow the steps below to implement the above approach:


Output
a
ab
abc
ac
b
bc
c

Time Complexity: O(2n)
Auxiliary Space: O(n), For recursive call stack

Using Bottom Up Approach

The idea is to pick each element one by one from the input set, then generate a subset for the same, and we follow this process recursively. 

Follow the steps below to implement the above idea:

  • The base condition for the recursive call, when the current index becomes negative then add empty list into the allSubsets.
  • Make recursive call for the current index - 1, then we'll receive allSubsets list from 0 to index -1
  • Create a list of list moreSubsets
  • Iterate over all the subsets that are received from above recursive call 
    • Copy all the subset into newSubset
    • Add the current item into newSubset
    • Add newSubset into moreSubsets
  • Add moreSubsets into allSubsets
  • Finally, return allSubsets.

Follow the steps below to implement the above approach:


Output
[[], [a], [b], [a, b], [c][/c], [a, c], [b, c], [a, b, c]]

Time Complexity: O(n*2n)
Auxiliary Space: O(n), For recursive call stack

Iterative program for the power set.

Comment
Article Tags:
Article Tags: