PHP dynamic planning solution for 0-1 knapsack problem instance analysis

Source: Internet
Author: User

PHP dynamic planning solution for 0-1 knapsack problem instance analysis

This article mainly introduces PHP dynamic planning to solve the 0-1 knapsack problem. The example analyzes the principle and implementation skills of the knapsack problem. For more information, see

 

 

This article analyzes the PHP dynamic planning to solve the 0-1 knapsack problem. Share it with you for your reference. The specific analysis is as follows:

Backpack Problem description: a backpack with a maximum weight of W now has n items, each with a weight of t and the value of each item is v.
To maximize the weight of this backpack (but not more than W), and at the same time, the greatest value of the backpack is required.

Idea: define a two-dimensional array, one-dimensional is the number of items (indicating each item), two-dimensional is the weight (no more than the maximum, here is 15), the following array,
Max (I-1, w), wi + opt (I-1, w-wi,
Opt (I-1, w-wi) refers to the previous Optimal Solution

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<? Php

// This is based on the dynamic planning principle.

// Max (opt (I-1, w), wi + opt (I-1, w-wi ))

// The maximum weight of a backpack

$ W = 15;

// There are four items, the weight of each item

$ Dx = array (3, 4, 5, 6 );

// Value of each item

$ Qz = array (8, 7, 4, 9 );

// Define an array

$ A = array ();

// Initialization

For ($ I = 0; $ I <= 15; $ I ++) {$ a [0] [$ I] = 0 ;}

For ($ j = 0; $ j <= 4; $ j ++) {$ a [$ j] [0] = 0 ;}

// Opt (I-1, w), wi + opt (I-1, w-wi)

For ($ j = 1; $ j <= 4; $ j ++ ){

For ($ I = 1; $ I <= 15; $ I ++ ){

$ A [$ j] [$ I] = $ a [$ J-1] [$ I];

// W = 15

If ($ dx [$ J-1] <= $ w ){

If (! Isset ($ a [$ J-1] [$ I-$ dx [$ J-1]) continue;

// Wi + opt (I-1, wi)

$ Tmp = $ a [$ J-1] [$ I-$ dx [$ J-1] + $ qz [$ J-1];

// Opt (I-1, w), wi + opt (I-1, w-wi) => comparison

If ($ tmp> $ a [$ j] [$ I]) {

$ A [$ j] [$ I] = $ tmp;

}

}

}

}

// Print this array and output the rightmost value.

For ($ j = 0; $ j <= 4; $ j ++ ){

For ($ I = 0; $ I <= 15; $ I ++ ){

Echo $ a [$ j] [$ I]. "/t ";

} Echo "/n ";

}

?>

I hope this article will help you with php programming.

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.