5

I am trying to find a method to find all the $N$ tuples $(a_1,a_2,\ldots,a_N)$ where all the $a_i \in \mathbb{Z}, a_i \geq 0$ such that the sum of the elements of the tuples is exactly $M=\sum_{i=1}^N a_i$. An obvious way to do this is to to generate all the $N$ tuples with entries between 0 and $M$ and then select the ones add up to the number $M$.

I wonder if there is a way to obtain said $N$ tuples without going through all the possible $N$ tuples.

Geometrically this problem is like finding all the points that belong to the hyperplane $\sum_{i=1}^N a_i = M$ in the set $(\mathbb{Z}^{\geq})^N$.

Thanks in advance for any help!

4 Answers4

5

The stars and bars page referenced by Quasicoherent is what you want. Add $1$ to all the $a_i$ to make them positive. Now you want $N$-tuples that sum to $N+M$. Make a line of $N+M$ stars and put $N-1$ bars to separate them. Count the stars between the bars to get your tuple. There are $N+M-1$ places to put bars, so $N+M-1 \choose N-1$ different tuples.

Ross Millikan
  • 374,822
  • (+1) I would have gone for this approach, but since you grabbed it, I went with a generating function approach. – robjohn Aug 29 '17 at 21:38
3

Generating Function approach $$ \begin{align} \left[x^m\right]\left(\frac1{1-x}\right)^n &=(-1)^m\binom{-n}{m}\\[6pt] &=\binom{n+m-1}{m} \end{align} $$


A Different Stars and Bars Approach

Either I misunderstand Ross Millikan's answer or this is a different approach.

Take $n-1$ bars to separate the $n$ numbers and $m$ stars to represent the numbers between the bars. The number of stars between the bars is one of the $n$ numbers. To count how many $n$-tuples there are, just count the number of ways to choose where the $m$ stars should go within the $n+m-1$ objects: $$ \binom{n+m-1}{m} $$

robjohn
  • 345,667
  • 1
    I see that I have answered a different question. This is how many $n$-tuples there are, not a list of them. – robjohn Aug 29 '17 at 21:40
1

Recursion-like solutions would suggest themselves: generate all $N$-tuples that sum to $M-1$ and add $1$ to every coordinate of all of them. And then go back.

Henno Brandsma
  • 242,131
1

Addendum in case anyone is looking for a software solution:

I stumbled upon this problem, and needed a Pythonic solution to list all of the allowed arrangements. This is the solution I got by reformatting code from Ben Paul Thurston:

def stars_and_bars(num_objects_remaining, num_bins_remaining, filled_bins=()):
    """
    The Stars and Bars (https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics)) problem can be thought of as
    1. Putting m-1 bars ("|") in amongst a row of n stars
    OR
    2. Putting n balls into m bins.
This program lists all the possible combinations by doing so.
"""
if num_bins_remaining > 1:
    # case 1: more than one bins left
    novel_arrangement = []  # receptacle to contain newly generated arrangements.
    for num_distributed_to_the_next_bin in range(0, num_objects_remaining + 1):
        # try putting in anything between 0 to num_objects_remaining of objects into the next bin.
        novel_arrangement.extend(
            stars_and_bars(
                num_objects_remaining - num_distributed_to_the_next_bin,
                num_bins_remaining - 1,
                filled_bins=filled_bins + (num_distributed_to_the_next_bin,),
            )
            # one or multiple tuple enclosed in a list
        )
    return novel_arrangement
else:
    # case 2: reached last bin. Termintae recursion.
    # return a single tuple enclosed in a list.
    return [
        filled_bins + (num_objects_remaining,),
    ]

So for example, if you'd like to list all the ways to write $N=6$-tuple of nonnegative integers such that they sum up to $M=10$, you can call the function

print(stars_and_bars(10, 6))
  • 1
    Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Aug 30 '21 at 20:33