0% found this document useful (0 votes)
7 views5 pages

DAA - Lab Manual - Exp - 5

Uploaded by

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

DAA - Lab Manual - Exp - 5

Uploaded by

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

SVKM’s NMIMS

MPSTME/School of Technology Management & Engineering,


Computer Engineering Department
Program: MBA Tech/BTECH. Sem IV

Design and Analysis of Algorithms


LAB Manual
PART A
(PART A: TO BE REFERRED BY STUDENTS)

Experiment No.05
A.1 Aim:
Analyze and implement the solution for Knapsack Problem using Greedy Technique.

Task1:

A thief is robbing a store and can carry a maximal weight of W into his knapsack.
There are n items available in the store and weight of i th item is wi and its profit is pi.
What items should the thief take?

In this context, the items should be selected in such a way that the thief will carry
those items for which he will gain maximum profit. Hence, the objective of the thief
is to maximize the profit.

A thief is robbing a store and has a knapsack that can carry a maximum weight of W
= 20 kg. There are 5 items available in the store, each with a specific weight and
corresponding profit, as shown in the table below:

Weight Profit
Item
(kg) (₹)

1 5 30

2 10 40

3 15 45

4 7 25

5 8 24
Using the Greedy Method, determine which items the thief should take to maximize
the total profit, considering that fractional parts of items can be taken (Fractional
Knapsack).

Task2: Implement the solution for the above Knapsack Problem using Greedy
Technique.

(Properly commented code with proper variable names)

A.2 Prerequisite:
1. Knowledge of Array Handling and

2. Concept of Greedy method

3. Knowledge of recursive programming.

A.3 Outcome:
After successful completion of this experiment students will be able to

1. Understand the concept of divide and conquer paradigm with the help
of Quick sort Algorithm
2. Write complex program based on recursion.

A.4 Theory:
A.4.1.
Fractional Knapsack:
In this case, items can be broken into smaller pieces, hence the thief can select
fractions of items. According to the problem statement,
 There are n items in the store
 Weight of ith item wi>0
 Profit for ith item pi>0 and
 Capacity of the Knapsack is W

In this version of Knapsack problem, items can be broken into smaller pieces. So, the
thief may take only a fraction xi of ith item.

0⩽xi⩽1
The ith item contributes the weight xi*wi to the total weight in the knapsack and profit
xi*pi to the total profit.

Hence, the objective of this algorithm is to


n
maximize=∑ Xi∗Pi
n=1

Subject to constraint,
n

∑ Xi∗Wi ≤W
n =1

It is clear that an optimal solution must fill the knapsack exactly, otherwise we could
add a fraction of one of the remaining items and increase the overall profit.
Thus, an optimal solution can be obtained by
n

∑ Xi∗Wi=W
n =1

In this context, first we need to sort those items according to the value of pi / wi, so
that (pi+1)/( wi+1) ≤ pi /wi . Here, x is an array to store the fraction of items.
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of
the practical.
The soft copy must be uploaded on the Portal.)

Program: MBA TECH CE Sem: 4

Roll No. A234 Name: AAYESHA PATEL

Division: - Batch : -

Date of Experiment: 31 JAN 2025 Date of Submission:

Grade : -

B.1 Software Code written by student:


(Paste your code completed during the 2 hours of practical in the lab here)

B.2 Input and Output:


(Paste your commented program input and output in following format, If there is
error then paste
the specific error in the output part. In case of error with due permission of the
faculty extension
can be given to submit the error free code with output in due course of time.)

B.3 Observations and learning:


(Students are expected to comment on the output obtained with clear
observations and learning for each task/ sub part assigned)

B.4. Practice Questions:

Q. Solve the following instance of Knapsack Problem where W = 60


using Greedy Method. Verify answer with a programmatic solution.
B.5 Conclusion:
(Students must write the conclusion as per the attainment of individual
outcome listed above and learning/observation noted in section B.3)

B.6 Question of Curiosity


(To be answered by student based on the practical performed and
learning/observations)
Q1. How can one tell if a Greedy Algorithm will solve a
particular optimization problem? (Hint: There is no way in
general, but Greedy-Choice Property and Optimal Sub-structure
are two key ingredients).
Q2. Show that the Fractional Knapsack Problem has the Greedy-
Choice Property.
************************

You might also like