0-1 knapsack problem using backtracking method to solve __input

Source: Internet
Author: User

Problem Description:

A traveler prepares to travel, so he decides to pick up some items and put them in his backpack. Each item has a volume and value, and the overall product of the backpack is also fixed, asking the traveler how to select the item, so that the total value is the maximum value. Note that items can not be divided, that is, can only be selected, or not selected.


Solution:

1, dynamic planning (now no matter what day is free to say ~ ~);

2, Backtracking method:

The most important thing to use backtracking is to determine the constraint function and the bounds function, only in order to determine which side needs to be subtracted, otherwise the solution space is too large to solve.

In this question, we can first see that the constraint function for this problem is:

If the total capacity of the items in the current backpack is CW, the front k-1 items have been decided whether to put in the package, then the K item whether put into the package depends on the inequality

CW + wk <= m (wherein, WK is the volume of the K-piece item, M for the capacity of the backpack) (this is the constraint)

Then we look for the clearance function, the problem is more troublesome, we can recall the knapsack problem greedy algorithm, that is, items according to the value of goods/items volume from large to small, and then the best solution to
(1,1,1.......,1,t,0,0, ...) ), where 0<=t<=1;

Therefore, we can consider the greatest value we can achieve in determining whether or not the K item is to be placed (in the case where the previous K-1 item has been determined), that is, we can calculate the maximum value by calculating the K item that is only put in a part. We want to make sure that the maximum value of the path currently selected is greater than the value of the path we have chosen. That is the gauge of the problem. By this condition, we can subtract a lot of branches and save the running time greatly.


Code:

#include <stdio.h> #include <stdlib.h> #include <memory.h> #include <string.h> int * pvalue = NU LL;		 Value of the goods int * Pweight = weight of null;//goods int counts = 0; Number of items int *realsolution = NULL; Storing the Final Solution int *testsolution = NULL;
The solution/* for storing each path obtained is calculated on the premise that the previous K-1 item has been made. Consider the maximum benefit value that can be achieved, return an upper bound, where the CP represents the current value of the backpack, the CW represents the current weight, and K represents the number of items to consider, and m represents the maximum capacity of the backpack * *
	float Boundfound (int cp, int cw, int k, int m) {int i = k;
	int c = CW;
	Float B = (float) CP;
		while (i<=counts) {c + = Pweight[i];
		if (c<m) {b + = Pvalue[i];
		else {return (b + (C-M)/pweight[i) * (Pvalue[i]));
	} i++;
return b;
	}//m for the capacity of the backpack void Backknap (int m) {int currentweight = 0;
	int currentvalue = 0;
	int k = 1;
	int finalvalue =-1;
			while (true) {while (k<=counts && (currentweight + pweight[k] <= m)) {testsolution[k] = 1;
			Currentweight + = Pweight[k];
			CurrentValue + = Pvalue[k];
		k++;
			} if (k>counts) {finalvalue = CurrentValue; K= counts;
		Memmove (void *) realsolution, (void *) Testsolution, (counts+1) * sizeof (int));
		else {testsolution[k] = 0;
			//If you find such a path to go the best results without my present results well, then decisively requires backtracking while (Boundfound (CurrentValue, Currentweight, K+1, M) <= Finalvalue) {
			while ((Testsolution[k]!= 1) && (k!= 0)) k--;
			if (k==0) {return;
			} Testsolution[k] = 0;
			Currentweight-= Pweight[k];
		CurrentValue-= Pvalue[k];
	} k++;
	} void Main () {printf ("Please input the Counts of packages:");
	scanf ("%d", &counts);
	printf ("Please input the volumn of the Bag:");
	int m = 0;
	scanf ("%d", &m);
	Pweight = (int *) malloc ((counts+1) * sizeof (int));
	memset ((void*) pweight, 0, sizeof (int) * (counts+1));
		for (int i = 1; I <= counts i++) {printf ("Please input the weight of the%dth package:", I);
	scanf ("%d", pweight + i);
	} pvalue = (int *) malloc ((counts+1) * sizeof (int));
	memset ((void*) pvalue, 0, sizeof (int) * (counts+1)); for (i = 1; I <= counts; i++) {
		printf ("Please input" the value of the%dth package: ", I);
	scanf ("%d", pvalue + i);
	} realsolution = (int *) malloc ((counts+1) * sizeof (int));
	memset ((void*) realsolution, 0, sizeof (int) * (counts+1));
	testsolution = (int *) malloc ((counts+1) * sizeof (int));
	memset ((void*) testsolution, 0, sizeof (int) * (counts+1));
	Backknap (m);
	printf ("The best reslut is choosing");
	int maximunvalue = 0;
			for (i = 1; i<=counts; i++) {if (realsolution[i) = = 1) {Maximunvalue + = Pvalue[i];
		printf ("The%dth package", I);
} printf ("\ n and the Maximun value is%d\n", maximunvalue); }
Note that when the above code enters the volume and value of the item, the order of input is arranged according to the value of the item or the volume of the item from large to small.

Compilation Environment vc++6.0

The main problems that arise:

1. The while loop in line 64th is written as if, so that the limit function is not allowed to function.

2, can be a step-by-step tracking debugging to familiarize the whole knapsack problem of the solution space of the tree.

3, still have to be familiar with the basic idea of retrospective method Ah.

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.