Dynamic Programming Solution 0-1 knapsack problem (C language Edition)

Source: Internet
Author: User

This semester open algorithm class, feel very difficult, light this problem to get a long time, I here code is not my original code, is to learn from the online code according to their own understanding to improve, the original web address

For http://www.cnblogs.com/qinyg/archive/2012/04/26/2471829.html

Problem Description:

Given the items in N and a backpack. The weight of the item I is WI, its value is VI, the capacity of the backpack is C. Ask how to choose the items to be loaded into the backpack, so that the total value of the items into the backpack is the largest.

When selecting items, there are only two options for each item I, namely, a backpack or no backpack. The goods I can not be loaded into several times, also cannot be loaded only part of the goods. Therefore, the problem is called 0-1 knapsack problem.

Problem Analysis: Order V (I,J) represents the maximum value of items in the first I (1<=i<=n) items that can fit into a backpack with a capacity of J (1<=j<=c), and the following dynamic programming function can be obtained:

(1) V (i,0) =v (0,j) =0

(2) V (i,j) =v (i-1,j) J<wi

V (i,j) =max{v (i-1,j), V (I-1,J-WI) +vi)} J>wi

(1) The formula indicates: if the weight of the first item is greater than the capacity of the backpack, then the maximum value of the first I goods to be loaded and the maximum price that the former i-1 goods get are the same, that is, the goods I can not be loaded into the backpack; the first (2) formula shows that if the weight of item I is less than the capacity of the backpack, there will be two situations: (a) If the item I is loaded into the backpack, the value of the knapsack item equals the value of the I-1 item loaded into the capacity-bit j-wi, plus the value of the first item VI; (b) If the first item is not packed in a backpack, the value of the item in the backpack equals the value of the first i-1 item in the backpack with a capacity of J. It is obvious that the most valuable of the two is the optimal solution in a backpack with a capacity of J in the first item.

01 The state transition equation of the backpack f[i,j] = max{F[i-1,j-wi]+pi (j >= Wi), F[i-1,j]} The following is a screenshot of the test data
The middle part is printed form information, the first row and the first column are not printed (all 0); The following is directly on the Code section.

#include <stdio.h> int v[200][200];//before I load the maximum value obtained in a backpack with a capacity of J int max (int a,int b) {if (a>=b) return A;
else return B;
	int knapsack (int n,int w[],int v[],int x[],int C) {int i,j;
    Fill in the form, where the first row and the first column are all 0 for (i=0;i<=n;i++) v[i][0]=0;
    for (j=0;j<=c;j++) v[0][j]=0;
        for (i=1;i<=n;i++) {printf ("%d%d%d", i,w[i-1],v[i-1]);
				for (j=1;j<=c;j++) {if (j<w[i-1]) {v[i][j]=v[i-1][j];
			printf ("[%d][%d]=%2d", I,j,v[i][j]);
				else {V[i][j]=max (v[i-1][j],v[i-1][j-w[i-1]]+v[i-1]);
			printf ("[%d][%d]=%2d", I,j,v[i][j]);
	printf ("\ n");
            //Determine which items are selected j=c;
					for (i=n;i>=1;i--) {if (V[i][j]>v[i-1][j]) {x[i]=1;
                J=J-W[I-1];
            else x[i]=0;
            printf ("Selected items are: \ n"); for (i=1;i<=n;i++) prinTF ("%d", x[i]);
        printf ("\ n");
        
return V[N][C];
    } void Main () {int s;//obtains the maximum value of int w[15];//item's weight int v[15];//item's value int x[15];//item's selected state int n,i;
    int c;//backpack maximum capacity n=5;
    printf ("Please enter the maximum size of the backpack: \ n");
    
    scanf ("%d", &c);
    printf ("Number of items entered: \ n");
    scanf ("%d", &n);
    printf ("Please enter the weight of the item separately: \ n");

    for (i=0;i<n;i++) scanf ("%d", &w[i]);
    printf ("Please enter the value of the item separately: \ n");

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

    S=knapsack (N,W,V,X,C);
    printf ("Maximum item value: \ n");
   
    
printf ("%d\n", s); }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.