C ++ (Data Structures and Algorithms) 76: --- greedy algorithm (greedy algorithm)

  • Also known as the greedy algorithm greedy algorithm

First, the optimization problem

  • This article describes the algorithm and examples are behind the "optimization problem" . Each contains a set of optimization problems "restricted conditions" and a "optimization function" . Meet the constraints of problem-solving program called "feasible solution" . The optimization function best value possible to achieve a feasible solution is called "optimal solution"

Second, the idea of ​​greedy algorithm

  • In the greedy algorithm, we need to gradually construct an optimal solution . Each step, we are certain criteria to make an optimal decision. Every step in the decision-making, and not be changed in a later step. Standard decisions are based is called greed guidelines

Third, the practice of looking for change

Problem Description

  • Customers who use dollars to buy candy salesman hoping to find a child to change with the least number of coins. Suppose there is a nominal value of 25 cents, 10 cents, 5 cents and 1 cent coins, but the number is not limited
  • Each time you select a coin shop assistant, make up looking for change. The choice is based on greed criteria: at no more than the total number of coins are looking for, every time the largest possible selection interview coin , until the total number of coins to make up the total number of equal're looking for change
  • Problem Description: assuming that customers are looking for 67 cents

Greedy algorithm to solve the step

  • Suppose to customers looking for 67 cents:
    • The first two steps chose two $ 25 coin (third step can not be selected to $ 25, otherwise the total to more than 67 cents)
    • Step 10 cent coin selection
    • The fourth step of selecting 5 cent coin
    • The final step is two 1-cent coin
  • Greedy algorithm gives us a feeling: this scrape out loose change, or at least close to the minimum number of coins

Fourth, the practical application of machine scheduling

Application Analysis

  • There are n number of tasks, the machine is not limited to, task processing on the machine. Start time of each task is Si, task completion time for Fi, Si <Fi. [Si, Fi] is the task i processing period
  • Duplication of tasks: two overlapping tasks i and j, if and only if the processing period overlap two tasks. E.g. period [1, 4] with a period [2,4] overlap, and [1,4] and time [4,7] not overlap
  • One task allocation scheme is feasible: refers to two periods without overlapping tasks assigned to the same machine, i.e. each machine at most only one task at any time processing
  • Optimal allocation: it refers to the use of the machine with minimal viable distribution plan

The actual description of the problem

  • Suppose n = 7 tasks, numbered from a to g, the processing time thereof as shown below:

  • Description of the problem: the machine is not limited, it should be assigned to this task 7 on the machine running, and the total number of machines required is minimal used (not required time)

Greedy algorithm to solve the step

  • Greed guidelines steps: according to the start time of the task, if the "old" machines are available, the task assigned to it. Otherwise, the task is assigned to a "new" machine
    • The old machine means: that has been used
    • New machines: again the machine has not been used
  • The start and end time of the task, the final execution result shown below, all of the tasks in the overall implementation of the process, three machines were used

Fifth, the practical application of the shortest path

Problem Description

  • To a network as shown below, the distance between the two figure numbers represent nodes
  • Now there is such a demand: from beginning to reach a certain point to another point, every step on the path to join an order. It assumed that the current path has peaked q, but has not yet reached the end. When the request of a path selection: Select a q recently associated, and the current vertex is not in the path

Greedy algorithm to solve the step

  • Assuming that peaked from the vertex 1 5, greedy algorithm solution is:
    • Step 1. Select 1-> 3 path length of 2
    • Step Select 3-> 2 path length of 2
    • The third step is select 4-> 2 path length of 1
    • Step fourth selection 2-> 5 of the path, a length of 5
    • Thus a total length of 10
  • Note (Key): According to the problem description and greedy algorithm, the final choice out of the path is 10, but not the shortest path (not optimal). May be performed, for example, 1-> 4-> 5 this path, a total length of 6. So greedy algorithm is not necessarily the optimal solution

Six other cases

  • For example, the Huffman tree algorithm ( https://blog.csdn.net/qq_41453285/article/details/103649092 ), using n-1 weighting step to establish the minimum external path length of a binary tree, each step will be two binary trees combined into one. Greedy algorithm The algorithm used is: from the smallest available weight binary tree the two trees is a tree composition
  • For example, the LPT scheduling rules ( https://blog.csdn.net/qq_41453285/article/details/103649089 also a greedy algorithm). It is used to schedule n steps n operations. First, the length of the job sorted by time. The next task is assigned as a machine then each step is. Greedy criterion is based on: the current schedule the shortest time. The new job completion scheduled first machine (i.e., the first idle machine). Note that this problem, the greedy algorithm does not guarantee the optimal solution

Seven others belong to the concept of

  • Can know through some of the problems above, greedy rule not guarantee optimal solutions (such as the shortest path problem above), but it's always close to the optimal solution under normal circumstances. This is a rule of thumb. The results obtained are usually close to the optimal solution, this algorithm is called "heuristics"
  • If between heuristics and the best offense there is a limited relationship, then we we call this heuristic method has "limited capability"
  • Having a heuristics defined defined properties referred to "Approximation Algorithm"

Eight, the practical application of the greedy algorithm

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

Guess you like

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