DP9 binary Coefficient Binomial Coefficient @ geeksforgeeks

Source: Internet
Author: User

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 shocould return 6 for n = 4 and k = 2, and it shoshould return 10 for n = 5 and k = 2.

1) Optimal Substructure
The value of C (n, k) can recursively calculated using following standard formula for Binomial Cofficients.

   C(n, k) = C(n-1, k-1) + C(n-1, k)   C(n, 0) = C(n, n) = 1

2) Overlapping Subproblems
Following is simple recursive implementation that simply follows the recursive structure mentioned above.



Package DP; public class BionomialCoefficient {public static void main (String [] args) {int n = 5, k = 2; System. out. println (binomialCoeffRec (n, k); System. out. println (binomialCoeffDP_2D (n, k); System. out. println (binomialCoeffDP_1D (n, k);} public static int binomialCoeffRec (int n, int k) {if (k = 0 | k = n) {return 1;} return binomialCoeffRec (n-1, k-1) + binomialCoeffRec (n-1, k);} // Returns value of Binomial Coefficient C (n, k) // Time Complexity: O (n * k), Auxiliary Space: O (n * k) public static int binomialCoeffDP_2D (int n, int k) {int [] [] dp = new int [n + 1] [k + 1]; // Caculate value of Binomial Coefficient in bottom up mannerfor (int I = 0; I <= n; I ++) {for (int j = 0; j <= Math. min (I, k); j ++) {if (j = 0 | j = I) {// Base Casesdp [I] [j] = 1 ;} else {// Calculate value using previsly stored valuesdp [I] [j] = dp [I-1] [J-1] + dp [I-1] [j] ;}} return dp [n] [k];} // A space optimized Dynamic Programming Solution // Time Complexity: O (n * k), Auxiliary Space: O (k) public static int binomialCoeffDP_1D (int n, int k) {int [] dp = new int [k + 1]; dp [0] = 1; for (int I = 1; I <= n; I ++) {for (int j = Math. min (I, k); j> 0; j --) {// reverse dp [j] + = dp [J-1] ;}} return dp [k] ;}}


Http://www.geeksforgeeks.org/dynamic-programming-set-9-binomial-coefficient/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.