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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of ways to arrange K different objects taking N objects at a time
- Count ways to arrange N distinct objects if all clockwise arrangements are considered the same
- Length of rope tied around three equal circles touching each other
- Permutations to arrange N persons around a circular table
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
- Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
- Ways to arrange Balls such that adjacent balls are of different types
- Count number of ways to arrange first N numbers
- Calculating Factorials using Stirling Approximation
- Program for Stirling Interpolation Formula
- Program to calculate the area between two Concentric Circles
- Number of ways to arrange N items under given constraints
- Number of ways to arrange a word such that all vowels occur together
- Number of ways to arrange a word such that no vowels occur together
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Smallest string without any multiplication sign that represents the product of two given numbers
- Radii of the three tangent circles of equal radius which are inscribed within a circle of given radius
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.