PHP Cashing Boxing algorithm

Source: Internet
Author: User
Tags php example
PHP Implementation Boxing algorithm

Greedy method is a kind of method that does not pursue the optimal solution, but only wants to get a satisfactory solution. The greedy method can get a satisfactory solution quickly, because it eliminates the need to spend a lot of time to find the best solution to exhaust all possible. Greed is often based on the current situation as the best choice, regardless of the possible overall situation, so greedy law do not backtrack.
? ? For example, when shopping for money, in order to make back the smallest amount of change coins, do not consider all the various publishing solutions to find change, but from the largest denomination of the currency, in descending order to consider each currency, first try to use a large denomination of the currency, when not large denominations of currency to consider a smaller denomination of the currency. This is the use of greedy methods. This approach is always optimal here because of the clever arrangement of the banks for the types of coins they issue and the value of their coins. In the case of coins with denominations of 1, 5 and 11 units respectively, the coins with a total value of 15 units will be recovered. According to greedy algorithm, should find 1 11 denominations of coins and 4 1 denominations of coins, a total of 5 coins recovered. But the best solution should be 3 5-denomination coins.
"Problem" Boxing issue
? ? Problem Description: Packing problem can be summarized as follows: There are 0, 1 、...、 n-1 n kinds of items, the volume is V0, V1 、...、 vn-1. Load the n items into several boxes with a capacity of V. Agreed that the volume of the N-type items does not exceed V, that is, for 0≤i<n, there is 0<vi≤v. The number of boxes required for different packing schemes may vary. The packing problem requires that the number of boxes for the N-type items be reduced.
? ? The optimal solution can be found if the collection of n items is divided into a subset of n or less than n items. But the total number of possible divisions is too large. The time it takes to find out all the possible divisions is unbearable for the appropriate large n. For this reason, a very simple approximate algorithm, namely greedy method, is adopted for the packing problem. The algorithm, in turn, puts the item into the first box it can put in, although the algorithm cannot guarantee the optimal solution, but it can still find a very good solution. Without losing its generality, the volume of n items is ordered from large to small, that is, there are v0≥v1≥ ... ≥vn-1. If the above requirements are not met, simply sort the n items by their size from large to small, and then renumber the items by the sorted result. The boxing algorithm is simply described as follows:
{Enter the volume of the box;
Enter the number of items n;
Enter the volume of each item by volume from large to small order;
Preset used box chain is empty;
Preset used box counter box_count is 0;
for (i=0;i
{Look for the box J that can put the item I in the order starting from the first used box;
If (no items can be placed in the box)
{Another box is used and the item I is placed in the box;
box_count++;
}
Else
Put the item I into the box J;
}
}
The above algorithm can find out the number of cases needed box_count, and can find out the contents of each box. The following example shows that the algorithm is not necessarily able to find the optimal solution, there are 6 kinds of items, their volume is: 60, 45, 35, 20, 20 and 20 unit volume, box volume of 100 units volume. According to the above algorithm, three boxes are required, each box is loaded with items 1, 3, the second box is loaded with items 2, 4, 5, and the third box is loaded with items 6. and the optimal solution is two boxes, respectively loaded items 1, 4, 5 and 2, 3, 6.
If the items in each box are represented by a linked list, the first node of the list is stored in a structure, and the structure records the amount of space remaining and the first pointer to the list of items loaded in the box. The information of all the boxes also forms the linked list. The following is a program written according to the above algorithm.
}

Attached PHP Example:

 
  

?

?

From: http://home.51.com/chenjiuchuan/diary/item/10049598.html

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