C ++ (Data Structures and Algorithms) 77: --- The greedy algorithm (cargo loading, knapsack problem, topological sorting, half covered, single-source shortest path, minimum cost spanning tree)

A, container loading

  • Problem Description: There is a cargo ship to the state, the size of all the cargo box are the same, but the weight of the cargo box vary
  • Now we ask is: without overloading, the largest number of states on the cargo ship cargo

Greedy algorithm to solve

  • : Thought up when selecting cartons, cargo box from birth, the selected minimum weight of the cargo container, then the container can ensure that the number of loaded
  • For example: Suppose other boxes n = 8, x is the number of container, respectively, the weight w [100,200,50,91,150,50,20,80], the maximum overload ship c = 400. The greedy algorithm then be loaded on board numbered 7,3,6,8,4,1,5,2 container, a total weight of 390, is the optimal solution [obtained at this time X1, ... x8 ] = [1,0,1,1,0,1,1,1]
  • Theorem: produce the optimal loading using the above greedy algorithm. Proof of the theorem follows

C ++ code to achieve

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//用于表示货箱
struct container {
    container(int _id, int _weight, int _flag = 0) 
        :id(_id), weight(_weight), flag(_flag) {}
    int id;    //货箱编号
    int weight;//货箱重量
    int flag;  //用于表示是否装载进货船。0表示没有,1表示有
};

/*
参数:
    c:货箱集合
    capacity:船的最大超载容量
*/
void containterLoading(const std::vector<container*>& c, int capacity)
{
    //如果船未超载,那么进行装箱操作
    for (auto iter = c.begin(); ((iter != c.end()) && ((*iter)->weight <= capacity)); ++iter) {
        (*iter)->flag = 1;
        capacity -= (*iter)->weight;
    }
}

bool isMax(const container* p1, const container* p2)
{
    return p1->weight < p2->weight;
}

int main()
{
    //初始哈8个货箱
    std::vector<container*> vec;
    vec.push_back(new container(1, 100));
    vec.push_back(new container(2, 200));
    vec.push_back(new container(3, 50));
    vec.push_back(new container(4, 90));
    vec.push_back(new container(5, 150));
    vec.push_back(new container(6, 50));
    vec.push_back(new container(7, 20));
    vec.push_back(new container(8, 80));

    //根据container的重量对所有的货箱进行排序
    std::sort(vec.begin(), vec.end(), isMax);

    //装箱操作
    containterLoading(vec, 400);

    //打印信息
    std::cout << "The number of the case being loaded is :";
    for (auto iter = vec.cbegin(); iter != vec.cend(); ++iter) {
        if ((*iter)->flag)
            std::cout << (*iter)->id << " ";
    }
    std::cout << std::endl;
    return 0;
}
  • Results are as follows:

Second, the knapsack problem

Problem Description

  • There are n items and a capacity c backpack. Weight of the items i is Wi, value for p. Now the n items from a number of selected items into a bag in
  • Best now claim is: the total weight of the article dispenser package does not exceed the capacity of the pack, so that the total value of the items into the highest
  • Description of the problem is:

  • Constraints are:

  • Xi = 1 indicates the article into the backpack, Xi = 0 indicates no article into the backpack
  • 0/1 knapsack problem is actually a generalized problem loading cartons, but different from each container obtained value

Greedy strategy ①

  • Rules: select the maximum value of the items can be loaded backpack from the rest of the article
  • This strategy does not guarantee the optimal solution
  •  

② greedy strategy

  • Rule: selecting the minimum weight of the backpack can be loaded from the remaining articles in the article
  • This strategy can not guarantee the optimal solution

③ greedy strategy

  • Rule: selecting the largest Pi / Wi values can be loaded backpack article from the remaining articles
  • This strategy can not guarantee the optimal solution

Greedy heuristic method

  • Three algorithms discussed above can not guarantee the optimal solution, but we need not be discouraged. 0/1 knapsack problem is a complex issue NP-
  • Wherein the policy ③ not guarantee the optimal solution, but we think it is a good heuristic algorithm, and many times, it's very close to the solution of the optimal solution. In one experiment, of 600 randomly generated knapsack problem, greedy heuristic algorithm using this solution as the optimal solution has 239, 583 have a solution with 10% difference in the optimal solution, therefore, all 600 solution with the optimal solution're just full within 25%. And the algorithm can be completed in O (nlogn) time, performance is very good

K order optimization

  • K order optimization principle is:
    • First, most k items into the backpack, no matter how much they value
    • If k items exceeds the maximum capacity of the knapsack c, this operation is abandoned
    • If k items does not exceed the maximum capacity of the backpack c, continued from the remaining items in decreasing order Pi / Wi value items one by one into the backpack
  • Assume now that n = 4, w = [6,10,12,13], p = [6,10,12,13], c = 11, Pi / Wi = [3,2.5,2,1.8]
  • 0 when k =: non-increasing order of items according to their density value into the backpack. First, the backpack into the article 1, article 2 is then. This is the remaining capacity of 5 backpack, can not put any article, so solution is x = [1,1,0,0], this solution has the value 16
  • When K = 1, K = 2, the results were as follows:

  • Greedy heuristic modified resulting solution is optimized order K. That is, if k items taken out from the solution and placed in another k items, then the results obtained are not the original good. Also within the value obtained in this manner at the optimum value of (100 / (k + 1))%. Therefore, we call this heuristic method is called bounded heuristic performance
    • When K = 1: the end result is less than 50% of the optimum value
    • When K = 2: 33.33% within the
  • Performance heuristics bounded execution time increases with increasing k, the number of attempts required to themselves (O n^{k}), time required for each subset O (n). Also, items sorted by value ratio required time is O (nlogn). Therefore, when k> 0, the total time of O ( n^{k+1})
  • The actual inspection of the performance is much better, the following figure shows the 600 kinds of random tests of statistical results:

Third, topological sorting

to be continued

Fourth, the binary cover

to be continued

Fifth, the single-source shortest path

to be continued

Sixth, the minimum cost spanning tree

to be continued

Released 1525 original articles · won praise 1085 · Views 450,000 +

Guess you like

Origin blog.csdn.net/qq_41453285/article/details/104447962