Knapsack problem greedy algorithm
[problem description] n items and a backpack are given. The weight of item i is Wi, its value is Vi, and the capacity of backpack is c. How to choose the items in the backpack to maximize the total value of the items in the backpack
[problem analysis]
·General knapsack problem!
Optimal idea: considering the comprehensive effect of value growth and capacity consumption, i.e. each time I choose the item with the largest value to weight ratio vi/wi, i.e. first load it into the backpack, which is the final greedy strategy.
Manually input knapsack capacity c and item number n, the value of weight array w[i] of random biological product I and the value array v[i] of item I.
Program source code:
package SF; import java.util.Scanner; public class Greedy knapsack problem { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.println("Please enter the number of items n: "); int n = reader.nextInt(); System.out.println("Please input the backpack capacity c: "); float c = reader.nextFloat(); float[] w = new float[n+1]; //Weight of articles float[] v = new float[n+1]; //Value of goods for(int i=0;i<n;i++){//Random number less than 20 for weight of goods w[i] = (float) Math.floor(Math.random()*20); } System.out.println("The weight of the item is:"); for(int j=0;j<n;j++){ System.out.print(w[j]+" "); } System.out.println(""); for(int i=0;i<n;i++){//Value of goods is within random number (20) v[i] = (float) Math.floor(Math.random()*20); } System.out.println("The value of the item is:"); for(int j=0;j<n;j++){ System.out.print(v[j]+" "); } System.out.println(""); float[] x = new float[n+1]; //Solution to the problem //Greedy knapsack problem p = new greedy knapsack problem (); p.Sort(n, v, w); float s = p.Knapsack(w, v, x, c, n); System.out.println("The final value of the optimal solution is:"+s); } float Knapsack(float w[],float v[],float x[],float c,int n){ float total = 0; int i; Sort(n,v,w); for(i=0;i<n;i++) x[i] = 0; i = 1; while(w[i]<c){ x[i] = 1; total = total + v[i]; c = c - w[i]; i++; } x[i] = c/w[i]; total = total + x[i]*v[i]; System.out.println("The solution of the problem is as follows:"); for(int j=0;j<n;j++){ System.out.print(x[j]+" "); } System.out.println(""); return total; } private void Sort(int n,float v[],float w[]) {//Sort from large to small int i; float j; float k; for(i =0;i<n;i++){ if( v[i+1]/w[i+1] > v[i]/w[i] ){ j = v[i]; k = w[i]; v[i] = v[i+1]; w[i] = w[i+1]; v[i+1] = j; w[i+1] = k; } } } }
Running screenshot: