- Design and Analysis of Algorithms
- Home

- Basics of Algorithms
- DAA - Introduction
- DAA - Analysis of Algorithms
- DAA - Methodology of Analysis
- Asymptotic Notations & Apriori Analysis
- DAA - Space Complexities

- Design Strategies
- DAA - Divide & Conquer
- DAA - Max-Min Problem
- DAA - Merge Sort
- DAA - Binary Search
- Strassen’s Matrix Multiplication
- DAA - Greedy Method
- DAA - Fractional Knapsack
- DAA - Job Sequencing with Deadline
- DAA - Optimal Merge Pattern
- DAA - Dynamic Programming
- DAA - 0-1 Knapsack
- Longest Common Subsequence

- Graph Theory
- DAA - Spanning Tree
- DAA - Shortest Paths
- DAA - Multistage Graph
- Travelling Salesman Problem
- Optimal Cost Binary Search Trees

- Heap Algorithms
- DAA - Binary Heap
- DAA - Insert Method
- DAA - Heapify Method
- DAA - Extract Method

- Sorting Methods
- DAA - Bubble Sort
- DAA - Insertion Sort
- DAA - Selection Sort
- DAA - Quick Sort
- DAA - Radix Sort

- Complexity Theory
- Deterministic vs. Nondeterministic Computations
- DAA - Max Cliques
- DAA - Vertex Cover
- DAA - P and NP Class
- DAA - Cook’s Theorem
- NP Hard & NP-Complete Classes
- DAA - Hill Climbing Algorithm

- DAA Useful Resources
- DAA - Quick Guide
- DAA - Useful Resources
- DAA - Discussion

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Design and Analysis Fractional Knapsack

The Greedy algorithm could be understood very well with a well-known problem referred to as Knapsack problem. Although the same problem could be solved by employing other algorithmic approaches, Greedy approach solves Fractional Knapsack problem reasonably in a good time. Let us discuss the Knapsack problem in detail.

## Knapsack Problem

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.

### Applications

In many cases of resource allocation along with some constraint, the problem can be derived in a similar way of Knapsack problem. Following is a set of example.

- Finding the least wasteful way to cut raw materials
- portfolio optimization
- Cutting stock problems

### Problem Scenario

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

**item is**

*i*^{th}**and its profit is**

*w*_{i}**. What items should the thief take?**

*p*_{i}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.

Based on the nature of the items, Knapsack problems are categorized as

- Fractional Knapsack
- Knapsack

## 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 storeWeight of

**i**item $w_{i} > 0$^{th}Profit for

**i**item $p_{i} > 0$ and^{th}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 ** x_{i}** of

**i**item.

^{th}$$0 \leqslant x_{i} \leqslant 1$$

The **i ^{th}** item contributes the weight $x_{i}.w_{i}$ to the total weight in the knapsack and profit $x_{i}.p_{i}$ to the total profit.

Hence, the objective of this algorithm is to

$$maximize\:\displaystyle\sum\limits_{n=1}^n (x_{i}.p_{}i)$$

subject to constraint,

$$\displaystyle\sum\limits_{n=1}^n (x_{i}.w_{}i) \leqslant W$$

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

$$\displaystyle\sum\limits_{n=1}^n (x_{i}.w_{}i) = W$$

In this context, first we need to sort those items according to the value of $\frac{p_{i}}{w_{i}}$, so that $\frac{p_{i}+1}{w_{i}+1}$ ≤ $\frac{p_{i}}{w_{i}}$ . Here, ** x** is an array to store the fraction of items.

Algorithm: Greedy-Fractional-Knapsack (w[1..n], p[1..n], W)for i = 1 to n do x[i] = 0 weight = 0 for i = 1 to n if weight + w[i] ≤ W then x[i] = 1 weight = weight + w[i] else x[i] = (W - weight) / w[i] weight = W break return x

### Analysis

If the provided items are already sorted into a decreasing order of $\mathbf{\frac{p_{i}}{w_{i}}}$, then the whileloop takes a time in ** O(n)**; Therefore, the total time including the sort is in

**.**

*O(n logn)*### Example

Let us consider that the capacity of the knapsack ** W = 60** and the list of provided items are shown in the following table −

Item | A | B | C | D |
---|---|---|---|---|

Profit | 280 | 100 | 120 | 120 |

Weight | 40 | 10 | 20 | 24 |

Ratio $(\frac{p_{i}}{w_{i}})$ | 7 | 10 | 6 | 5 |

As the provided items are not sorted based on $\mathbf{\frac{p_{i}}{w_{i}}}$. After sorting, the items are as shown in the following table.

Item | B | A | C | D |
---|---|---|---|---|

Profit | 100 | 280 | 120 | 120 |

Weight | 10 | 40 | 20 | 24 |

Ratio $(\frac{p_{i}}{w_{i}})$ | 10 | 7 | 6 | 5 |

### Solution

After sorting all the items according to $\frac{p_{i}}{w_{i}}$. First all of ** B** is chosen as weight of

**is less than the capacity of the knapsack. Next, item**

*B***is chosen, as the available capacity of the knapsack is greater than the weight of**

*A***. Now,**

*A***is chosen as the next item. However, the whole item cannot be chosen as the remaining capacity of the knapsack is less than the weight of**

*C***.**

*C*Hence, fraction of ** C** (i.e. (60 − 50)/20) is chosen.

Now, the capacity of the Knapsack is equal to the selected items. Hence, no more item can be selected.

The total weight of the selected items is **10 + 40 + 20 * (10/20) = 60**

And the total profit is **100 + 280 + 120 * (10/20) = 380 + 60 = 440**

This is the optimal solution. We cannot gain more profit selecting any different combination of items.