PHP backtracking to solve 0-1 knapsack problem example analysis _ PHP Tutorial

Source: Internet
Author: User
PHP backtracking solves the problem of 0-1 backpacks. PHP backtracking to solve the 0-1 backpack problem example analysis this article mainly introduces the PHP backtracking method to solve the 0-1 backpack problem, the example analyzes the php backtracking method to solve the knapsack problem skills, some examples of solving the 0-1 backpack problem by using PHP backtracking

This article mainly introduces the PHP backtracking method to solve the problem of 0-1 backpacking. The example analyzes the php backtracking method to solve the problem of backpacking, which has some reference value. For more information, see

This article describes how PHP backtracking solves the problem of 0-1 backpacks. Share it with you for your reference. The specific analysis is as follows:

This code is written according to the pseudocode in the software designer tutorial;
The most troublesome thing is not to change the pseudo code to php, but to judge the array subscript starting from 0 and the corresponding subscript;
Write it with debugging output

?

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

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

$ V_arr = array (, 33 );

$ W_arr = array );

$ N = count ($ w_arr );

// Test output

Var_dump (bknap1 (110 ));

// Var_dump (bound (7,110 ));

Function bound ($ v, $ w, $ k, $ W_total ){

Global $ v_arr, $ w_arr, $ n;

$ B = $ v;

$ C = $ w;

// Var_dump ($ W_total); var_dump ($ n); var_dump ($ k); var_dump ($ v); var_dump ($ w );

// Die;

For ($ I = $ k + 1; $ I <$ n; $ I ++ ){

$ C = $ c + $ w_arr [$ I];

// Var_dump ($ W_total); var_dump ($ c );

If ($ c <$ W_total)

$ B + = $ v_arr [$ I];

Else {

// Var_dump (1-($ c-$ W_total)/$ w_arr [$ I]) * $ v_arr [$ I]);

$ B = $ B + (1-($ c-$ W_total)/$ w_arr [$ I]) * $ v_arr [$ I];

Return $ B;

}

}

/* Var_dump ('------ bound head ');

Var_dump ($ k );

Var_dump ($ B );

Var_dump ('------ bound end ');*/

Return $ B;

}

Function bknap1 ($ W_total ){

Global $ v_arr, $ w_arr, $ n;

$ Cw = $ cp = 0;

$ K = 0;

$ Fp =-1;

While (true ){

While ($ k <$ n & $ cw + $ w_arr [$ k] <= $ W_total ){

$ Cw + = $ w_arr [$ k];

$ Cp + = $ v_arr [$ k];

$ Y_arr [$ k] = 1;

$ K + = 1;

}

// Var_dump ($ cw); var_dump ($ cp); var_dump ($ Y_arr); var_dump ($ k); var_dump ($ n );

If ($ k = $ n ){

$ Fp = $ cp;

$ Fw = $ cw;

$ K = $ n-1;

$ X_arr = $ Y_arr;

// Bound ($ cp, $ cw, $ k, $ W_total );

// Var_dump (bound ($ cp, $ cw, $ k, $ W_total), $ fp, $ k); die;

// Var_dump ($ fp); var_dump ($ fw); var_dump ($ Y_arr); var_dump ($ k); var_dump ($ n );

} Else {

$ Y_arr [$ k] = 0;

}

// Var_dump ($ Y_arr); var_dump ($ k); var_dump ($ n); // die;

// Var_dump (bound ($ cp, $ cw, $ k, $ W_total), $ fp); die;

While (bound ($ cp, $ cw, $ k, $ W_total) <= $ fp)

{

While ($ k> = 0 & $ Y_arr [$ k]! = 1 ){

$ K-= 1;

}

If ($ k <0)

{

Return $ X_arr;

}

Var_dump ($ k );

$ Y_arr [$ k] = 0;

$ Cw-= $ w_arr [$ k];

$ Cp-= $ v_arr [$ k];

}

$ K + = 1;

}

}

?>

I hope this article will help you with php programming.

Ghost This article mainly introduces PHP backtracking to solve the problem of 0-1 backpacks. Examples of php backtracking to solve the problem of backpacks are analyzed...

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.