Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this DP solution to the 0/1 Knapsack Problem not give the correct output with GCC?

#include<stdio.h>

int max(int a,int b)
{
    if(a>b)
        return a;
    else
        return b;
}

void knapsack(int m,int n,int w[],int p[])
{
    int v[10][10],x[10],i,j;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=m;j++)
        {
            if(j==0||i==0)
                v[i][j]=0;
            if(j-w[i]<0)
                v[i][j]=v[i-1][j];
            else
                v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]);
        }
    }
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=m;j++)
            printf("%d\t",v[i][j]);
        printf("\n");
    }
    printf("THE OPTIMAL SOLUTION IS:%d",v[n][m]);
    for(i=1;i<=n;i++)
        x[i]=0;
    i=n;
    j=m;
    while(i>0 && j>0)
    {
        if(v[i][j]!=v[i-1][j])
        {
            x[i]=1;
            j=j-w[i];
        }
        i--;
    }
    printf("THE OPTIMAL SET OF WEIGHTS IS:");
    for(i=1;i<=n;i++)
        if(x[i]==1)
            printf("%d\t",i);
    printf("\n");
}

int main()
{
    int w[10],p[10],i,m,n;
    printf("ENTER THE NUMBER OF ITEMS:");
    scanf("%d",&n);
    printf("ENTER THE WEIGHTS OF THE ITEMS:");
    for(i=1;i<=n;i++)
        scanf("%d",&w[i]);
    printf("ENTER THE PROFITS OF THE ITEMS:");
    for(i=1;i<=n;i++)
        scanf("%d",&p[i]);
    printf("ENTER THE CAPACITY OF KNAPSACK:");
    scanf("%d",&m);
    knapsack(m,n,w,p);
    return 0;
}

SAMPLE OUTPUT:

chaitanya@chaitanya-laptop:~/Desktop/My prog$ ./a.out

ENTER THE NUMBER OF ITEMS:5

ENTER THE WEIGHTS OF THE ITEMS:3
2
1
2
3

ENTER THE PROFITS OF THE ITEMS:2
3
2
3
2

ENTER THE CAPACITY OF KNAPSACK: 8

0   -72 -1080992920 -72 0   1   -1080993280 0   13403040    
0   -72 -1080992920 2   0   1   -70 2   13403040    
0   -72 3   2   0   5   3   4   13403040    
0   2   3   5   4   5   7   5   13403040    
0   2   3   5   6   8   7   8   13403040    
0   2   3   5   6   8   7   8   13403040    

THE OPTIMAL SOLUTION IS:13403040

THE OPTIMAL SET OF WEIGHTS IS:

Note: The same program produces a legitimate output for the same input when compiled in the "Turbo C" compiler.

So that leads me to believe that i am not adhering to C standards. Is that so?

like image 745
Chaitanya Nettem Avatar asked Nov 20 '11 08:11

Chaitanya Nettem


People also ask

Can we solve knapsack problem using dynamic programming?

Knapsack algorithm can be further divided into two types: In this Knapsack algorithm type, each package can be taken or not taken. Besides, the thief cannot take a fractional amount of a taken package or take a package more than once. This type can be solved by Dynamic Programming Approach.

What is knapsack problem in DP?

The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

What is the best solution to the knapsack problem?

A greedy algorithm is the most straightforward approach to solving the knapsack problem, in that it is a one-pass algorithm that constructs a single final solution. At each stage of the problem, the greedy algorithm picks the option that is locally optimal, meaning it looks like the most suitable option right now.

What is the time complexity of 0 1 knapsack problem in dynamic programming?

Time complexity for 0/1 Knapsack problem solved using DP is O(N*W) where N denotes number of items available and W denotes the capacity of the knapsack.


1 Answers

When you initialize w you are using 1-based indexing:

for(i=1;i<=n;i++)
        scanf("%d",&w[i]);

But when you access it, you are using 0-based indexing.

for(i=0;i<=n;i++)
{
    for(j=0;j<=m;j++)
    {
        if(j==0||i==0)
            v[i][j]=0;
        if(j-w[i]<0)   // This line accesses w[0] when i is 0. Missing an else?
            v[i][j]=v[i-1][j];
        else
            v[i][j]=max(v[i-1][j],v[i-1][j-w[i]]+p[i]);
    }
}

In C arrays use 0-based indexing. Change your code to use 0-based indexing consistently.

Also, you should check the return value of scanf otherwise invalid input will give strange results instead of an error.

for (i=0; i < n; i++) {
    if (scanf("%d", &w[i]) != 1) {
        return EXIT_FAILURE; // Handle the error appropriately.
    }
}
like image 122
Mark Byers Avatar answered Nov 15 '22 09:11

Mark Byers