LP3 Assignment
LP3 Assignment
tssignment-3
Iite: Fractional Knapsack prablem
Problem Statement: Write a program to solve a frachonal
knapsack problem using qreeds methad.
Sof hoareHaxdaare requrements: C++Compiler, Text editor.
Than
Fracional knapsackprohlenn: Given the weights and proits
ot Nitenas in the form ot Aprathteight put fhese
items in a knapaack of capacity o' to qet maximum
total proit in the knapsack- IN frachion al Khapsack, L)e
Can break items fox maximizing the total value o the
Knapsack
a MaX weigkt = So
Pacht 60 L00 120
10 80
Algarithng
1Calcuate the ratio Cprokt/weight) fo each item
2-Sort all the items in the decreasing onder ot the ratio
3.Inithalize reg =0cuYY Cap =giYen-cap.
4. Do the tollaoing tor every item i in the Soted ardert
-TA the weigkt of the current item is less fhanor
eual to tne remaining capacity then add the
t yalue
ot that item into the resut
-Else add the CUrrent item ag much as oe Can k
break out od the loop
S.Return res.
Test Cases :
K. K. Wagh Institute of Engineering Edu. &Research / Polytechnic, Nashik -3
. Input:
Ohjects 1 2 3 4 6
Profit 5 15
1 S 4 3 2
status: pas5:
Tnputi
Lobfects 4 S
Proit 3 2 S1
2 8
Assiqnment
ITtle: 0-1 Knapsack problen
Problem Statement: White a program to solye a o-1
KnapSack prablem using dynamic Qrogramming ar
branch and bound
Theomyi
0-1 Knapsatk prohlem Griveh Nitem3_ ohere each item
has gome weight and profit as90ciated with itand also
given a bag oith capacity The task is to puct the
items fnti the bag suh that the Aunn oB profi+s
La9s0ciated oith them is the maximnum possible.
-The Constraint here is we can either put an item Camp
letely into the bag 0Y cannot put it at all
Prokst Maximum = 4
hcighe 4 1 weight
K. K. Wagh Institute of Engineering Edu. &Research / Polytechnic,Nashik -3
Output:9
Dynamie pxogxamming approach : Asimple.salution is.
to Considerali subsets ofitems calculate total weigkt
Aprohit of all subsets. Consider the only subsets whose
total weight is less than W. Erom al such subsets,
pick the Subset with maximum prohit
Ophmal Swbstructure , To consider all subsets otitems,
there can be too subcases for every tem,
- Case 1i The item i includedin opimal ub set
- Case9: The item ig not included in ophmal 6ubset
Agarithrn:
o K. K. Wagh Institute of Engineering Edu. &Research / Polytechnic, Nashik -3
Tnput: Wtet
Maximum weight
Number ofitema N
w<W,,w2,->
Dynamic-o-1-knapsack (Viw,n,W)
for w=o to hdo
toy i= 1 ton do
cCi,o1E0
0 f o Y )= 1 to W doehe
it 0f <w then
if yi tc[i-1,w -w1] then.
CCi,w] = Yit cCt-1, w-w]
else
eci,o]= cCi-1,w]
ele
Test Cases:
Input:
Item 2 4
Pookit 4 10
1 3
o K. K. Wagh Institute of Engineering Edu. &Research / Polytechnic, Nashik -3