Obtaining a powerset of a set in Java
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The power set of a set contains every possible subset, including empty and full subsets. For a set with n elements, the power set size is 2^n, so any complete algorithm is inherently exponential in output. In Java, the best implementation choice depends on whether you need all subsets in memory or streamed one by one.
Core Sections
Backtracking implementation
Backtracking is expressive and easy to customize for constraints.
This pattern is useful when later you want pruning rules.
Bitmask implementation
Bitmask enumeration is iterative and compact.
For small to medium n, this is often straightforward and fast.
Working with Set input
Since Java Set has no guaranteed order unless using ordered variants, convert to a list first for deterministic subset ordering.
Deterministic ordering helps tests and reproducible outputs.
Memory aware streaming approach
When n grows, storing all subsets can exhaust heap quickly. Instead, consume subsets as generated.
Streaming keeps memory proportional to recursion depth instead of full output size.
Complexity expectations
Time complexity is O(n * 2^n) when accounting for subset construction work. Space complexity varies:
- full materialization uses
O(n * 2^n)storage. - streaming uses
O(n)auxiliary stack plus consumer state.
Common Pitfalls
- Expecting polynomial runtime for full power set generation. Output size is exponential.
- Using unordered input sets and assuming stable subset order. Convert to ordered list first.
- Storing all subsets for large
nand exhausting memory. Use streaming consumer approach. - Forgetting empty subset and failing mathematical completeness. Include empty case explicitly.
- Mutating shared subset lists without copying at leaf nodes. Clone current subset before storing.
Summary
- Power set generation is inherently exponential because output count is
2^n. - Backtracking and bitmask methods are both valid in Java.
- Deterministic ordering requires list conversion from set inputs.
- Streaming subsets is essential for larger inputs.
- Correct copying and clear complexity expectations prevent subtle bugs.
Related reading
- Occlusion algorithms collection
- OCR error correction algorithms
- Ok to have stack depth linearly proportional to some input size?
- Oklogk time algorithm to find kth smallest element from a binary heap
- Olog n algorithm to find the element having rank i in union of pre-sorted lists
- On On On?
- Omitting one Setter/Getter in Lombok
- On the Kafka Java consumer client, is there a way to monitor health status as opposed to simply no-data?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.