The number of ways of choosing k items from n has a neat recurrence that dynamic programming evaluates without large factorials.

The Recurrence

The binomial coefficient C(n, k), read "n choose k", counts the ways of choosing k objects out of n. It satisfies C(n, k) = C(n − 1, k − 1) + C(n − 1, k) for 0 < k < n, with base cases C(n, 0) = C(n, n) = 1. The reasoning: any chosen set either contains a particular element (choose k − 1 from the other n − 1) or does not (choose k from the other n − 1). These values form Pascal's triangle, in which each entry is the sum of the two above it.

Why Not Use Factorials

The formula C(n, k) = n! / (k!(n − k)!) involves factorials which overflow quickly: 21! already exceeds a 64-bit integer, although C(21, 2) is only 210. The recurrence uses only additions of values no larger than the answer, so it is safe. Naive recursion on the recurrence has overlapping sub-problems (C(n − 2, k − 1) appears twice from C(n, k)) and takes exponential time, so we tabulate.

The Table Algorithm

Create a table with rows 0 to n and columns 0 to k. Fill row by row: for each i from 0 to n and j from 0 to min(i, k): if j = 0 or j = i set the entry 1, else set it to the sum of the entry above and the entry above-left. The answer is the entry in row n, column k. The number of additions is about n·k/2, so the time is Θ(n·k) and the space Θ(n·k), reducible to Θ(k) by keeping one row at a time. Applications include probability, the binomial theorem, combinatorial counting and the number of paths on a grid.

Definition: Binomial coefficient. The number C(n, k) of ways to choose k items from n, computed by C(n, k) = C(n − 1, k − 1) + C(n − 1, k) with C(n, 0) = C(n, n) = 1.

Table 3.2: Pascal's triangle up to n = 5

n \ k 0 1 2 3 4
2 1 2 1 - -
3 1 3 3 1 -
4 1 4 6 4 1
5 1 5 10 10 5

Example 3.2: Computing C(5, 2)

Problem: Find C(5, 2) with the recurrence.

Solution:

  1. C(5, 2) = C(4, 1) + C(4, 2).
  2. C(4, 1) = C(3, 0) + C(3, 1) = 1 + 3 = 4.
  3. C(4, 2) = C(3, 1) + C(3, 2) = 3 + 3 = 6.
  4. So C(5, 2) = 4 + 6 = 10, which agrees with 5!/(2! 3!) = 120/12 = 10.

Result: C(5, 2) = 10.

def binom(n, k):
    C = [[0] * (k + 1) for _ in range(n + 1)]
    adds = 0
    for i in range(n + 1):
        for j in range(min(i, k) + 1):
            if j == 0 or j == i:
                C[i][j] = 1
            else:
                C[i][j] = C[i-1][j-1] + C[i-1][j]
                adds += 1
    return C[n][k], adds
print("C(5,2) =", binom(5, 2))
print("C(10,4) =", binom(10, 4))
print("C(30,15) =", binom(30, 15))

Output

C(5,2) = (10, 7)
C(10,4) = (210, 30)
C(30,15) = (155117520, 330)

Common mistakes

  • Computing n! and dividing; large factorials overflow.
  • Applying the recurrence without the base cases C(n, 0) = C(n, n) = 1.
  • Filling cells with j > i, which should be zero.
  • Using plain recursion, which repeats sub-problems exponentially.

Key takeaways

  • C(n, k) = C(n − 1, k − 1) + C(n − 1, k); C(n, 0) = C(n, n) = 1.
  • The table is Pascal's triangle; time and space are Θ(nk).
  • Only additions are needed, so there is no overflow from factorials.
  • C(n, k) = C(n, n − k).

Practice questions

  1. State Pascal's rule.
  2. What is the time complexity of the table method?
  3. What is C(6, 3)?

Answers to the practice questions

  1. C(n, k) = C(n − 1, k − 1) + C(n − 1, k).
  2. Θ(n·k).

Sources

  • Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit III