实验内容
- 使用贪心算法、回溯法、分支限界法解决0-1背包问题;
- 通过上机实验进行算法实现;
- 保存和打印出程序的运行结果,并结合程序进行分析,上交实验报告。
动态:
package org.csu.shiyan_5;
import sun.dc.pr.PRError;
public class Dinggui0_1BAG{
public static void main(String[] args) {
int MaxWeight = 10;
int NumofItems = 5;
int []weight = new int[]{2,2,6,5,4};
int[] value = new int []{6,3,5,4,6};
System.out.println("最后的结果为");
System.out.println(solution(MaxWeight,NumofItems,weight,value));
System.out.println("表格为");
for(int j = 1;j<=NumofItems;j++){
for(int i = 1;i<=MaxWeight;i++){
System.out.print(solution(i,j,weight,value));
System.out.print("\t");
}
System.out.println();
}
}
public static int solution(int N,int W,int []weight,int []value){
int [][]map = new int[N+1][W+1];
for(int i = 1;i<=W;i++){
int values = value[i-1];
int weights = weight[i-1];
for(int j = 0;j<=N;j++){
if(j<weights){
map[j][i] = map[j][i-1];
continue;
}
map[j][i] = Math.max(map[j][i-1],map[j-weights][i-1]+values);
}
}
return map[N][W];
}
}
回溯:
package org.csu.shiyan_5;
import java.util.Scanner;
class back{
public int value = 0;
public int weight = 0;
back(){}
}
public class Feizhixianjie0_2BAG {
static int cw,cp,bestp,n,c;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入物品的个数:");
n = input.nextInt();
System.out.println("请输入背包的容量:");
c = input.nextInt();
back []b = new back[1000];
System.out.println("请输入每个物品的价值和重量:");
for(int i=1;i<=n;i++) {
b[i] = new back();
b[i].value = input.nextInt();
b[i].weight = input.nextInt();
}
Backtrack(1,b);
System.out.println("最优解为:");
System.out.println(bestp);
}
public static float bound(int t,back []b)
{
float a=cp;
int i;
for(i=t;i<=n;i++)
a+=b[i].value;
return a;
}
public static void Backtrack(int t,back[]b)
{
if(t>n)
{
if(cp>bestp)
bestp=cp;
return;
}
if(cw+b[t].weight<c)
{
cw+=b[t].weight;
cp+=b[t].value;
Backtrack(t+1,b);
cw-=b[t].weight;
cp-=b[t].value;
}
if(bound(t+1,b)>bestp)
Backtrack(t+1,b);
}
}
分支限界法:
package org.csu.shiyan_5;
import java.util.Scanner;
class back1{
public int value = 0;
public int weight = 0;
back1(){}
}
public class Huisufa0_1PBAG{
static int cw,cp,bestp,n,c;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入物品的个数:");
n = input.nextInt();
System.out.println("请输入背包的容量:");
c = input.nextInt();
back1 []b1 = new back1[1000];
System.out.println("请输入每个物品的价值和重量:");
for(int i=1;i<=n;i++) {
b1[i] = new back1();
b1[i].value = input.nextInt();
b1[i].weight = input.nextInt();
}
for(int i = 1;i < n;i++ ) {
if (cmp(b1[i], b1[i + 1])) {
int t = b1[i].weight;
b1[i].weight = b1[i + 1].weight;
b1[i + 1].weight = t;
t = b1[i].value;
b1[i].value = b1[i + 1].value;
b1[i + 1].value = t;
}
}
Backtrack(1,b1);
System.out.println("最优解为:");
System.out.println(bestp);
}
public static boolean cmp(back1 b1,back1 b2)
{
if(b1.value*1.0/b1.weight>b2.value*1.0/b2.weight)
return true;
return false;
}
public static float bound(int t,back1 []b)
{
float a=cp;
int left=c-cw;
while(t<=n)
{
if(left > b[t].weight) {
a += b[t].value;
left -= b[t].weight;
t++;
}
}
if(t<=n)
{
a+=b[t].value*left*1.0/b[t].weight;
}
return a;
}
public static void Backtrack(int t,back1[]b) {
if (t > n) {
if (cp > bestp)
bestp = cp;
return;
}
if (cw + b[t].weight < c) {
cw += b[t].weight;
cp += b[t].value;
Backtrack(t + 1,b);
cw -= b[t].weight;
cp -= b[t].value;
}
if (bound(t + 1,b) > bestp)
Backtrack(t + 1,b);
}
}