1

Suppose I want to find all the possible solutions for the equation below.

$$x_1 + x_2 + \dots + x_k + \dots + x_K = N$$

where

$$x_k \in \text{integer}, \text{i.e.}, x_1,x_2, \dots, x_k \in \left\{0,1,2, \dots, N\right\} $$

Right now I have written a simple script in MATLAB which is basically an exhaustive search algorithm which tests all the possible combinations for $x_k$ and returns the set of $x_k$s result in a summation of $N$. Although this works perfectly fine with small values of $N$ and $K$, as the those parameters become larger, it would take forever to see the result and it requires a gigantic amount of RAM which is beyond the capability of PC.

Hence, I am wondering has anyone seen an algorithm or something in Matlab or anywhere which would be less complex and faster and easier to implement? Any suggestion or hint would be much appreciated.


Edit

Sorry English is not my first language, I am looking for the actual solutions. I am not asking for the total number of possible solutions.

RobPratt
  • 45,619
Cardinal
  • 860
  • See Theorem 2 on the linked page. – N. F. Taussig Oct 19 '20 at 02:06
  • @N.F.Taussig Would you please tell ne how that theorem could help me find the valid solutions. I am an average joe. – Cardinal Oct 19 '20 at 02:08
  • 3
    You should use a backtracking algorihtm. – Alt Oct 19 '20 at 02:19
  • 2
    Are you trying to explicitly write out all the solutions, or just count the number of them? – JimmyK4542 Oct 19 '20 at 02:21
  • Unless I am interpreting this incorrectly, this is simple Stars and Bars: https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics).
    Applying this, we get $\boxed{\binom{N-k+K-1}{K-1}}$
    – TheBestMagician Oct 19 '20 at 02:24
  • 1
    According to the question in the current form, the OP is asking for the actual solutions, not the number of solutions. – Alt Oct 19 '20 at 02:27
  • I am looking for "all the solutions" not total "number" of them. – Cardinal Oct 19 '20 at 02:30
  • 1
    For a relatively low $N$, $k$ and $K$ the total number of solutions can be huge, so no matter how quick your algorithm is, you're going to run into trouble listing all the solutions, especially if symmetry is not accounted for. For example for $N=50$, $k=25$, $K=0$ there are approx $2^{64}$ solutions. – Randy Marsh Oct 19 '20 at 02:45
  • See this answer. The main while loop there is essentially an iterator function. One implicitly order all the solutions in a list and given any solution (other than the last one) in the list, the iterator function knows how to figure out the next one in the list. – achille hui Oct 19 '20 at 04:23

2 Answers2

1

Several integer linear programming or constraint programming solvers will find all feasible solutions upon request, without you having to write a specialized algorithm. Gurobi and Cplex both support this functionality and have MATLAB APIs.

RobPratt
  • 45,619
  • Thank you very much for the answer, but would you please give me an example? If in Matlab, that would be much appreciated. – Cardinal Oct 19 '20 at 02:34
  • https://www.mathworks.com/matlabcentral/answers/437815-solve-system-of-equations-and-inequalities-with-multiple-solutions#answer_354561 – RobPratt Oct 19 '20 at 02:37
  • Integer solvers are too-heavy hammers for this problem, and they usually won't scale when $N$ and $K$ are large. In addition, they would provide "one" feasible solution, not all solutions. – Alt Oct 19 '20 at 18:33
  • @Alt many integer solvers do find all solutions upon request. – RobPratt Oct 19 '20 at 19:04
  • 1
    @RobPratt, thanks, I have used Gurobi and CPLEX in the past. I was not aware of this. However, I still believe unless you have some sophisticated-enough constraints, generic integer and SMT solvers are not a good fit here. – Alt Oct 19 '20 at 20:16
1

I just wrote a small python script that performs the backtracking algorithm that I mentioned in the comments.

def get_sols(N, K, sol):
    if N == 0:
        sol = sol + [0] * (K - len(sol))
        print(sol)
    elif len(sol) == K - 1:
        sol.append(N)
        print(sol)
    else:
        sol_tmp = sol
        for j in range(N + 1):
            sol = sol_tmp + [j]
            get_sols(N - j, K, sol)
        sol = sol_tmp    

Example usage:

K = 5
N = 5    
get_sols(N, K, [])

I hope this can help.

As @Randy Marsh mentioned in the comments, there might be exponential number of possible solutions, so if you want to use this code for large values of $N$ or $K$, you should include other constraints in the code that significantly filter possible solutions.

Alt
  • 2,592