Home > Backend Development > PHP Tutorial > PHP backtracking method to solve the 0-1 knapsack problem example analysis_PHP tutorial

PHP backtracking method to solve the 0-1 knapsack problem example analysis_PHP tutorial

WBOY
Release: 2016-07-13 10:00:27
Original
886 people have browsed it

An example analysis of PHP backtracking method to solve the 0-1 knapsack problem

This article mainly introduces the PHP backtracking method to solve the 0-1 knapsack problem and an example analysis of the PHP backtracking method to solve the knapsack problem. The skills of asking questions have certain reference value. Friends who need them can refer to them

The example in this article describes the PHP backtracking method to solve the 0-1 knapsack problem. Share it with everyone for your reference. The specific analysis is as follows:

This code is written based on the pseudocode of the "Software Designer" tutorial;
The most troublesome thing is not that the pseudocode is changed to PHP, but that the array subscript starts from 0 and the corresponding subscript judgment problem;
Along with the debugging output, write

?

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(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((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 be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/973133.htmlTechArticlePHP backtracking method to solve 0-1 knapsack problem example analysis This article mainly introduces the PHP backtracking method to solve 0-1 Knapsack problem, an example analysis of the skills of PHP backtracking method to solve the knapsack problem, with certain reference...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template