Find all k-tuples of (strictly) monotonically increasing numbers

139 Views Asked by At

Topic might be missleading so here's what I want to achieve in Oactave/Matlab (result preferably row-wise in a matrix):

Let $\lambda=(\lambda_1,\ldots,\lambda_k)\in\mathbb{N}^k_{\geq0}$. So I want all the tuples fullfilling: $0\leq\lambda_1\leq\lambda_2\leq\ldots\leq\lambda_k\leq n$ for a given $n\in\mathbb{N}$. I also would like to able to teawk the alogrithm to exchange the $\leq$ for a $<$.

So for $k=2$, $n=3$ it should give something like that:

0 0
0 1
0 2
0 3
1 1
1 2
1 3
2 2
2 3
3 3
1

There are 1 best solutions below

1
On BEST ANSWER

The following Octave code should work in the $\leq$ case. It can be modified to work in the $<$ case as well. The amount of work to compute all tuples in the $\leq$ case is proportional to $k {n + k\choose k}$.

n = 3;
k = 5;
curr = zeros(k, 1);
A = zeros(nchoosek(n + k, k), k);
for r = 1:size(A, 1)
    A(r,:) = curr;
    ++curr(k);
    j = k;
    while (j > 1 && curr(j) > n)
        curr(j) = 0;
        ++curr(--j);
    end
    for i = 2:k
        if (curr(i - 1) > curr(i))
            curr(i) = curr(i - 1);
        end
    end
end