Suppose f (n, k) = the number of possible paths using k inequality (and therefore nk-1 equality), therefore: suppose you have the number n-1, now you want to add another number and calculate f (n, k), then we have two possibilities:
1) There are (k-1) inequalities in those (n-1) numbers, and there are (k + 1) (f (n-1, k-1)) ways to add an n-dimensional number so a new inequality is added.
2) These (n-1) numbers have k inequalities, and there are (k + 1) (f (n-1, k)) ways to add the nth number without additional inequality.
f(n,k) = (k+1)(f(n-1,k-1) + f(n-1,k))
and what you want is their sum (from zero to n-1), the Bellow code is in C # (just checked for simple cases), in fact we just calculate the number of possible ways that do not generate all the paths.
class EqualInequalPermutation
{
public int OrderingsNumber(int n)
{
int[][] f = new int[n+1][];
for (int i = 0; i < n+1; i++)
{
f[i] = new int[n];
for (int j = 0; j < n; j++)
f[i][j] = 0;
}
f[1][0] = 1;
int factorial = 1;
for (int i = 1; i <= n; i++)
{
f[i][0] = 1;
factorial *= i;
f[i][i - 1] = factorial;
for (int k = 1; k < n; k++)
{
f[i][k] = (k + 1) * (f[i - 1][k - 1] + f[i - 1][k]);
}
}
int answer = 0;
for (int i = 0; i < n; i++)
{
answer += f[n][i];
}
return answer;
}
}
source
share