Greedy Algorithm for data structure (Think About Knapsack issues)-(10) greedy backpack
Greedy policy. Thinking about greedy algorithms is included in the Code.
Package com. lip. datastructure;/*** greedy algorithm: Packing Problem * @ author Lip * The packing problem can be an extension of the time adjustment problem. When a box has no volume limit, this is the issue of Time Scheduling * in the issue of Time Scheduling: There are two issues that can be discussed. 1. Average shortest time 2. Total shortest time * these two problems are similar to the problems in the packing problem. * // ** The above is the packing problem I understand. It was originally intended to address the problem of a backpack * The Problem description: There are N items and a backpack with a capacity of V. The weight of the I-th item is w [I], and the value is v [I]. * Solving which items are loaded into a backpack can make the total weight of these items not exceed the total size of the backpack, and the total value is the largest. * // ** The greedy algorithm can solve the packing problem or the knapsack problem, but the solution obtained by the greedy algorithm may not be the optimal solution. * For example, 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 best choice of jewelry with the most cost-effective. * From this perspective, we can use greedy algorithms to solve the problem of a backpack, even if it is not the optimal solution. However, this solution is a greedy strategy for everyone. */Public class Pack {public static void main (String [] args) ************* * *****************/int [] weight = {8, 7, 5, 4, 4, 3, 3, 2, 2, 1}; int [] box = {12, 12, 10}; int [] result = loadInBox (Type. OFFLINE, box, weight); for (int I = 0; I <box. length; I ++) {System. out. println ("no." + (I + 1) + "No.:"); print (weight, result, I + 1); System. out. println ();} /// ************ 0-1 backpack problem *************** // int [] weight = {,}; // Int [] value = {,}; // 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 ("Maximum Benefit:" + sum); // System. out. println ("how much space is not used:" + (c-sumW ));} ************** * **************** // ***** @ param type * @ param box * @ param weight goods weight * @ return */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; // total box volume for (int I = 0; I <box. length; I ++) sum2 + = box [I]; if (sum> sum2) // empty, the box does not return null; if (type = Type. OFFLINE) // allocate the largest cargo to each box {for (int I = weight. length-1, j = 0; I>-1; I --) {int find = box. length; while (weight [I]> box [j]) // {j = (j + 1) % box cannot be installed. l Ength; find --; if (find = 0) // The box is not enough {System. out. println ("------ not enough backpack ---------"); 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 surplus of the box, that is, the goods that can be loaded for (int j = weight. length-1; j>-1; j --) // keep loading until it is 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 the packing problem: online and offline. * Online means that after a box is fully filled with goods, it starts to process the next box. * offline means to read all goods, distribute the goods to the boxes in ascending order until each box is filled with */public enum Type {ONLINE, OFFLINE ;}; /************************** 0-1 backpack problem (Greedy Algorithm) ************************************//** ** @ param c backpack capacity * @ param value the value of each item * @ param weight the volume of each item * // ** when a greedy algorithm is used to solve a backpack problem, so considering the greedy strategy, we must ensure that the current selection is the best. * Another reference item "cost-effective" will be referenced. p = value/weight * always chooses the item with the highest cost-effective ratio and puts it in 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 with while (c> 0) {double max =-1; int pos =-1; for (int I = 0; I <price. length; I ++) // find the most cost-effective, and the {if (pr) Ice [I]! =-1 & price [I]! = 0 & max <price [I]) {max = price [I]; pos = I ;}} if (pos =-1) // although there is space left, however, the appropriate break cannot be found. if (c> = weight [pos]) {c-= weight [pos]; price [pos] =-1; // position [p] = pos + 1; p ++;} else {price [pos] = 0; // cannot be installed, but it can be smaller than this.} return position ;}}
Greedy Algorithm running effect:
Offline packing:
Online packing:
Greedy Algorithm solves the problem of 0-1 backpacks:
In the next article, we will use dynamic programming to solve the 0-1 knapsack problem.