//---------------------------------------------------------------------------- // GreedyKnapsack.java // Implementation of Greedy Algorithm for both the continuous and discrete // versions of the Knapsack problem. Note continuous case only works if // function select() is set to be v[i]/w[i]. Also discrete case may not be // optimum on certain instances. //---------------------------------------------------------------------------- class GreedyKnapsack{ public static void main( String[] args ){ int i; double W = 9; // capacity of knapsack double[] v = {0, 5, 5, 9, 4, 4, 12}; // values of objects (disregard index 0) double[] w = {0, 1, 4, 3, 4, 1, 6}; // weights of objects (disregard index 0) int n = v.length-1; // number of objects double[] x_cont = {0, 0, 0, 0, 0, 0, 0}; // solution to continuous case [0, 1] double[] x_disc = {0, 0, 0, 0, 0, 0, 0}; // solution to discrete case {0, 1} double weight, value; // solve continuous case using greedy algorithm weight = 0.0; value = 0.0; while( weight0 ){ System.out.printf("%5.1f%% of object %d%n", 100*x[i], i); } } } // findBestObject() // Returns the index of the "best" remaining object, or returns 0 if // no objects remain. static int findBestObject(double[] v, double[] w, double[] x){ int n = v.length-1; int i, i_best = 0; double temp, max = 0; for(i=1; i<=n; i++){ temp = select(i, v, w); if( x[i]==0 && (temp>max) ){ max = temp; i_best = i; } } return i_best; } // select() // Selection function for greedy knapsack problem static double select(int i, double[] v, double[] w){ return v[i]/w[i]; } }