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.
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.
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 spaces 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:
The Stirling Number s(9, 2) is : 109584
Please refer Stirling numbers of the first kind to read more about the Stirling numbers.
- Number of ways to arrange K different objects taking N objects at a time
- Count number of ways to arrange first N numbers
- Number of ways to arrange N items under given constraints
- Number of ways to arrange a word such that no vowels occur together
- Number of ways to arrange a word such that all vowels occur together
- Number of ways of distributing N identical objects in R distinct groups
- Arrange given numbers to form the smallest number
- Program to calculate the area between two Concentric Circles
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Number of ways to represent a number as sum of k fibonacci numbers
- Number of ways to get even sum by choosing three numbers from 1 to N
- Bell Numbers (Number of ways to Partition a Set)
- Count ways to express a number as sum of consecutive numbers
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.