C Language algorithm

Source: Internet
Author: User
Tags range

This problem comes from example 1-2. The ship can be loaded in a step-by-step, with one container per step, and which container to consider. According to this idea, the following greedy guidelines can be used: from the remaining crates, select the smallest weight of the box. This order of choice ensures that the total weight of the selected containers is minimal, allowing for more containers to be loaded. Based on this greedy strategy, the lightest container is selected first, then the lighter one, so that all the crates are loaded on board or no other container can be accommodated on board.

Example 1-7 assumes n=8, [W1, ... w8]=[100,200,50,90,150,50,20,80], c=4 0 0. In the case of greedy algorithms, the order of the examined crates is 7, 3, 6, 8, 4, 1, 5, 2. The total weight of boxes 7, 3, 6, 8, 4, 1 is 3 9 and 0 units and has been loaded, with the remaining loading capacity of 1 0 units, smaller than any of the remaining crates. In this greedy solution we get [x1, ..., x8]=[1, 0, 1, 1, 0, 1, 1, 1] and xi=6.

Theorem 1-1 uses greedy algorithms to produce optimal loads.

This method can be used to prove the optimality of greedy algorithm: Make x=[x1, ..., xn] for the solution obtained by greedy algorithm, make y=[y1, ..., yn] as any feasible solution, just prove the n i=1xi≥n? I=1yi. Without losing generality, it can be assumed that the crates are lined up: Wi≤wi + 1 (1≤i≤n). Then convert y to X in a few steps, and each step in the conversion process produces a viable new Y, and N i=1yi is greater than or equal to the value before the conversion, and finally proves N i=1xi≥n j=1yi.

According to the working process of greedy algorithm, we can know that there is a k in the range of [0, N], which makes Xi=1, I≤k and Xi=0, i>k. Look for the smallest integer j in the range [1, n], making xj≠yj. If no such j exists, then n i=1xi=n i=1yi. If there is such a J existence, then j≤k, otherwise y is not a feasible solution, because Xj≠yj, Xj=1 and yj=0. Yj=1, if the result of Y is not a feasible solution, then in [j+ 1, N] range must have an L to make yl=1. Make yl=0, because of the WJ≤WL, then the obtained Y is feasible. Moreover, the resulting new Y at least has the same number of 1 as the original Y.

After several times this transformation, Y can be converted to X. Because each transformation produces a new Y with at least the same number of 1 as the previous Y, X is at least 1 the same number as the original Y. The container loading algorithm C + + code implementation See Program 1 3-1. Since greedy algorithms are loaded in ascending order by weight of the container, program 1 3-1 first uses the indirect addressing sort function i n d i r e C t S o r t to sort the weight of the container (see 3. 5 the definition of indirect addressing), the containers can then be loaded in ascending order of weight. Because the time required for indirection sorting is O (nl o gn) (9 is also available). 5. The heap ordering of section 1 and the Merge Order of chapter 2nd, the remainder of the algorithm takes O (n), so the total complexity of program 1 3-1 is O (nl o gn).

Procedure 13-1 Container Shipment

template
void ContainerLoading(int x[], T w[], T c, int n)
{// 货箱装船问题的贪婪算法
// x[i]=1 当且仅当货箱i被装载,1<=i<=n
// c是船的容量, w是货箱的重量
// 对重量按间接寻址方式排序
// t是间接寻址表
int *t=new int [n+1];
I n d i r e c t S o r t ( w, t, n);
// 此时, w[t[i]]<=w[t[i+1]], 1<=i
// 初始化x
for (int i=1; i<=n; i++)
x[i]=0;
// 按重量次序选择物品
for (i=1; i<=n && w[t[i]]<=c; i++) {
x[t[i]]=1;
c -=w[t[i]];} // 剩余容量
delete [] t;
}

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.