How to find all partitions of a set
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
A partition of a set splits elements into non-empty, disjoint blocks whose union is the original set. Generating all partitions is useful in combinatorics, clustering search, and exhaustive reasoning tasks. The challenge is that partition counts grow very quickly, so implementation must be correct and output-aware.
What A Set Partition Means
For a set S, a partition is a collection of subsets where:
- no subset is empty,
- subsets do not overlap,
- every element in
Sappears in exactly one subset.
For example, partitions of 1, 2, 3 are:
- '
1, 2, 3' - '
1, 2with3' - '
1, 3with2' - '
2, 3with1' - '
1with2with3'
There are 5 total, matching Bell number B3.
Recursive Construction Strategy
A reliable recursive strategy processes one element at a time.
For new element x:
- insert
xinto each existing block of every smaller partition, - also create a new singleton block containing only
x.
This guarantees complete enumeration without logical duplicates when list copying is handled correctly.
Python Generator Implementation
This code is easy to adapt for constraint-based filtering.
Why Copying Matters
Without copying lists at each branch, recursive paths share mutable structures and corrupt each other.
If you see duplicate or missing partitions, shared mutation is usually the cause. Clone blocks before append operations.
Add Constraints To Reduce Search Space
Real applications rarely need all partitions. Add filters early.
Constraint pruning keeps runtime manageable for slightly larger sets.
Validate Using Bell Numbers
A simple correctness check compares count to known Bell numbers for small n.
This is useful as a unit test and regression guard.
Complexity Reality
Enumeration complexity is dominated by output size itself. Bell numbers grow rapidly:
- '
B5 = 52' - '
B6 = 203' - '
B7 = 877' - '
B8 = 4140'
Full enumeration becomes expensive quickly in time and memory. Use generators and streaming consumers where possible.
Practical Engineering Advice
- Keep output lazy with generators.
- Add domain constraints early.
- Normalize block ordering only when needed for display.
- Avoid materializing all results unless required.
- Profile memory, not only CPU.
For large-scale partition-like problems, heuristic or approximate approaches are often more practical than exact enumeration.
Common Pitfalls
- Mutating shared block lists across recursive branches.
- Assuming runtime is polynomial and underestimating Bell growth.
- Materializing all partitions when only count or subset is needed.
- Forgetting to test against known small Bell values.
- Confusing combinations with partitions and implementing wrong algorithm.
Summary
- Set partition generation is a classic recursive branching problem.
- Insert-each-or-new-block recursion gives complete enumeration.
- Careful copying is required to avoid mutation bugs.
- Bell number growth makes exhaustive generation expensive quickly.
- Use lazy generation and constraints to keep solutions practical.
Related reading
- How to find all positions of the maximum value in a list?
- How to find all possible subsets of a given array?
- How to find all vertex-disjoint paths in a graph?
- How to find cycles of a given length in a directed graph? Using networkx
- How to find all permutations of a given word in a given text?
- How to find all taxicab numbers less than N?
- How to find if there are n consecutive set bits in a 32 bit buffer?
- How to find index of list item in Swift?

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.