背包0-1路径重建(拿哪些物品)

发布于 2024-10-16 14:16:56 字数 102 浏览 2 评论 0原文

我知道如何用动态规划方法解决背包 0-1 问题,但我很难弄清楚要拿哪些物品而不影响 O(N * C)(N 个物品,C 容量)的复杂性。

有什么想法(我更喜欢自下而上的方法)?

I know how to solve knapsack 0-1 problem with dynamic programming approach, but I am having troubles figuring out which items to take without compromising the complexity of O(N * C) (N items, C capacity).

Any ideas (I would prefer a bottom-up approach)?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无尽的现实 2024-10-23 14:16:56

假设,现在您将结果存储在数组 bool[] a 中,其中当 i 可以达到总和时,a[i] 为 true .
您将需要另一个数组 int[] b,其中 b[i] 是您放入背包中以求和 i 的最后一个元素>。

因此,在您拥有的地方,

a[i] = true;

您将需要

a[i] = true;
b[i] = current_item;

然后,找到可以采取哪些项目来实现 sum i 是一个简单的循环。

PS 为了简单起见,我使用两个数组,但显然数组 a 可以被删除。

Suppose, right now you're storing results in array bool[] a, where a[i] is true when sum i can be achieved.
You'll need another array int[] b, where b[i] is a last element you've placed into knapsack to achieve sum i.

So, where you had

a[i] = true;

you'll need

a[i] = true;
b[i] = current_item;

Then, finding which items can be taken to achieve sum i is a simple loop.

PS I use two arrays for simplicity, but obviously array a can be removed.

扛刀软妹 2024-10-23 14:16:56

这是在 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];
}

Here is a modification to reconstruct path in O(n) times

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];
}
凉城已无爱 2024-10-23 14:16:56
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;
    }
}
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;
    }
}
纵情客 2024-10-23 14:16:56

检查附图中的 sol下面的图片有片段

Check the sol in the attached imageThe below image has the snippet

一抹苦笑 2024-10-23 14:16:56
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));
    }
}
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));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文