Dynamic programming and backtracking method for solving 0-1 knapsack problem __ Dynamic programming

Source: Internet
Author: User
Problem Description:

0-1 Backpack: There are n items and a backpack with a weight of M. (Each item is only one) the weight of item I is w[i], value is p[i]. Solving which items are loaded into the backpack can make the sum of the greatest value.

Dynamic planning:

Dynamic programming algorithms are often used to solve problems with some of the best properties. There may be many possible solutions to such problems. Each solution corresponds to a value, and we want to find the solution with the optimal value. The dynamic programming algorithm is similar to the partition method, whose basic idea is to decompose the problem into several sub problems, solve the problem first, and then get the solution of the original problem from the solution of these sub problems. In contrast to the divide-and-conquer method, it is not always independent to solve the problem of dynamic programming. If we use the partition method to solve this kind of problem, the number of sub problems that can be decomposed is too many, and some sub problems are repeatedly computed many times. If we can save the answer to the solved child problem and find the answer when it is needed, we can avoid a lot of repetitive computations and save time. We can use a table to record the answers to all the solved child problems. Whether or not the child problem is used later, as long as it has been computed, the result is filled in the table. This is the basic idea of the dynamic programming method. The specific dynamic programming algorithms are various, but they have the same form.

The dynamic programming algorithm can be decomposed into 4 steps after from:

1. Describe the structure of an optimal solution, look for the sub problem, and divide the problem.

0-1 knapsack problem has the property of optimal substructure. Setting (Y1,y2,y3,......,yn) is an optimal solution to the given 0-1 knapsack problem, then an optimal solution of the corresponding sub problem.

Formula 1-1

Formula 1-2

Formula 1-1 and the N of formula 1-2 refer to the capacity of the backpack that n,m is the maximum size of the backpack, which can be seen, the optimal solution of the original problem is now in the optimal solution of the sub problem, so we can record the optimal solution records of the problem of the form of the butt now, This is the most commonly seen in the dynamic planning of the idea of the table, by adding or subtracting more than one matrix of space complexity to improve the time complexity of the program.

2. Define the state. A set of values for each variable associated with a child problem is often defined as a state. The value of a state is the solution of this child problem.

Suppose we enter the following values, the capacity of the backpack is n = 3, the density of the backpack M = 10. We have three sets of goods to choose from, namely p[1] = 4,p[2] = 5,p[3] = 6. W[1] = 3, w[2] = 4, w[3] = 5.

3. Find the equation of state transition. A variable value is generally changed from one state to another.

Formula 1-3

According to Formula 1-3, we can easily get the following table:

Figure 1-1

4. Calculates the value of the optimal solution in a "bottom-up" manner.

