count number of partitions of a set with n elements into k subsets












7















This program is for count number of partitions of a set with n elements into k subsets I am confusing here return k*countP(n-1, k) + countP(n-1, k-1);
can some one explain what is happening here?
why we are multiplying with k?



NOTE->I know this is not the best way to calculate number of partitions that would be DP



// A C++ program to count number of partitions 
// of a set with n elements into k subsets
#include<iostream>
using namespace std;

// Returns count of different partitions of n
// elements in k subsets
int countP(int n, int k)
{
// Base cases
if (n == 0 || k == 0 || k > n)
return 0;
if (k == 1 || k == n)
return 1;

// S(n+1, k) = k*S(n, k) + S(n, k-1)
return k*countP(n-1, k) + countP(n-1, k-1);
}

// Driver program
int main()
{
cout << countP(3, 2);
return 0;
}









share|improve this question





























    7















    This program is for count number of partitions of a set with n elements into k subsets I am confusing here return k*countP(n-1, k) + countP(n-1, k-1);
    can some one explain what is happening here?
    why we are multiplying with k?



    NOTE->I know this is not the best way to calculate number of partitions that would be DP



    // A C++ program to count number of partitions 
    // of a set with n elements into k subsets
    #include<iostream>
    using namespace std;

    // Returns count of different partitions of n
    // elements in k subsets
    int countP(int n, int k)
    {
    // Base cases
    if (n == 0 || k == 0 || k > n)
    return 0;
    if (k == 1 || k == n)
    return 1;

    // S(n+1, k) = k*S(n, k) + S(n, k-1)
    return k*countP(n-1, k) + countP(n-1, k-1);
    }

    // Driver program
    int main()
    {
    cout << countP(3, 2);
    return 0;
    }









    share|improve this question



























      7












      7








      7








      This program is for count number of partitions of a set with n elements into k subsets I am confusing here return k*countP(n-1, k) + countP(n-1, k-1);
      can some one explain what is happening here?
      why we are multiplying with k?



      NOTE->I know this is not the best way to calculate number of partitions that would be DP



      // A C++ program to count number of partitions 
      // of a set with n elements into k subsets
      #include<iostream>
      using namespace std;

      // Returns count of different partitions of n
      // elements in k subsets
      int countP(int n, int k)
      {
      // Base cases
      if (n == 0 || k == 0 || k > n)
      return 0;
      if (k == 1 || k == n)
      return 1;

      // S(n+1, k) = k*S(n, k) + S(n, k-1)
      return k*countP(n-1, k) + countP(n-1, k-1);
      }

      // Driver program
      int main()
      {
      cout << countP(3, 2);
      return 0;
      }









      share|improve this question
















      This program is for count number of partitions of a set with n elements into k subsets I am confusing here return k*countP(n-1, k) + countP(n-1, k-1);
      can some one explain what is happening here?
      why we are multiplying with k?



      NOTE->I know this is not the best way to calculate number of partitions that would be DP



      // A C++ program to count number of partitions 
      // of a set with n elements into k subsets
      #include<iostream>
      using namespace std;

      // Returns count of different partitions of n
      // elements in k subsets
      int countP(int n, int k)
      {
      // Base cases
      if (n == 0 || k == 0 || k > n)
      return 0;
      if (k == 1 || k == n)
      return 1;

      // S(n+1, k) = k*S(n, k) + S(n, k-1)
      return k*countP(n-1, k) + countP(n-1, k-1);
      }

      // Driver program
      int main()
      {
      cout << countP(3, 2);
      return 0;
      }






      c++ algorithm combinatorics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 16 '18 at 6:41

























      asked Dec 16 '18 at 6:22







      user10796621































          4 Answers
          4






          active

          oldest

          votes


















          4














          What you mentioned is the Stirling numbers of the second kind which enumerates the number of ways to partition a set of n objects into k non-empty subsets and denoted by enter image description here or enter image description here.



          Its recursive relation is:



          enter image description here



          for k > 0 with initial conditions:



          enter image description here



          .



          Calculating it using dynamic programming is more faster than recursive approach:



          int secondKindStirlingNumber(int n, int k) {

          int sf[n + 1][n + 1];
          for (int i = 0; i < k; i++) {
          sf[i][i] = 1;
          }
          for (int i = 1; i < n + 1; i++) {
          for (int j = 1; j < k + 1; j++) {
          sf[i][j] = j * sf[i - 1][j] + sf[i - 1][j - 1];
          }
          }
          return sf[n][k];
          }





          share|improve this answer

































            3














            Each countP call implicitly considers a single element in the set, lets call it A.



            The countP(n-1, k-1) term comes from the case where A is in a set by itself. In this case, we just have to count how many ways there are to partition all the other elements (N-1) into (K-1) subsets, since A takes up one subset by itself.



            The k*countP(n-1, k) term, then, comes from the case where A is not in a set by itself. So we figure out the number of ways of partitioning all the other (N-1) values into K subsets, and multiply by K because there are K possible subsets we could add A to.



            For example, consider the set [A,B,C,D], with K=2.



            The first case, countP(n-1, k-1), describes the following situation:



            {A, BCD}


            The second case, k*countP(n-1, k), describes the following cases:



            2*({BC,D}, {BD,C}, {B,CD}) 


            Or:



            {ABC,D}, {ABD,C}, {AB,CD}, {BC,AD}, {BD,AC}, {B,ACD}





            share|improve this answer































              2














              How do we get countP(n,k)? Assuming that we have devided previous n-1 element into a certain number of partions, and now we have the n-th element, and we try to make k partition.



              we have two option for this:



              either




              1. we have devided the previous n-1 elements into k partions(we have countP(n-1, k) ways of doing this), and we put this n-th element into one of these partions(we have k choices). So we have k*countP(n-1, k).


              or:




              1. we divide previous n-1 elements into k-1 partition(we have countP(n-1, k-1); ways of doing this), and we make the n-th element a single partion to achieve a k partition(we only have 1 choice: putting it seperately). So we have countP(n-1, k-1);.


              So we sum them up and get the result.






              share|improve this answer


























              • Great explanation, finally got it :-)

                – Siyon DP
                Dec 16 '18 at 9:12



















              1














              Based on This a partition of a set is a grouping of the set's elements into non-empty subsets, in such a way that every element is included in one and only one of the subsets. So the total number of partitions of an n-element set is the Bell number which is calculated like below:
              Bell number formula
              Hence if you want to convert the formula to a recursive function it will be like:
              k*countP(n-1,k) + countP(n-1, k-1);






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53799915%2fcount-number-of-partitions-of-a-set-with-n-elements-into-k-subsets%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown
























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                4














                What you mentioned is the Stirling numbers of the second kind which enumerates the number of ways to partition a set of n objects into k non-empty subsets and denoted by enter image description here or enter image description here.



                Its recursive relation is:



                enter image description here



                for k > 0 with initial conditions:



                enter image description here



                .



                Calculating it using dynamic programming is more faster than recursive approach:



                int secondKindStirlingNumber(int n, int k) {

                int sf[n + 1][n + 1];
                for (int i = 0; i < k; i++) {
                sf[i][i] = 1;
                }
                for (int i = 1; i < n + 1; i++) {
                for (int j = 1; j < k + 1; j++) {
                sf[i][j] = j * sf[i - 1][j] + sf[i - 1][j - 1];
                }
                }
                return sf[n][k];
                }





                share|improve this answer






























                  4














                  What you mentioned is the Stirling numbers of the second kind which enumerates the number of ways to partition a set of n objects into k non-empty subsets and denoted by enter image description here or enter image description here.



                  Its recursive relation is:



                  enter image description here



                  for k > 0 with initial conditions:



                  enter image description here



                  .



                  Calculating it using dynamic programming is more faster than recursive approach:



                  int secondKindStirlingNumber(int n, int k) {

                  int sf[n + 1][n + 1];
                  for (int i = 0; i < k; i++) {
                  sf[i][i] = 1;
                  }
                  for (int i = 1; i < n + 1; i++) {
                  for (int j = 1; j < k + 1; j++) {
                  sf[i][j] = j * sf[i - 1][j] + sf[i - 1][j - 1];
                  }
                  }
                  return sf[n][k];
                  }





                  share|improve this answer




























                    4












                    4








                    4







                    What you mentioned is the Stirling numbers of the second kind which enumerates the number of ways to partition a set of n objects into k non-empty subsets and denoted by enter image description here or enter image description here.



                    Its recursive relation is:



                    enter image description here



                    for k > 0 with initial conditions:



                    enter image description here



                    .



                    Calculating it using dynamic programming is more faster than recursive approach:



                    int secondKindStirlingNumber(int n, int k) {

                    int sf[n + 1][n + 1];
                    for (int i = 0; i < k; i++) {
                    sf[i][i] = 1;
                    }
                    for (int i = 1; i < n + 1; i++) {
                    for (int j = 1; j < k + 1; j++) {
                    sf[i][j] = j * sf[i - 1][j] + sf[i - 1][j - 1];
                    }
                    }
                    return sf[n][k];
                    }





                    share|improve this answer















                    What you mentioned is the Stirling numbers of the second kind which enumerates the number of ways to partition a set of n objects into k non-empty subsets and denoted by enter image description here or enter image description here.



                    Its recursive relation is:



                    enter image description here



                    for k > 0 with initial conditions:



                    enter image description here



                    .



                    Calculating it using dynamic programming is more faster than recursive approach:



                    int secondKindStirlingNumber(int n, int k) {

                    int sf[n + 1][n + 1];
                    for (int i = 0; i < k; i++) {
                    sf[i][i] = 1;
                    }
                    for (int i = 1; i < n + 1; i++) {
                    for (int j = 1; j < k + 1; j++) {
                    sf[i][j] = j * sf[i - 1][j] + sf[i - 1][j - 1];
                    }
                    }
                    return sf[n][k];
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 16 '18 at 9:16

























                    answered Dec 16 '18 at 8:59









                    aminographyaminography

                    6,32521535




                    6,32521535

























                        3














                        Each countP call implicitly considers a single element in the set, lets call it A.



                        The countP(n-1, k-1) term comes from the case where A is in a set by itself. In this case, we just have to count how many ways there are to partition all the other elements (N-1) into (K-1) subsets, since A takes up one subset by itself.



                        The k*countP(n-1, k) term, then, comes from the case where A is not in a set by itself. So we figure out the number of ways of partitioning all the other (N-1) values into K subsets, and multiply by K because there are K possible subsets we could add A to.



                        For example, consider the set [A,B,C,D], with K=2.



                        The first case, countP(n-1, k-1), describes the following situation:



                        {A, BCD}


                        The second case, k*countP(n-1, k), describes the following cases:



                        2*({BC,D}, {BD,C}, {B,CD}) 


                        Or:



                        {ABC,D}, {ABD,C}, {AB,CD}, {BC,AD}, {BD,AC}, {B,ACD}





                        share|improve this answer




























                          3














                          Each countP call implicitly considers a single element in the set, lets call it A.



                          The countP(n-1, k-1) term comes from the case where A is in a set by itself. In this case, we just have to count how many ways there are to partition all the other elements (N-1) into (K-1) subsets, since A takes up one subset by itself.



                          The k*countP(n-1, k) term, then, comes from the case where A is not in a set by itself. So we figure out the number of ways of partitioning all the other (N-1) values into K subsets, and multiply by K because there are K possible subsets we could add A to.



                          For example, consider the set [A,B,C,D], with K=2.



                          The first case, countP(n-1, k-1), describes the following situation:



                          {A, BCD}


                          The second case, k*countP(n-1, k), describes the following cases:



                          2*({BC,D}, {BD,C}, {B,CD}) 


                          Or:



                          {ABC,D}, {ABD,C}, {AB,CD}, {BC,AD}, {BD,AC}, {B,ACD}





                          share|improve this answer


























                            3












                            3








                            3







                            Each countP call implicitly considers a single element in the set, lets call it A.



                            The countP(n-1, k-1) term comes from the case where A is in a set by itself. In this case, we just have to count how many ways there are to partition all the other elements (N-1) into (K-1) subsets, since A takes up one subset by itself.



                            The k*countP(n-1, k) term, then, comes from the case where A is not in a set by itself. So we figure out the number of ways of partitioning all the other (N-1) values into K subsets, and multiply by K because there are K possible subsets we could add A to.



                            For example, consider the set [A,B,C,D], with K=2.



                            The first case, countP(n-1, k-1), describes the following situation:



                            {A, BCD}


                            The second case, k*countP(n-1, k), describes the following cases:



                            2*({BC,D}, {BD,C}, {B,CD}) 


                            Or:



                            {ABC,D}, {ABD,C}, {AB,CD}, {BC,AD}, {BD,AC}, {B,ACD}





                            share|improve this answer













                            Each countP call implicitly considers a single element in the set, lets call it A.



                            The countP(n-1, k-1) term comes from the case where A is in a set by itself. In this case, we just have to count how many ways there are to partition all the other elements (N-1) into (K-1) subsets, since A takes up one subset by itself.



                            The k*countP(n-1, k) term, then, comes from the case where A is not in a set by itself. So we figure out the number of ways of partitioning all the other (N-1) values into K subsets, and multiply by K because there are K possible subsets we could add A to.



                            For example, consider the set [A,B,C,D], with K=2.



                            The first case, countP(n-1, k-1), describes the following situation:



                            {A, BCD}


                            The second case, k*countP(n-1, k), describes the following cases:



                            2*({BC,D}, {BD,C}, {B,CD}) 


                            Or:



                            {ABC,D}, {ABD,C}, {AB,CD}, {BC,AD}, {BD,AC}, {B,ACD}






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 16 '18 at 6:48









                            wowserxwowserx

                            43938




                            43938























                                2














                                How do we get countP(n,k)? Assuming that we have devided previous n-1 element into a certain number of partions, and now we have the n-th element, and we try to make k partition.



                                we have two option for this:



                                either




                                1. we have devided the previous n-1 elements into k partions(we have countP(n-1, k) ways of doing this), and we put this n-th element into one of these partions(we have k choices). So we have k*countP(n-1, k).


                                or:




                                1. we divide previous n-1 elements into k-1 partition(we have countP(n-1, k-1); ways of doing this), and we make the n-th element a single partion to achieve a k partition(we only have 1 choice: putting it seperately). So we have countP(n-1, k-1);.


                                So we sum them up and get the result.






                                share|improve this answer


























                                • Great explanation, finally got it :-)

                                  – Siyon DP
                                  Dec 16 '18 at 9:12
















                                2














                                How do we get countP(n,k)? Assuming that we have devided previous n-1 element into a certain number of partions, and now we have the n-th element, and we try to make k partition.



                                we have two option for this:



                                either




                                1. we have devided the previous n-1 elements into k partions(we have countP(n-1, k) ways of doing this), and we put this n-th element into one of these partions(we have k choices). So we have k*countP(n-1, k).


                                or:




                                1. we divide previous n-1 elements into k-1 partition(we have countP(n-1, k-1); ways of doing this), and we make the n-th element a single partion to achieve a k partition(we only have 1 choice: putting it seperately). So we have countP(n-1, k-1);.


                                So we sum them up and get the result.






                                share|improve this answer


























                                • Great explanation, finally got it :-)

                                  – Siyon DP
                                  Dec 16 '18 at 9:12














                                2












                                2








                                2







                                How do we get countP(n,k)? Assuming that we have devided previous n-1 element into a certain number of partions, and now we have the n-th element, and we try to make k partition.



                                we have two option for this:



                                either




                                1. we have devided the previous n-1 elements into k partions(we have countP(n-1, k) ways of doing this), and we put this n-th element into one of these partions(we have k choices). So we have k*countP(n-1, k).


                                or:




                                1. we divide previous n-1 elements into k-1 partition(we have countP(n-1, k-1); ways of doing this), and we make the n-th element a single partion to achieve a k partition(we only have 1 choice: putting it seperately). So we have countP(n-1, k-1);.


                                So we sum them up and get the result.






                                share|improve this answer















                                How do we get countP(n,k)? Assuming that we have devided previous n-1 element into a certain number of partions, and now we have the n-th element, and we try to make k partition.



                                we have two option for this:



                                either




                                1. we have devided the previous n-1 elements into k partions(we have countP(n-1, k) ways of doing this), and we put this n-th element into one of these partions(we have k choices). So we have k*countP(n-1, k).


                                or:




                                1. we divide previous n-1 elements into k-1 partition(we have countP(n-1, k-1); ways of doing this), and we make the n-th element a single partion to achieve a k partition(we only have 1 choice: putting it seperately). So we have countP(n-1, k-1);.


                                So we sum them up and get the result.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Dec 16 '18 at 7:19

























                                answered Dec 16 '18 at 6:49









                                ZhaoGangZhaoGang

                                2,0751118




                                2,0751118













                                • Great explanation, finally got it :-)

                                  – Siyon DP
                                  Dec 16 '18 at 9:12



















                                • Great explanation, finally got it :-)

                                  – Siyon DP
                                  Dec 16 '18 at 9:12

















                                Great explanation, finally got it :-)

                                – Siyon DP
                                Dec 16 '18 at 9:12





                                Great explanation, finally got it :-)

                                – Siyon DP
                                Dec 16 '18 at 9:12











                                1














                                Based on This a partition of a set is a grouping of the set's elements into non-empty subsets, in such a way that every element is included in one and only one of the subsets. So the total number of partitions of an n-element set is the Bell number which is calculated like below:
                                Bell number formula
                                Hence if you want to convert the formula to a recursive function it will be like:
                                k*countP(n-1,k) + countP(n-1, k-1);






                                share|improve this answer




























                                  1














                                  Based on This a partition of a set is a grouping of the set's elements into non-empty subsets, in such a way that every element is included in one and only one of the subsets. So the total number of partitions of an n-element set is the Bell number which is calculated like below:
                                  Bell number formula
                                  Hence if you want to convert the formula to a recursive function it will be like:
                                  k*countP(n-1,k) + countP(n-1, k-1);






                                  share|improve this answer


























                                    1












                                    1








                                    1







                                    Based on This a partition of a set is a grouping of the set's elements into non-empty subsets, in such a way that every element is included in one and only one of the subsets. So the total number of partitions of an n-element set is the Bell number which is calculated like below:
                                    Bell number formula
                                    Hence if you want to convert the formula to a recursive function it will be like:
                                    k*countP(n-1,k) + countP(n-1, k-1);






                                    share|improve this answer













                                    Based on This a partition of a set is a grouping of the set's elements into non-empty subsets, in such a way that every element is included in one and only one of the subsets. So the total number of partitions of an n-element set is the Bell number which is calculated like below:
                                    Bell number formula
                                    Hence if you want to convert the formula to a recursive function it will be like:
                                    k*countP(n-1,k) + countP(n-1, k-1);







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Dec 16 '18 at 6:59









                                    Alireza.NAlireza.N

                                    916




                                    916






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53799915%2fcount-number-of-partitions-of-a-set-with-n-elements-into-k-subsets%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Bundesstraße 106

                                        Verónica Boquete

                                        Ida-Boy-Ed-Garten