Java 带部分集的背包递归解

Java 带部分集的背包递归解,java,recursion,knapsack-problem,Java,Recursion,Knapsack Problem,我有java中背包问题的递归解决方案,下面是我的代码: public class OptimalIncome{ final static int length[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; final static int price[] = new int[] {1, 5, 8, 9, 10, 17, 17, 20, 24, 30}; public static int totalLength = 9; public stati

我有java中背包问题的递归解决方案,下面是我的代码:

public class OptimalIncome{
final static int length[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
final static int price[] = new int[] {1, 5, 8, 9, 10, 17, 17, 20, 24, 30};
public static int totalLength = 9;

public static int bestCut(int i, int totalLength){
    if(i < 0){
        return 0;
    }
    else if(length[i] > totalLength){
        return bestCut(i - 1, totalLength);
    }
    else{
        return Math.max(bestCut(i - 1, totalLength), bestCut(i - 1, totalLength - length[i]) + price[i]);
    }
}

public static void main(String[] args){
    System.out.println("Given total rod length : " + totalLength);
    System.out.println("Maximum income         : " + bestCut(price.length-1, totalLength));
    System.out.println("Smaller part sets      : ");
}
}
公共收入{
最终静态整数长度[]=新整数[]{1,2,3,4,5,6,7,8,9,10};
最终静态整数价格[]=新整数[]{1,5,8,9,10,17,17,20,24,30};
公共静态总长度=9;
公共静态整数最佳剪切(整数i,整数总长度){
if(i<0){
返回0;
}
else if(长度[i]>总长度){
返回最佳切割(i-1,总长度);
}
否则{
返回Math.max(最佳剪切(i-1,总长度),最佳剪切(i-1,总长度[i])+价格[i]);
}
}
公共静态void main(字符串[]args){
System.out.println(“给定总杆长:+总长度”);
System.out.println(“最大收入:+bestCut(price.length-1,totalLength));
System.out.println(“较小的零件集:”);
}
}
它工作得很好,正如您所看到的,我想打印选择集(较小的部分)。我该怎么做? 谢谢

我们开始吧:

导入java.util.ArrayList; 导入java.util.List

public class OptimalIncome {
    final static int length[] = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    final static int price[] = new int[] { 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };
    public static int totalLength = 9;
    static List<Integer> pickedObjects = new ArrayList<Integer>();
    public static int bestCut(int i, int totalLength) {
        if (i < 0) {
            return 0;
        } else if (length[i] > totalLength) {
            return bestCut(i - 1, totalLength);
        } else {
            return Math.max(bestCut(i - 1, totalLength),
                    bestCut(i - 1, totalLength - length[i]) + price[i]);
        }
    }

    public static void printSolution(int i,int totalLength){
        if(i < 0)
            return;
        else if(length[i]>totalLength){
            printSolution(i-1, totalLength);
        }else{
            int sol1 = bestCut(i-1,totalLength);
            int sol2 = bestCut(i - 1, totalLength - length[i]) + price[i];
            // check whether the optimal solution coming from picking the object or not .
            if(sol1>sol2){
                printSolution(i-1, totalLength);
            }else{

                pickedObjects.add(i);
                printSolution(i-1, totalLength - length[i]);
            }
        }
    }
    public static void main(String[] args) {
        System.out.println("Given total rod length : " + totalLength);
        System.out.println("Maximum income         : "
                + bestCut(price.length - 1, totalLength));
        System.out.println("Smaller part sets      : ");
        printSolution(price.length-1, totalLength);
        for (Integer i : pickedObjects) {
            System.out.println("picked object: "+ i +" length : "+ length[i]+ " price "+ price[i]);
        }
    }
}
公共收入{
最终静态整数长度[]=新整数[]{1,2,3,4,5,6,7,8,9,10};
最终静态整数价格[]=新整数[]{1,5,8,9,10,17,17,20,24,30};
公共静态总长度=9;
静态列表PickedObject=new ArrayList();
公共静态整数最佳剪切(整数i,整数总长度){
if(i<0){
返回0;
}else if(长度[i]>总长度){
返回最佳切割(i-1,总长度);
}否则{
返回最大值(最佳切割(i-1,总长度),
最佳切割(i-1,总长度[i])+价格[i];
}
}
公共静态void打印解决方案(整数i,整数总长度){
if(i<0)
返回;
else if(长度[i]>总长度){
打印液(i-1,总长度);
}否则{
int sol1=最佳切割(i-1,总长度);
int sol2=最佳切割(i-1,总长度[i])+价格[i];
//检查最佳解决方案是否来自拾取对象。
如果(sol1>sol2){
打印液(i-1,总长度);
}否则{
选择的对象。添加(i);
打印溶液(i-1,总长度-长度[i]);
}
}
}
公共静态void main(字符串[]args){
System.out.println(“给定总杆长:+总长度”);
System.out.println(“最大收入:
+最佳切割(价格.长度-1,总长度);
System.out.println(“较小的零件集:”);
打印解决方案(价格。长度-1,总长度);
for(整数i:PickedObject){
System.out.println(“拾取的对象:+i+”长度:+length[i]+“price”+price[i]);
}
}
}
我们只需要做一个逆递归,检查你是否得到了构造输出的最优解

尽管我认为你可以在你的解决方案中添加一些备忘录,以便它足够快。
希望能有所帮助。

正如您所见,我想打印一组选项(较小的部分)
不知道您在这句话中的意思。什么是“选择集”?你想打印实际拾取的对象而不仅仅是最佳剪切?是的,对不起,我没有充分解释这个问题。我想查看程序为最佳剪切相关/复制选择的项目,在KNPACK最佳解决方案中查找元素:。我不想自己标记dupe,因为我是这些问题的答案,但这些线程确实处理了这个问题。(一个解释如何在DP解决方案中实现,另一个解释如何在递归解决方案中实现,这就是你的情况)。@amit谢谢我查看了它们,但它们使用矩阵,我的只是一个数组,我如何在代码中实现它?(对不起,我不太擅长重用其他代码)