The same analysis I played the best substructure of the table now we can call this table through the bottom-up sequence to the optimal substructure sequence output. Starting with the bottom zyj[3][10], the size of the judgment and the zyj[2][10] is obviously more than the zyj[2][10] description is obtained by calling the substructure plus p[i], and now we subtract the w[i from it. Get the next position zyj[2][5] and the same as above to launch the result sequence.

5. The path of the optimal solution is constructed from the calculated information.

The source code is as follows: Bad writing.

/** 01 Backpack: Use dynamic programming to solve 01 knapsack, first draw table, output by Table/#include <stdio.h> #include "timex.h" #define MAX 32////////////function declaration//////////// /void Printzyj (int zyj[max][max],int row,int col)//Output optimal solution table void printjg (int zyj[max][max],int p[max],int n,int m);//output result table void Creatematrix (int zyj[max][max],int w[max],int p[max],int jg[max],int row,int col);//table recording optimal solution int main () {//PRINTM
	Ytime ();/The function of the name and time int m,n;
	int i; int A; Indicates that there is a group of goods to choose int P[max] = {0};//price array int W[max] = {0};//weight array int Zyj[max][max] = {0};//ZYJ[I][J] indicates that when the loaded goods are 1<i<n, the quality
	The amount of 1<j<m is, the largest price zyj[i][j]//to be optimized int Jg[max] = {0};//output sequence printf ("Please enter the backpack's bulk weight and the capacity of the backpack \ T");
	scanf ("%d%d", &m,&n);
	printf ("Backpack up to the quality is:%d\n Backpack the largest number of seats are:%d\n", m,n);
	printf ("Enter the number of items you want to select \ T");	
	scanf ("%d", &a);
	printf ("Enter the quality price sequence that needs to be selected for the goods \ n");
		for (i = 1; i <= A; i++) {printf ("goods%d is: \ t", I);
	scanf ("%d%d", &w[i],&p[i]);
	} creatematrix (Zyj,w,p,jg,a,m);
	Printzyj (ZYJ,A,M);
	printf ("Correct output sequence: \ n");

	
	PRINTJG (ZYJ,JG,N,M);
return 0; } void CREAtematrix (int zyj[max][max],int w[max],int p[max],int jg[max],int row,int col) {int i, J; for (i = 1; I <= row; i++) {for (j = 1; J <= Col; j) {if (w[i]>j) {Zyj[i][j] = zyj[i-1][j];//quality disallowed or
				The above optimal solution} else{if (Zyj[i-1][j] > Zyj[i-1][j-w[i]]+p[i]) {zyj[i][j] = zyj[i-1][j];
				} else{Zyj[i][j] = zyj[i-1][j-w[i]]+p[i];
		}} j = col;
				for (i = row;i >= 1; i--) {if (Zyj[i][j] > Zyj[i-1][j]) {jg[i] = 1;
			j = J-w[i];
			} else{Jg[i] = 0;
	}} void Printzyj (int zyj[max][max],int row,int col) {int i, J;
		for (i = 1; I <= row; i++) {for (j = 1; J <= Col; j +) {printf ("%3d", Zyj[i][j]);
	printf ("\ n");
	} void Printjg (int zyj[max][max],int p[max],int n,int m) {int i;
	for (i = 1;i <= n; i++) {printf ("%3d", P[i]);
printf ("\ n final maximum quality is:%d\n", zyj[n][m]);
 }

Backtracking method:

In backtracking, you first need to be clear about the following three concepts:

1. Constraint function: The constraint function is based on the required order. By describing the general characteristics of the legal solution to remove the illegal solution, so as to avoid continuing to search out the remainder of this illegal solution. Therefore, the constraint function is valid and equivalent to any node on the state space tree.

2. State space tree: The Status space tree is a graphical description of all solutions. There is only one part of the solution for each child node in the tree that is different from the parent node.

3. Exhibition node, slip knot point, knot point: The so-called extension node, is currently being derived from its child node node, in DFS, only one extension node allowed. The knot point is that the node itself and its parent node meet the node of the constraint function by the control of the constraint function; The deadlock point is conversely. It is easy to know that the knot point is not necessary to find its child nodes (meaningless).

The concrete steps of using backtracking method to solve problems

First, complete the following three steps by reading a question:

(1) Describe the form of the solution and define a solution space, which contains all the solutions of the problem.

Suppose our input is the same size as the one above and the capacity of the backpack is n = 3, the density of the backpack M = 10. We have three sets of goods to choose from, namely p[1] = 4,p[2] = 5,p[3] = 6. W[1] = 3,w[2] = 4, w[3] = 5. Assuming that the final JG sequence is {x1,x2,...... xn} then each x[i] has two possible {0,1}, which indicates whether X enters the backpack, then this 0-1 knapsack problem can be formalized as follows:

Formula 1-4

Formula 1-5

That is to say, 0-1 knapsack problem is essentially to ask for such an n vader vector {x1,x2,...... xn},x belong to {0,1},1<=i<=n, make the value biggest, simultaneously satisfies.

(2) Constructing the state space tree.

Is the solution space of vector {X1,X2,X3} The left arrow indicates that the 0 right arrow indicates 1, and naturally {x1,x2,...... xn} contains 2n neutron trees, so as long as the search is possible, the optimal solution can be found. In order to reduce the space complexity, we only record an optimal subsequence, which requires us to search the solution space tree two times. Find the maximum value for the first time, and finally find the result sequence based on the maximum value.

(3) Construct the constraint function (used to kill the node).

Recursive implementation kills the node, each judgment is not beyond the weight of the backpack if it is directly intercepted.

The source code is as follows: Bad writing.

/** Program Description: Using backtracking method to solve 01 Knapsack 1 to establish solution space tree to find the optimal condition max ((i = 1-n) p[i]jg[i]), end condition (i = 1-n) w[i]jg[i]<=m jg[i]={0,1}; * * #include <stdio.h> #include "timex.h" #define MAX 32////////////function declaration/////////////void PRINTJG (int p[max],int N, int m);//output result table void bljkj (int count,int m,int n,int *price);/traverse solution space tree void bljkj_2 (int count,int m,int n,int price);//Second traversal Find the best solution int isover (int count,int m);//judge is not exceeding boundary int getjg (int count);//Getting a set of possible solutions price int P[max] = {0};//price array int W[max] = {0
	};//weight array int Jg[max] = {0};//output result sequence int main () {//printmytime ();//Call own function, output time, and name int m,n;
	int i; int A; Indicates that there is a group of goods for option int price = 0;
	Represents the optimal price printf ("Please enter the weight of the backpack and the capacity of the backpack \ T");
	scanf ("%d%d", &m,&n);
	printf ("Backpack up to the quality is:%d\n Backpack the largest number of seats are:%d\n", m,n);
	printf ("Enter the number of items you want to select \ T");	
	scanf ("%d", &a);
	printf ("Enter the quality price sequence that needs to be selected for the goods \ n");
		for (i = 1; i <= A; i++) {printf ("goods%d is: \ t", I);
	scanf ("%d%d", &w[i],&p[i]);
	} bljkj (0,m,n,&price);
	printf ("Best price is: \ t");
	printf ("%d\n", price); printf ("Correct output sequence: \ n");
	Bljkj_2 (0,m,n,price);
return 0;
	int isover (int count,int m) {int i,j = 0;
	for (i = 1;i <= count; i++) {j = j + Jg[i]*w[i];
	if (J > m) return 1;
else return 0;
	int GETJG (int count) {int I, j = 0;
	for (i = 1; I <= count; i++) {j = j +jg[i]*p[i];
} return J;
	} void bljkj (int count,int m,int n,int *price) {int j = 0,k;
	if (Isover (count,m)) return;
		if (count = = N) {j = GETJG (count);
		if (J > *price) *price = j;
	return;
		for (k = 1; k >= 0; k--) {jg[count+1] = k;
	BLJKJ (count + 1,m,n,price);
	} void bljkj_2 (int count,int m,int n,int price) {int i,j = 0,k;
	if (Isover (count,m)) return;
		if (count = = N) {j = GETJG (count);
			if (Price = = j) {for (i = 1; I <= n; i++) {printf ("%3d", Jg[i));
			printf ("\ n");
		return;
	} return;
		for (k = 1; k >= 0; k--) {jg[count+1] = k;
	Bljkj_2 (count + 1,m,n,price); }
}







The source code is as follows: Bad writing.

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.