C Language greedy algorithm algorithm-algorithm thought

Source: Internet
Author: User
Tags sort

In the greedy algorithm (greedy method), the methods of constructing the optimal solution gradually are adopted. At each stage, a seemingly optimal decision is made (under certain criteria). Once a decision is made, it cannot be changed. The basis for making greedy decisions is called the greedy rule (greedy criterion).

Example 1-4 [for change] a child buys less than 1 dollars of sugar and gives 1 dollars to a salesperson. The salesman hopes to find the child with the least number of coins. Suppose to offer an unlimited number of coins with a nominal value of 2 5 cents, 1 0 cents, 5 cents, and 1 cents. The salesman steps together to find the change number, each time add a coin. The greedy guidelines for choosing coins are as follows: Each time you choose, you should increase your money by as much as 0. In order to ensure the feasibility of the solution (i.e.: the given change is equal to the amount of change to be found), the selected coins should not make the total amount of change in excess of the final number required.

Suppose you need to find 6 7 cents for your child, and the first one is two 2 5-cent coins, the third one could not be 2 5 cents of the coin, otherwise the choice of coins will not be feasible (the total change of more than 6 7 cents), the third should choose 1 0 cents of the coin, then 5 cents, and finally add two 1 cents of the coin.

Greedy algorithms have an intuitive tendency to tell us that we should make the smallest number of coins (at least close to the minimum) when looking for change. This greedy algorithm can prove that the number of coins used in the change is indeed the fewest (see Exercise 1).

Example 1-5 [machine scheduling] existing N-piece tasks and an unlimited number of machines, tasks can be processed on the machine. The start time for each task is SI, finish time for fi, si < fi. [Si, Fi] for the time range of the processing task I. Two task i,j refer to the time range overlap of the two tasks, not the i,j starting point or end point coincidence. For example: the interval [1,4] overlaps with the interval [2,4], but does not overlap with the interval [4,7]. A viable task assignment is that there are no two overlapping tasks assigned to the same machine in the assignment. Therefore, in a workable allocation, each machine handles at most one task at any time. Optimal allocation refers to the least feasible allocation scheme for the machine used.

Suppose there is a n=7 piece of work, the label is a to G. Their start and finish times are shown in Figure 13-1a. If the task A to machine M1, Task B to machine M2, ..., task G to machine M7, this allocation is a feasible allocation, a total of seven machines used. However, it is not an optimal allocation because there are other allocations to make fewer machines available, for example, if tasks A, B, D can be assigned to the same machine, the number of machines is reduced to five.

A greedy way to get optimal distribution is to distribute tasks progressively. Each step is assigned a task and is assigned in a non descending order of the start time of the task. If at least one task has been assigned to a machine, the machine is said to be old, and if the machine is not old, it is new. When choosing a machine, use the following greedy guidelines: Depending on the start time of the task, if the old machine is available at this time, the task is assigned to the older machine. Otherwise, assign the task to a new machine. According to the data in the example, the greedy algorithm is divided into n = 7 steps, the Order of task assignment is a, F, B, C, G, E, D. The first step has no old machines, so assign a to a new machine (such as M1). The machine is busy at 0 to 2 hours. In the second step, consider the task F. Because the old machine is still busy when F starts, assign F to a new machine (set to M2). The third step is to consider task B, because the old machine M1 at SB =3 moment has been idle, so assigns B to M1 execution, M1 the next available time becomes FB =7,m2 the available time becomes FF = 5. Step fourth, consider task C. Since no old machines are available at SC =4, the C is assigned to a new machine (M3), and the next available time for this machine is FC = 7. The fifth step is to consider task G, assign it to machine M2, step sixth assigns task E to Machine M1, and finally in step seventh, Task 2 assigns to machine M3. (Note: Task D can also be assigned to machine M2).

The above greedy algorithm can cause the proof of optimal machine assignment to be left for practice (exercise 7). A greedy algorithm with an O (NL O-GN) complexity can be implemented as follows: First, a sort algorithm with an O (NL O-GN) complexity (such as heap sorting) arranges each task in ascending order of Si, and then uses a minimum heap of available time for the old machine.

Example 1-6 [Shortest path] gives a direction to the network, the length of the path is defined as the cost of each side of the path passed. Requires finding a shortest path from the initial vertex s to the destination vertex d.

The greedy algorithm constructs this path step-by-step, with each step adding a vertex to the path. Assuming the current path has reached vertex Q,

and vertex q is not the destination vertex d. The greedy rule for joining the next vertex is to select the vertices that are closest to the Q and are not currently in the path.

This greedy algorithm does not necessarily get the shortest path. For example, suppose that in Figure 1 3-2 you want to construct the shortest path from vertex 1 to vertex 5, using the greedy algorithm above, start at Vertex 1 and look for the closest vertex 1 vertices that are not currently in the path. The vertex 3 is reached, the length is only 2 units, the nearest vertex that can be reached from Vertex 3 is 4, the vertex 4 arrives from Vertex 2, and finally the destination vertex 5 is reached. The paths established are 1, 3, 4, 2, 5, with a length of 1 0. This path is not the shortest path from 1 to 5 in the graph. In fact, there are several shorter paths, such as the length of the path 1,4,5 6.

Based on the above three examples, recall some of the applications examined in the previous chapters, some of which are greedy algorithms. For example, Huffman tree algorithm, using the n-1 step to establish a minimum weighted external path of the two-fork tree, each step of the two binary tree merged into one tree, the algorithm used in the greedy rule is: from the available two fork tree to choose the least weight of the two trees. L P t Dispatch rule is also a greedy algorithm, which uses n steps to dispatch n jobs. First, sort the job by the length of time, and then assign a machine to a task in each step. The greedy rule for choosing a machine is: making the current scheduling time the shortest. Schedule the new job to the first completed machine (that is, the first idle machine).

It is noted that greedy algorithms do not guarantee optimal performance in machine scheduling problems, however, that is an intuitive tendency and generally results are always very close to the optimal value. The rule that it uses is the rule in the actual environment that wants the manual machine to be dispatched. The algorithm is not guaranteed to get the optimal result, but the result is almost the same as the optimal solution, which is also called the Heuristic method (H e u r i s t i c s). So the L P T method is a heuristic machine scheduling method. Theorem 9-2 states the relationship between the completion time of the L p T schedule and the completion time of the optimal schedule, so the L-P-T heuristic method has a limited performance (bounded performance). A heuristic method with limited performance is called an approximate algorithm (a p p r o x i m a t i o na l g o r i t).

The remainder of this chapter will introduce the application of several greedy algorithms. In some applications, the result of greedy algorithms is always the optimal solution. But for other applications, the generated algorithm is only a heuristic, and may or may not be an approximate algorithm.

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.