简体   繁体   English

使用动态编程从背包中检索物品

[英]Retrieval of items from a knapsack using dynamic programming

I'm new to dynamic programming and have tried my first DP problem. 我是动态编程的新手,并尝试了我的第一个DP问题。 The problem statement is 问题陈述是

Given a knapsack of size C, and n items of sizes s[] with values v[], maximize the capacity of the items which can be put in the knapsack. 给定一个大小为C的背包,以及n个大小为s []的物品,其值为v [],则最大化可放入背包的物品的容量。 An item may be repeated any number of times. 一个项目可以重复多次。 (Duplicate items are allowed). (允许重复的项目)。

Although I was able to formulate the recurrence relation and create the DP table, and eventually get the maximum value that can be put in the knapsack, I am not able to device a method to retrieve which values have to be selected to get the required sum. 尽管我能够制定递归关系并创建DP表,并最终获得可以放入背包的最大值,但是我无法提供一种方法来检索必须选择哪些值才能获得所需的总和。 。

Here is my solution: 这是我的解决方案:

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    int s[] = { 1, 3, 4, 5, 2, 7, 8 , 10};
    int v[] = { 34, 45, 23, 78, 33, 5, 7 , 1};
    int n = ( (sizeof(s)) / (sizeof(s[0])) );
    vector<int> backtrack;
    int C = 15;
    int pos;
    int m[20];
    m[0] = 0;
    int mx = 0;
    for ( int j = 1; j <= C; j++) {
        mx = 0;
        m[j] = m[j-1];
        pos = j-1;  
        for ( int i = 0; i < n; i++) {
            mx = m[i-s[i]] + v[i];
            if ( mx > m[i] ) {
                m[i] = mx;
                pos = i - s[j];
            }
        }   
        backtrack.push_back(pos);
    }
    cout << m[C] << endl<<endl;
    for ( int i = 0; i < backtrack.size(); i++) {
        cout << s[backtrack[i]]  <<endl;
    }   
    return 0;
}

In my solution, I've attempted to store the positions of the maximum value item selcted in a vector, and eventually print them. 在我的解决方案中,我尝试将选择的最大值项的位置存储在向量中,并最终打印出来。 However this does not seem to give me the correct solution. 但是,这似乎没有给我正确的解决方案。

Running the program produces: 运行该程序会产生:

79

2
3
0
5
2
7
8
10
34
45
23
78
33
5
7

It is obvious from the output that the numbers in the output cant be the sizes of the items selected as there there no item of size 0 as shown in the output. 从输出中可以明显看出,输出中的数字不能是所选项目的大小,因为输出中没有显示大小为0的项目。

I hope that you will help me find the error in my logic or implementation. 希望您能帮助我找到我的逻辑或实现中的错误。 Thanks. 谢谢。

You are following the greedy approach. 您正在遵循贪婪的方法。 It's pretty clever , but it is heuristic. 这很聪明,但是很启发。 I will not give you the correct code, as it might be a homework, but the recursive function knapsack would look like this: 我不会给您正确的代码,因为这可能是一项家庭作业,但是递归函数knapsack将看起来像这样:

knapsack(C): maximum profit achivable using a knapsack of Capacity C
knapsack(C) = max { knapsack(C-w[i]) + v[i] } for all w[i] <= C
knapsack(0) = 0

In code: 在代码中:

dp(0) = 0;
for i = 1 to C
  dp(i) = -INF;
  for k = i-1 downto 0
     if w[k] < i then
        dp(i) = max{dp(i-w[k]) + v[k], dp(i)};
print dp(Capacity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM