DAA Assign 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Assignment No.

: 3
TITLE

To solve a fractional knapsack problem using a greedy method.

OBJECTIVES

1. Understand how to solve knapsack problem using greedy method.


2. Understand greedy method

PROBLEM STATEMENT

Write a program to solve a fractional problem using a greedy method.

OUTCOME
Students will be able to,
1. Detect the spam email using various algorithms.
SOFTWARE & HARDWARE REQUIREMENTS
1. VS Code
2. Notepad

THEORY•CONCEPT

The fractional knapsack problem is also one of the techniques which are used to solve the knapsack problem.
In fractional knapsack, the items are broken in order to maximize the profit. The problem in which we break
the item is known as a Fractional knapsack problem.

This problem can be solved with the help of using two techniques:

o Brute-force approach: The brute-force approach tries all the possible solutions with all the different
fractions but it is a time-consuming approach.
o Greedy approach: In Greedy approach, we calculate the ratio of profit/weight, and accordingly, we
will select the item. The item with the highest ratio would be selected first.

Matoshri College of Engineering & R.C. Nashik Dept. of Comp. Engg. Page 1
There are basically three approaches to solve the problem:

o The first approach is to select the item based on the maximum profit.
o The second approach is to select the item based on the minimum weight.
o The third approach is to calculate the ratio of profit/weight.

Knapsack Problem Using Greedy Method: The selection of some things, each with profit and weight
values, to be packed into one or more knapsacks with capacity is the fundamental idea behind all families
of knapsack problems. The knapsack problem had two versions that are as follows:

1. Fractional Knapsack Problem


2. 0 /1 Knapsack Problem

The fractional Knapsack problem using the Greedy Method is an efficient method to solve it, where you
need to sort the items according to their ratio of value/weight. In a fractional knapsack, we can break items
to maximize the knapsack's total value. This problem in which we can break an item is also called
the Fractional knapsack problem.

In this method, the Knapsack's filling is done so that the maximum capacity of the knapsack is utilized so
that maximum profit can be earned from it. The knapsack problem using the Greedy Method is referred to
as:
Given a list of n objects, say {I1, I2,……, In) and a knapsack (or bag).
The capacity of the knapsack is M.
Each object Ij has a weight wj and a profit of pj
If a fraction xj (where x ∈ {0...., 1)) of an object Ij is placed into a knapsack, then a profit of pjxj is earned.
The problem (or Objective) is to fill the knapsack (up to its maximum capacity M), maximizing the total
profit earned.

Knapsack Problem Using Greedy Method Pseudocode

