背包问题的描述:已知有n种物品和一个可容纳M重量的背包,每种物品i的重量为。假定将物品i的一部分放入背包就会得到的效益,这里,, 。显然,由于背包容量是M,因此,要求所有选中要装入背包的物品总重量不得超过M.。如果这n件物品的总重量不超过M,则把所有物品装入背包自然获得最大效益。现需解决的问题是,在这些物品重量的和大于M的情况下,该如何装包,使得得到更大的效益值。
用贪心策略求解背包问题
策略1:按物品价值p降序装包,
策略2:按物品重w升序装包
策略3:按物品价值与重量比值p/w的降序装包
java实现
package com.coffee.test2;
public class Goods {
private int goodsNum;
private double[] weights;
private double[] values;
// orderbyweight
public void orderByWeight() {
// weight temp
double wtemp = 0.0;
// value temp;
double vtemp = 0.0;
for (int i = 0; i < goodsNum; i++) {
for (int j = i + 1; i < goodsNum; i++) {
if (weights[j] > weights[i]) {
wtemp = weights[i];
weights[i] = weights[j];
weights[j] = wtemp;
vtemp = values[i];
values[i] = values[j];
values[j] = vtemp;
}
}
}
}
public void orderByValue() {
double wtemp = 0.0;
// value temp;
double vtemp = 0.0;
for (int i = 0; i < goodsNum; i++) {
for (int j = i + 1; i < goodsNum; i++) {
if (values[j] > values[i]) {
wtemp = weights[i];
weights[i] = weights[j];
weights[j] = wtemp;
vtemp = values[i];
values[i] = values[j];
values[j] = vtemp;
}
}
}
}
// order by value/weight
public void orderByWV() {
double wtemp = 0.0;
// value temp;
double vtemp = 0.0;
double wv[] = new double[goodsNum];
// init value/weight[]
for (int i = 0; i < goodsNum; i++) {
wv[i] = weights[i] / values[i];
}
for (int i = 0; i < goodsNum; i++) {
for (int j = i + 1; i < goodsNum; i++) {
if (wv[j] > wv[i]) {
wtemp = weights[i];
weights[i] = weights[j];
weights[j] = wtemp;
vtemp = values[i];
values[i] = values[j];
values[j] = vtemp;
}
}
}
}
public void print() {
System.out.print("weight:");
for (int i = 0; i < goodsNum; i++) {
System.out.print(weights[i] + " ");
}
System.out.println();
System.out.print("value: ");
for (int i = 0; i < goodsNum; i++) {
System.out.print(values[i] + " ");
}
System.out.println();
}
public int getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(int goodsNum) {
this.goodsNum = goodsNum;
}
public void setWeights(double[] weights) {
this.weights = weights;
}
public double[] getWeights() {
return weights;
}
public double[] getValues() {
return values;
}
public void setValues(double[] values) {
this.values = values;
}
}
package com.coffee.test2;
/**
* define packge class
* @author apple
*
*/
public class Packge {
//max weight
private double maxweight;
//have put weight
private double putweight;
//total values;
private double putvalues;
public double getMax() {
return maxweight;
}
public void setMax(int maxweight) {
this.maxweight = maxweight;
}
public double putGoods(Goods goods){
//last goods,just put part of it
int last=0;
for(int i=0;i<goods.getGoodsNum();i++){
if((putweight+goods.getWeights()[i])>maxweight){
last=i;
break;
}
putweight+=goods.getWeights()[i];
putvalues+=goods.getValues()[i];
}
//the last good's part weight
double leisure=maxweight-putweight;
putvalues+=(goods.getValues()[last]/goods.getWeights()[last])*leisure;
return putvalues;
}
}
package com.coffee.test2;
public class Test {
public static void main(String[] args) {
Goods goods = new Goods();
goods.setGoodsNum(10);
double[] w = new double[]{ 12, 3 , 5 , 6 , 20 , 18 , 32 , 26 , 30 , 10 };
double[] v = new double[]{ 20, 16 , 10 , 10 , 4 , 8 , 10 , 2 , 4 , 5 };
goods.setValues(v);
goods.setWeights(w);
goods.print();
Packge p1 = new Packge();
p1.setMax(100);
//orderbywei
goods.orderByWeight();
double totalv1=p1.putGoods(goods);
//orderbyvalue
goods.orderByValue();
double totalv2=p1.putGoods(goods);
//orderbywei/val
goods.orderByWV();
double totalv3=p1.putGoods(goods);
System.out.println("orderbyweight, the total v:"+totalv1);
System.out.println("orderbyvalue, the total v:"+totalv2);
System.out.println("orderbyweight/values, the total v:"+totalv3);
}
}