回溯法、分支限界法解0-1背包問題(計(jì)算機(jī)算法設(shè)計(jì)與分析實(shí)驗(yàn)(精)_第1頁
回溯法、分支限界法解0-1背包問題(計(jì)算機(jī)算法設(shè)計(jì)與分析實(shí)驗(yàn)(精)_第2頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、實(shí)驗(yàn)報(bào)告課程名稱:算法設(shè)計(jì)與分析 實(shí)驗(yàn)名稱:回溯法、分支限界法解 0-10-1 背包問題 任 課教師:張錦雄專業(yè):計(jì)算機(jī)科學(xué)與技術(shù) 班級(jí):2007:2007 級(jí) 1 1 班學(xué)號(hào):姓名: 藍(lán)冠恒完成日期:20112011 年 1 1 月 1212 日、實(shí)驗(yàn)?zāi)康模簯T擁創(chuàng)測(cè)法、彷支就界法的庶岬月能夠按攤脈殲軸用賓現(xiàn)解決 61 弁包的恿.W 加洋對(duì)冋淵決、分支臥界沐的則解”-】浚實(shí)凝內(nèi)容及建求:L蔓求分別用冋潮法和分址限界法求解(M祈包樹嗨:2.要忒交互輪入背也壽赧物舶電皿獨(dú)41 杓品仰值敵齟; 乩整求顯示踽果匡實(shí)驗(yàn)環(huán)境和壬具;援件家境叫i眉操作蘇瓏卄址1 Hi Ck?lipsc.V4 . |dk l

2、.b* javaw、實(shí)齡給果 m 外也 (緒調(diào)試IF確的訓(xùn)碼庁;和和用的運(yùn)fr給果L k冋溯法求解0T背包問題源代碼:p kag uru JLgh;.Lapox LJ AVA. j_o. Buf f.*Ledfte&der;114(1 curtjwr Inputr r iamP tad*!;t npor tjav. uti丄inpoxt j A V u LLtiX丄.L? GIBip A EI 鼻斗*HWWo-il*0 diitllor醞諒機(jī) Jpublic cla修TKhapvAfk |dmiiil豺c; .-*1int n; /double J対丨doubleFE,hHdouble

3、 cw; /!川:itdouble cp; / l滬:也QparAHIccOrel urn対優(yōu)價(jià)(ftfor (ini i = 0; i n; )(q (i.) = new element (x 1, pp (x / ww 1;Arrays borx( (q n;IbacktEAck(1;retttrn bestp;Idouble btp; , I;l ”/*何欄eparaai pp鞫胡價(jià)伯敕姐OparAMwpublic double knapsack(double pp9double ww(9double cc) c = cc;n = pp丄ength;cw = 0.0;cp 0.0;bet

4、p = 0.0;Element I) q = new Elemen(n;p nw doublen 1;w = new doublen令丄;for (int x = 1; i n; i*)(P( (iJ PPq(i - 11. id - 1);w(i) = ww(q(i - IJ.id - 1);/ HNHfVprivate void backtrack(int i)(if n) (/ S! W bestp = up;return;if b tp)( backtrack(1 i;1/ private double bound(int i)( doub丄0 cleft = u cw;double

5、bound - cp;/以檎此3位篁罠價(jià)序您入物叢while (i 5 n 44 wi ccleft) cleftwi;bound = p(i;I/ ttiAHtiif (i (thi id id;thio.d d;itttaOautlior M.融卜public class EleaCompacatoc 1 implements CoaipArutor ( public int compare(Object objctl. Objact object2) 童丄nt rlBine) objeel;Element elwaentZ = (Bleaent) objectZ;if (leMtntl.

6、d 丄ntC.d(return 1; else (return 0;public static void a&xn(Stcxng ( ) acgs) I3ring input;String仝丄g;double capacity = 0;double( pp;doiibl() ww;double bestfD.0;BTKnapsack btKnapoacknew BTKnapsAck();But f eredftadE in = neM But ter edReadt: (new InputStceAmKeadti: (Syste in);do (try do (System, oue

7、. prxntlnf|f t(/功能fib JL檢入1HW 2-XllllJflag = ceadLineO)wKile (Hflag.equalB(wl) | | f l ;if (flag-equaliCC-)(break;do (Sy9te.0ue.prinein(-i,mAWlV “ StlHZ何必須以頓匕M利分H!);xnput = r eadLxne (tirun();input iri K dlin O * *;)whl(inpu qu丄8(*);if(input.equal(-2-)(break;1dtcjing datas ( = xnput 9p丄itd、);int nl

8、= datas丄ength;ppnew doublenl;ww=new double(nl;for (int 10; 1 nl;wwi* Doubl p4rAePoubJe(daasi);11。(ystni.oue.printanf入價(jià)仏IKIKZ同必縊以 radtxne() replaceAll(N, *:)wkl (input oqu丄(”;if (input equAla(*2w)(bTeak;1dataa input.)H; int n2 daA lenqh;丄f(nll=n)(System. oueprinln;continue;1for (xnt 1=0; i pp(i=Doubl

9、e p4r3eDoub2e(daa9x);1do |out. psxn.ln;)while (input qu丄(l9vv;it (input equals (二)(break;1capacityDoub丄( (input);bestP=btKnapsack knapsack(pptwwtcapacity);System, out. pcintln(:Utt.價(jià)W *bestP;)catch (exception )(e printStackTrace();)1.2、運(yùn)行結(jié)果:j 2; BTKMpsackJ“ 23 1 Outline 3、曰)1uakaao;: =:;4 1_ 九Q * 1:

10、 Problems Jovadpc edaratior Q Ccrap c 12=ns BTKnapsack Java Applction E:InsUll5 肖 *it .:;J / J : 青送擇數(shù)字功餵疑:1一諭入數(shù)扼一退出系塊 諸輸入各物品求蚩.鶉據(jù)之間必須以領(lǐng)號(hào)間隔分開!4SA&輛品席值.數(shù)據(jù)之閻必須以楓號(hào)間隔分開!ico. so if eo諾侑入背色的容豈,4:回浹住解得晶優(yōu)價(jià)俏:220.0渚送擇魏字功R:一諭入針據(jù)2遇出系統(tǒng)謁迭汗數(shù)宇功龍律:輸入歆帰,2週出系統(tǒng) 請(qǐng)籀入各物品重量,數(shù)撼之間必須以槓號(hào)間隔分開!iC. 0Mli. 2i. 10名輸入昔物品們值.的抿之間必須以

11、額邑間隔分幵!21D S0,沢1QG 20輸入背色的吝SH0回洌恙解得最優(yōu)價(jià)值,350.0二送庠齣字功iStt: 1一誡入埶療.2隕出系統(tǒng)2,1.分支柬界法求解0-1背包問題源代碼:package cn 丄gh;inport j ava丄o Bui fec edReadez;import j av I.Q InputdtceamKeades;iinport j av util鼻rfy;4/JCVAWiZiro-丄介包問Author iSJtHUpubl 1.Q clas8 BBKnapsack (double c;/ Hftl 1* uint n;】*i:double ) w; /吃;:i故糾

12、doublet) p;/小AA價(jià)fl. t*1l double cw; / i.卜;l double cp; / * t i ffr int ( brtx;/MaxHeap maxHeap = new MaxHeapO; hi :l. I計(jì)口n點(diǎn)所的B理的iw private double bound(int i)( double clft c - cw;double b = cp;/以的丄臥位耋鈦價(jià)位遞就整以創(chuàng)余滸址while (i = n 4 wi = cleft)( cleft w(i);b = pH);/if (i n) Ib pi Z wi) cleft;return b;/那如的応

13、*戍洌FQ:樹in優(yōu)尤敗列叩private void ddLiveNode(double uppecPeofit, doitble pp double ww, int levels BBnode parent. boolean lfChi丄d)(BBnode b new BBnode(parene, leftchild); HeapHode code = new HeapNode(b/uppecPcofit, pp. ww, level);maxHeap put(node);I/優(yōu)處隊(duì)列式分支界private double bbKnap ck()(BBnode enod null;int i

14、= 1;doiibl b#stp 0 0;double up bound(1);n點(diǎn)妁圧兒f VAif (wt (“ (cp pi btp bestp - cp p(x);*4iAl (i n * 1) Idouble wt cw w(i);ddliv Nodup. cp p(i, cw w(iri丄,enodtftrue);up = bound(i 1);T,n cbestp) AddtiveNod (up9up. cw#i 1, enoderf al ; 3-)(b tx(3 ( nod丄ftchild?1 : 0;tnode = enode將卜竹體依15他暇fit價(jià)他從人勢(shì)卜帥W 能4;

15、iBPUbbKn puk宅成U分支HBUlhfc. ereturn WttHiptiblic double knapsack (doublet pp. doubled wwrdouble cc9int ( xx)(c cc;n = pp丄ength;EL q = new Elenen( (n;double ws 0 0;doubl p9 0 0;for (int is0; i n; i*)( q(i) = new eieent;(i 丄.ppi / wwi); P 2 PP( (i; W8 = ww(i;11 (W8 for (int i - 1; i return ps;/依單位載就價(jià)tMI

16、序Arrays 3oxX(q. new KleuCoaiparator ( ); p = new doublen令丄】;w = new doublen丄;fox (int i 1; in; !)(p(i) = pptqli - 1.id - 1; w(i) = wwq(i - IJ.id - 1;Icw = 0.0;cp = 0.0; b J new int n 1;maxHtap ne%public static void naxn(Strxn9 argf)( Spring input;Sexing flag; double capacity=0; doublet pp; doublet w

17、w;Int (J xx;double b tP90 0;BBKnapaack bbKnap 2kfew BBKnapsack ();SyateM. out. prxnlnf入?yún)`ti的客!:u );xnput = m readtxncO trxa();inpu9in readLxne().replaceAll|* , *;)wfkLLe (input eqiut丄if(input.equals ;System. oue.println;System, out. printin(個(gè)被轉(zhuǎn)XWMM況丄&小嵌鐵入0農(nóng)不未帙錢入) Jfor (int i 0; i System out .prin

18、t丄)catch (Exception e)(o printStackTrce I wtiile (txue);) pckr i妊們/public C14S8 BBcode IBBnodt parent;/ 4 tiboolean leftChxld;/). i-public BBnode( BBnode patent#boolean leftChild( thi parentparen; thio.leftChildxleftChiid;pck9 cn.lgh;inport jav* uti丄.Covwparacor;ElementllSl比9utlor tfMtV/public clAfl

19、fl ElCompat:ACor iaplevents CompartorObj ( public lot coaipace(Object object!* Objectobject)( element elementl = (Blement)obj ectl;BleBtnt elem*nt2c(ELemenX)obj c2;if (lMbntl. d lmnc2 . d)(rctuxn 1;I丄(return 0;package cn丄gh; Bauthor收包術(shù)/public classIInt id;杓品隈;double d;人和;V IFpublic Bleaient (int xde

20、double d)( thi idid this d=d;package cn丄gh;import j av utxl.Compat:ator;MI 0山比B.4 author代址和public cla HpConp4t acor iapleMnto Comprtor ( public int compre(Object objecU* Objectobjec2)( HeapNode heApNodel = Heapt obj ectl;HeapNode heapNodeZ = (HeapNode)obj ect2;11 (heapNode丄.uppetPEofit hepNod2 uppex

21、Pr( return 1;I(xetuzn 0;package cn丄gh;/*MV A author酋丑帕/public cJLasB HeapNode (BBnode liveNodt; /人H!,double upperPcofxt; /1 I ! *double profx; /|J, *b;* !double weight; / i iInt level; (門沏4譏】/細(xì)&力注public HpNod(BBnod liveNodt, double upprProfi9double pcofi9dotable weight, nt丄v (th丄xveNode = liveNo

22、de;thi .upprProfi r npp*rProfit thl profit = profit;thi wexght=weight;thi level level;inport java整駛切仔円XpNode知嫁. Cautlior ifi1n/public clatfo MaxHeap /斥H點(diǎn)祚;Kprivate Li8t heapnew AcEAyList(); public void pu(MeapNode heapNode)(heap add(heapNode);public KeapNode rrmovMax()(Collectxons 9orr(hG p. new HedpCoapatatoc();HeapNode aiaxNGdenull;if(Iheap i epty()(nxNod(!P (0);h0請(qǐng)輸入背包的&

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論