Listing the number of ways of choosing $m$ objects with replacement from $n$ objects

computabilitycomputational complexitycomputational mathematicselementary-set-theorypermutations

In general there are $n^m$ total ways of writing down $m$ objects from a set of $n$ objects. Listing all possible combinations will have a $\mathcal{O}(n^m)$ complexity and can be done via using $m$ for-loops in a programming language.

If we look at the problem of choosing $m$ elements from a set of $n$ elements with replacement for small $n$, e.g. $n=4$, and large enough $m$, there are $${{n+m-1}\choose{m}}\approx \mathcal{O}(m^3)$$ cases in total.

This suggests that one may find an algorithm which lists all such elements with cubic complexity.

Here is an example: Let $m=3$ and $n=3$ corresponding to the set $\{0,1,2\}$. I would like to pick $m=3$ elements and I have ${{3+3-1}\choose{3}}=10$ different elements of my set.

Here are the elements:

$1. \{\{0,1,2\},\{0,2,1\},\{1,0,2\},\{1,2,0\},\{2,1,0\},\{2,0,1\}\}$

$2. \{\{0,0,0\}\}$

$3. \{\{1,1,1\}\}$

$4. \{\{2,2,2\}\}$

$5. \{\{0,0,1\},\{0,1,0\},\{1,0,0\}\}$

$6. \{\{0,2,0\},\{2,0,0\},\{0,0,2\}\}$

$7. \{\{1,2,1\},\{1,1,2\},\{2,1,1\}\}$

$8. \{\{1,1,0\},\{1,0,1\},\{0,1,1\}\}$

$9. \{\{2,2,0\},\{2,0,2\},\{0,2,2\}\}$

$10. \{\{1,2,2\},\{2,1,2\},\{2,2,1\}\}$

Given the original set $\{0,1,2\}$, one can list all those $10$ sets with $\mathcal{O}(m^n)$ complexity. First we list all possible combinations:
$\{\{0,0,0\},\{0,0,1\},\ldots,\{2,2,2\}\}$. Then for all element we get, we make all possible permutations and remove all the same elements from the set and move to the next element and so on. This requires one to list first of all, all possible combinations, which requires $n^m$ complexity. I am wondering if there is an efficient algorithm which can do the same thing in cubic complexity.

My aim is actually to sum up the indexes of my vectors given by those $10$ index sets. I have $3$ vectors $v_1,v_2,v_3$ each indexed from $0$ to $2$, I would like to get a vector which sums the indexes as given by those $10$ cases. For example,

$v(1)=v_1(0)v_2(1)v_3(2)+v_1(0)v_2(2)v_3(1)+v_1(1)v_2(0)v_3(2)+v_1(1)v_2(2)v_3(0)+v_1(2)v_2(1)v_3(0)+v_1(2)v_2(0)v_3(1)$

$v(2)=v_1(0)v_2(0)v_3(0)$

$v(3)=v_1(1)v_2(1)v_3(1)$

$v(4)=v_1(2)v_2(2)v_3(2)$

$v(10)=v_1(1)v_2(2)v_3(2)+v_1(2)v_2(1)v_3(2)+v_1(2)v_2(2)v_3(1)$

and so on.

Question: Is it possible to list all such elements with no more than cubic complexity? Or in other words is there an efficient way of computing my vector $v$, given $v_1,v_2,v_3$.

Note: The order of indexes of vector $v$ is not important. Namely the algorithm can calculate $v$ as any permuted way of the $v$ that I described above, e.g. $v(0):=v(6)$ and $v(6):=v(0)$ is no problem.

Best Answer

Your algorithm could do the following:

1) List all subsets of $ \{1,...,n+m-1\} $ of size $ m $ (without repetition). For m=3 we have a cubic algorithm for this. But in general this is $ \mathcal{O}(n^m) $. * Edit: For $ n \leq 4 $ this will be in $ \mathcal{O}(m^3).$ But in general it will be in $ \mathcal{O}(m^n) $ which isn't necessarily cubic.*

All the subsets listed above have m distinct elements, so we can write them with their elements in ascending order, turning the sets into lists.

2) From the $i$-th element of every list, subtract $ i $. (For $0 \leq i \leq m-1 $)

This will give you all lists of n elements with repetition but each list will appear exactly once. To see why this works look in the post you linked, in the first answer by Arturo Magidin, the proof that there are $ \binom{n+m-1}{m} $ ways of choosing m elements from n with repetition.

Related Question