背包0-1路径重建(要采取的项目)

我知道如何用动态编程方法解决背包0-1问题,但是我在找出哪些项目而不影响O(N * C)(N项,C容量)的复杂性时遇到了麻烦。 任何想法(我更喜欢自下而上的方法)?     
已邀请:
假设,现在你将结果存储在数组
bool[] a
中,当
a[i]
可以实现时,
a[i]
为真。 你需要另一个阵列
int[] b
,其中
b[i]
是你放入背包以实现总和
i
的最后一个元素。 所以,你在哪里
a[i] = true;
你需要
a[i] = true;
b[i] = current_item;
然后,找到可以采用哪些项来实现和
i
是一个简单的循环。 PS我为了简单起见使用了两个数组,但显然可以删除数组
a
。     
这是对O(n)次重建路径的修改
int knapsack(int weight[], int profit[], int no_of_items, int capacity) {
    for (int var = 0; var <= capacity; ++var) {
        dp[0][var] = 0;
    }
    for (int var = 0; var <= no_of_items; ++var) {
        path[var] = false;
    }
    int using_item_i, without_using_item_i;
    for (int i = 1; i <= no_of_items; ++i) {
        for (int j = 1; j <= capacity; ++j) {
            without_using_item_i = dp[i - 1][j];
            using_item_i = 0;
            if ((weight[i]) <= j) {
                using_item_i = dp[i - 1][j - weight[i]] + profit[i];
            }

            if (using_item_i >= without_using_item_i) {
                taken[i][j] = true;
                dp[i][j] = using_item_i;
            } else {
                taken[i][j] = false;
                dp[i][j] = without_using_item_i;
            }
        }
    }
    //Reconstructing back the path
    int j = capacity;
    for (int i = no_of_items; i >= 0; --i) {
        if (taken[i][j]) {
            path[i] = true;
            cnt++;
        }
        j = j -  weight[i];
    }
    return dp[no_of_items][capacity];
}
    
boolean[] solution = new boolean[nItems];

for (int i = nItems, c = maxCapacity; i > 0 && c > 0; i--) {
    int iThItemAddedValue = value[i - 1][c - weights[i - 1]] + values[i - 1];
    int iThItemInheritedValue = value[i - 1][c];

    if (iThItemAddedValue > iThItemInheritedValue) {
        solution[i - 1] = true;
        c = c - weights[i - 1];
    } else {
        solution[i - 1] = false;
    }
}
    
检查附加图像中的溶胶     
public class Knapsackproblem {
    private static int[][] cache;
    public static void main(String[] args) {
        int val[] = new int[]{60, 100, 120};
        int wt[] = new int[]{10, 20, 30};
        int  W = 50;
        int n = val.length;
        System.out.println(knapSack(W, wt, val, n));
        printValues(wt,val);
    }

    /**
     * This method will find the result with
     * more value with weight less than or equal
     * to given weight
     * @param w given weight
     * @param wt arrays of weights
     * @param val array of values
     * @param n length of the array
     * @return max value we can obtain
     */
    private static int knapSack(int w, int[] wt, int[] val, int n) {
    cache = new int[n+1][w+1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= w; j++) {
                if(j < wt[i-1]){
                    cache[i][j] = cache[i-1][j];
                }else {
                    cache[i][j] = Math.max(cache[i-1][j],(cache[i-1][j-wt[i-1]])+val[i-1]);
                }
            }
        }
        for (int[] aCache : cache) {
            System.out.println(Arrays.toString(aCache));
        }
        return cache[n][w];
    }

    private static void printValues(int[] wt, int[] val) {
        int m = cache.length-1;
        int n = cache[0].length-1;
        util(wt,val,m,n);
    }

    private static void util(int[] wt, int[] val, int m, int n) {
        if(m <=0 || n<=0) return;
        if((cache[m][n] != cache[m-1][n]) && (cache[m][n] != cache[m][n-1])){
            System.out.println(val[m-1]+"-->"+wt[m-1]);
            util(wt, val, m-1, (n - wt[m - 1] + 1));
        }else
        if(cache[m][n] == cache[m-1][n]){
            util(wt,val,m-1,n);
        }
        else if(cache[m][n] == cache[m][n-1])
            util(wt,val,m,n-1);
        else
            util(wt,val,m,(n-val[m-1]+1));
    }
}
    
https://www.dropbox.com/s/ish7t5vgy91fovt/Screenshot%202017-01-01%2015.16.31.png?dl=0 在调用者中打印tmpList,您将得到答案     

要回复问题请先登录注册