Following are common definition of Binomial Coefficients.
1) A binomial coefficient C(n, k) can be defined as the coefficient of X^k in the expansion of (1 + X)^n.

2) A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects; more formally, the number of k-element subsets (or k-combinations) of an n-element set.

The Problem
Write a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2.

  • Optimal Substructure
    The value of C(n, k) can be recursively calculated using following standard formula for Binomial Coefficients.
   C(n, k) = C(n-1, k-1) + C(n-1, k)
   C(n, 0) = C(n, n) = 1

Following is a simple recursive implementation that simply follows the recursive structure mentioned above.

[pastacode lang=”c” manual=”%2F%2F%20A%20Naive%20Recursive%20Implementation%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20Returns%20value%20of%20Binomial%20Coefficient%20C(n%2C%20k)%0Aint%20binomialCoeff(int%20n%2C%20int%20k)%0A%7B%0A%20%20%2F%2F%20Base%20Cases%0A%20%20if%20(k%3D%3D0%20%7C%7C%20k%3D%3Dn)%0A%20%20%20%20return%201%3B%0A%20%0A%20%20%2F%2F%20Recur%0A%20%20return%20%20binomialCoeff(n-1%2C%20k-1)%20%2B%20binomialCoeff(n-1%2C%20k)%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%205%2C%20k%20%3D%202%3B%0A%20%20%20%20printf(%22Value%20of%20C(%25d%2C%20%25d)%20is%20%25d%20%22%2C%20n%2C%20k%2C%20binomialCoeff(n%2C%20k))%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]
  • Overlapping Subproblems
    It should be noted that the above function computes the same subproblems again and again. See the following recursion tree for n = 5 an k = 2. The function C(3, 1) is called two times. For large values of n, there will be many common subproblems.
                             C(5, 2)
                    /                      \
           C(4, 1)                           C(4, 2)
            /   \                          /           \
       C(3, 0)   C(3, 1)             C(3, 1)               C(3, 2)
                /    \               /     \               /     \
         C(2, 0)    C(2, 1)      C(2, 0) C(2, 1)          C(2, 1)  C(2, 2)
                   /        \              /   \            /    \
               C(1, 0)  C(1, 1)      C(1, 0)  C(1, 1)   C(1, 0)  C(1, 1)

Since same suproblems are called again, this problem has Overlapping Subproblems property. So the Binomial Coefficient problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, re-computations of same subproblems can be avoided by constructing a temporary array C[][] in bottom up manner. Following is Dynamic Programming based implementation.

