0% found this document useful (0 votes)
13 views

LP3 Assignment

Assignment

Uploaded by

TORNADO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

LP3 Assignment

Assignment

Uploaded by

TORNADO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

BE-A-34

kos K. K. Wagh Institute of Engineering Edu. &Research / Polytechnic, Nashik -3

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

By taking items of weiga te and 2o Kg and /s trachiona.


K. K. Wagh Institute of Engineering Edu. &Research / Polytechnic, Nashik - 3

Here tatal proft ill be éo tioo-t2x 120) =240


Solution using Greedy algoithm: The basic idea is to
calculate the raho pYokit/oeigkt tor cach item d sort
the item_on fhe basis of thig ratio Then take the
item with highest ratio &add themn cs much as we can
This oill aluans give the maximum proit becausein
each step it adds an elem ent euch that this is the
maximum possible protit fox that much uweig ht.

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

Expected Qutput i Maximunn poht = S1


Achual output: Maxi mum_ pnkt 51

status: pas5:
Tnputi
Lobfects 4 S

Proit 3 2 S1
2 8

Expected autputi Maximum panht =31


Acual output t maximum pxofit =91
Status ags
Conclusian: Hence greedy algorithm Can be sucesstully
used to implement a Galuhon tor the frachional knapsack
problen
BE-9-31

k K. K. Wagh Institute of Engineering Edu. &Research / Polytechnic, Nashik -3

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

Software/ Haxdware Requrements: Ct+ Compiler, Text


editoy

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

The above tunction using recursion Computes the Same


Sar
Sub problem9. again d&again, To avaid this
we get a suboblem the irst time we can
9olve this pToblem by creahing a 2-0 array that can
Gtore a paricul qr sBate Cnw) Noo i4 we come
Qcross the Same state Cnw)again instead ot
calculaing itin exponential complexiy we Can
directly return its result stored in the table in
Constant ime

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

Expected solution: weigkts fi,73


Moximum pxoit=12.
Actual Solution: weights 21,13
Maximum prit =12
Status : ass
Condusion:

Hdynamic programming aproach can be used to impkment


a solutionfoy 0-1 knapsack problem.
Time 0CN* W) As redundant calcudation of
camglodty
0t states are avoided

You might also like