You are on page 1of 6

Roll No.

: C064 Name: VIDULA BHALERAO


Class: SE-C Batch: C3
Date of Submission: Grade:

EXPERIMENT NO: 04

AIM:
To implement Knapsack Problem using Greedy Method Approach.

THEORY:
The knapsack problem is a classic optimisation problem where given a set of
items, each with a weight and a value, we aim to determine the maximum value
that can be accommodated within a knapsack of limited capacity.

ALGORITHM:

1. Create a matrix to store the maximum value that can be achieved at each
combination of items and weights.
2. Initialize the matrix with zeros.
3. Iterate through each item and weight combination, updating the matrix based
on whether adding the item would increase the total value without exceeding
the weight limit.
4. The final value in the matrix represents the maximum value achievable within
the given constraints.
5. Backtrack through the matrix to determine which items were selected to
achieve this maximum value.

INPUT/OUTPUT:
OBSERVATION AND CONCLUSION:

OBSERVATION:

1. Greedy approach selects items based on their immediate benefit, typically the
highest value-to-weight ratio.
2. It doesn't guarantee an optimal solution in all cases, unlike dynamic
programming.
3. Greedy algorithms tend to be faster and simpler to implement compared to
dynamic programming for the knapsack problem.
4. The greedy approach might result in selecting items that don't collectively
maximise the total value within the knapsack capacity.

CONCLUSION:

1. The greedy approach can be effective for certain variations of the knapsack
problem, especially when the items have similar weights and values.
2. However, it may fail to find the optimal solution in scenarios where the value-to-
weight ratio is not a reliable indicator of overall benefit.
3. While the greedy approach is intuitive and easy to implement, it's essential to
recognise its limitations and consider dynamic programming or other methods
for scenarios where optimality is crucial.
4. Overall, the choice of algorithm depends on the specific constraints and
requirements of the problem at hand.

You might also like