2023年8月1日发(作者:)

0—1背包问题

一、实验目的

学习掌握回溯思想。

二、实验内容

用回溯求解0—1背包问题,并输出问题的最优解。0—1背包问题描述如下:

给定n种物品和一背包。物品i的重量是Wi,其价值为Vi,背包的容量是c,问应如何选择装入背包中的物品,使得装入背包中物品的总价值最大。

三、实验条件

Jdk1.5以上

四、需求分析

对于给定n种物品和一背包。在容量最大值固定的情况下,要求装入的物品价值最大化。

五、回溯法的基本思想:

确定了解空间的组织结构后,回溯法就从开始结点(根结点)出发,以深度优先的方式搜索整个解空间。这个开始结点就成为一个活结点,同时也成为当前的扩展结点。在当前的扩展结点处,搜索向纵深方向移至一个新结点。这个新结点就成为一个新的活结点,并成为当前扩展结点。如果在当前的扩展结点处不能再向纵深方向移动,则当前扩展结点就成为死结点。

换句话说,这个结点不再是一个活结点。此时,应往回移动(回溯)至最近的一个活结点处,并使这个活结点成为当前的扩展结点。回溯法即以这种工作方式递归地在解空间中搜索,直至找到所要求的解或解空间中已没有活结点时为止。

六、详细设计

1 / 7

/*

*

*

* Created on 2007年6月2日,下午6:09

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package sunfa;

import ;

public class BackTrace {

/**

* @param args

*/

public static void main(String[] args) {

double w[]={2,2,6,5,4};

double v[]={6,3,5,4,6};

int n=5;

double c=10;

knapsack(v,w,c);

n(bestp);

2 / 7

}

//比较两个元素大小的类

private static class Element implements Comparable{

int id;

double d;

private Element(int idd,double dd){

id=idd;

d=dd;

}

public int compareTo(Object x){

double xd=((Element)x).d;

if(d

if(d==xd)return 0;

return 1;

}

public boolean equals(Object x){

return d==((Element)x).d;

}

}

static double c; //背包容量

static int n;//物品数

3 / 7

static double[]w;//物品重量数组

static double[]p; //物品价值数组

static double cw;//当前重量

static double cp;//当前价值

static double bestp; //当前最优值

static int [] x;//解

static int [] sortX;//排好序之后的解

static int [] bestX;//最有解

static Date date = null;//@jve:decl-index=0:

public static double knapsack(double[]pp,double[]ww,double cc){

c=cc;

n=-1;

cw=0.0;

cp=0.0;

bestp=0.0;

Element[]q=new Element[n];

//q为单位重量价值数组

for(int i=1;i<=n;i++)

q[i-1]=new Element(i,pp[i]/ww[i]);

ort(q);

p=new double[n+1];

4 / 7

w=new double[n+1];

x=new int[n+1];

sortX=new int[n+1];

bestX=new int[n+1];

for(int i=1;i<=n;i++){

p[i]=pp[q[n-i].id];

w[i]=ww[q[n-i].id];

sortX[i]=q[n-i].id;

}

backtrack(1);//回溯搜索

return bestp;

}

private static void backtrack(int i){

if(i>=n){

if(cp>bestp){

bestp=cp;

for(int j=1;j<=n;j++){

bestX[j]=x[j];

}

}

return;

5 / 7

}

//搜索子树

if(cw+w[i]<=c){

//进入左子树

x[sortX[i]]=1;

cw+=w[i];

cp+=p[i];

backtrack(i+1);

cw-=w[i];

cp-=p[i];

}

if(bound(i+1)>bestp)

x[sortX[i]]=0;

backtrack(i+1);//进入右子树

}

//计算上界

private static double bound(int i){

double cleft=c-cw;

double bound=cp;

//以物品重量价值递减顺序装入物品

while(i<=n&&w[i]<=cleft){

6 / 7

cleft-=w[i];

bound+=p[i];i++;

}

//装满背包

if(i<=n)

bound+=p[i]/w[i]*cleft;

return bound;

}

public static String getX(){

String solution=f(bestX[1]);

for(int i=2;i<;i++){

solution+=",";

solution+=f(bestX[i]);

}

return solution;

}

public static double getBestValue(){

return bestp;

}

}

主程序运行结果:

7 / 7

