Case study of PHP greedy algorithm for solving the 0-1 knapsack problem

Source: Internet
Author: User

Case study of PHP greedy algorithm for solving the 0-1 knapsack problem

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

 

 

This article describes how to solve the 0-1 knapsack problem by using the PHP greedy algorithm. Share it with you for your reference. The specific analysis is as follows:

The greedy algorithm solves the problem of 0-1 backpacks. The global optimal solution is obtained through the local optimal solution! This solution is more flexible than dynamic planning!

?

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

// 0-1 knapsack greedy algorithm Problem

Class tanxin {

Public $ weight;

Public $ price;

Public function _ construct ($ weight = 0, $ price = 0)

{

$ This-> weight = $ weight;

$ This-> price = $ price;

}

}

// Generate data

$ N = 10;

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

$ Weight = rand (1, 20 );

$ Price = rand (1, 10 );

$ X [$ I] = new tanxin ($ weight, $ price );

}

// Output result

Function display ($ x)

{

$ Len = count ($ x );

Foreach ($ x as $ val ){

Echo $ val-> weight, '', $ val-> price;

Echo '<br> ';

}

}

// Sort by price and weight ratio

Function tsort (& $ x)

{

$ Len = count ($ x );

For ($ I = 1; $ I <= $ len; $ I ++)

{

For ($ j = 1; $ j <= $ len-$ I; $ j ++)

{

$ Temp = $ x [$ j];

$ Res = $ x [$ j + 1]-> price/$ x [$ j + 1]-> weight;

$ Temres = $ temp-> price/$ temp-> weight;

If ($ res> $ temres ){

$ X [$ j] = $ x [$ j + 1];

$ X [$ j + 1] = $ temp;

}

}

}

}

// Greedy Algorithm

Function tanxin ($ x, $ totalweight = 50)

{

$ Len = count ($ x );

$ Allprice = 0;

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

If ($ x [$ I]-> weight> $ totalweight) break;

Else {

$ Allprice + = $ x [$ I]-> price;

$ Totalweight = $ totalweight-$ x [$ I]-> weight;

}

}

If ($ I <$ len) $ allprice + = $ x [$ I]-> price * ($ totalweight/$ x [$ I]-> weight );

Return $ allprice;

}

Tsort ($ x); // sort by non-incrementing order

Display ($ x); // display

Echo '0-1 the optimal solution of the backpack is :';

Echo tanxin ($ x );

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.