Be greedy algorithm algorithm I first systematic contact in the process of learning pages read some books and examples, then introduces the concept of greed and an example:
Greedy algorithm The main idea is locally optimal solution. After the greedy algorithm to make local optima on currently available information, and made a choice, no matter what the future results will not change selection, while the greedy selection strategy has a direct algorithm for good or bad Impact.
Characteristics greedy algorithm (after satisfying use):
1: Select greedy
Refers to the overall optimal solution of the original problem can be solved through a series of local optimum results, apply the same rules, the original problem into a similar but smaller sub-problems, then each step is currently the best option. This choice depends on the choices made. Meanwhile, the greedy strategy without backtracking process.
2: optimal substructure
When the optimal solution contains optimal solution to a problem of his son's problems, saying that this issue is optimal substructure property. This property is the key to this problem can be solved with a greedy.
Greedy algorithm using the steps:
1: Select a greedy strategy to develop the best solution currently looks (options is particularly important).
2: get local optimal solution step by step in accordance with greedy strategy.
3: an optimal solution for all the local best and become the original question.
The next two examples:
1: optimal loading problem:
Problem Description: There is a boat, a capacity of C, has i antiques weight are Wi, how to load as many antiques?
Outline of Solution: antiques weight descending order, and then sequentially loaded into a ship, the ship is stopped when the weight is greater than the final number of reproduced returned.
Title data: C: 30, W: 4,10,7,11,3,5,14,2
. 1 public int Solution1 () { 2 int C = 30 ; . 3 int [] W is = new new int [] {. 4, 10,. 7,. 11,. 3,. 5, 14, 2 }; . 4 Arrays.sort (W is); . 5 int tmp = 0 ; . 6 int I = 0 ; . 7 the while (tmp < C) { . 8 tmp + = W is [I]; . 9 I ++ ; 10 } . 11 return I-. 1 ; 12 is } // optimal loading problem Solution
Finally, the return value is 5, the time complexity of O (n + nlogn)
2: knapsack problem:
Problem Description: There is a mountain cave, the cave n kinds of treasures, treasures each have a certain weight value W and V, only the weight of the treasures carried away M a take, like, divisible treasure, how to make the maximum value of the treasure?
Problem-solving ideas: Select the largest treasure greedy strategy unit value, and then select from high to low price, the final output of the maximum value of the treasure.
Data Title: W: 4,2,9,5,5,8,5,4,5,5; V: 3,8,18,6,8,20,5,6,7,15; M: 30
. 1 public a float Solution2 () { 2 int M = 30 ; . 3 int [] W is = new new int [] {4,2,9,5,5,8,5,4,5,5 }; . 4 int [] V = new new int [] {3,8,18,6,8,20,5,6,7,15 }; . 5 Item [] items = new new Item [W.length]; . 6 for ( int I = 0; I <W.length; I ++ ) { . 7 items [I] = new new Item (W is [I], V [I]); . 8 } // calculate the unit weight value, and store it as an array . 9 Arrays.sort ( items); 10 Item[] item =new Item[items.length]; 11 for(int i=0;i<10;i++){ 12 item[i]=items[9-i]; 13 }//数组翻转 14 float Value=0; 15 int count=0; 16 int tmp=0;//装载的重量 17 while(tmp<M){ 18 if(tmp+item[count].getW()>M || tmp+item[count].getW()==M){ 19 break; 20 }else{ 21 is tmp + = Item [COUNT] .getW (); 22 is the Value + = Item [COUNT] .getV (); 23 is COUNT ++ ;} 24 } 25 int m = M-tmp; // the rest of the weight 26 is the Value + = m * item [count] .getV1 (); // away after the divided part 27 return the Value; 28 } 29 public class Item the implements the Comparable <Item> { 30 Private int W is; // weight 31 is Private int V; //Value 32 Private a float Vl; // Unit value 33 is public Item ( int I1, int I2) { 34 is the this .W = I1; 35 the this .V = I2; 36 the this .V1 = (( a float ) I2) / I1; 37 [ } 38 is 39 public a float getV1 () { 40 return Vl; 41 is } 42 is 43 is public int getv () { 44 is return V; 45 } 46 47 public int getW() { 48 return W; 49 } 50 51 public int compareTo(Item i){ 52 if (this.V1>i.getV1()){ 53 return 1; 54 55 }else if(this.V1==i.getV1()){ 56 return 0; 57 }else if(this.V1<i.getV1()){ 58 return -1; 59 } 60 return 0; 61 } 62 }
Finally, the return value is 70.5, the time complexity of O (n + nlogn)
In solving the problem-solving ideas, if java structured experience in the c ++ code behind less redundancy in java need to achieve comparable interface object comparison, a little more trouble, but the overall idea is the same problem-solving of.