The basic idea of greedy algorithm: through a series of steps to construct the solution of the problem, each step is an extension of the constructed part solution, until the complete solution of the problem is obtained.
Greedy algorithm, each step "greedy" to choose the best part of the solution, but do not take into account the impact of such a choice on the overall ( local optimal), so the global solution is not necessarily the best solution, but for many problems it can produce the overall optimal solution.
Specific algorithm Description:
public static void greedy ()
{
float cu = c;
int temp = 0;
int i = 0;
for (i = 0; i < n; ++i)
{
X[i] = 0;//initialized to 0
}
for (i = 0; i < n; ++i) br> {
temp = sortresult[i];//Gets the order of the objects
if (w[temp] > Cu)
{
Break
}
//loading items into a backpack
x[temp] = 1;
Cu-= w[temp];// The backpack capacity is correspondingly reduced by
}
if (i <= N)//Make backpack full
{
X[temp] = cu/w[temp];//such as backpack capacity remaining 10,w[temp] = 9.8f; To fill
}
Display ();
}
Each step of the greedy algorithm needs to meet 3 conditions:
1. Feasibility: It is necessary to meet the constraints of the problem.
2. Local optimization: It is the best local choice among all feasible options in the current step.
3. Non-cancellation: Once the selection is made, it cannot be changed in the subsequent steps.
Basic elements of the greedy algorithm:
1. Greedy Choice nature: refers to the overall optimal solution of the problem can be achieved by a series of local optimal selection, that is, greedy choice.
2. Optimal substructure property: Refers to the optimal solution of a problem with its sub-problem.
The similarities and differences between greedy algorithm and dynamic programming algorithm:
The same point: all have the best substructure properties.
Different points: The dynamic programming algorithms usually solve the sub-problems in the bottom-up way, while the greedy algorithm is usually carried out in the top-down way;
The following is a study of 2 classic combinatorial optimization examples, and illustrates the main differences between greedy and dynamic programming algorithms.
0-1 knapsack problem:
Given n kinds of items and a backpack. The weight of item I is WI, its value is VI, the capacity of the backpack is C. How do you choose which items are loaded into your backpack so that the total value of the items loaded into your backpack is greatest?
Knapsack problem:
Similar to the 0-1 knapsack problem, the difference is that when you select item I in the backpack, you can select part of the item I, and not necessarily all loaded into the backpack, 1≤i≤n.
These 2 kinds of problems have the best substructure properties, But the knapsack problem can be solved by greedy algorithm, while the 0-1 knapsack problem can't be solved by greedy algorithm.
For the 0-1 knapsack problem:
Example: N=3, w={10,20,30}, v={60,100,120}, c=30
What is the best partial solution? --Do not seek unit value.
By greedy method: Choose the most valuable put: all put in the 3rd item, the value of 120.
But this is not the best, if the amount of goods put in, the total value of 160.
For knapsack problems:
Example: N=3 w={10,20,30} v={60,100,120} c=50
Unit value: v/w={6,5,4}
So, the first pick of a number of items loaded, r=40,pv=60
Second pick number 2nd, all loaded into the r=20,pv=160
Third pick number 3rd, part loaded into the r=0,pv=160+80=240
The specific code is as follows
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceseqlistsort{/// <summary> /// <ather> ///Lihonlgin/// </ather> /// <content> ///knapsack problem:///similar to the 0-1 knapsack problem, the difference is that when you select the item I load the backpack, you can select part of the item I, and not necessarily all installed///into the backpack, 1≤i≤n. ///these 2 kinds of problems all have the optimal substructure property, which is very similar; but knapsack problem can be solved by greedy algorithm;///The 0-1 knapsack problem can not be solved by greedy algorithm. /// </content> /// </summary> classGreedy_knapsack {Const intSize = -; Static float[] W =New float[size]; Static float[] v =New float[size]; Static intn =5;//10 Items Static intc = -;//Backpack Capacity Static float[] x =New float[size]; Static int[] Sortresult =New int[Size];//Save unit value from large to small subscript Public Static voidInitData () {Random R=NewRandom (); for(inti =0; I < n; ++i) {V[i]= R.next (Ten, to); W[i]= R.next (5, -); X[i]= V[i]/W[i]; Console.Write ("weight: {0:f2}", W[i]); Console.WriteLine ("value is: {0:F2}", V[i]); } Console.WriteLine (); Sort ();//Sort First } Static voidSort () {floattemp =0.0f; intindex =0; intK =0; for(inti =0; I < n1; ++i) {temp=X[i]; Index=i; //find the maximum benefit and save the subscript at this time for(intj = i+1; J < N; ++j) {if(Temp < X[J] && (0==Sortresult[j])) {Temp= X[j];//The first trip compares to get the maximum valueindex = j;//Mark Subscript } } //sort the w[i] as a marker if(0==Sortresult[index]) {Sortresult[index]= k++; } } //Modify the least effective sortresult[i] tag for(inti =0; I < n; i++) { if(0==Sortresult[i]) {Sortresult[i]= k++; } } } Public Static voidgreedy () {floatCU =C; inttemp =0; inti =0; for(i =0; I < n; ++i) {X[i]=0;//initialized to 0 } for(i =0; I < n; ++i) {temp= Sortresult[i];//the order in which objects are taken if(W[temp] >cu) { Break; } //load items into a backpackX[temp] =1; Cu-= W[temp];//The backpack capacity is correspondingly reduced } if(I <= N)//make the backpack full{X[temp]= Cu/w[temp];//e.g. backpack capacity remaining 10,w[temp] = 9.8f; to fill} Display (); } Static voidDisplay () { for(inti =0; I < n; ++i) {Console.Write ("Number:"+i); Console.WriteLine ("number of items put in {0:f}", X[i]); } Console.WriteLine (); } }}
Knapsack Problem of greedy algorithm