Php's greedy method to implement the packing algorithm is a method that does not pursue the optimal solution, but only wants to obtain a satisfactory solution. The greedy method can quickly get a satisfactory solution, because it saves a lot of time to find the optimal solution to exhaust all possibilities. The greedy method is usually based on the current situation for optimal selection, regardless of the overall situation of various possibilities, so the greedy method should not backtrack .? Php boxing algorithm
The greedy method is a method that does not pursue the optimal solution and only wants to obtain a satisfactory solution. The greedy method can quickly get a satisfactory solution, because it saves a lot of time to find the optimal solution to exhaust all possibilities. The greedy method is usually based on the current situation for optimal selection, regardless of the overall situation of various possibilities, so the greedy method should not backtrack.
? ? For example, when purchasing and looking for money, in order to minimize the number of coins to be recovered, we do not consider all kinds of publishing schemes for finding the change, but start from the currency with the largest face value, consider each currency in a descending order. First, try to use a currency with a large nominal value. when the amount of a currency with a large nominal value is insufficient, consider the next currency with a smaller nominal value. This is the use of the greedy method. This method is always optimal here because banks cleverly arrange the types and denominations of coins they issue. For example, only coins with a nominal value of 1, 5, and 11 are expected to be recovered. According to the greedy algorithm, we should find one coin with 11 nominal values and four coins with one nominal value, and retrieve five coins in total. However, the optimal solution should be 3 coins with 5 nominal values.
[Problem] Packing problem
? ? Problem Description: the packing problem is described as follows: 0, 1 ,... N types of n-1 items, Volume: v0, v1 ,... Vn-1. Pack the n items into several boxes with a capacity of V. It is agreed that the volume of these n items shall not exceed V, that is, for 0 ≤ I <n, there is 0 <vi ≤ V. The number of boxes required for different packing schemes may be different. The packing problem requires that the number of boxes containing the n items be less.
? ? If we divide the set of n items into n or all subsets of n items, the optimal solution can be found. However, the total number of all possible partitions is too large. For an appropriately large n, it is unacceptable to find out all possible classifications. Therefore, we adopt a very simple approximation algorithm for the packing problem, that is, the greedy method. This algorithm puts items in the first box that can be placed in sequence. Although this algorithm cannot find the optimal solution, it can still find a very good solution. Without interruption, the volume of n items is sorted in ascending order, that is, v0 is ≥v1 ≥... ≥ Vn-1. If the preceding requirements are not met, you only need to first sort the n items by their volume from large to small, and then re-number the items according to the sorting result. The packing algorithm is described as follows:
{Input box volume;
Number of input items n;
Input the volume of each item in the order of size from large to small;
The pre-used box chain is empty;
The pre-configured box counter box_count is 0;
For (I = 0; I
{Search for the box j that can be placed in item I in sequence from the first box in use;
If (no more items can be placed in used boxes I)
{Use another box and put item I into it;
Box_count ++;
}
Else
Put item I into the box j;
}
}
The preceding algorithm can calculate the number of boxes box_count and find the items in each box. The following example shows that the algorithm may not be able to find the optimal solution. There are 6 items in size: 60, 45, 35, 20, 20, and 20, respectively, the box volume is 100 units. Based on the above algorithm, three boxes are required. the items in each box are: 1 and 3 in the first box; 2, 4, and 5 in the second box; the third box contains 6 items. The optimal solution is two boxes with items 1, 4, 5, 2, 3, and 6 respectively.
If the items in each box are represented by a linked list, the first node pointer of the linked list is stored in a structure, and the remaining space is recorded in the structure and the first pointer of the linked list of the items in the box. In addition, the information of all boxes is also a linked list. The following is a program written based on the above algorithms.
}
Php example:
?
?
From: http://home.51.com/chenjiuchuan/diary/item/10049598.html