Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles - GeeksforGeeks

21 May, 2021
S(r, n), represents the number of ways that we can arrange r objects around indistinguishable circles of length n, and every circle n must have at least one object around it.
Examples:  
Input: r = 9, n = 2
Output: 109584

Input: r = 6, n = 3
Output: 225
The special cases are:  
S(r, 0) = 0, trivial.
S(r, 1) represents the circular permutation which is equal to (r – 1)!
S(r, n) where r = n, equals 1.
S(r, r -1) = rC2
An important identity of the Stirling numbers that S(r, n) = S(r – 1, n – 1) + (r – 1) * S(r – 1, n)Approach: For simplicity, denote the r distinct objects by 1, 2, ..., r. Consider the object “1”. In any arrangement of the objects, either  
“1” is the only object in a circle or“1” is mixed with others in a circle.
“1” is the only object in a circle or
“1” is mixed with others in a circle.
In case 1, there are s(r – 1, n – 1) ways to form such arrangements. In case 2, first of all, the r — 1 objects 2, 3, ..., r are put in n circles in s(r — 1, n) ways; then “1” can be placed in one of the r — 1 distinct space to the “immediate right” of the corresponding r — 1 distinct objects. By multiplication principle, there are (r — 1)s(r — 1, n) ways to form such arrangements in case 2. The identity now follows from the definition of s(r, n) and addition principle. Using the initial values S(0, 0) = 1, s(r, 0) = 0 for r > 1 and s(r, 1) = (r — 1)! for r > 1, and applying the identity we proved, we can easily get the Stirling number by computing it in a recursive way.In the code, we have three functions that are used to generate the Stirling numbers, which are nCr(n, r), which is a function to compute what we call (n – choose – r), the number of ways we can take r objects from n objects without the importance of orderings. factorial (int n) is, unsurprisingly, used to compute the factorial of a number n. The function Stirling number(r, n) works recursively using the four base cases discussed above and then recursing using the identity we proved.
Below is the implementation of the above approach: 
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to implement above approach#include <iostream>using namespace std; // Calculating factorial of an integer n.long long factorial(int n){    // Our base cases of factorial 0! = 1! = 1    if (n == 0)        return 1;     // n can't be less than 0.    if (n < 0)        return -1;    long long res = 1;    for (int i = 2; i < n + 1; ++i)        res *= i;    return res;} // Function to compute the number of combination// of r objects out of n objects.int nCr(int n, int r){    // r cant be more than n so we'd like the    // program to crash if the user entered    // wrong input.    if (r > n)        return -1;     if (n == r)        return 1;     if (r == 0)        return 1;     // nCr(n, r) = nCr(n - 1, r - 1) + nCr(n - 1, r)    return nCr(n - 1, r - 1) + nCr(n - 1, r);} // Function to calculate the Stirling numbers.// The base cases which were discussed above are handled// to stop the recursive calls.long long stirlingNumber(int r, int n){     // n can't be more than    // r, s(r, 0) = 0.    if (n > r)        return -1;     if (n == 0)        return 0;     if (r == n)        return 1;     if (n == 1)        return factorial(r - 1);     if (r - n == 1)        return nCr(r, 2);    else        return stirlingNumber(r - 1, n - 1)               + (r - 1) * stirlingNumber(r - 1, n);} // Driver programint main(){    // Calculating the stirling number s(9, 2)    int r = 9, n = 2;     long long val = stirlingNumber(r, n);    if (val == -1)        cout << " No stirling number";    else        cout << "The Stirling Number s(" << r             << ", " << n << ") is : "  << val;    return 0;}
// Java program to implement// above approachimport java.io.*; class GFG{ // Calculating factorial of// an integer n.static long factorial(int n){    // Our base cases of factorial    // 0! = 1! = 1    if (n == 0)        return 1;     // n can't be less than 0.    if (n < 0)        return -1;    long res = 1;    for (int i = 2; i < n + 1; ++i)        res *= i;    return res;} // Function to compute the number// of combination of r objects// out of n objects.static int nCr(int n, int r){    // r cant be more than n so    // we'd like the program to    // crash if the user entered    // wrong input.    if (r > n)        return -1;     if (n == r)        return 1;     if (r == 0)        return 1;     return nCr(n - 1, r - 1) +           nCr(n - 1, r);} // Function to calculate the Stirling// numbers. The base cases which were// discussed above are handled to stop// the recursive calls.static long stirlingNumber(int r, int n){     // n can't be more than    // r, s(r, 0) = 0.    if (n > r)        return -1;     if (n == 0)        return 0;     if (r == n)        return 1;     if (n == 1)        return factorial(r - 1);     if (r - n == 1)        return nCr(r, 2);    else        return stirlingNumber(r - 1, n - 1) +                                    (r - 1) *               stirlingNumber(r - 1, n);} // Driver Codepublic static void main (String[] args){    // Calculating the stirling number s(9, 2)    int r = 9, n = 2;         long val = stirlingNumber(r, n);    if (val == -1)        System.out.println(" No stirling number");    else        System.out.println( "The Stirling Number s(" +                      r + ", " + n + ") is : " + val);}} // This Code is Contributed by anuj_67
# Python 3 program to implement above approach # Function to compute the number of combination# of r objects out of n objects.# nCr(n, n) = 1, nCr(n, 0) = 1, and these are# the base cases. def nCr(n, r):    if(n == r):        return 1    if(r == 0):        return 1    # nCr(n, r) = nCr(n - 1, r - 1) + nCr(n - 1, r)    return nCr(n - 1, r - 1) + nCr(n - 1, r)     # This function is used to calculate the# factorial of a number n.def factorial(n):    res = 1         # 1 ! = 0 ! = 1    if(n <= 1):        return res    for i in range(1, n + 1):        res *= i    return res     # Main function to calculate the Stirling numbers.# the base cases which were discussed above are# handled to stop the recursive call, n can't be# more than r, s(r, 0) = 0.# s(r, r) = 1. s(r, 1) = (r - 1)!.# s(r, r - 1) = nCr(r, 2)# else as we proved, s(r, n) = s(r - 1, n - 1)# + (r - 1) * s(r - 1, n) def stirlingNumber(r, n):    if(r == n):        return 1    if(n == 0):        return 0    if(n == r -1):        return nCr(r, 2)    if(r - n == 1):        return factorial(r - 1)    return (stirlingNumber(r - 1, n - 1)        + (r - 1) * stirlingNumber(r - 1, n))         r, n = 9, 2 print(stirlingNumber(r, n))
// C# program to implement// above approachusing System; class GFG{ // Calculating factorial of// an integer n.static long factorial(int n){    // Our base cases of factorial    // 0! = 1! = 1    if (n == 0)        return 1;     // n can't be less than 0.    if (n < 0)        return -1;    long res = 1;    for (int i = 2; i < n + 1; ++i)        res *= i;    return res;} // Function to compute the number// of combination of r objects// out of n objects.static int nCr(int n, int r){    // r cant be more than n so    // we'd like the program to    // crash if the user entered    // wrong input.    if (r > n)        return -1;     if (n == r)        return 1;     if (r == 0)        return 1;     return nCr(n - 1, r - 1) +        nCr(n - 1, r);} // Function to calculate the Stirling// numbers. The base cases which were// discussed above are handled to stop// the recursive calls.static long stirlingNumber(int r, int n){     // n can't be more than    // r, s(r, 0) = 0.    if (n > r)        return -1;     if (n == 0)        return 0;     if (r == n)        return 1;     if (n == 1)        return factorial(r - 1);     if (r - n == 1)        return nCr(r, 2);    else        return stirlingNumber(r - 1, n - 1) +                                    (r - 1) *            stirlingNumber(r - 1, n);} // Driver Codepublic static void Main (){    // Calculating the stirling    // number s(9, 2)    int r = 9, n = 2;         long val = stirlingNumber(r, n);    if (val == -1)        Console.WriteLine(" No stirling number");    else        Console.WriteLine( "The Stirling Number s(" +                     r + ", " + n + ") is : " + val);}} // This code is contributed by inder_verma..
<?php// PHP program to implement above approach // Calculating factorial of an integer n.function factorial($n){    // Our base cases of factorial 0! = 1! = 1    if ($n == 0)        return 1;     // n can't be less than 0.    if ($n < 0)        return -1;             $res = 1;    for ($i = 2; $i < $n + 1; ++$i)        $res *= $i;    return $res;} // Function to compute the number of combination// of r objects out of n objects.function nCr($n, $r){    // r cant be more than n so we'd like the    // program to crash if the user entered    // wrong input.    if ($r > $n)        return -1;     if ($n == $r)        return 1;     if ($r == 0)        return 1;     // nCr($n, $r) = nCr($n - 1, $r - 1) + nCr($n - 1, $r)    return nCr($n - 1, $r - 1) + nCr($n - 1, $r);} // Function to calculate the Stirling numbers.// The base cases which were discussed above are handled// to stop the recursive calls.function stirlingNumber($r, $n){     // n can't be more than    // r, s(r, 0) = 0.    if ($n > $r)        return -1;     if ($n == 0)        return 0;     if ($r == $n)        return 1;     if ($n == 1)        return factorial($r - 1);     if ($r - $n == 1)        return nCr($r, 2);    else        return stirlingNumber($r - 1, $n - 1)               + ($r - 1) * stirlingNumber($r - 1, $n);}      // Calculating the stirling number s(9, 2)    $r = 9;    $n = 2;     $val = stirlingNumber($r, $n);    if ($val == -1)        echo " No stirling number";    else        echo  "The Stirling Number s(", $r             ,", " , $n , ") is : " , $val;              // This code is contributed by ANKITRAI1?>
<script>// js program to implement above approach  // Calculating factorial of an integer n.function factorial( n){    // Our base cases of factorial 0! = 1! = 1    if (n == 0)        return 1;     // n can't be less than 0.    if (n < 0)        return -1;    let res = 1;    for (let i = 2; i < n + 1; ++i)        res *= i;    return res;} // Function to compute the number of combination// of r objects out of n objects.function nCr(n, r){    // r cant be more than n so we'd like the    // program to crash if the user entered    // wrong input.    if (r > n)        return -1;     if (n == r)        return 1;     if (r == 0)        return 1;     // nCr(n, r) = nCr(n - 1, r - 1) + nCr(n - 1, r)    return nCr(n - 1, r - 1) + nCr(n - 1, r);} // Function to calculate the Stirling numbers.// The base cases which were discussed above are handled// to stop the recursive calls.function stirlingNumber( r, n){     // n can't be more than    // r, s(r, 0) = 0.    if (n > r)        return -1;     if (n == 0)        return 0;     if (r == n)        return 1;     if (n == 1)        return factorial(r - 1);     if (r - n == 1)        return nCr(r, 2);    else        return stirlingNumber(r - 1, n - 1)               + (r - 1) * stirlingNumber(r - 1, n);} // Driver program// Calculating the stirling number s(9, 2)    let r = 9, n = 2;     let val = stirlingNumber(r, n);    if (val == -1)        document.write( " No stirling number");    else        document.write( "The Stirling Number s(", r             , ", " , n , ") is : "  , val); </script>
The Stirling Number s(9, 2) is : 109584
 
Note: The above solution can be optimized by Dynamic Programming. Please refer, Bell Numbers (Number of ways to Partition a Set) for example.Please refer Stirling numbers of the first kind to read more about the Stirling numbers. 
vt_m
inderDuMCA
ankthon
rohitsingh07052
Combinatorial
Dynamic Programming
Mathematical
Dynamic Programming
Mathematical
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Lexicographic rank of a string
Count of subsets with sum equal to X
Print all permutations in sorted (lexicographic) order
Print all possible strings of length k that can be formed from a set of n characters
Python program to get all subsets of given size of a set
0-1 Knapsack Problem | DP-10
Largest Sum Contiguous Subarray
Bellman–Ford Algorithm | DP-23
Longest Common Subsequence | DP-4
Floyd Warshall Algorithm | DP-16