0-1 knapsack Problem | Dp

Source: Internet
Author: User
Tags array length printf

Knapsack Problem Description:

Excerpted from Baidu Encyclopedia http://baike.baidu.com/view/841810.htm

There are n items and a backpack with a capacity of V. The weight of article I is c[i], the value is w[i]. The solution of which items are loaded into the backpack makes these items less than the total weight of the backpack and the maximum value.


Workaround:

Backpack Nine: http://love-oriented.com/pack/#sec3

Dynamic planning, Reference: http://www.cnblogs.com/justinzhang/archive/2012/04/10/2441199.html

Introduction to Algorithms: Http://courses.csail.mit.edu/6.006/fall10/lectures/lecture20.pdf (below)


Given a backpack, the capacity is C, there are n items, the weight is n koriyuki vector W, the value is n Koriyuki vector v. |v|=|w|=n, n-dimensional vector Y=[0,1,0....yn] represents the selection of items, in which there is no element Yi, either 0, or 1, 0 for the selection of the item I, 0 means that I do not select the article I. The objective function is: Max (sum (vi*yi)), the constraint is sum (wi*yi) <=c, where 1=< i <=n.

Knapsack problem has the optimal substructure, so that f (n,c) represents, there are n items to be selected, the knapsack capacity is the optimal solution of C, when the item selection vector is Y=[y1,y2,... yn], then when Yn=1, y ' =[y1, y2, ... yn-1], must be f (n-1, C-wn) The object selection vector, when yn=0, is bound to be f (n-1,c) of the optimal item selection vector. So the knapsack problem can be solved by dynamic programming.

Based on the above analysis, we can get the following recursive formula:

When Wn>c, F (n,c) =f (N-1,C);

When Wn<=c, f (n,c) = Max (f (n-1,c), Vn+f (n-1, c-wn));

The initial conditions are: f (i, 0) = 0; F (0,i) = 0; F (0,0) = 0;

PHP Implementation:

<?php/* Find the optimum solution */$weight = array (77,22,29,50,99); Weight Ascend $value = Array (92,22,87,46,90); Value random $solution = Array ( -1,-1,-1,-1,-1); Solution Vector/* * N:number of items * content:the maximum content of the knapsack */function Knapsack ($n, $con
	Tent) {global $weight;
	Global $value;
	
	Global $solution; 
	if ($n = = 0 | | $content = = 0) {return 0;
				} else {for ($i = $n-1; $i >= 0; $i-) {if ($weight [$i] > $content) {$solution [$i] = 0;
			Return Knapsack ($n-1, $content);  } else {if ($value [$i] + knapsack ($n-1, $content-$weight [$i])) > Knapsack ($n-1, $content)) {$solution [$i] =
					1;
				return $value [$i] + knapsack ($n-1, $content-$weight [$i]);
					} else {$solution [$i] = 0;
				Return Knapsack ($n-1, $content);
}}}} $maxvalue = Knapsack (5,100);
		for ($i = 0; $i < 4; $i + +) {if (1 = = $solution [$i]) {printf ("Item%d is selected.", $i + 1); printf ("{value:%d,weight:%d} ", $value [$i], $weight [$i]);
	echo "<br/>";
 }} printf ("Maximum value is:%d", $maxvalue);


Results:



--------------------------------------------------------------------------------------------------------------- --------------------------------

Update

http://bbs.csdn.net/topics/390680023 here someone asked the same question, I wrote it in C again:

#include <stdio.h>
#define LIMIT

int result (int i, int *a, int lim);
int max (int a, int b);

int main () {
	int a[] = {1, 4, one, 7, 8};

	int n = result (0, A, LIMIT);
	printf ("result=%d\n", n);
	return 0;
}

int result (int i, int a[], int lim) {
	if (i = = 4) {/*array length-1*/
		return a[i] <= Lim? A[i]: 0;
	}
	Else {
		if (A[i] > Lim) {
			return result (I+1, A, Lim);
		}
		else {
			return max (Result (I+1, A, Lim), A[i]+result (I+1, A, lim-a[i]));

}}} int max (int a, int b) {
	return a >= b? a:b;
}


Summary: Through this simple question, I find that the problem of dynamic programming has two key points (difficulties):

1. Transforming problems into sub-problems and recursion with the idea of dynamic programming

2. Recursive boundary conditions, i.e. when to return


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.