c++ - 使用递归算法求解背包

标签 c++ algorithm recursion knapsack-problem

所以,我正在尝试根据我们的教科书实现这个算法。

enter image description here

我是这样写的:

// Knapsack_memoryfunc.cpp : Defines the entry point for the console application.
//Solving Knapsack problem using dynamic programmig and Memory function

#include "stdafx.h"
#include "iostream"
#include "iomanip"
using namespace std;

int table[20][20] = { 0 };
int value, n, wt[20], val[20], max_wt;

// ---CONCERNED FUNCTION-----

int MNSack(int i, int j)
{
    value = 0;
    if (table[i][j] < 0)
        if (j < wt[i])
            value = MNSack(i - 1, j);
        else
            value = fmax(MNSack(i - 1, j), val[i] + MNSack(i - 1, j - wt[i]));

    table[i][j] = value;
    return table[i][j];
}

// --------------------------

void items_picked(int n, int max_wt)
{
    cout << "\n Items picked : " << endl;
    while (n > 0)
    {
        if (table[n][max_wt] == table[n - 1][max_wt])   // if value doesnot change in table column-wise, item isn't selected
            n--;                                        // n-- goes to next item
        else                                            // if it changes, it is selected
        {
            cout << " Item " << n << endl;
            max_wt -= wt[n];                            // removing weight from total available (max_wt)
            n--;                                        // next item
        }
    }
}

int main()
{

    cout << " Enter the number of items : ";
    cin >> n;
    cout << " Enter the Maximum weight : ";
    cin >> max_wt;
    cout << endl;
    for (int i = 1; i <= n; i++)
    {
        cout << " Enter weight and value of item " << i << " : ";
        cin >> wt[i] >> val[i];
    }

    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= max_wt; j++)
            table[i][j] = 0;

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= max_wt; j++)
            table[i][j] = -1;

    cout << " Optimum value : " << MNSack(n, max_wt);

    cout << " \n Table : \n";
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= max_wt; j++)
            if (table[i][j] == -1)
                cout << setw(5) << "-";
            else
                cout << setw(5) << table[i][j];
        cout << endl;
    }

    items_picked(n, max_wt);


    return 0;
}

这是问题和输出:
enter image description here

它在某些地方(例如最佳值)似乎是正确的,但并不完全可以接受。 我试过调试它,但是使用递归函数很难。有人可以帮忙吗?

最佳答案

int MNSack(int i, int j)
{
    value = 0;
    if (table[i][j] < 0)
    {
        if (j < wt[i])
            value = MNSack(i - 1, j);
        else
            value = max(MNSack(i - 1, j), val[i] + MNSack(i - 1, j - wt[i]));

        table[i][j] = value;
    }
    return table[i][j];
}

问题就在这里。当您的表项大于或等于 0 时,您将跳过递归,但仍将表项设置为 0,如果您的表项大于 0,则不正确。

你只需要在需要改变的时候更新表项,所以把它放在大括号里会纠正这个问题。

关于c++ - 使用递归算法求解背包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36287956/

相关文章:

java - 我的递归快速排序中的小错误

c++ - 二叉搜索树递归插入

c++ - 模板元编程 NamedPipe 客户端服务器

python - 如何计算递归调用次数?

java - 递归回文一遍又一遍地返回语句

c - 我的代码中执行二分查找的错误在哪里?

c++ - 将 vector 放置到 map 中

c++ - 执行系统命令很慢

c - 删除静态声明的数组的前两个元素不起作用

c++ - 是 VAR |= 1 << 2;可逆的?