ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:59.64KB ,
资源ID:404454      下载积分:12 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/404454.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(算法01背包问题.docx)为本站会员(b****2)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

算法01背包问题.docx

1、算法01背包问题一、实验目的与要求掌握回溯法、分支限界法的原理,并能够按其原理编程实现解决0-1背包问题,以加深对回溯法、分支限界法的理解。1 要求分别用回溯法和分支限界法求解0-1背包问题;2 要求交互输入背包容量,物品重量数组,物品价值数组;3 要求显示结果。二、实验方案在选择装入背包的物品时,对每种物品i只有2种选择,即装入背包或不装入背包。不能将物品i装入背包多次,也不能只装入部分的物品i。三、实验结果和数据处理 1用回溯法解决0-1背包问题:代码:import java.util.*;public class Knapsack private double p,w;/分别代表价值和重

2、量 private int n; private double c,bestp,cp,cw; private int x; /记录可选的物品 private int cx; public Knapsack (double pp,double ww,double cc) this.p=pp;this.w=ww;this.n=pp.length-1; this.c=cc;this.cp=0;this.cw=0; this.bestp=0; x=new intww.length; cx=new intpp.length; void Knapsack() backtrack(0); void back

3、track(int i) if(in) /判断是否到达了叶子节点 if(cpbestp) for(int j=0;jx.length;j+) xj=cxj; bestp=cp; return; if(cw+wi=c) /搜索右子树 cxi=1; cw+=wi; cp+=pi; backtrack(i+1); cw-=wi; cp-=pi; cxi=0; backtrack(i+1); /检查左子树 void printResult() System.out.println(回溯法); System.out.println(物品个数:n=4); System.out.println(背包容量:c

4、=7); System.out.println(物品重量数组:w= 3,5,2,1); System.out.println(物品价值数组:p= 9,10,7,4); System.out.println(最优值:=+bestp); System.out.println(选中的物品是:); for(int i=0;ix.length;i+) System.out.print(xi+ ); public static void main(String args) double p=9,10,7,4; double w=3,5,2,1; int maxweight=7; Knapsack ks=n

5、ew Knapsack(p,w,maxweight); ks.Knapsack(); /回溯搜索 ks.printResult(); 运行结果:2用优先队列式分支限界法解决0-1背包问题:代码:public class Knapsack static double c; static int n; static double w; static double p; static double cw; static double cp; static int bestX; static MaxHeap heap; /上界函数bound计算结点所相应价值的上界 private static dou

6、ble bound(int i) double cleft=c-cw; double b=cp; while(i=n&wi=cleft) cleft=cleft-wi; b=b+pi; i+; /装填剩余容量装满背包 if(i=n) b=b+pi/wi*cleft; return b; /addLiveNode将一个新的活结点插入到子集树和优先队列中 private static void addLiveNode(double up,double pp,double ww,int lev,BBnode par,boolean ch) /将一个新的活结点插入到子集树和最大堆中 BBnode b=

7、new BBnode(par,ch); HeapNode node =new HeapNode(b,up,pp,ww,lev); heap.put(node); private static double MaxKnapsack() /优先队列式分支限界法,返回最大价值,bestx返回最优解 BBnode enode=null; int i=1; double bestp=0;/当前最优值 double up=bound(1);/当前上界 while(i!=n+1)/非叶子结点 /检查当前扩展结点的左儿子子结点 double wt=cw+wi; if(wtbestp) bestp=cp+pi;

8、 addLiveNode(up,cp+pi,cw+wi,i+1,enode,true); up=bound(i+1); if(up=bestp) addLiveNode(up,cp,cw,i+1,enode,false); HeapNode node =(HeapNode)heap.removeMax(); enode=node.liveNode; cw=node.weight; cp=node.profit; up=node.upperProfit; i=node.level; for(int j=n;j0;j-) bestXj=(enode.leftChild)?1:0; enode=en

9、ode.parent; return cp; public static double Knapsack(double pp,double ww,double cc,int xx) /返回最大值,bestX返回最优解 c=cc; n=pp.length-1; /定义以单位重量价值排序的物品数组 Element q=new Elementn; double ws=0.0; double ps=0.0; for(int i=0;in;i+) qi=new Element(i+1,ppi+1/wwi+1); ps=ps+ppi+1; ws=ws+wwi+1; if(ws=c) return ps;

10、p=new doublen+1; w=new doublen+1; for(int i=0;in;i+) pi+1=ppqi.id; wi+1=wwqi.id; cw=0.0; cp=0.0; bestX = new intn+1; heap = new MaxHeap(n); double bestp = MaxKnapsack(); for(int j=0;jn;j+) xxqj.id=bestXj+1; return bestp; public static void main(String args) double w=new double5; w1=3;w2=5;w3=2;w4=1;

11、 double p=new double5; p1=9;p2=10;p3=7;p4=4; double c=7; int x = new int5; double m = Knapsack(p,w,c,x); System.out.println(优先队列式分支限界法:); System.out.println(物品个数:n=4); System.out.println(背包容量:c=7); System.out.println(物品重量数组:w= 3,5,2,1); System.out.println(物品价值数组:p= 9,10,7,4); System.out.println(最优值:

12、=+m); System.out.println(选中的物品是:); for(int i=1;i=4;i+) System.out.print(xi+ ); /子空间中节点类型class BBnode BBnode parent;/父节点 boolean leftChild;/左儿子节点标志 BBnode(BBnode par,boolean ch) parent=par; leftChild=ch; class HeapNode implements Comparable BBnode liveNode; / 活结点 double upperProfit; /结点的价值上界 double p

13、rofit; /结点所相应的价值 double weight; /结点所相应的重量 int level; / 活结点在子集树中所处的层次号 /构造方法 public HeapNode(BBnode node, double up, double pp , double ww,int lev) liveNode = node; upperProfit = up; profit = pp; weight = ww; level = lev; public int compareTo(Object o) double xup = (HeapNode)o).upperProfit; if(upperProfit xup) return -1; if(upperProfit = xup) return 0; else return 1; class Element implements Comparable int id; double d; public Element(int idd,double dd) id=idd; d=dd; public int compareTo(Object x) double xd=(Element)x)

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1