VOOZH about

URL: https://www.geeksforgeeks.org/dsa/bell-numbers-number-of-ways-to-partition-a-set/

⇱ Bell Numbers (Number of ways to Partition a Set) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Bell Numbers (Number of ways to Partition a Set)

Last Updated : 23 Jul, 2025

Given a set of n elements, find the number of ways of partitioning it. 

Examples: 

Input: n = 2
Output: 2
Explanation: Let the set be {1, 2}. The partitions are {{1},{2}} and {{1, 2}}.

Input: n = 3
Output: 5
Explanation: Let the set be {1, 2, 3}. The partitions are {{1},{2},{3}}, {{1},{2, 3}}, {{2},{1, 3}}, {{3},{1, 2}}, {{1, 2, 3}}.

What is a Bell Number?
Let S(n, k) be total number of partitions of n elements into k sets. The value of the n'th Bell Number is the sum of S(n, k) for k = 1 to n
Bell(n)=∑​S (n,k) for k ranges from [1,n]
Value of S(n, k) can be defined recursively as, S(n+1, k) = k*S(n, k) + S(n, k-1)

How does above recursive formula work? 
When we add a (n+1)'th element to k partitions, there are two possibilities. 
1) It is added as a single element set to existing partitions, i.e, S(n, k-1) 
2) It is added to all sets of every partition, i.e., k*S(n, k).
First few Bell numbers are 1, 1, 2, 5, 15, 52, 203, .... 

Using recursion - O(2 ^ n)  Time and O(n) Space

We can recursively calculate the number of ways to partition a set of n elements by considering each element and either placing it in an existing subset or creating a new subset. For each element, we calculate the number of ways to partition the remaining n-1 elements into k subsets and then sum these values for all possible k. This gives us the following recurrence relation for Stirling numbers of the second kind:

  • S(n,k) = k * S( n - 1, k) + S(n - 1, k - 1)

Finally, to find the Bell number, we sum S(n, k) for all values of k from 1 to n. This gives us the total number of ways to partition the set.


Output
52

Using Top-Down DP (Memoization) - O(n^2) Time and O(n^2) Space

If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:

1. Optimal Substructure: The number of ways to partition a set of n elements into k subsets depends on two smaller subproblems:

  • The number of ways to partition the first n-1 elements into k subsets, and
  • The number of ways to partition the first n-1 elements into k-1 subsets and then add the new element as its own subset. By combining these optimal solutions, we can efficiently calculate the total number of ways to partition the set.

2. Overlapping Subproblems: In the recursive approach, certain subproblems are recalculated multiple times. For example, when computing S(n, k), the subproblems S(n-1, k) and S(n-1, k-1) are recomputed multiple times. This redundancy leads to overlapping subproblems, which can be avoided using memoization or tabulation.

Follow the steps below to solve the problem:

  • Use recursion with memoization to calculate Stirling numbers of the second kind, S(n,k), which represent the number of ways to partition n items into k non-empty subsets.
  • Define base cases for S(0, 0),S(n, 0), S(n, n), and S(n, 1) to handle specific situations in the recursive formula.
  • Use the formula S(n, k) = k × S(n-1, k) + S(n-1, k-1) to compute the Stirling numbers with previously stored results.
  • For a given n, sum S(n, k) for all from 1 to n to calculate the Bell number, which represents the total ways to partition n elements.
  • Output the computed Bell number for the given n.

Output
52

Using Bottom-Up DP (Tabulation - 1) - O(n^2) Time and O(n^2) Space

A Simple Method to compute n'th Bell Number is to one by one compute S(n, k) for k = 1 to n and return sum of all computed values. Refer Count number of ways to partition a set into k subsets for computation of S(n, k).


Output
52

Using Bottom-Up DP (Tabulation - 2) - O(n^2) Time and O(n^2) Space

A Better Method is to use Bell Triangle. Below is a sample Bell Triangle for first few Bell Numbers. 

1
1 2
2 3 5
5 7 10 15
15 20 27 37 52

Follow the steps below to solve the problem:

  • Create a 2D array to store Bell numbers, starting with bell[0][0] = 1 as the base case.
  • Use dynamic programming to fill the table based on the recurrence relation. For each i, set bell[i][0] = bell[i-1][i-1] and compute bell[i][j] using the formula bell[i][j] = bell[i-1][j-1] + bell[i][j-1] for all valid j.
  • The formula computes the Bell numbers by using the Stirling numbers of the second kind, which count ways to partition a set of i elements into j non-empty subsets.
  • The Bell number for n is stored in bell[n][0].

Output
52

Using Space Optimized DP - O(n^2) Time and O(n) Space

We can use a 1-D array to represent the previous row of the Bell triangle. We initialize dp[0] to 1, since there is only one way to partition an empty set.

To compute the Bell numbers for n > 0, we first set dp[0] = dp[i-1], since the first element in each row is the same as the last element in the previous row. Then, we use the recurrence relation dp[j] = prev + dp[j-1] to compute the Bell number for each partition, where prev is the value of dp[j] in the previous iteration of the inner loop. We update prev to the temporary variable temp before updating dp[j]. Finally, we return dp[0], which is the Bell number for the partition of a set with n elements into non-empty subsets.


Output
52
Comment