Example: tourism industry

Lecture 13: The Knapsack Problem - Electronic Systems

Lecture13:TheKnapsack ProblemOutlineofthisLecture Introductionof the0-1 Knapsack Problem . A dynamicprogrammingsolutionto ProblemInformalDescription:We have computed datafilesthatwe wantto store, andwe have available bytesof hassize bytesandtakes wanttoavoidasmuchrecomputingaspossible,s owe wantto finda subsetof filesto storesuchthat Thefileshave combinedsize at most . cannotstorepartsoffiles, it is shouldwe selectthefiles?20-1 Knapsack ProblemFormaldescription:Giventwo -tuplesofpositivenumbers and and , we wishto determinethesubset !#" %$& ' ( *)(offilesto store)thatmaximizes ,+ - . subjectto ,+ - 0/ Remark:Thisis :Try all$ possible subsets .Question:Any solutionbetterthanthebrute-force? solve :If thesubproblemsarenotindependent, , thena , it doesmorework thannecessary!Question:Any bettersolution?

Lecture 13: The Knapsack Problem Outline of this Lecture Introduction of the 0-1 Knapsack Problem. A dynamic programming solution to this problem.

Tags:

  Programming, Dynamics, Problem, Knapsack, Dynamic programming, Knapsack problem

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Lecture 13: The Knapsack Problem - Electronic Systems

1 Lecture13:TheKnapsack ProblemOutlineofthisLecture Introductionof the0-1 Knapsack Problem . A dynamicprogrammingsolutionto ProblemInformalDescription:We have computed datafilesthatwe wantto store, andwe have available bytesof hassize bytesandtakes wanttoavoidasmuchrecomputingaspossible,s owe wantto finda subsetof filesto storesuchthat Thefileshave combinedsize at most . cannotstorepartsoffiles, it is shouldwe selectthefiles?20-1 Knapsack ProblemFormaldescription:Giventwo -tuplesofpositivenumbers and and , we wishto determinethesubset !#" %$& ' ( *)(offilesto store)thatmaximizes ,+ - . subjectto ,+ - 0/ Remark:Thisis :Try all$ possible subsets .Question:Any solutionbetterthanthebrute-force? solve :If thesubproblemsarenotindependent, , thena , it doesmorework thannecessary!Question:Any bettersolution?

2 Yes Dynamicprogramming(DP)!4 TheIdeaofDynamicProgrammingDynamicprogra mmingis a :Computethesolutionsto thesubsub-problemsonceandstorethesolutio nsina table, sothattheycanbereused(repeatedly) :We DPAlgorithmStep1:Structure:Characterize thestructureofanoptimalsolution. Decomposetheproblemintosmallerproblems,a ndfinda relationbetweenthestructureoftheoptimals olutionoftheoriginalproblemandthesolutio nsof :Principleof Optimality:Recursivelydefinethevalueof anoptimalsolution. Expressthesolutionoftheoriginalproblemin termsof DPAlgorithmStep3:Bottom-upcomputation:Co mputethevalueofanoptimalsolutionina bottom-upfashionbyusinga table : and4 may Steps1-3form thebasisof a dynamic-programmingsolutionto a Problem . Step4 canbeomittedif onlythevalueof anopti-malsolutionis DPAlgorithmforKnapsackStep1 constructanarray12 3 4 5 3 "/ / , and / / , theentry127 8 ( 6willstorethemaximum(combined)computingt imeof any subsetof files!)

3 #" %$& ( 9)of (combined)size at most .If we cancomputealltheentriesof thisarray, thenthearray entry127 5 6willcontainthemaximumcomputingtimeof filesthatcanfit intothestorage, thatis, thesolutionto DPAlgorithmforKnapsackStep2:Recursivelyd efinethevalueofanoptimalsolutionin termsof solutionsto :Set12 : 60; for / / ,noitem12< 8 65;=>for ? ,illegalRecursiveStep:Use127 8 ( 65;@ACBED127 =" 6 ( (F127 =" ( = "/ / , / / .10 CorrectnessoftheMethodforComputing127 8 ( 6 Lemma:For"/ / , / / ,127 8 ( 6H;@ACBED127 =" : 6 F127 =" = 6G Proof:To compute12< 8 6we notethatwe have onlytwo choicesforfile :Leavefile :Thebestwe candowithfiles!#" %$& ( =")andstoragelimit is127 =" 8 (onlypossibleif I / ):Thenwe gain ofcomputingtime, buthave spent bytesofourstorage. Thebestwe candowithremainingfiles!J" %$K =")andstorageD = Gis127 =" = , we get L F127 =" : = , then F127 =" 8 = 60;=>sothelemmais correctin any DPAlgorithmforKnapsackStep3:Bottom-upcom puting127 8 6(usingiteration,notrecursion).)))))

4 Bottom:12 6H; forall / / .Bottom-upcomputation:Computingthetable using12< 8 6H;@AMBED12< =" 6 : NF12< =" = by 0V[i,w]w= ;" and 1234 10403050 5463OP ,QSRUTV12345678910 XWV0000000000010000010101010101020000404 0404040505030000404040404050704000505050 5090909090 Remarks:YThefinaloutputisOP[ZQ\ V]TXW^ (Itis` CQZbain thisexample).13 TheDynamicProgrammingAlgorithmKnapSack(c QSRdQ Qfe)gfor(RWVtoe)OPVCQhRiT WV;for( W to )for(RWVtoe)if (RP 3T jR)OP ,QkRUT Wlmon`OP Mp qQhRUTrQcP 3 TtsOP Mp qQhRpRP 3T3Ta;elseOP ,QkRUT WOIP Mp qQhRUT;returnOP QueT;vTimecomplexity:Clearly,wD Thealgorithmforcomputing127 8 6describedinthepreviousslidedoesnotkeepr ecordofwhichsubsetof itemsgivestheoptimalsolution. To computetheactualsubset,we canaddanauxiliary booleanarrayx#y]y(z*27 8 { 6whichis 1 if wedecidetotake the -thfilein12< 8 6and0 :How dowe useallthevaluesx#y]y(z*2< 8 6todeterminethesubset offileshavingthemaximumcomputingtime?))}

5 15 ConstructingtheOptimalSolutionQuestion:H owdowe usethevaluesx#yqy(z|27 8 % 6todeterminethesubset of itemshavingthemaximumcomputingtime?If keep[ 5 ] is 1,then } . We cannow repeatthisargumentforkeep[ =" = ].If keep[ H ] is 0,the ~} andwe repeattheargu-mentforkeep[ =" ].Therefore, thefollowingpartialprogramwilloutputthee lementsof : ; ;for( ; downto1)if (keep2< 8 60;;") outputi; ; = 27 6; 16 TheCompleteAlgorithmfortheKnapsack ProblemKnapSack(cQSRdQ Qfe)gfor(RWVtoe)OPVMQhRUT WV;for( W to )for(RWVtoe)if ((RP 3 TXjR) and(cP 3 TtsOP Cp qQhRpRP 3T3T OP Mp qQhRUT))gOIP ,QhRUT WcP 3 TtsOP Cp qQhRpRP 3T3T;keepP ,Q RUT W ;velsegOIP ,QhRUT WOP Mp qQhRUT;keepP ,Q RUT WV;v We;for( W downto1)if (keepP ,Q TJWW )goutputi; W pRP 3T;vreturnOP Q eT;v17)


Related search queries