{"id":26052,"date":"2017-10-26T19:40:52","date_gmt":"2017-10-26T14:10:52","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26052"},"modified":"2017-10-26T19:40:52","modified_gmt":"2017-10-26T14:10:52","slug":"java-programming-binomial-coefficient","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-binomial-coefficient\/","title":{"rendered":"Java Programming &#8211; Binomial Coefficient"},"content":{"rendered":"<p>Following are common definition of Binomial Coefficients.<br \/>\n1) A binomial coefficient C(n, k) can be defined as the coefficient of X^k in the expansion of (1 + X)^n.<span id=\"more-17806\"><\/span><\/p>\n<p>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.<\/p>\n<p><strong>The Problem<\/strong><br \/>\n<em>Write a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k).<\/em> For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2.<\/p>\n<ul>\n<li><strong>Optimal Substructure<\/strong><br \/>\nThe value of C(n, k) can be recursively calculated using following standard formula for Binomial Coefficients.<\/li>\n<\/ul>\n<pre>   C(n, k) = C(n-1, k-1) + C(n-1, k)\r\n   C(n, 0) = C(n, n) = 1\r\n<\/pre>\n<p>Following is a simple recursive implementation that simply follows the recursive structure mentioned above.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ A Naive Recursive Implementation<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/\/ Returns value of Binomial Coefficient C(n, k)<br\/>int binomialCoeff(int n, int k)<br\/>{<br\/>  \/\/ Base Cases<br\/>  if (k==0 || k==n)<br\/>    return 1;<br\/> <br\/>  \/\/ Recur<br\/>  return  binomialCoeff(n-1, k-1) + binomialCoeff(n-1, k);<br\/>}<br\/> <br\/>\/* Driver program to test above function*\/<br\/>int main()<br\/>{<br\/>    int n = 5, k = 2;<br\/>    printf(&quot;Value of C(%d, %d) is %d &quot;, n, k, binomialCoeff(n, k));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<ul>\n<li><strong>Overlapping Subproblems<\/strong><br \/>\nIt 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.<\/li>\n<\/ul>\n<pre>                             C(5, 2)\r\n                    \/                      \\\r\n           C(4, 1)                           C(4, 2)\r\n            \/   \\                          \/           \\\r\n       C(3, 0)   C(3, 1)             C(3, 1)               C(3, 2)\r\n                \/    \\               \/     \\               \/     \\\r\n         C(2, 0)    C(2, 1)      C(2, 0) C(2, 1)          C(2, 1)  C(2, 2)\r\n                   \/        \\              \/   \\            \/    \\\r\n               C(1, 0)  C(1, 1)      C(1, 0)  C(1, 1)   C(1, 0)  C(1, 1)\r\n<\/pre>\n<p>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.<\/p>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ A Dynamic Programming based solution that uses table C[][] to <br\/>\/\/ calculate the Binomial Coefficient <br\/> <br\/>class BinomialCoefficient<br\/>{<br\/>    \/\/ Returns value of Binomial Coefficient C(n, k)<br\/>    static int binomialCoeff(int n, int k)<br\/>    {<br\/>    int C[][] = new int[n+1][k+1];<br\/>    int i, j;<br\/>     <br\/>        \/\/ Calculate  value of Binomial Coefficient in bottom up manner<br\/>    for (i = 0; i &lt;= n; i++)<br\/>    {<br\/>        for (j = 0; j &lt;= min(i, k); j++)<br\/>        {<br\/>            \/\/ Base Cases<br\/>            if (j == 0 || j == i)<br\/>                C[i][j] = 1;<br\/>      <br\/>            \/\/ Calculate value using previosly stored values<br\/>            else<br\/>                C[i][j] = C[i-1][j-1] + C[i-1][j];<br\/>          }<br\/>     }<br\/>      <br\/>    return C[n][k];<br\/>    }<br\/> <br\/>    \/\/ A utility function to return minimum of two integers<br\/>    static int min(int a, int b)<br\/>    {<br\/>    return (a&lt;b)? a: b; <br\/>    }<br\/> <br\/>    \/* Driver program to test above function*\/<br\/>    public static void main(String args[])<br\/>    {<br\/>    int n = 5, k = 2;<br\/>    System.out.println(&quot;Value of C(&quot;+n+&quot;,&quot;+k+&quot;) is &quot;+binomialCoeff(n, k));<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Value of C[5][2] is 10<\/pre>\n<p>Time Complexity: O(n*k)<br \/>\nAuxiliary Space: O(n*k)<\/p>\n<p>Following is a space optimized version of the above code. The following code only uses O(k).<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C++ program for space optimized Dynamic Programming<br\/>\/\/ Solution of Binomial Coefficient<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>int binomialCoeff(int n, int k)<br\/>{<br\/>    int C[k+1];<br\/>    memset(C, 0, sizeof(C));<br\/> <br\/>    C[0] = 1;  \/\/ nC0 is 1<br\/> <br\/>    for (int i = 1; i &lt;= n; i++)<br\/>    {<br\/>        \/\/ Compute next row of pascal triangle using<br\/>        \/\/ the previous row<br\/>        for (int j = min(i, k); j &gt; 0; j--)<br\/>            C[j] = C[j] + C[j-1];<br\/>    }<br\/>    return C[k];<br\/>}<br\/> <br\/>\/* Drier program to test above function*\/<br\/>int main()<br\/>{<br\/>    int n = 5, k = 2;<br\/>    printf (&quot;Value of C(%d, %d) is %d &quot;,<br\/>            n, k, binomialCoeff(n, k) );<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Value of C[5][2] is 10<\/pre>\n<p>Time Complexity: O(n*k)<br \/>\nAuxiliary Space: O(k)<\/p>\n<p>Explanation:<br \/>\n1==========&gt;&gt; n = 0, C(0,0) = 1<br \/>\n1\u20131========&gt;&gt; n = 1, C(1,0) = 1, C(1,1) = 1<br \/>\n1\u20132\u20131======&gt;&gt; n = 2, C(2,0) = 1, C(2,1) = 2, C(2,2) = 1<br \/>\n1\u20133\u20133\u20131====&gt;&gt; n = 3, C(3,0) = 1, C(3,1) = 3, C(3,2) = 3, C(3,3)=1<br \/>\n1\u20134\u20136\u20134\u20131==&gt;&gt; n = 4, C(4,0) = 1, C(4,1) = 4, C(4,2) = 6, C(4,3)=4, C(4,4)=1<br \/>\nSo here every loop on i, builds i\u2019th row of pascal triangle, using (i-1)th row<\/p>\n<p>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.<br \/>\nIn statement,<br \/>\nC[j] = C[j] + C[j-1]\nRight hand side represents the value coming from previous iteration (A row of Pascal\u2019s triangle depends on previous row). Left Hand side represents the value of current iteration which will be obtained by this statement.<\/p>\n[ad type=&#8221;banner&#8221;]\n<pre>Let's say we want to calculate C(4, 3), \r\ni.e. n=4, k=3:\r\n\r\nAll elements of array C of size 4 (k+1) are\r\ninitialized to ZERO.\r\n\r\ni.e. C[0] = C[1] = C[2] = C[3] = C[4] = 0;\r\nThen C[0] is set to 1\r\n\r\nFor i = 1:\r\nC[1] = C[1] + C[0] = 0 + 1 = 1 ==&gt;&gt; C(1,1) = 1\r\n\r\nFor i = 2:\r\nC[2] = C[2] + C[1] = 0 + 1 = 1 ==&gt;&gt; C(2,2) = 1\r\nC[1] = C[1] + C[0] = 1 + 1 = 2 ==&gt;&gt; C(2,2) = 2\r\n\r\nFor i=3:\r\nC[3] = C[3] + C[2] = 0 + 1 = 1 ==&gt;&gt; C(3,3) = 1\r\nC[2] = C[2] + C[1] = 1 + 2 = 3 ==&gt;&gt; C(3,2) = 3\r\nC[1] = C[1] + C[0] = 2 + 1 = 3 ==&gt;&gt; C(3,1) = 3\r\n\r\nFor i=4:\r\nC[4] = C[4] + C[3] = 0 + 1 = 1 ==&gt;&gt; C(4,4) = 1\r\nC[3] = C[3] + C[2] = 1 + 3 = 4 ==&gt;&gt; C(4,3) = 4\r\nC[2] = C[2] + C[1] = 3 + 3 = 6 ==&gt;&gt; C(4,2) = 6\r\nC[1] = C[1] + C[0] = 3 + 1 = 4 ==&gt;&gt; C(4,1) = 4\r\n\r\nC(4,3) = 4 is would be the answer in our example.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java Programming &#8211; Binomial Coefficient &#8211; Dynamic Programming binomial coefficient can be defined as the coefficient of X^k in the expansion of (1 + X)^n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,70145,2139],"tags":[76723,76721,76727,76733,76731,76725,76726,76719,76729,76720,72847,72842,72845,70483,72848,72840,72846,72987,72985,72843,72844,72850,72839,76730,76732,72852,72853,72851],"class_list":["post-26052","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-dynamic-programming","category-java","tag-algorithm-for-binomial-coefficient","tag-binomial-coefficient-dynamic-programming-java","tag-binomial-coefficient-examples","tag-binomial-coefficient-in-java","tag-binomial-coefficient-in-java-program","tag-binomial-coefficient-problems","tag-binomial-coefficient-properties","tag-binomial-coefficient-time-complexity","tag-binomial-coefficient-using-dynamic-programming-in-java","tag-computing-binomial-coefficients-using-dynamic-programming","tag-concept-of-dynamic-programming","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-in-java","tag-dynamic-programming-java","tag-dynamic-programming-problems","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-how-to-solve-dynamic-programming-problems","tag-java-program-for-binomial-coefficient-using-dynamic-programming","tag-java-program-for-binomial-coefficient-using-recursion","tag-problems-on-dynamic-programming","tag-types-of-dynamic-programming","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26052"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26052\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}