A pseudo-code for solving knapsack problems using the greedy method is;
greedy fractional-knapsack (P[1...n], W[1...n], X[1..n]. M)
/*P[1...n] and W[1...n] contain the profit and weight of the n-objects ordered such that X[1...n] is a

Matoshri College of Engineering & R.C. Nashik Dept. of Comp. Engg. Page 2
solution set and M is the capacity of knapsack*/
{

For j ← 1 to n do
X[j]← 0
profit ← 0 // Total profit of item filled in the knapsack
weight ← 0 // Total weight of items packed in knapsacks
j←1
While (Weight < M) // M is the knapsack capacity

Given a set of items, each with a weight and a value, determine a subset of items 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.

The knapsack problem is in combinatorial optimization problem. It appears as a subproblem in many, more
complex mathematical models of real-world problems. One general approach to difficult problems is to
identify the most restrictive constraint, ignore the others, solve a knapsack problem, and somehow adjust
the solution to satisfy the ignored constraints.

TECHNOLOGY / TOOL

What is Knapsack Problem?

The knapsack 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 the decision-makers have to choose from a set of non-
divisible projects or tasks under a fixed budget or time constraint, respectively.

The knapsack problem has been studied for more than a century, with early works dating as far back as
1897. The name "knapsack problem" dates back to the early works of the mathematician Tobias
Dantzig (1884–1956), and refers to the commonplace problem of packing the most valuable or useful items
without overloading the luggage.

Matoshri College of Engineering & R.C. Nashik Dept. of Comp. Engg. Page 3
APPLICATIONS

Knapsack problems appear in real-world decision-making processes in a wide variety of fields, such as
finding the least wasteful way to cut raw materials, selection of investments and portfolios, selection of
assets for asset-backed securitization, and generating keys for the Merkle–Hellman and other knapsack
cryptosystems.

One early application of knapsack algorithms was in the construction and scoring of tests in which the test-
takers have a choice as to which questions, they answer. For small examples, it is a fairly simple process to
provide the test-takers with such a choice. For example, if an exam contains 12 questions each worth 10
points, the test-taker need only answer 10 questions to achieve a maximum possible score of 100 points.
However, on tests with a heterogeneous distribution of point values, it is more difficult to provide choices.
Feuerman and Weiss proposed a system in which students are given a heterogeneous test with a total of 125
possible points. The students are asked to answer all of the questions to the best of their abilities. Of the
possible subsets of problems whose total point values add up to 100, a knapsack algorithm would determine
which subset gives each student the highest possible score.

A 1999 study of the Stony Brook University Algorithm Repository showed that, out of 75 algorithmic
problems, the knapsack problem was the 19th most popular and the third most needed after suffix trees and
the bin packing problem.

Computational Complexity
The knapsack problem is interesting from the perspective of computer science for many reasons:

• The decision problem form of the knapsack problem (Can a value of at least V be achieved
without exceeding the weight W?) is NP-complete, thus there is no known algorithm both correct
and fast (polynomial-time) in all cases.
• While the decision problem is NP-complete, the optimization problem is not, its resolution is at
least as difficult as the decision problem, and there is no known polynomial algorithm which can
tell, given a solution, whether it is optimal (which would mean that there is no solution with a
larger V, thus solving the NP-complete decision problem).
• There is a pseudo-polynomial time algorithm using dynamic programming.
• There is a fully polynomial-time approximation scheme, which uses the pseudo-polynomial time
algorithm as a subroutine, described below.

Matoshri College of Engineering & R.C. Nashik Dept. of Comp. Engg. Page 4
• Many cases that arise in practice, and "random instances" from some distributions, can
nonetheless be solved exactly.

There is a link between the "decision" and "optimization" problems in that if there exists a polynomial
algorithm that solves the "decision" problem, then one can find the maximum value for the optimization
problem in polynomial time by applying this algorithm iteratively while increasing the value of k. On the
other hand, if an algorithm finds the optimal value of the optimization problem in polynomial time, then the
decision problem can be solved in polynomial time by comparing the value of the solution output by this
algorithm with the value of k. Thus, both versions of the problem are of similar difficulty.

One theme in research literature is to identify what the "hard" instances of the knapsack problem look like, or
viewed another way, to identify what properties of instances in practice might make them more amenable
than their worst-case NP-complete behavior suggests. The goal in finding these "hard" instances is for their
use in public key cryptography systems, such as the Merkle-Hellman knapsack cryptosystem.

Furthermore, notable is the fact that the hardness of the knapsack problem depends on the form of the input.
If the weights and profits are given as integers, it is weakly NP-complete, while it is strongly NP-complete if
the weights and profits are given as rational numbers. However, in the case of rational weights and profits
it still admits a fully polynomial-time approximation scheme.

Steps for knapsack problem:

1. Sort items by worth, in descending order.

2. Start with the highest worth item. Put items into the bag until the next item on the list cannot fit.

3. Try to fill any remaining capacity with the next item on the list that can fit.

Matoshri College of Engineering & R.C. Nashik Dept. of Comp. Engg. Page 5
Code For Knapsack Problem:
// C++ program to solve fractional Knapsack Problem
#include <bits/stdc++.h>

using namespace std;

// Structure for an item which stores weight and


// corresponding value of Item
struct Item {
int value, weight;

// Constructor
Item(int value, int weight)
{
this->value = value;
this->weight = weight;
}
};

// Comparison function to sort Item according to val/weight


// ratio
bool cmp(struct Item a, struct Item b)
{
double r1 = (double)a.value / (double)a.weight;
double r2 = (double)b.value / (double)b.weight;
return r1 > r2;
}

// Main greedy function to solve problem


double fractionalKnapsack(int W, struct Item arr[], int N)
{
// sorting Item on basis of ratio
sort(arr, arr + N, cmp);

double finalvalue = 0.0; // Result (value in Knapsack)

// Looping through all Items


for (int i = 0; i < N; i++) {
// If adding Item won't overflow, add it completely
if (arr[i].weight <= W) {
W -= arr[i].weight;
finalvalue += arr[i].value;
}

// If we can't add current Item, add fractional part


// of it
else {

Matoshri College of Engineering & R.C. Nashik Dept. of Comp. Engg. Page 6
finalvalue
+= arr[i].value
* ((double)W / (double)arr[i].weight);
break;
}
}

// Returning final value


return finalvalue;
}

// Driver's code
int main()
{
int W = 50; // Weight of knapsack
Item arr[] = { { 60, 10 }, { 100, 20 }, { 120, 30 } };

int N = sizeof(arr) / sizeof(arr[0]);

// Function call
cout << "Maximum value we can obtain = "
<< fractionalKnapsack(W, arr, N);
return 0;
}

TEST CASES

Manual testing is check weather greedy method is successfully applied or not

CONCLUSION / ANALYSIS

Hence, we have studied fractional knapsack problem using a greedy method.

ORAL QUESTIONS

1. Explain the knapsack problem?


2. Why greedy method for this problem?
3. Explain computational complexity of knapsack problem?
4. Explain the steps of knapsack problem?

Matoshri College of Engineering & R.C. Nashik Dept. of Comp. Engg. Page 7

You might also like