Greedy Algorithm and activity Arrangement

Source: Internet
Author: User

1. Greedy Algorithm (1) Principle: When solving a problem, always make the best choice for the moment. That is to say, without considering the overall optimization, what he makes is only a local optimal solution in a certain sense. The greedy algorithm does not obtain the overall optimal solution for all problems, but it can produce the overall optimal solution or the approximate solution of the overall optimal solution for many problems with a wide range. (2) features: greedy algorithms use top-down algorithms and iterative methods to make successive greedy choices. Each greedy choice simplifies the problem to a smaller subproblem, through greedy selection at each step, you can obtain an optimal solution of the problem. Although each step must ensure that the local optimal solution can be obtained, the global solution is sometimes not necessarily the optimal, therefore, the greedy method should not be traced back. The problems that can be solved using greedy algorithms generally have two important features: greedy selection and optimal sub-structure. 1) greedy choice properties the so-called greedy choice nature means that the overall optimal solution of the problem can be achieved through a series of local optimal choices, that is, greedy choice. This is the first basic element for the feasibility of greedy algorithms. Greedy algorithms are usually carried out in the top-down mode, making successive greedy choices in iterative mode. Each greedy choice simplifies the problem as a subproblem of smaller size. For a specific problem, to determine whether it has the greedy choice nature, it must prove that the greedy choice made by each step ultimately leads to the overall optimal solution of the problem. The general process of proof is: first examine the overall optimal solution of the problem, and prove that the optimal solution can be modified, so that it can begin with greedy choice. After greedy selection, the original problem is simplified to a similar subproblem of smaller size. Then it is proved by mathematical induction that the overall optimal solution of the problem can be obtained through greedy Selection in each step. It is proved that the key to simplifying the problem after greedy selection into a similar subproblem of smaller scale is to use the optimal substructure of the problem. 2) optimal sub-structure properties when the optimal solution of a problem includes the optimal solution of its sub-problems, the problem is called an optimal sub-structure. (3) differences between greedy algorithms and dynamic planning algorithms: both dynamic planning and greedy algorithms are recursive algorithms with optimal substructures. global optimal solutions are derived through local optimal solutions. The difference between the two lies in that every greedy decision made in the greedy algorithm cannot be changed, because the greedy policy derives the next optimal solution from the optimal solution in the previous step, the optimal solution of the previous step is not reserved. The optimal solution of each step of the greedy algorithm must include the optimal solution of the previous step. The global optimal solution in the dynamic planning algorithm must include a local optimal solution, but not necessarily the previous local optimal solution. Therefore, all the previous optimal solutions must be recorded. (4) Basic Ideas: 1) Establish a mathematical model to describe the problem. 2) divide the problem to several subproblems. 3) solve each subproblem and obtain the local optimal solution of the subproblem. 4) Merge the local optimal solution of the subproblem into a solution of the original solution. 2. Activity arrangement problem the problem of activity arrangement is to select the largest set of compatible sub-sets in the given activity set, which is a good example that can be effectively solved using greedy algorithms. This issue requires efficient scheduling of a series of activities to compete for a certain public resource. Greedy algorithms provide a simple and beautiful way to allow as many activities as possible to use public resources. Problem description: a set of n activities, E = {1, 2 ,..., N}. Each activity requires the use of the same resource, such as the speech venue. Only one activity can use this resource within the same time period. Each activity I has a start time si that requires the use of the resource and an end time fi, and si <fi. If activity I is selected, it occupies resources within the half-open time range [si, fi. If the interval [si, fi) and interval [sj, fj) do not overlap, it is said that activity I is compatible with activity j. That is to say, when si ≥ fj or sj ≥ fi, activity I is compatible with activity j. The most compatible sub-set is selected in the given activity set. Solution: Sort activities in ascending order based on the end time. Then I represents the I activity, s [I] represents the I Activity start time, and f [I] represents the I activity end time. Sort the events from small to large, and select the activities whose end time is as early as possible. The start time of the last activity is later than the end time of the previous activity, all of these activities are the largest set of compatible activities. In fact, the system checks whether activity I is compatible with all selected activities. If compatible activity I is added to the selected activity set, otherwise, the compatibility between the next activity and the activity in Set A is not selected. If activity I is compatible with it, I becomes the activity recently added to set A and replaces the position of activity j. The following is a greedy algorithm for solving the problem of activity arrangement. the start time and end time of each activity are stored in arrays s and f, and arranged in non-descending order of the end time. If the given activities are not arranged in this order, the O (nlogn) time can be used for rescheduling. The Code is as follows: [cpp] // greedy algorithm for 4d1 activity arrangement # include "stdafx. h "# include <iostream> using namespace std; template <class Type> void GreedySelector (int n, Type s [], Type f [], bool A []); const int N = 11; int main () {// subscript starts from 1. Storage Activity start time int s [] =, }; // The subscript starts from 1. The storage activity end time is int f [] = }; bool A [N + 1]; cout <"start time of each activity. The end time is:" <endl; for (int I = 1; I <= N; I ++) {cout <"[" <I <<"]:" <"(" <S [I] <"," <f [I] <")" <endl ;} greedySelector (N, s, f, A); cout <"Maximum compatible activity subset:" <endl; for (int I = 1; I <= N; I ++) {if (A [I]) {cout <"[" <I <"]: "<" ("<s [I] <", "<f [I] <") "<endl ;}} return 0 ;} template <class Type> void GreedySelector (int n, Type s [], Type f [], bool A []) {A [1] = true; int j = 1; // record the activity for (int I = 2; I <= n; I ++) that was last added to) // check whether activity I is compatible with the selected activity in sequence {if (s [I]> = f [j]) {A [I] = true; j = I;} else {A [I] = false ;}}because the input activities are arranged in A non-descending order of their completion time, therefore, the greedySelector algorithm always selects compatible activities with the earliest completion time to join set. Intuitively, selecting a compatible activity in this way leaves as much time as possible for unscheduled activities. That is to say, the greedy choice of this algorithm is to maximize the remaining configurable time periods so as to arrange as many compatible activities as possible. The greedySelector algorithm is highly efficient. When the input activities are arranged in a non-descending order of the end time, the algorithm only requires O (n) Time to schedule n activities so that the most activities can use public resources in a compatible manner. If the given activities are not arranged in a non-descending order, you can use the O (nlogn) Time to rearrange them. For example, set the start time and end time of the 11 activities to be arranged in a non-descending order of the end time as follows: shows the calculation process of the greedySelector algorithm. Each row in the figure corresponds to an iteration of the algorithm. The Shadow strip indicates that the activity has been selected for set A, while the blank strip indicates that the activity is currently checking for compatibility. If the start time Si of the checked activity I is less than the end time fi of the recently selected activity j, no activity I is selected; otherwise, activity I is added to set. Greedy algorithms cannot always obtain the overall optimal solution of the problem. But for the issue of activity arrangement, the greedy algorithm greedySelector can always obtain the overall optimal solution, that is, the final range of compatible activity set A is the largest. This conclusion can be proved by mathematical induction. Proof: set E = {0, 1, 2 ,..., N-1} is the set of given activities. Because the security end time of the activity arrangement in E is not sorted in descending order, activity 0 has the earliest completion time. First, it is proved that the problem of activity arrangement has an optimal solution starting with greedy selection, that is, the optimal solution contains activity 0. set a to an optimal solution for the given activity arrangement problem, and activities in a are arranged in non-descending order based on the end time. The first activity in a is activity k. If k = 0, a is an optimal solution starting with greedy choice. If k> 0, we set B = a-{k} bytes {0 }. Because end [0] is less than or equal to end [k], and activities in a are mutually compatible, activities in B are also mutually compatible. Because the number of activities in B is the same as that in a, and a is the best, B is also the best. That is to say, B is an optimal activity arrangement that begins with activity 0 with greed. Therefore, it is proved that there is always an optimal activity arrangement scheme starting with greedy selection, that is, the algorithm has the nature of greedy selection. The program running result is as follows:

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.