如何用Java实现子集和问题
有谁知道如何通过这个伪代码在Java中实现子集和问题?
w = an array of positive integers sorted in non-decreasing order.
W = the target sum value
include = an array or arraylist of the solutions who's weight adds up to W. After the print statement, this array can be deleted so that it can store the next solution.
weight = weight of elements in the include array.
total = weight of the remaining elements not in the include array.
public static void sum_of_subsets(index i, int weight, int total)
{
if(promising(i))
{
if(weight == W)
{
System.out.print(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]);
}
}
}
public static boolean promising(index i);
{
return (weight + total >= W) && (weight == W || weight + w[i + 1] <= W);
}
这真的让我感到困惑,所以如果你能添加评论那就太好了!
Does anyone know how to implement the Sum-of-Subsets problem in Java from this pseudo code?
w = an array of positive integers sorted in non-decreasing order.
W = the target sum value
include = an array or arraylist of the solutions who's weight adds up to W. After the print statement, this array can be deleted so that it can store the next solution.
weight = weight of elements in the include array.
total = weight of the remaining elements not in the include array.
public static void sum_of_subsets(index i, int weight, int total)
{
if(promising(i))
{
if(weight == W)
{
System.out.print(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]);
}
}
}
public static boolean promising(index i);
{
return (weight + total >= W) && (weight == W || weight + w[i + 1] <= W);
}
this is really baffling me, so if you could add comments that would be great!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该程序从一组数字中找到精确的对以形成所需的总和,该程序最后还会返回唯一的对。如果没有找到确切的子集,程序还会返回最近的/最接近的子集以形成所需的总和。您可以按原样运行程序来查看演示,然后根据需要进行修改。我在这里应用的逻辑是基于给定集合中的所有数字组合来获得所需的总和,您可以参考内联注释以获取更多信息
This program finds the exact pairs from a set of numbers to form the desired sum, the program also returns the unique pairs in the end. The program also returns a nearest/closest subset to form the desired sum if no exact subset is found. You can run the program as is to see the demo and then do the modification if needed. The logic I have applied here is based on all combinations of numbers in a given set to get the desired sum, you can refer to inline comments for further information
用 Java 编码的算法
首先,算法会删除所有大于开始时之和的数字。
然后,对于小于总和的最大数字,它检查列表中是否有任何数字可以与自身相加以获得总和。一旦我们找到一对,或者总和大于所需的总和,我们就可以中断,因为列表已排序。然后我们考虑第二大的数字,看看是否可以与它配对,依此类推。
上面的算法不返回对,但是相加很简单。
Algorithms Coded in Java
First the algorithm removes all numbers that are larger than the sum to begin with.
Then for the largest number smaller than the sum, it checks if there are any numbers in the list that it can add to itself to get the sum. Once we have either found a pair, or the sum is greater than the desired sum, we can break since the list is sorted. We then consider the second largest number and see if we can make a pair with that, and so on.
The algorithm above does not return the pairs, but that is trivially to add.