PHP Backtracking method solves 0-1 knapsack problem case Analysis _php Tutorial

Source: Internet
Author: User

A case study of 0-1 knapsack problem solved by PHP backtracking


This article mainly introduced the PHP backtracking method solves 0-1 knapsack problem, the example analyzes the PHP backtracking method solves the knapsack question the skill, has the certain reference value, needs the friend to refer to under

This paper describes the method of PHP backtracking to solve the 0-1 knapsack problem. Share to everyone for your reference. The specific analysis is as follows:

This code is written according to the pseudo code of the software designer tutorial;
The most troublesome is not to change the pseudo-code to PHP, but the array subscript starting from 0, and the corresponding subscript judgment problem;
With a debug output piece written on

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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 (11,21,31,33,43,53,55,65);

$w _arr = Array (1,11,21,23,33,43,45,55);

$n = count ($w _arr);

Test output

Var_dump (BKNAP1 (110));

Var_dump (Bound (139,89,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 (($c-$W _total)/$w _arr[$i]) * $v _arr[$i]);

$b = $b + (n ($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);d ie;

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);d ie;

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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/973133.html www.bkjia.com true http://www.bkjia.com/PHPjc/973133.html techarticle A case analysis of 0-1 knapsack problem solved by PHP backtracking This article mainly introduces the PHP backtracking method to solve the problem of 0-1 knapsack, the case analysis of the PHP backtracking method to solve knapsack problem, with a certain reference to borrow ...

  • 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.