欢迎来到天天文库
浏览记录
ID:11603489
大小:67.00 KB
页数:4页
时间:2018-07-12
《回溯算法之01背包问题java源程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验报告11课程数据结构与算法实验名称回溯法第页班级11计本学号105032011130姓名风律澈实验日期:2013年5月20日报告退发(订正、重做)一、实验目的掌握回溯法的原理和应用。二、实验环境1、微型计算机一台2、WINDOWS操作系统,JavaSDK,Eclipse开发环境三、实验内容必做题:1、编写程序,实现n后问题算法。(如果上周没完成,这周完成)2、编写程序,采用回溯法实现0-1背包问题。四、实验步骤和结果(附上代码和程序运行结果截图)2、0-1回溯背包问题importjava.util.Ar
2、rayList;importjava.util.PriorityQueue;publicclassmain{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstu//初始化工作baga=newbag(7);ArrayListobjects=newArrayList();PriorityQueuepq=newPriorityQueue();i
3、ntv[]={9,10,7,4};intw[]={3,5,2,1};for(inti=0;i4、s,a);//输出System.out.println(a.getmaxvalue());}privatestaticvoidbacktrack(inti,ArrayListobjects,baga){//TODOAuto-generatedmethodstubif(i>objects.size())//叶子{a.setmaxvaule(a.getvalue());return;}if(a.getcontain()+objects.get(i-1).getweight()<=a.getmax5、weight()){a.addobject(objects.get(i-1));backtrack(i+1,objects,a);a.removeobjec(objects.get(i-1));}if(bound(i+1,objects,a)>a.getmaxvalue()){backtrack(i+1,objects,a);}}privatestaticintbound(inti,ArrayListobjects,baga){//TODOAuto-generatedmethodstubin6、tleftcontain=a.getmaxweight()-a.getcontain();intbound=a.getvalue();while((i<=objects.size())&&(objects.get(i-1).getweight()<=leftcontain)){leftcontain-=objects.get(i-1).getweight();bound+=objects.get(i-1).getvalue();i++;}if(i<=objects.size())bound+=objects7、.get(i-1).getvalue()*leftcontain/objects.get(i-1).getweight();returnbound;}}importjava.util.ArrayList;publicclassbag{privateintcontain;privateintvalue;privateintmaxweight;privateintmaxvalue;privateArrayListobjects;publicbag(intmaxw){this.contain=0;8、this.value=0;this.maxweight=maxw;this.maxvalue=0;this.objects=newArrayList();}publicintgetcontain(){returncontain;}publicvoidresetcontain(intc){this.contain=c;}publicintgetvalue(){returnvalue;};public
4、s,a);//输出System.out.println(a.getmaxvalue());}privatestaticvoidbacktrack(inti,ArrayListobjects,baga){//TODOAuto-generatedmethodstubif(i>objects.size())//叶子{a.setmaxvaule(a.getvalue());return;}if(a.getcontain()+objects.get(i-1).getweight()<=a.getmax
5、weight()){a.addobject(objects.get(i-1));backtrack(i+1,objects,a);a.removeobjec(objects.get(i-1));}if(bound(i+1,objects,a)>a.getmaxvalue()){backtrack(i+1,objects,a);}}privatestaticintbound(inti,ArrayListobjects,baga){//TODOAuto-generatedmethodstubin
6、tleftcontain=a.getmaxweight()-a.getcontain();intbound=a.getvalue();while((i<=objects.size())&&(objects.get(i-1).getweight()<=leftcontain)){leftcontain-=objects.get(i-1).getweight();bound+=objects.get(i-1).getvalue();i++;}if(i<=objects.size())bound+=objects
7、.get(i-1).getvalue()*leftcontain/objects.get(i-1).getweight();returnbound;}}importjava.util.ArrayList;publicclassbag{privateintcontain;privateintvalue;privateintmaxweight;privateintmaxvalue;privateArrayListobjects;publicbag(intmaxw){this.contain=0;
8、this.value=0;this.maxweight=maxw;this.maxvalue=0;this.objects=newArrayList();}publicintgetcontain(){returncontain;}publicvoidresetcontain(intc){this.contain=c;}publicintgetvalue(){returnvalue;};public
此文档下载收益归作者所有