greedy algorithm when the university has learned also, perhaps the weekend did not expect to write anything, it was learning the knowledge of the year, greedy algorithm (also known as greedy algorithm) , the greedy algorithm always makes the best choice in the current view. The greedy algorithm is not considered from the overall optimum, and the choice made by it is only the local optimal choice in some sense. Of course, it is hoped that the final result of greedy algorithm is also the overall optimal. Although the greedy algorithm cannot get the whole optimal solution for all problems, it can produce the whole optimal solution for many problems.
greedy Elements
This is the concept, and if you need more information, go ahead and search for more. this time there is a problem, what is the use of greedy algorithms? Only need to meet two points, the first is the solution of the problem of the optimal solution can be a series of local optimization to solve, and then whether the optimal solution of a problem contains a sub-problem of the optimal solution, also known as the optimal sub-structure, generally if the inclusion of sub-problems of the optimal solution can be obtained by dynamic algorithm or greedy algorithm. the premise of the greedy strategy is that the local optimal strategy can lead to the global optimal solution. General framework for solving problems:
Starting from an initial solution of a problem;
While (one step forward for a given total target)
{
a solution element of the feasible solution is obtained by using the feasible decision;
}
a feasible solution to the problem of all the solution elements, please refer to the demo for details.Greedy Demo
Greedy demo This dude is a freshman in a very classic C language topic, knapsack problem, There is a backpack, backpack capacity is m=150. There are 7 items that can be divided into any size. It is required to maximize the total value of the items loaded into the backpack, but not to exceed the total capacity. (different from 0-1 backpack, 0-1 need to use to dynamic planning)
Item A B C D E F G
Weight 35 30 60 50 40 10 25
Value 10 40 30 50 35 40 30
Problem Solving Ideas:
The constraint is that the total weight of the loaded item does not exceed the backpack capacity: ∑wi<=m (m=150).
(1) According to the greedy strategy, each time to select the most valuable items loaded into the backpack, the results are optimal?
(2) Can the optimal solution be obtained for each item with the smallest weight selected?
(3) Each time the unit weight value of the most valuable items, become the solution of the strategy.
That's the guess. and then need to prove that can see the blog basically also understand the third is the best answer, generally this problem at night C code, C + + code, C # code less, I have nothing to write, will look under, define an object class:
public class Product {public string Name {get; set;} Public float Weight {get; set;} Public float Value {get; set;} Public float Unitvalue {get; set;} }
Console code, the code is relatively simple:
float[] weight = new float[] {35, 30, 60, 50, 40, 15, 20}; float[] value = new float[] {10, 40, 30, 50, 35, 40, 30}; string[] name = new string[] {"A", "B", "C", "D", "E", "F", "G"}; list<product> list = new list<product> (); for (int i = 0; i < weight. Length; i++) {Product Product = new Product (); Product. Name = Name[i]; Product. Weight = Weight[i]; Product. Value = Value[i]; Product. Unitvalue = Value[i]/weight[i]; List. ADD (product); } float sum = 0; foreach (Product item in list) {Console.Write (item. Name + "-" + item. Unitvalue+ "\ t"); } list<product> result = new list<product> (); foreach (var product in list. OrderByDescending (item = Item). Unitvalue)) {sum + = product. Weight; if (sum >) break; Result. ADD (product); } foreach (var product in result) {Console.Write (product. Name + "--" + product. weight+ "\ t"); } Console.WriteLine (); Console.WriteLine ("Total Value:" + result.) Sum (item = Item. Value) + "\ t total weight:" + result. Sum (item = Item. Weight)); Console.readkey ();
The results are as follows:
In this way, the optimal result is that the goods are not segmented, some require cutting, the results of how to see how to forget;
Backpack is the past, there is a meeting that is not around, the teacher also often take classes of teachers to do examples, first look at the topic:
There is a set of n activity time, each activity must use the same resource, such as the Conference field, and only one activity can be used at the same time, each activity has a starting SI and the end time of the activity, FI, that is, his use interval is (SI,FI), now requires you to allocate the activity occupy the timetable, That is, which activities occupy the conference room, which do not occupy, so that they do not conflict, the requirement is as much as possible to maximize the participation of activities, that is, the maximum time interval ~
Look at the hard words, then look at a network image, I means activity, s[i] start time, F[i] indicates the end time:
This topic is also very simple, it is necessary to think clearly that if two activities need to be compatible, then the second activity must be greater than the start time is equal to the end time of the first activity, think clearly this is OK;
Int[] start = {1, 3, 0, 5, 3, 5, 6, 8, 8, 2, n}; Int[] End = {4, 5, 6, 7, 8, 9, ten, one, one, and one}; list<int> list = new list<int> () {0}; int j = 0; for (int i = 1; i < start. Length; i++) { if (Start[i] >= end[j]) { list. ADD (i); j = i; } } for (int i = 0; i < list. Count (); i++) { console.write (List[i]. ToString () + "\ t"); } Console.readkey ();
The answer to the above question is 0,3,7,10; This problem has a similar sibling problem is how to solve the same line of each segment of the length of coverage, the specific will be used as a reference bar, detailed values see the following code:
Int[] start = {2, 3, 4, 5, 6, 7, 8, 9, ten, one}; Int[] End = {3, 5, 7, 6, 9, 8, N , ten, and- int j = 0; int sum = end[0]-start[0]; for (int i = 1; i < start. Length; i++) { if (Start[i] >= end[j]) { sum + = End[i]-start[i]; j = i; } else { if (End[i] > End[j]) { sum + = End[i]-end[j]; j = i;}} } Console.WriteLine ("Total number of miles:" + sum.) ToString ()); Console.readkey ();
The answer is 13 km ~ small count of good feelings, big count of the bad, strong grey ash annihilation, I will count ~ everybody, good night ~
Algorithm-Greedy algorithm