0% found this document useful (0 votes)
9 views7 pages

AOA Exp 4 b44

The document outlines an experiment to implement the Knapsack Problem using the Greedy Method Approach, detailing the aim, theory, and expected outcomes. It explains the difference between Fractional and 0/1 Knapsack problems, the efficiency of the greedy algorithm, and its applications. Additionally, it discusses the limitations of the greedy approach compared to dynamic programming in terms of optimality and efficiency.

Uploaded by

shettysiya09
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)
9 views7 pages

AOA Exp 4 b44

The document outlines an experiment to implement the Knapsack Problem using the Greedy Method Approach, detailing the aim, theory, and expected outcomes. It explains the difference between Fractional and 0/1 Knapsack problems, the efficiency of the greedy algorithm, and its applications. Additionally, it discusses the limitations of the greedy approach compared to dynamic programming in terms of optimality and efficiency.

Uploaded by

shettysiya09
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

PART A

(PART A: TO BE REFFERED BY STUDENTS)

Experiment No.04
A.1 Aim:
Write a program to implement Knapsack Problem using Greedy Method Approach.

A.2 Prerequisite:

A.3 Outcome:

After successful completion of this experiment students will be able apply greedy method
approach to different problems to find optimal solution and analyze the complexity of the
problem.

A.4 Theory:

The knapsack problem or rucksack problem is a problem in combinatorial optimization:


Given a set of items, each with a weight and a value, determine the number of each item
to include in a collection so that the total weight is less than or equal to a given limit and
the total value is as large as possible. It derives its name from the problem faced by
someone who is constrained by a fixed-size knapsack and must fill it with the most
valuable items. The problem often arises in resource allocation where there are financial
constraints and is studied in fields such as combinatorics, computer science, complexity
theory, cryptography, applied mathematics, and daily fantasy sports. Based on the nature of
the items, Knapsack problems are categorized as

- Fractional Knapsack
- 0/1 Knapsack
Fractional Knapsack

In this case, items can be broken into smaller pieces, so that fraction of item can be
selected. According to the problem statement,

There are n items.

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 piece i.e.
fraction xi of ith item.

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

Subject to constraint

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

In this context, first we need to sort those items according to the value of pi/wi so that
Time Complexity:

If the provided items are already sorted into a decreasing order of pi/wi, then the while
loop takes a time in O(n); Therefore, the total time including the sort is in O(n logn).
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll No: B044 Name: Siya Shetty


Class: SE COMPS Batch: B2
Date of Experiment: 24/02/25 Date of Submission: 04/03/25
Grade:

B.1 Software Code written by student:

B.2 Input and Output:


B.3 Observations and learning:
Observation:
The Greedy Method for the Knapsack Problem selects items based on the highest value-to-weight
ratio, leading to an efficient but not always optimal solution. It performs well for fractional
knapsack but may fail for the 0/1 variant.

Learning:
We learned that the greedy approach provides quick approximations but does not guarantee
optimality in all cases.

B.4 Conclusion:
The Greedy Method efficiently solves the Fractional Knapsack Problem by prioritizing items with the
highest value-to-weight ratio. However, it is not optimal for the 0/1 Knapsack Problem, highlighting the need for
dynamic programming or other approaches in such cases.

B.5 Question of Curiosity


Q1: Is greedy algorithm an efficient way to solve the problem?

Ans:- The greedy algorithm can be efficient for some problems, but not always. It works well
when the problem has the greedy choice property (local decisions lead to an optimal solution)
and optimal substructure. Problems like Huffman coding or Dijkstra's algorithm are good
examples.

However, it's not always optimal. In problems like the 0/1 Knapsack or Traveling Salesman,
greedy algorithms may fail to find the best solution. So, while they can be fast and simple, their
efficiency depends on the problem's structure.

Q2: What is difference between greedy approach and dynamic programming


approach?

Ans:- The main differences are:

1. Approach:
o Greedy: Makes local, optimal choices at each step.
o Dynamic Programming: Solves subproblems and stores solutions for future use.
2. Optimality:
o Greedy: Doesn’t always guarantee the best solution.
o Dynamic Programming: Guarantees an optimal solution.
3. Efficiency:
o Greedy: Faster (O(n), O(n log n)).
o Dynamic Programming: Slower but ensures correctness (O(n²), O(n³)).

In short, greedy is faster but not always optimal, while dynamic programming guarantees
optimal solutions but can be slower.

Q3: Explain applications of greedy algorithm.

Ans:- Greedy algorithms are used in various applications, including:

1. Activity Selection: Selects the maximum number of non-overlapping activities.


2. Huffman Coding: Efficient data compression using variable-length codes.
3. Kruskal's Algorithm: Finds the minimum spanning tree in a graph.
4. Dijkstra's Algorithm: Solves shortest path problems in weighted graphs.
5. Fractional Knapsack: Maximizes value by selecting items based on value-to-weight
ratio.
6. Change Making: Finds the fewest coins for a given amount of change.
7. Job Scheduling: Schedules jobs to maximize profit within deadlines.

These problems benefit from making locally optimal choices to achieve global solutions.

************************

You might also like