Greedy method uses the method of gradually constructing the optimal solution. Make a seemingly optimal decision at each stage (under certain standards ). Once a decision is made, it cannot be changed. The basis for making greedy decisions is greedy criterion ). Example 1-4 [looking for change] A Child buys sugar worth less than $1 and gives it to the salesclerk. The salesperson wants to find the child with the smallest number of coins. Assume that you provide an unlimited number of coins with a nominal value of 2 5 cents, 1 0 cents, 5 cents, and 1 cent. The salesperson makes up the change number in several steps and adds a coin each time. The greedy rules adopted when selecting coins are as follows: each choice should increase the number of changes as much as possible. To ensure the feasibility of the solution (that is, the given change is equal to the number of changes to be obtained), the selected coin should not make the total number of changes more than the final number.
Suppose you want to get 6 7 cents for a child. First, you should select two 2 5 cents coins. The third one cannot be 2 5 cents, otherwise, the coin selection will be unavailable (the total change is more than 6 7 cents), the third should select a 1 0 cent coin, then 5 cents, and finally add two 1 cent coins. Greedy algorithms have an intuitive tendency. When looking for change, intuition tells us that we should minimize the number of coins we find (at least close to the minimum number ). It can be confirmed that the number of coins used when the above greedy algorithm is used to find the change is indeed the least (see Exercise 1 ). Example 1-5 [Machine Scheduling] There are n tasks and an unlimited number of machines. The tasks can be processed on the machines. The Start Time and completion time of each task are Si and Fi.
, Si <fi. [Si, FI] is the time range for processing task I. For two task I, j Refers to the overlap of the time ranges of the two tasks, not the overlap of I, j's start point or end point. For example, the interval [] overlaps with the interval [], but does not overlap with the interval. One feasible task allocation means that no two overlapping tasks are allocated to the same machine. Therefore, in a feasible allocation, each machine can process at most one task at any time. Optimal Allocation refers to the least feasible allocation scheme of machines used. Assume there are n = 7 tasks, labeled as A to G. Their start time and completion time are 13-1A. If task a is assigned to machine M1 and Task B
To machine m2,..., task G to machine M7, this allocation is feasible, a total of seven machines are used. However, it is not the optimal allocation because there are other allocation schemes that can reduce the number of machines used. For example, you can allocate tasks a, B, and D to the same machine, then the number of machines is reduced to five. A greedy method for obtaining optimal allocation is to gradually allocate tasks. Each step is assigned a task in a non-descending order of the task start time. If at least one task has been assigned to a machine, the machine is old. If the machine is not old, the machine is new. When selecting a machine, use the following greedy principle: Based on the start time of the task to be assigned, if the old machine is available at this time, the task is assigned to the old machine. Otherwise, assign the task to a new machine.
Based on the data in the example, the greedy algorithm is divided into n = 7 steps. The task allocation sequence is A, F, B, C, G, E, and D. In the first step, there is no old machine, so a is allocated to a new machine (such as M1 ). This machine is busy at 0 to 2. In step 2, consider task f. When F is started, the old machine is still busy, so f is assigned to a new machine (set to m2 ). Step 3 consider Task B. Because the old machine M1 is idle at the time of Sb = 3, allocate B to M1 for execution, and the next available time of M1 is changed to Fb = 7, the availability time of M2 is FF = 5. Step 4: Consider task C. Since no old machine is available at SC = 4, C is allocated to a new machine (m3), and the next time this machine is available is FC.
= 7. Step 5 considers task G and assigns it to machine m2. Step 6 assigns task e to machine M1, and step 7 assigns Task 2 to machine m3. (Note: Task D can also be assigned to machine m2 ). The above greedy algorithms can cause the validation of optimal machine allocation to be reserved for exercises (Exercise 7 ). A greedy algorithm with the complexity of O (NL o GN) can be implemented as follows: first, a sorting algorithm with the complexity of O (NL o GN) (such as heap sorting) can be used) tasks are arranged in ascending order of Si, and a minimum heap about the available time of old machines is used. In example 1-6 [shortest path], a directed network is provided. The path length is defined as the sum of the consumption of each edge passing through the path. It is required to find a string from the initial vertex S.
The shortest path to the destination vertex D. Greedy algorithms construct this path step by step, and each step adds a vertex to the path. Assume that the current path has reached vertex Q, and vertex Q is not the target vertex D. The greedy principle used to add the next vertex is: select the vertex closest to Q and not in the path currently. This greedy algorithm does not necessarily obtain the shortest path. For example, if you want to construct the shortest path from vertex 1 to vertex 5 in Figure 1 3-2, use the greedy algorithm described above, start from vertex 1 and find the vertex closest to vertex 1 that is not currently in the path. To reach vertex 3, the length is only 2 units, from vertex 3 to the nearest vertex 4, from vertex 4 to vertex 2, and finally to the destination vertex 5. The created path is 1,
3, 4, 2, 5, the length is 1 0. This path is not the shortest path from 1 to 5 in the directed graph. In fact, there are several shorter paths, such as paths 1, 4, and 5, with a length of 6. Based on the above three examples, let's look back at some of the applications discussed in the previous chapters. Several of them are greedy algorithms. For example, the Hoffmann algorithm uses n-1 steps to create a binary tree with the minimum weighted external path. Each step combines the two binary trees into one. The greedy criterion used in the algorithm is: select the two trees with the least weight from the available binary tree. The L p t scheduling rule is also a greedy algorithm that uses n steps to schedule n jobs. First, sort jobs by time, and then assign a machine to a task in each step. Select the greedy principle used by the machine to minimize the current scheduling time. Schedule a new job to the first completed machine (that is, the first idle machine ).
It is important that in machine scheduling, greedy algorithms cannot guarantee the Optimum. However, it is an intuitive tendency and generally the results are always very close to the optimal value. The rules it uses are the rules used in the actual environment for manual machine scheduling. The algorithm does not guarantee optimal results, but the results are usually the same as the optimal solution. This algorithm is also called a heuristic method (h e u r I s t I C S ). Therefore, the L p t method is a heuristic machine scheduling method. Theorem 9-2 describes the relationship between the completion time of l p t Scheduling and the completion time of the optimal scheduling. Therefore, the L p t heuristic method has limited performance ). A Heuristic Method with limited performance is called an approximate algorithm (
A p r o x I m a t I o na l g o r I t h m ). The rest of this chapter describes the application of several greedy algorithms. In some applications, greedy algorithms always produce the optimal solution. However, for other applications, the generated algorithm is only a heuristic method, either or not an approximate algorithm.