繁体   English   中英

回溯 - 给定一组数字,找到总和等于 M 的所有子集(给定 M)

[英]Backtracking - Given a set of numbers, find all the subsets with a sum equal to M (M is given)

正如标题所说,我们得到一组数字,我们必须找到总和等于给定数字的所有子集(我们称之为 M)。

你们中的大多数人可能已经熟悉这个问题,所以让我们切入正题。 我最近才开始回溯编程(我得告诉你,到目前为止我是一个彻头彻尾的人),这就是为什么我试图解决更“经典”的问题。

现在,在下面你会看到我的代码,它试图以回溯的方式解决这个问题。 但是,代码给出

线程“main”中的异常 java.lang.StackOverflowError

在第 44 行(我将突出显示它)而且,我真的不知道它是否真的以回溯的方式解决了问题,或者我的代码是否只是完整的和完全的便便。

package project3;

import java.util.*;

public class Main {
    static int[] A = { 1, 2, 3, 4 }; // the array in which we are given the numbers.->
    static int n = A.length;  // -> I have it filled with 1, 2, 3, 4 for testing purposes
    static int m = 5;  // the number which the subsets' sum must be
    static int[] Sol = new int[50];  // the array in which solutions are stored up-> 
                            //->until they are syso'ed, after that it gets zero'ed
    static void makeZero() {          // make the solution array 0 again
        for (int i = 0; i < 50; i++)
            Sol[i] = 0;
    }

    static void show() {  // outputs the solution array
        int i = 0;
        while (Sol[i] != 0 && i < 49) {
            System.out.print(Sol[i] + " ");
            i++;
        }
    }

    public static void main(String[] args) {
        Sol[0]=A[0]; back(0, 1, A[0], 1);// we start with the first number in the array as->
    }                        // -> both the first element as the solution and part of the sum

    static int back(int i, int j, int S, int nr) {
        if (i < n && j < n) {

            if (A[j] + S == m) {// if we got a solution, we output it and then go to the ->
                Sol[nr] = A[j]; // -> next element if possible, if not, we start again with ->
                show();         // -> the following element
                if (j < n - 1)
                    back(i, j++, S, nr);
                else if (i < n - 1) {
                    makeZero();
                    back(i + 1, i + 2, 0, 0);
                }
            }

            else if (A[j] + S > m) {  // condition for stoping and starting over with another element
                if (j < n - 1)  // we try again with the following element
                    back(i, j++, S, nr);// LINE 44 : Exception in thread "main" java.lang.StackOverflowError
                else if (i < n - 2 && j == n - 1) { // if not possible, we start again with the following element
                    makeZero();
                    back(i + 1, i + 2, 0, 0);
                } else if (i == n - 2 && j == n - 1)  // if we are down to the last element-> 
                    if (A[i + 1] == m)             // ->we check if it is ==m
                        System.out.println(A[i + 1]);
                }


            else if (j < n - 1 && A[j] + S < m) {  // obvious
                Sol[nr++] = A[j];
                S = S + A[j];
                back(i, j + 1, S, nr);
            }

            else if (j == n - 1 && A[j] + S < m && i < n - 2) {// if the sum!=m and the are no more elements-> 
                makeZero();                                   // ->start again with another element
                back(i + 1, i + 2, 0, 0);
            }
            else { // if we are down to the last element, we check if it is ==m
                if(A[i+1]==n-1)
                    System.out.println(A[i + 1]);
            }

        }

        return 0;
    }

}

注意:我希望我的评论有用,但如果它们比帮助忽略它们更令人困惑,我认为您可以了解没有它们我在做什么。

尽管如此,我想找出代码给出该错误的原因(我确实知道在什么情况下通常会给出该错误,但我不明白为什么我在这里得到它,因为我看不到任何无限循环) 以及如何使代码工作,以及它是否正在回溯。

为了在不出现堆栈溢出错误的情况下找到所有子集,我强烈建议不要使用递归。 使用递归通常会在运行时产生大量开销。 这种开销会导致堆栈溢出错误。 您应该使用更稳定的算法方法/设计,称为动态规划。

动态编程示例应该向您展示如何获取您当前拥有的内容并将其转换为动态编程概念。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM