欢迎来到天天文库
浏览记录
ID:14640549
大小:67.00 KB
页数:4页
时间:2018-07-29
《回溯算法之01背包问题java源程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验报告11课程数据结构与算法实验名称回溯法第页班级11计本学号105032011130姓名风律澈实验日期:2013年5月20日报告退发(订正、重做)一、实验目的掌握回溯法的原理和应用。二、实验环境1、微型计算机一台2、WINDOWS操作系统,JavaSDK,Eclipse开发环境三、实验内容必做题:1、编写程序,实现n后问题算法。(如果上周没完成,这周完成)2、编写程序,采用回溯法实现0-1背包问题。四、实验步骤和结果(附上代码和程序运行结果截图)2、0-1回溯背包问题importjava.util.ArrayList;importjava.util.Pr
2、iorityQueue;publicclassmain{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstu//初始化工作baga=newbag(7);ArrayListobjects=newArrayList();PriorityQueuepq=newPriorityQueue();intv[]={9,10,7,4};intw[]={3,5,2,1};for(inti=0;i3、gth;i++){pq.add(newobject(v[i],w[i]));objects.add(newobject(v[i],w[i]));}//生成单位价值排序数组,将物品以单位价值从高到低排列,记录数组intvw[]=newint[v.length];for(inti=0;i4、ArrayListobjects,baga){//TODOAuto-generatedmethodstubif(i>objects.size())//叶子{a.setmaxvaule(a.getvalue());return;}if(a.getcontain()+objects.get(i-1).getweight()<=a.getmaxweight()){a.addobject(objects.get(i-1));backtrack(i+1,objects,a);a.removeobjec(objects.get(i-1));}if(bou5、nd(i+1,objects,a)>a.getmaxvalue()){backtrack(i+1,objects,a);}}privatestaticintbound(inti,ArrayListobjects,baga){//TODOAuto-generatedmethodstubintleftcontain=a.getmaxweight()-a.getcontain();intbound=a.getvalue();while((i<=objects.size())&&(objects.get(i-1).getweight()<=leftc6、ontain)){leftcontain-=objects.get(i-1).getweight();bound+=objects.get(i-1).getvalue();i++;}if(i<=objects.size())bound+=objects.get(i-1).getvalue()*leftcontain/objects.get(i-1).getweight();returnbound;}}importjava.util.ArrayList;publicclassbag{privateintcontain;privateintvalue;priva7、teintmaxweight;privateintmaxvalue;privateArrayListobjects;publicbag(intmaxw){this.contain=0;this.value=0;this.maxweight=maxw;this.maxvalue=0;this.objects=newArrayList();}publicintgetcontain(){returncontain;}publicvoidresetcontain(intc){this.contain=c;}publicintgetva8、lue(){returnvalue;};public
3、gth;i++){pq.add(newobject(v[i],w[i]));objects.add(newobject(v[i],w[i]));}//生成单位价值排序数组,将物品以单位价值从高到低排列,记录数组intvw[]=newint[v.length];for(inti=0;i4、ArrayListobjects,baga){//TODOAuto-generatedmethodstubif(i>objects.size())//叶子{a.setmaxvaule(a.getvalue());return;}if(a.getcontain()+objects.get(i-1).getweight()<=a.getmaxweight()){a.addobject(objects.get(i-1));backtrack(i+1,objects,a);a.removeobjec(objects.get(i-1));}if(bou5、nd(i+1,objects,a)>a.getmaxvalue()){backtrack(i+1,objects,a);}}privatestaticintbound(inti,ArrayListobjects,baga){//TODOAuto-generatedmethodstubintleftcontain=a.getmaxweight()-a.getcontain();intbound=a.getvalue();while((i<=objects.size())&&(objects.get(i-1).getweight()<=leftc6、ontain)){leftcontain-=objects.get(i-1).getweight();bound+=objects.get(i-1).getvalue();i++;}if(i<=objects.size())bound+=objects.get(i-1).getvalue()*leftcontain/objects.get(i-1).getweight();returnbound;}}importjava.util.ArrayList;publicclassbag{privateintcontain;privateintvalue;priva7、teintmaxweight;privateintmaxvalue;privateArrayListobjects;publicbag(intmaxw){this.contain=0;this.value=0;this.maxweight=maxw;this.maxvalue=0;this.objects=newArrayList();}publicintgetcontain(){returncontain;}publicvoidresetcontain(intc){this.contain=c;}publicintgetva8、lue(){returnvalue;};public
4、ArrayListobjects,baga){//TODOAuto-generatedmethodstubif(i>objects.size())//叶子{a.setmaxvaule(a.getvalue());return;}if(a.getcontain()+objects.get(i-1).getweight()<=a.getmaxweight()){a.addobject(objects.get(i-1));backtrack(i+1,objects,a);a.removeobjec(objects.get(i-1));}if(bou
5、nd(i+1,objects,a)>a.getmaxvalue()){backtrack(i+1,objects,a);}}privatestaticintbound(inti,ArrayListobjects,baga){//TODOAuto-generatedmethodstubintleftcontain=a.getmaxweight()-a.getcontain();intbound=a.getvalue();while((i<=objects.size())&&(objects.get(i-1).getweight()<=leftc
6、ontain)){leftcontain-=objects.get(i-1).getweight();bound+=objects.get(i-1).getvalue();i++;}if(i<=objects.size())bound+=objects.get(i-1).getvalue()*leftcontain/objects.get(i-1).getweight();returnbound;}}importjava.util.ArrayList;publicclassbag{privateintcontain;privateintvalue;priva
7、teintmaxweight;privateintmaxvalue;privateArrayListobjects;publicbag(intmaxw){this.contain=0;this.value=0;this.maxweight=maxw;this.maxvalue=0;this.objects=newArrayList();}publicintgetcontain(){returncontain;}publicvoidresetcontain(intc){this.contain=c;}publicintgetva
8、lue(){returnvalue;};public
此文档下载收益归作者所有