实验目的:
1) 掌握动回溯法的设计思想;
2)掌握回溯法解题步骤;
3)学习运用回溯法分析并解决0-1背包问题。
实验要求:运用回溯法分析并解决下表给定的0-1背包问题,5个物品重量分别是3,5,7,8,9和价值分别是4,6,7,9,10,求背包总价值最大问题
package 算法实验;
public class bag01 {
int n = 5;
int capacity = 20;
int[] weight = {3,5,7,8,9};
double[] value = {4,6,7,9,10};
int maxValue = 0;
int tempValue;
int tempWeight;
int[] way = new int[n];
int[] bestWay = new int[n];
public void BackTrack(int t){
//已经搜索到根节点
if(t>n-1){
if(tempValue > maxValue){
maxValue = tempValue;
for(int i=0;i<n;i++)
bestWay[i] = way[i];
}
return;
}
//搜索左边节点
if(tempWeight + weight[t] <= capacity){
tempWeight += weight[t];
tempValue += value[t];
way[t] = 1;
BackTrack(t+1);
tempWeight -= weight[t];
tempValue -= value[t];
way[t] = 0;
}
//不装入这个物品,直接搜索右边的节点
if( Bound(t+1) >= maxValue){
BackTrack(t+1);
}
}
//用于计算剩余物品的最高价值上界
public double Bound(int k){
double maxLeft = tempValue;
int leftWeight = capacity - tempWeight;
//尽力依照单位重量价值次序装剩余的物品
while(k <= n-1 && leftWeight > weight[k] ){
leftWeight -= weight[k];
maxLeft += value[k];
k++;
}
//不能装时,用下一个物品的单位重量价值折算到剩余空间。
if( k <= n-1){
maxLeft += value[k]/weight[k]*leftWeight;
}
return maxLeft;
}
public static void main(String[] args){
bag01 b = new bag01();
b.BackTrack(0);
System.out.println("该背包能够取到的最大价值为:"+b.maxValue);
System.out.println("取出的方法为:");
for(int i : b.bestWay)
System.out.print(i+" ");
}
}
结果