Backtracking method to solve 01 knapsack problem (C language Edition)

Source: Internet
Author: User

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 article I is larger than the capacity of the backpack, then the maximum value obtained by the person in front of I goods and the maximum price obtained from the former i-1 goods are the same, that is, the goods I can not be loaded into the backpack; the first (2) formula indicates that if the weight of the first item 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, backpack capacity:

/*test.h*/
#include <stdio.h>
//-------------macro definition------------
#define OK 0
//----------- Variable declaration--------------
int x[100],bestx[100];
int CV = 0,CW = 0,MW = 0,MV = 0;
int c,n;
int weight[100];
int value[100];
-------------function declaration------------
int Output ();
int Input ();
void Init ();
BOOL Place (int t);
void Track (int t);


/*test.cpp*/#include "test.h" void Init () {int i;
		for (i = 0;i<100;i++) {X[i] = 0;
		Bestx[i] = 0;
		Weight[i] = 0;
	Value[i] = 0;
	int Input () {int i;
	printf ("Please enter the capacity of the backpack:");
	scanf ("%d", &c);
	printf ("Please enter the weight of the item in turn:");
	for (i=0;i<n;i++) scanf ("%d", (weight+i));
	printf ("Please enter the value of the item in turn:");
	for (i=0;i<n;i++) scanf ("%d", (value+i));
return OK;
	int Output () {printf ("Selected items are:");
	for (int m = 0;m<n;m++) printf ("%d", bestx[m]);
	printf ("\nmax value is:%d\n", MV);
return OK;
	BOOL Place (int t) {if (cw+weight[t] > C) return false;
return true;
	}//---------backtracking method to solve 01 knapsack problem-----------------void Track (int t) {int m;
			if (t>=n) {//output if (CV&GT;MV) {MV=CV;
		for (M = 0;m<n;m++) bestx[m] = x[m];
			} else {for (M = 0;m<=1;m++) {x[t] = m;
				if (x[t] = = 0) {Track (t+1);
			X[t] = 0;
				else if (place (t) && x[t]==1) {CV = CV + value[t];
				CW = CW + weight[t];
				Track (t+1);
				X[t] = 0; Cv = cv-value[t];
			CW = Cw-weight[t];
	}} void Main () {Init ();
	printf ("Please enter the number of items:");
	scanf ("%d", &n);
	Input ();
	Track (0);
Output (); }


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.