Bell Numbers

Bell numbers BnB_n are named after Eric Temple Bell and form a sequence of integers in combinatorics. They begin as follows (OEIS A000110:

B0=1,B1=1,B2=2,B3=5,B4=15,B5=52,B6=203,…B_0 = 1,B_1 = 1,B_2=2,B_3=5,B_4=15,B_5=52,B_6=203,\dots

BnB_n is the number of partitions of a set with cardinality nn. A partition of a set SS is defined as a family of pairwise disjoint nonempty subsets of SS whose union is SS. For example, B3=5B_3 = 5 because the set a,b,c{a, b, c} of 3 elements has 5 different partitions:

{{a},{b},{c}}{{a},{b,c}}{{b},{a,c}}{{c},{a,b}}{{a,b,c}}\begin{aligned} &\{ \{a\},\{b\},\{c\}\} \\ &\{ \{a\},\{b,c\}\} \\ &\{ \{b\},\{a,c\}\} \\ &\{ \{c\},\{a,b\}\} \\ &\{ \{a,b,c\}\} \\ \end{aligned}

B0B_0 is 1 because the empty set has exactly 1 partition.

Recurrence

The Bell numbers satisfy the recurrence:

Bn+1=∑k=0n(nk)BkB_{n+1}=\sum_{k=0}^n\binom{n}{k}B_{k}

Proof:

Bn+1B_{n+1} is the number of partitions of a set containing n+1n+1 elements. Let the set for BnB_n be {b1,b2,b3,…,bn}\{b_1,b_2,b_3,\dots,b_n\} and the set for Bn+1B_{n+1} be {b1,b2,b3,…,bn,bn+1}\{b_1,b_2,b_3,\dots,b_n,b_{n+1}\}. Thus, Bn+1B_{n+1} can be regarded as being obtained from BnB_{n} by adding one element, bn+1b_{n+1}. Consider the element bn+1b_{n+1}.

  • If it is placed in a class by itself, nn elements remain, and the number of partitions in this case is (nn)Bn\dbinom{n}{n}B_{n};

  • If it is placed in a class with some 1 element, n−1n-1 elements remain, and the number of partitions in this case is (nn−1)Bn−1\dbinom{n}{n-1}B_{n-1};

  • If it is placed in a class with some 2 elements, n−2n-2 elements remain, and the number of partitions in this case is (nn−2)Bn−2\dbinom{n}{n-2}B_{n-2};

  • ...

Continuing in this way gives the formula above.

Each Bell number is the sum of the corresponding Stirling numbers of the second kind. The Stirling numbers of the second kind are the number of ways to partition a set of cardinality nn into exactly kk nonempty sets.

Bn=∑k=0n{nk}B_{n} = \sum_{k=0}^n{n\brace k}

Bell Triangle

Construct a triangular matrix as follows (in a form similar to Pascal's triangle):

  • a0,0=1a_{0,0} = 1;
  • For n≥1n \ge 1, the first entry of row nn equals the last entry of the previous row, namely an,0=an−1,n−1a_{n,0}=a_{n-1,n-1};
  • For m,n≥1m,n \ge 1, the nn-th row's mm-th entry equals the sum of the entry to its left and the one to its upper left, namely an,m=an,m−1+an−1,m−1a_{n,m}=a_{n,m-1}+a_{n-1,m-1}.

Some results are as follows:

1122355710   1515   20   27   37   5252   67   87   114151203203255322409523674877\begin{aligned} & 1 \\ & 1\quad\qquad 2 \\ & 2\quad\qquad 3\quad\qquad 5 \\ & 5\quad\qquad 7\quad\qquad 10\,\,\,\qquad 15 \\ & 15\,\,\,\qquad 20\,\,\,\qquad 27\,\,\,\qquad 37\,\,\,\qquad 52 \\ & 52\,\,\,\qquad 67\,\,\,\qquad 87\,\,\,\qquad 114\qquad 151\qquad 203\\ & 203\qquad 255\qquad 322\qquad 409\qquad 523\qquad 674\qquad 877 \\ \end{aligned}

The first entry of each row is a Bell number. The Bell numbers can be computed recursively using this triangle.

Reference implementation

C++

constexpr int MAXN = 2000 + 5;
int bell[MAXN][MAXN];

void f(int n) {
  bell[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    bell[i][0] = bell[i - 1][i - 1];
    for (int j = 1; j <= i; j++)
      bell[i][j] = bell[i - 1][j - 1] + bell[i][j - 1];
  }
}

Python

MAXN = 2000 + 5
bell = [[0 for i in range(MAXN + 1)] for j in range(MAXN + 1)]


def f(n):
    bell[0][0] = 1
    for i in range(1, n + 1):
        bell[i][0] = bell[i - 1][i - 1]
        for j in range(1, i + 1):
            bell[i][j] = bell[i - 1][j - 1] + bell[i][j - 1]

Exponential Generating Function

Consider the exponential generating function of the Bell numbers and its derivative:

B^(x)=∑n=0+∞Bnn!xn=1+∑n=0+∞Bn+1(n+1)!xn+1B^′(x)=∑n=0+∞Bn+1n!xn\begin{aligned} \hat B(x) &= \sum_{n = 0}^{+\infty}\frac{B_n}{n!}x^n \\ &= 1 + \sum_{n = 0}^{+\infty}\frac{B_{n+1}}{(n + 1)!}x^{n + 1} \\ \hat B'(x) &= \sum_{n = 0}^{+\infty}\frac{B_{n+1}}{n!}x^{n} \end{aligned}

By the recurrence for the Bell numbers, we obtain:

Bn+1n!=∑k=0n1(n−k)!Bkk!\frac{B_{n+1}}{n!} = \sum_{k = 0}^{n}\frac{1}{(n-k)!}\frac{B_{k}}{k!}

This is a convolution, so:

B^′(x)=exB^(x)\hat B'(x) = \mathrm{e}^x \hat B(x)

This is a differential equation, whose solution is:

B^(x)=exp⁡(ex+C)\hat B(x) = \exp\left(\mathrm{e}^x + C\right)

Finally, when x=0x = 0, B^(x)=1\hat B(x) = 1. Substituting gives C=−1C = -1, yielding the closed form of the exponential generating function of the Bell numbers:

B^(x)=exp⁡(ex−1)\hat B(x) = \exp\left(\mathrm{e}^x - 1\right)

After preprocessing ex−1\mathrm{e}^x - 1 through its first nn terms, one polynomial exp operation yields the first nn Bell numbers. The bottleneck is the polynomial exp operation, so the time complexity can be O(nlog⁡n)O(n \log n).

References

https://en.wikipedia.org/wiki/Bell_number

This content comes from OI Wiki: Bell Numbers. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.