The Greedy Method of the Five Algorithms

Problem: A traveler has a backpack that can use at most c kg, and now there are n items, each with weights w1, w2, ..., wn, and each with a value of v1, v2, ... ,vn, if each item can be subdivided infinitely, find the maximum total value that the traveler can obtain.

Abstract: Combining items of the specified weight with the greatest value.

Idea: First find the value density of each item, put the item with the highest value density first, then put the item with the highest value density of the remaining items, and proceed in turn until the backpack is full.

code show as below:

package test;
 
import java.util.Arrays;
 
/**
 * Greedy method to find knapsack problem (divisible)
 * @author yanghang
 *
 */
public class Tanxin {
 
    public static void main(String[] args) {
        /**
         * initialize
         */
        // the capacity of the backpack
        double c = 12;
        // the number of items
        int n = 5;
        // array of item weights
        double[] w = new double[n];
        // array of item prices
        double[] v = new double[n];
        // item weight 1,2,3,4,5
        for (int i = 0; i < n; i++) {
            w[i] = i + 1;
        }
        // The value of the item is 5,4,3,2,1
        for (int i = 0; i < n; i++) {
            v[i] = n - i;
        }
         
        /**
         * Find the unit weight value r[i] = v[i] / w[i]
         *
         */
        double[] r = new double[n];
        int[] index = new int[n];
        for (int i = 0; i < n; i++) {
            r[i] = (double) v[i] / (double) w[i];
            index[i] = i;
        }
 
        /**
         * Sort by value density
         */
        double temp = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                if (r[i] < r[j]) {
                    temp = r[i];
                    r[i] = r[j];
                    r[j] = temp;
                    temp = w[i];
                    w[i] = w[j];
                    w[j] = temp;
                    temp = v[i];
                    v[i] = v[j];
                    v[j] = temp;
                }
            }
        }
 
        /**
         * Initialize the solution vector x[n], solve and print the solution vector
         */
        double[] x = new double[n];
        for (int i = 0; i < n; i++) {
            if (w[i] <= c) {
                x[i] = 1;
                c = c - w[i];
            } else if (w[i] > c) {
                x[i] = (double)c / w[i];
                c = 0;
                break;
            }
        }
        System.out.println("The solution vector is: " + Arrays.toString(x));
         
         
        /**
         * Find the maximum value of the items stored in the backpack according to the solution vector and print
         */
        double maxValue = 0;
        for (int i = 0; i < n; i++) {
            maxValue += v[i] * x[i];
        }
        System.out.println("The maximum value of items in the backpack is: " + maxValue);
 
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325594880&siteId=291194637