How to resolve the algorithm Stirling numbers of the first kind step by step in the C programming language
How to resolve the algorithm Stirling numbers of the first kind step by step in the C programming language
Table of Contents
Problem Statement
Stirling numbers of the first kind, or Stirling cycle numbers, count permutations according to their number of cycles (counting fixed points as cycles of length one). They may be defined directly to be the number of permutations of n elements with k disjoint cycles. Stirling numbers of the first kind express coefficients of polynomial expansions of falling or rising factorials. Depending on the application, Stirling numbers of the first kind may be "signed" or "unsigned". Signed Stirling numbers of the first kind arise when the polynomial expansion is expressed in terms of falling factorials; unsigned when expressed in terms of rising factorials. The only substantial difference is that, for signed Stirling numbers of the first kind, values of S1(n, k) are negative when n + k is odd. Stirling numbers of the first kind follow the simple identities:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Stirling numbers of the first kind step by step in the C programming language
Overview:
This C program computes and prints the first kind of Stirling numbers, which count the number of ways to partition a set of n
elements into k
non-empty subsets.
Data Structures:
stirling_cache
: A structure representing a cache of precomputed Stirling numbers. It consists of:max
field: Maximum value ofn
for which Stirling numbers are cached.values
field: Pointer to an array of integers storing the cached values.
Functions:
stirling_number1
: Computes the unsigned Stirling number of the first kind for the given values ofn
andk
using recursion and the cache.stirling_cache_create
: Creates a cache for Stirling numbers up to a specified maximum value. It allocates memory for the cache and precomputes the values.stirling_cache_destroy
: Frees the memory allocated for the Stirling number cache.print_stirling_numbers
: Prints a table of Stirling numbers of the first kind up to the specified maximum value.main
: Entry point of the program. It creates a cache, computes and prints Stirling numbers for a specified maximum value, and then destroys the cache.
Algorithm:
The program uses the following formula to compute Stirling numbers:
S(n, k) = S(n - 1, k - 1) + (n - 1) * S(n - 1, k)
where S(n, k)
is the Stirling number of the first kind for n
and k
.
Execution Flow:
- The program creates a Stirling number cache up to a specified maximum value.
- It computes and prints a table of Stirling numbers of the first kind for all values of
n
andk
up to the maximum value. - Finally, the program releases the memory allocated for the cache.
Source code in the c programming language
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct stirling_cache_tag {
int max;
int* values;
} stirling_cache;
int stirling_number1(stirling_cache* sc, int n, int k) {
if (k == 0)
return n == 0 ? 1 : 0;
if (n > sc->max || k > n)
return 0;
return sc->values[n*(n-1)/2 + k - 1];
}
bool stirling_cache_create(stirling_cache* sc, int max) {
int* values = calloc(max * (max + 1)/2, sizeof(int));
if (values == NULL)
return false;
sc->max = max;
sc->values = values;
for (int n = 1; n <= max; ++n) {
for (int k = 1; k <= n; ++k) {
int s1 = stirling_number1(sc, n - 1, k - 1);
int s2 = stirling_number1(sc, n - 1, k);
values[n*(n-1)/2 + k - 1] = s1 + s2 * (n - 1);
}
}
return true;
}
void stirling_cache_destroy(stirling_cache* sc) {
free(sc->values);
sc->values = NULL;
}
void print_stirling_numbers(stirling_cache* sc, int max) {
printf("Unsigned Stirling numbers of the first kind:\nn/k");
for (int k = 0; k <= max; ++k)
printf(k == 0 ? "%2d" : "%10d", k);
printf("\n");
for (int n = 0; n <= max; ++n) {
printf("%2d ", n);
for (int k = 0; k <= n; ++k)
printf(k == 0 ? "%2d" : "%10d", stirling_number1(sc, n, k));
printf("\n");
}
}
int main() {
stirling_cache sc = { 0 };
const int max = 12;
if (!stirling_cache_create(&sc, max)) {
fprintf(stderr, "Out of memory\n");
return 1;
}
print_stirling_numbers(&sc, max);
stirling_cache_destroy(&sc);
return 0;
}
You may also check:How to resolve the algorithm Ascending primes step by step in the Prolog programming language
You may also check:How to resolve the algorithm Set, the card game step by step in the Quackery programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Erlang programming language
You may also check:How to resolve the algorithm Events step by step in the Oz programming language