Greedy strategy. Thinking about the greedy algorithm, the thinking process is put in the code.
Package com.lip.datastructure;/** * Greedy algorithm: The thinking of packing problem * @author Lip * Boxing problem can be an extension of the time adjustment problem, when a box has no volume limit, then is the time scheduling problem * In a time-scheduling problem: There are two issues that can be discussed. 1. Average shortest time 2. Total shortest time * Both issues are so similar to the problem with boxing. *//* * Above is my understanding of the boxing problem, originally wanted to say knapsack problem of the knapsack problem description: There are n items and a capacity of a V backpack. The weight of article I is w[i], the value is v[i]. * Solve which items are loaded into the backpack so that the total weight of these items does not exceed the backpack capacity, and the value of the largest sum. *//* * Greedy algorithm can solve the problem of boxing, but also can solve the knapsack problem, but the greedy algorithm to find out the solution is not the optimal solution. * such as: In fact, as a rational person, we are greedy. When you face a pile of gold and silver jewelry, you have a backpack, your choice will certainly be the first choice of the most cost-effective jewelry. * So, from this point of view, we can use the greedy algorithm to solve the knapsack problem, even if it is not the optimal solution of the problem. However, this solution is a common choice for everyone's greedy strategy. */public class Pack{public static void Main (string[] args) {/*********************** boxing problem ***************************** /int []weight={8,7,5,4,4,3,3,2,2,2,1};int []box={12,12,12,10};int []result=loadinbox (Type.OFFLINE, box, weight); for (int i=0;i<box.length;i++) {System.out.println ("No." + (i+1) + "box cargo:");p rint (weight, result, i+1); System.out.println ();} 0-1 knapsack problem **************///int []weight={2,3,4,6,2,5,4,3,8,1};//int []value={7,8,13,20,17,9,12,15,5,5 };//int C=20;//int []position=loadinpack (c, value, weight);//int sum=0;//int sumw=0;//for (int i=0;i<position.length;i++)//{//if (position[i]!=0)//{//sumw+=weight[position[i]-1];/ /sum+=value[position[i]-1];//System.out.println (position[i]+ "," +weight[position[i]-1]+ "," +value[position[ i]-1]+ ")");//}//else break;//}//system.out.println ("The greatest gains are:" +sum);//system.out.println ("How much space is not utilized:" + (C-SUMW)); }/*********************** Packing Problem ********************************//** * @param type * @param box box * @param weight Cargo weight * @r Eturn */public static Int[]loadinbox (Type type,int box[],int []weight) {int []result=new int[weight.length]; Sort.quicksort (weight); int sum=0;for (int i=0;i<weight.length;i++) sum+=weight[i];int sum2=0;//Box total volume for (int i=0;i <box.length;i++) sum2+=box[i];if (sum>sum2)//艹, Box not enough return null;if (type==type.offline)//To each box is assigned a maximum of goods {for ( int i=weight.length-1,j=0;i>-1;i--) {int find=box.length;while (WEIGHT[I]>BOX[J])//Can not be installed {j= (j+1)%box.length; Find--;if (find==0)//box is not enough {System.out.println ("------backpack is not enough---------"); return null;}} result[i]=j+1;box[j]-=weight[i];j= (j+1)%box.length;}} else if (type==type.online)//First load a box {for (int i=0;i<box.length;i++) {//box[i] The current remainder of the box, which is also the cargo for (int j=weight . length-1;j>-1;j--)//Always install until filled with {if (box[i]==0)//The box is full of break; if (Result[j]==0&&weight[j]<=box[i])//The goods are not loaded {BOX[I]-=WEIGHT[J]; result[j]=i+1; }}}}return result;} public static void print (int []weight,int []result,int K] {for (int i=0;i<result.length;i++) if (result[i]==k) System.out.print (weight[i]+ "");} /** * * @author LIP * There are two ways to solve boxing problems, one is online and one is offline. * The so-called online is to put a box completely full of goods, and then start processing the next box * offline is to read all the goods, according to the order from large to small distribution to the box, until each box is filled with */public enum type{online,offline;};/ 0-1 knapsack problem (greedy algorithm) ************************************//** * * @param c Backpack capacity * @param value The value of each item * @param weight The volume of each item *//* * When using the greedy algorithm to solve knapsack problem, then consider the greedy strategy, is to ensure that the current selection is the best. * Then another reference "value for money" is quoted, P=value/weight * has been selected to put the most cost-effective items into the backpack, until the backpack is full */public static int[] Loadinpack (int c,int[]value , int []weight) { Double []price=new double[value.length];//cost-effective int []position=new int[value.length]; int p=0; for (int i=0;i<value.length;i++) price[i]= (double) value[i]/weight[i]; Start loaded while (c>0) {double max=-1; int pos=-1; for (int i=0;i<price.length;i++)//Find the most cost-effective and not be loaded into the backpack {if (PRICE[I]!=-1&&PRICE[I]!=0&&MAX&L T;price[i]) {max=price[i]; Pos=i; }} if (Pos==-1)//Although the rest of the space, but can not find a suitable break; if (C>=weight[pos]) {C-=weight[pos]; price[pos]=-1;//has been installed position[p]=pos+1; p++; } else {price[pos]=0;//cannot be installed, but can be installed smaller than this}} return position; }}
Greedy algorithm running effect:
Offline Boxing:
Online Boxing:
Greedy algorithm solves 0-1 knapsack problem:
Using dynamic programming to solve the 0-1 knapsack problem in the next chapter
Greedy algorithm of data structure (thinking of knapsack problem)-(10)