0

I'm trying to calculate what this problem describes as well, to estimate computation time before execution of a script, which uses cartesian power. Their 'simple example' and it's solution section directly underneath covers exactly what you need to know.
I'm really not sure on how to calculate this after heaving read about this topic for a while now. While I understand that strategy 1 is faulty, I don't fully understand strategy 2 from the linked post

My parameters are: $X^n$ and the constraint $k$. $|X|$ is known and finite. I want to compute the number of all combinations in $X^n$ where no item $x$ in each combination is found $k$ or more times within each combination.

  • $X^n$ with $n\in \Bbb N$, $|X|$ is known and finite
  • $x\in X$
  • $0 < k < n$ constraint

For my use case it doesn't make sense to just compute and filter the the cartesian power, so I'm looking for some formula which can take my parameters to give me a possibly exact number as a result. I'm not a mathematician and unsure about how to come up with a solution for this on my own. Thanks in advance!

tsk
  • 1

1 Answers1

1

We might as well say that $X=\{1,\dots,x\}$, so $|X|=x$.

Let $f(n,x,k)$ denote the number of elements of elements of $X^n$ where each entry appears less than $k$ times. There is no arithmetic formula for $f(n,x,k)$, but it can be computed using the recurrence $$ f(n,x,k)=\sum_{i=0}^{\min(k-1,n)}\binom{n}if(n-i,x-1,k) $$ This recurrence lets you compute $f(n,x,k)$ by filling out an $n\times |X|$ dynamic programming table. Each entry takes $O(k)$ arithmetic operations to compute, so the overall complexity of this approach is $O(nk|X|)$.

If you can use a computer algebra system, there is a more efficient method to compute $f(n,x,k)$ available. Namely, $$ f(n,m,k)=n!\times \left(\text{coef. of $t^n$ in } \left(1+\frac{t^1}{1!}+\dots+\frac{t^{k-1}}{(k-1)!}\right)^m\right) $$ That is, just expand the polynomial $\left(1+\frac{t^1}{1!}+\dots+\frac{t^{k-1}}{(k-1)!}\right)^m$, and the answer is the coefficient of $t^n$ in that polynomial times $n!$. This follows from the theory of exponential generating functions. The big-Oh complexity of this method should be smaller, especially if you use FFT in the polynomial multiplication algorithm.

Mike Earnest
  • 75,930
  • Awesome, thank you! I will be computing this with python probably, so fft won't be too hard to implement for me. But one question I've got left now: what exactly does the $t$ mean in the polynomial part? How does $t$ relate to what i know from my parameters? – tsk May 27 '22 at 19:04
  • I grasp that in your example $x = n! \times ($coef. of $t^n$ in $($polynomial$)^m)$, but I don't get how I can reach $t^n$ without knowing $m$. I hope you can elaborate this for me too – tsk May 27 '22 at 20:54
  • @tsk $m$ is just $|X|$, in that context. I just used $m$ instead of $x$ because the letter "$x$" looks weird as an exponent. – Mike Earnest May 27 '22 at 20:57
  • This way, for the simple example of $n=3$, $X={A,B,C}$ and $k=2$ I'd expect 24 as a result, as only ${AAA}$, ${BBB}$ and ${CCC}$ should not be counted, while there are $3^3=27$ possible combinations, right? But the coefficient of $t^3$ after expanding $(1+ \frac{t^{2-1}}{(2-1)!})^3$ to $t^3+3t^2+3t+1$ equals $1$. And $3! = 6$, so i would get $6$ as a result. What am I missing? – tsk May 27 '22 at 23:54
  • $6$ is the correct count. The only valid words are ABC, ACB, BAC, BCA, CAB, CBA. The rules are that no item is found $k$ or more times. When $k=2$, that means no item appears twice, so each item appears exactly once. – Mike Earnest May 28 '22 at 03:04
  • If instead of "no item appears $k$ or more times", you wanted, "each item appears at most $k$ times", then you would have to modify my answer to $n$ times the coefficient of $t^n$ in $(1+t^1/1! + \dots t^{k}/k!)^m$. – Mike Earnest May 28 '22 at 03:07