[ad type=”banner”] [pastacode lang=”java” manual=”%2F%2F%20A%20Dynamic%20Programming%20based%20solution%20that%20uses%20table%20C%5B%5D%5B%5D%20to%20%0A%2F%2F%20calculate%20the%20Binomial%20Coefficient%20%0A%20%0Aclass%20BinomialCoefficient%0A%7B%0A%20%20%20%20%2F%2F%20Returns%20value%20of%20Binomial%20Coefficient%20C(n%2C%20k)%0A%20%20%20%20static%20int%20binomialCoeff(int%20n%2C%20int%20k)%0A%20%20%20%20%7B%0A%20%20%20%20int%20C%5B%5D%5B%5D%20%3D%20new%20int%5Bn%2B1%5D%5Bk%2B1%5D%3B%0A%20%20%20%20int%20i%2C%20j%3B%0A%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Calculate%20%20value%20of%20Binomial%20Coefficient%20in%20bottom%20up%20manner%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%3D%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(j%20%3D%200%3B%20j%20%3C%3D%20min(i%2C%20k)%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Base%20Cases%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(j%20%3D%3D%200%20%7C%7C%20j%20%3D%3D%20i)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20C%5Bi%5D%5Bj%5D%20%3D%201%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Calculate%20value%20using%20previosly%20stored%20values%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20C%5Bi%5D%5Bj%5D%20%3D%20C%5Bi-1%5D%5Bj-1%5D%20%2B%20C%5Bi-1%5D%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20return%20C%5Bn%5D%5Bk%5D%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20A%20utility%20function%20to%20return%20minimum%20of%20two%20integers%0A%20%20%20%20static%20int%20min(int%20a%2C%20int%20b)%0A%20%20%20%20%7B%0A%20%20%20%20return%20(a%3Cb)%3F%20a%3A%20b%3B%20%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Driver%20program%20to%20test%20above%20function*%2F%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20int%20n%20%3D%205%2C%20k%20%3D%202%3B%0A%20%20%20%20System.out.println(%22Value%20of%20C(%22%2Bn%2B%22%2C%22%2Bk%2B%22)%20is%20%22%2BbinomialCoeff(n%2C%20k))%3B%0A%20%20%20%20%7D%0A%7D” message=”Java” highlight=”” provider=”manual”/]

Output:

Value of C[5][2] is 10

Time Complexity: O(n*k)
Auxiliary Space: O(n*k)

Following is a space optimized version of the above code. The following code only uses O(k).

[pastacode lang=”c” manual=”%2F%2F%20C%2B%2B%20program%20for%20space%20optimized%20Dynamic%20Programming%0A%2F%2F%20Solution%20of%20Binomial%20Coefficient%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0Aint%20binomialCoeff(int%20n%2C%20int%20k)%0A%7B%0A%20%20%20%20int%20C%5Bk%2B1%5D%3B%0A%20%20%20%20memset(C%2C%200%2C%20sizeof(C))%3B%0A%20%0A%20%20%20%20C%5B0%5D%20%3D%201%3B%20%20%2F%2F%20nC0%20is%201%0A%20%0A%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%3D%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Compute%20next%20row%20of%20pascal%20triangle%20using%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20previous%20row%0A%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%20min(i%2C%20k)%3B%20j%20%3E%200%3B%20j–)%0A%20%20%20%20%20%20%20%20%20%20%20%20C%5Bj%5D%20%3D%20C%5Bj%5D%20%2B%20C%5Bj-1%5D%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20C%5Bk%5D%3B%0A%7D%0A%20%0A%2F*%20Drier%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%205%2C%20k%20%3D%202%3B%0A%20%20%20%20printf%20(%22Value%20of%20C(%25d%2C%20%25d)%20is%20%25d%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20n%2C%20k%2C%20binomialCoeff(n%2C%20k)%20)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output:

Value of C[5][2] is 10

Time Complexity: O(n*k)
Auxiliary Space: O(k)

Explanation:
1==========>> n = 0, C(0,0) = 1
1–1========>> n = 1, C(1,0) = 1, C(1,1) = 1
1–2–1======>> n = 2, C(2,0) = 1, C(2,1) = 2, C(2,2) = 1
1–3–3–1====>> n = 3, C(3,0) = 1, C(3,1) = 3, C(3,2) = 3, C(3,3)=1
1–4–6–4–1==>> n = 4, C(4,0) = 1, C(4,1) = 4, C(4,2) = 6, C(4,3)=4, C(4,4)=1
So here every loop on i, builds i’th row of pascal triangle, using (i-1)th row

At any time, every element of array C will have some value (ZERO or more) and in next iteration, value for those elements comes from previous iteration.
In statement,
C[j] = C[j] + C[j-1] Right hand side represents the value coming from previous iteration (A row of Pascal’s triangle depends on previous row). Left Hand side represents the value of current iteration which will be obtained by this statement.

[ad type=”banner”]
Let's say we want to calculate C(4, 3), 
i.e. n=4, k=3:

All elements of array C of size 4 (k+1) are
initialized to ZERO.

i.e. C[0] = C[1] = C[2] = C[3] = C[4] = 0;
Then C[0] is set to 1

For i = 1:
C[1] = C[1] + C[0] = 0 + 1 = 1 ==>> C(1,1) = 1

For i = 2:
C[2] = C[2] + C[1] = 0 + 1 = 1 ==>> C(2,2) = 1
C[1] = C[1] + C[0] = 1 + 1 = 2 ==>> C(2,2) = 2

For i=3:
C[3] = C[3] + C[2] = 0 + 1 = 1 ==>> C(3,3) = 1
C[2] = C[2] + C[1] = 1 + 2 = 3 ==>> C(3,2) = 3
C[1] = C[1] + C[0] = 2 + 1 = 3 ==>> C(3,1) = 3

For i=4:
C[4] = C[4] + C[3] = 0 + 1 = 1 ==>> C(4,4) = 1
C[3] = C[3] + C[2] = 1 + 3 = 4 ==>> C(4,3) = 4
C[2] = C[2] + C[1] = 3 + 3 = 6 ==>> C(4,2) = 6
C[1] = C[1] + C[0] = 3 + 1 = 4 ==>> C(4,1) = 4

C(4,3) = 4 is would be the answer in our example.