回溯算法的Java程序,用于填充问题问题

发布于 2025-01-18 15:15:09 字数 2337 浏览 1 评论 0原文

我目前正在处理一个问题,我应该在Java中实现的算法算法。

回溯用于解决问题,其中从指定的集合中选择了对象序列,以便序列满足某些标准。在此任务中,您应该为填充问题的问题应用回溯算法,该算法解释为:

•问题:给定n个正整数(重量)和正整数W,确定整数组合的所有组合,这些组合的所有组合总和为W。

•输入:正整数n,排序(非降低顺序)正整数的阵列W索引从1到n,以及一个正整数W.

•输出:整数的所有组合总和

W。通常的惯例,n,w,w和includn不是我们例行程序的输入。如果这些变量是在全球定义的,那么对sum_of_subsets的顶级呼叫将如下:

sum_of_subsets(0, 0, total);

书中包含的算法:

void sum_of_subsets(index i, 
                    int weight, int total)  {
  if (promising(i))
     if (weight == W)
        cout << include[1] through include [i];
     else {
        include[i + 1] = "yes";               // Include w[i + 1].
        sum_of_subsets(i + 1, weight + w[i + 1], total - w[i + 1]);
        include[i + 1] = "no";                // Do not include w[i + 1].
        sum_of_subsets(i + 1, weight, total - w[i + 1]);
     }
}

bool promising (index i);  {
  return (weight + total >= W) && (weight == W || weight + w[i + 1] <= W);
}

我试图使用Java实现上述算法,但是我不错的



public class Backtracking {

    //public static boolean include[];
    public static int W = 52;
    public static int w[] = { 2, 10, 13, 17, 22, 42 };
    public static boolean include[] = new boolean[50];

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("solution: ");
        for(int i=0; i<w.length-1;i++) {
            sum_of_subsets(i, w[i], W);
        }

    }

    public static void sum_of_subsets(int i, int weight, int total) {

        if (promising(i, weight, total)) {
            if (weight == W) {
                for (int j = 0; j < w.length; j++) {
                    if(include[j])
                        System.out.println(w[j]);
                }
            } else {
                include[i + 1] = true;
                sum_of_subsets(i + 1, weight + w[i + 1], total - w[i + 1]);
                include[i + 1] = false;
                sum_of_subsets(i + 1, weight, total - w[i + 1]);

            }
            

        }
        
    }

    public static boolean promising(int i, int weight, int total) {
        return (weight + total >= W) && (weight == W || weight + w[i+1] <= W);
    }

}

输出,我得到的是错误的,我得到了{17我得到{17 ,22}而不是总和多达52的子集,例如{42,10},{17,13,22}等

I'm currently working on a problem where I'm supposed to implement in Java the backtracking algorithm of the sum-of-subset.

Backtracking is used to solve problems in which a sequence of objects is selected from a specified set so that the sequence satisfies some criteria. In this assignment, you are expected to apply the Backtracking algorithm for the Sum-of-Subsets problem, which is explained as follows:

• Problem: Given n positive integers (weights) and a positive integer W, determine all combinations of the integers that sum to W.

• Inputs: Positive integer n, sorted (non-decreasing order) array of positive integers w indexed from 1 to n, and a positive integer W.

• Outputs: all combinations of the integers that sum to W.

Following our usual convention, n, w, W, and include are not inputs to our routines. If these variables were defined globally, the top-level call to sum_of_subsets would be as follows:

sum_of_subsets(0, 0, total);

the algorithm included in the book:

void sum_of_subsets(index i, 
                    int weight, int total)  {
  if (promising(i))
     if (weight == W)
        cout << include[1] through include [i];
     else {
        include[i + 1] = "yes";               // Include w[i + 1].
        sum_of_subsets(i + 1, weight + w[i + 1], total - w[i + 1]);
        include[i + 1] = "no";                // Do not include w[i + 1].
        sum_of_subsets(i + 1, weight, total - w[i + 1]);
     }
}

bool promising (index i);  {
  return (weight + total >= W) && (weight == W || weight + w[i + 1] <= W);
}

I have tried to implement the above algorithm using Java but it's not working



public class Backtracking {

    //public static boolean include[];
    public static int W = 52;
    public static int w[] = { 2, 10, 13, 17, 22, 42 };
    public static boolean include[] = new boolean[50];

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("solution: ");
        for(int i=0; i<w.length-1;i++) {
            sum_of_subsets(i, w[i], W);
        }

    }

    public static void sum_of_subsets(int i, int weight, int total) {

        if (promising(i, weight, total)) {
            if (weight == W) {
                for (int j = 0; j < w.length; j++) {
                    if(include[j])
                        System.out.println(w[j]);
                }
            } else {
                include[i + 1] = true;
                sum_of_subsets(i + 1, weight + w[i + 1], total - w[i + 1]);
                include[i + 1] = false;
                sum_of_subsets(i + 1, weight, total - w[i + 1]);

            }
            

        }
        
    }

    public static boolean promising(int i, int weight, int total) {
        return (weight + total >= W) && (weight == W || weight + w[i+1] <= W);
    }

}

the output i get is wrong i get {17,22} instead of subsets that sums up to 52 like {42,10} , {17,13,22} etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文