2023年8月1日发(作者:)

0—1背包问题

一、实验目的

学习掌握回溯思想。

二、实验内容

用回溯求解0—1背包问题,并输出问题的最优解。0—1背包问题描述如下:

给定n种物品和一背包。物品i的重量是Wi,其价值为Vi,背包的容量是c,问应如何选择装入背包中的物品,使得装入背包中物品的总价值最大。

三、实验条件

Jdk1.5以上

四、需求分析

对于给定n种物品和一背包。在容量最大值固定的情况下,要求装入的物品价值最大化。

五、回溯法的基本思想:

确定了解空间的组织结构后,回溯法就从开始结点(根结点)出发,以深度优先的方式搜索整个解空间。这个开始结点就成为一个活结点,同时也成为当前的扩展结点。在当前的扩展结点处,搜索向纵深方向移至一个新结点。这个新结点就成为一个新的活结点,并成为当前扩展结点。如果在当前的扩展结点处不能再向纵深方向移动,则当前扩展结点就成为死结点。

换句话说,这个结点不再是一个活结点。此时,应往回移动(回溯)至最近的一个活结点处,并使这个活结点成为当前的扩展结点。回溯法即以这种工作方式递归地在解空间中搜索,直至找到所要求的解或解空间中已没有活结点时为止。

六、详细设计

1 / 7

/*

*

*

* Created on 2007年6月2日,下午6:09

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package sunfa;

import ;

public class BackTrace {

/**

* @param args

*/

public static void main(String[] args) {

double w[]={2,2,6,5,4};

double v[]={6,3,5,4,6};

int n=5;

double c=10;

knapsack(v,w,c);

n(bestp);

2 / 7

}

//比较两个元素大小的类

private static class Element implements Comparable{

int id;

double d;

private Element(int idd,double dd){

id=idd;

d=dd;

}

public int compareTo(Object x){

double xd=((Element)x).d;

if(d

if(d==xd)return 0;

return 1;

}

public boolean equals(Object x){

return d==((Element)x).d;

}

}

static double c; //背包容量

static int n;//物品数

3 / 7

static double[]w;//物品重量数组

static double[]p; //物品价值数组

static double cw;//当前重量

static double cp;//当前价值

static double bestp; //当前最优值

static int [] x;//解

static int [] sortX;//排好序之后的解

static int [] bestX;//最有解

static Date date = null;//@jve:decl-index=0:

public static double knapsack(double[]pp,double[]ww,double cc){

c=cc;

n=-1;

cw=0.0;

cp=0.0;

bestp=0.0;

Element[]q=new Element[n];

//q为单位重量价值数组

for(int i=1;i<=n;i++)

q[i-1]=new Element(i,pp[i]/ww[i]);

ort(q);

p=new double[n+1];

4 / 7

w=new double[n+1];

x=new int[n+1];

sortX=new int[n+1];

bestX=new int[n+1];

for(int i=1;i<=n;i++){

p[i]=pp[q[n-i].id];

w[i]=ww[q[n-i].id];

sortX[i]=q[n-i].id;

}

backtrack(1);//回溯搜索

return bestp;

}

private static void backtrack(int i){

if(i>=n){

if(cp>bestp){

bestp=cp;

for(int j=1;j<=n;j++){

bestX[j]=x[j];

}

}

return;

5 / 7

}

//搜索子树

if(cw+w[i]<=c){

//进入左子树

x[sortX[i]]=1;

cw+=w[i];

cp+=p[i];

backtrack(i+1);

cw-=w[i];

cp-=p[i];

}

if(bound(i+1)>bestp)

x[sortX[i]]=0;

backtrack(i+1);//进入右子树

}

//计算上界

private static double bound(int i){

double cleft=c-cw;

double bound=cp;

//以物品重量价值递减顺序装入物品

while(i<=n&&w[i]<=cleft){

6 / 7

cleft-=w[i];

bound+=p[i];i++;

}

//装满背包

if(i<=n)

bound+=p[i]/w[i]*cleft;

return bound;

}

public static String getX(){

String solution=f(bestX[1]);

for(int i=2;i<;i++){

solution+=",";

solution+=f(bestX[i]);

}

return solution;

}

public static double getBestValue(){

return bestp;

}

}

主程序运行结果:

7 / 7