FREE E LEARNING PLATFORM
☰ HOMEEXCEPTIONSOOPSJVMINTRO
 

Fractional Knapsack Problem



Knapsack Problem

The Knapsack Problem is a fundamental optimization problem in Design and Analysis of Algorithms (DAA). In this problem, we are given a collection of items, where each item has a weight and a profit. A knapsack has a limited carrying capacity. The objective is to select items so that the total profit is maximized without exceeding the capacity of the knapsack.

The Knapsack Problem is important because different restrictions on the items lead to different algorithmic solutions. Depending on whether an item can be divided, selected once, selected a limited number of times, or selected unlimited times, different versions of the Knapsack Problem are obtained.



Problem Statement

Suppose there are n items. Each item has a weight wi and a profit pi. The knapsack has a maximum capacity of W. We have to select items such that the total weight does not exceed W and the total profit is maximum.

Maximize Total Profit

Subject to: Total Weight ≤ Knapsack Capacity


Types of Knapsack Problem

The Knapsack Problem can be classified according to how many times an item can be selected and whether an item can be divided.

Type Can Item Be Divided? Number of Times an Item Can Be Selected Common Technique
Fractional Knapsack Yes At most one complete item, with fractions allowed Greedy Method
0/1 Knapsack No 0 or 1 time Dynamic Programming
Bounded Knapsack No Limited number of times Dynamic Programming
Unbounded Knapsack No Unlimited number of times Dynamic Programming

Basic Difference Between the Types

Feature Fractional 0/1 Bounded Unbounded
Fraction of item allowed Yes No No No
Complete item allowed Yes Yes Yes Yes
Item can be selected more than once No No Yes, but limited Yes, without a fixed limit
Typical approach Greedy Dynamic Programming Dynamic Programming Dynamic Programming
Greedy always optimal? Yes No No No

Knapsack Problem Structure


                         KNAPSACK PROBLEM
                                |
          +---------------------+---------------------+
          |                     |                     |
          v                     v                     v
     Fractional             0/1 Knapsack       Repetition Allowed
          |                     |                     |
          v                     v             +-------+-------+
       Greedy               Dynamic            |               |
                             Programming        v               v
                                           Bounded         Unbounded
                                             |               |
                                             v               v
                                      Limited Copies   Unlimited Copies


Important Terms

Term Meaning
Weight The amount of knapsack capacity occupied by an item.
Profit The value obtained by selecting an item.
Capacity The maximum weight that the knapsack can carry.
Profit/Weight Ratio Profit obtained per unit of weight.
Quantity The maximum number of copies of an item that may be selected in the Bounded Knapsack Problem.
Optimal Solution A solution having the maximum possible profit without exceeding the capacity.

1. Fractional Knapsack Problem

In the Fractional Knapsack Problem, an item can be divided into smaller portions. If the complete item cannot fit into the knapsack, a fraction of that item can be selected.

The Fractional Knapsack Problem is optimally solved using the Greedy Method. Items are arranged according to their Profit/Weight ratio in descending order.


Working Principle of Fractional Knapsack

Profit/Weight Ratio = Profit ÷ Weight

  1. Calculate the Profit/Weight ratio for every item.
  2. Sort the items in descending order of the ratio.
  3. Select the item having the highest ratio.
  4. If the complete item fits, select it completely.
  5. If it does not fit, select only the required fraction.
  6. Continue until the knapsack becomes full.

Fractional Knapsack: Solved Example 1

Knapsack capacity = 50 kg

Item Weight (kg) Profit (₹) Profit / Weight
A 10 60 6
B 20 100 5
C 30 120 4

Step 1: Sort According to Profit/Weight

A → B → C

Step 2: Fill the Knapsack

Step Selected Item Weight Used Remaining Capacity Profit Added Total Profit
1 A completely 10 40 60 60
2 B completely 20 20 100 160
3 2/3 of C 20 0 80 240

Profit obtained from C:

120 × 20/30 = ₹80

Maximum Profit = ₹240


Fractional Knapsack: Solved Example 2

Knapsack capacity = 60 kg

Item Weight (kg) Profit (₹) Profit / Weight
A 20 100 5
B 30 120 4
C 10 60 6
D 40 80 2

Sorted order:

C → A → B → D

Selection Weight Used Remaining Capacity Profit Added
C completely 10 50 60
A completely 20 30 100
B completely 30 0 120

Maximum Profit = 60 + 100 + 120 = ₹280


Fractional Knapsack: Solved Example 3

Knapsack capacity = 50 kg

Item Weight (kg) Profit (₹) Profit / Weight
A 10 100 10
B 20 120 6
C 30 150 5

Order:

A → B → C

Take A and B completely:

Weight = 10 + 20 = 30 kg

Profit = ₹100 + ₹120 = ₹220

Remaining capacity = 20 kg. Only 20 kg of C can be selected.

Fraction = 20/30 = 2/3

Profit from C = 150 × 2/3 = ₹100

Maximum Profit = ₹320


Fractional Knapsack Algorithm

FractionalKnapsack(Items, Capacity) Step 1 : Calculate Profit / Weight ratio for every item. Step 2 : Sort items in descending order of Profit / Weight ratio. Step 3 : Set TotalProfit = 0. Step 4 : Select items in sorted order. Step 5 : If the complete item fits: Add its complete profit. Reduce the remaining capacity. Step 6 : Otherwise: Select the required fraction. Add the corresponding fraction of profit. Stop because the knapsack is full. Step 7 : Return TotalProfit.

Complexity of Fractional Knapsack

Operation Complexity
Calculate ratios O(n)
Sorting O(n log n)
Selection O(n)
Total Time Complexity O(n log n)

2. 0/1 Knapsack Problem

In the 0/1 Knapsack Problem, an item cannot be divided and cannot be selected more than once. Every item has exactly two possibilities:

0 → Item is not selected

1 → Item is selected completely

Dynamic Programming is commonly used because the problem has overlapping subproblems and optimal substructure.


0/1 Knapsack Formula

Let DP[i][w] represent the maximum profit obtained using the first i items with capacity w.

If the item is too heavy:

DP[i][w] = DP[i−1][w]

Otherwise:

DP[i][w] = max(DP[i−1][w], pi + DP[i−1][w−wi])


0/1 Knapsack: Solved Example 1

Knapsack capacity = 50 kg

Item Weight (kg) Profit (₹)
1 10 60
2 20 100
3 30 120

Possible Valid Combinations

Selected Items Total Weight Total Profit
1 10 60
2 20 100
3 30 120
1 + 2 30 160
1 + 3 40 180
2 + 3 50 220
1 + 2 + 3 60 280

The last combination is invalid because its weight is 60 kg, which exceeds the capacity.

Maximum Profit = ₹220

Selected Items = 2 and 3


0/1 Knapsack: Solved Example 2

Capacity = 5 kg

Item Weight (kg) Profit (₹)
1 2 3
2 3 4
3 4 5
4 5 6
Valid Selection Weight Profit
1 2 3
2 3 4
3 4 5
4 5 6
1 + 2 5 7

Maximum Profit = 7

Selected Items = 1 and 2


0/1 Knapsack: Solved Example 3

Capacity = 7 kg

Item Weight Profit
A 1 1
B 3 4
C 4 5
D 5 7
Combination Weight Profit
B + C 7 9
A + D 6 8
A + B 4 5
A + C 5 6

Maximum Profit = ₹9

Selected Items = B and C


0/1 Knapsack Dynamic Programming Algorithm

Knapsack01(weights, profits, n, W) Step 1 : Create DP table of size (n+1) × (W+1). Step 2 : Initialize the first row and first column with 0. Step 3 : For each item i: If weight[i] <= w: DP[i][w] = max( DP[i-1][w], profit[i] + DP[i-1][w-weight[i]] ) Step 4 : Otherwise: DP[i][w] = DP[i-1][w] Step 5 : Return DP[n][W].

Complexity of 0/1 Knapsack

Method Time Complexity Space Complexity
Recursive O(2n) O(n)
Dynamic Programming O(nW) O(nW)

3. Bounded Knapsack Problem

In the Bounded Knapsack Problem, items cannot be divided, but each item has a limited number of available copies. Therefore, an item may be selected more than once, but only up to its specified quantity.

For example, if an item has a maximum quantity of 3, it may be selected:

0, 1, 2, or 3 times

but it cannot be selected 4 times.


Bounded Knapsack Example 1

Knapsack capacity = 10 kg

Item Weight (kg) Profit (₹) Maximum Quantity
A 2 3 2
B 3 4 2
C 5 7 1

Step 1: Examine Valid Quantities

Selection Total Weight Total Profit
A + A 4 6
B + B 6 8
A + B 5 7
A + A + B 7 10
A + B + B 8 11
A + A + C 9 13
B + B + C 11 15
A + B + C 10 14

The combination B + B + C is invalid because its weight is 11 kg, exceeding the capacity of 10 kg.

The best valid combination is:

A + B + C

Weight = 2 + 3 + 5 = 10 kg

Profit = 3 + 4 + 7 = ₹14

Maximum Profit = ₹14


Bounded Knapsack Example 2

Knapsack capacity = 12 kg

Item Weight Profit Maximum Quantity
A 2 6 3
B 3 7 2
C 5 12 1

Important Valid Combinations

Selection Weight Profit
A + A + A 6 18
B + B 6 14
A + A + B + B 10 26
A + B + C 10 25
A + A + C 9 24
A + A + A + B 9 25
A + A + A + B + B 12 32

The maximum valid selection is:

3A + 2B

Weight = 3(2) + 2(3) = 12 kg

Profit = 3(6) + 2(7) = ₹32

Maximum Profit = ₹32


Bounded Knapsack Example 3

Knapsack capacity = 15 kg

Item Weight Profit Maximum Quantity
A 3 8 2
B 5 12 2
C 7 16 1

Compare Important Selections

Selection Weight Profit
2A 6 16
2B 10 24
A + B 8 20
2A + B 11 28
A + 2B 13 32
2A + C 13 32
B + C 12 28
A + B + C 15 36

Therefore:

Selected Items = A + B + C

Weight = 3 + 5 + 7 = 15 kg

Maximum Profit = ₹36


Bounded Knapsack Dynamic Programming

The Bounded Knapsack Problem can be solved using Dynamic Programming by considering the number of copies of each item that can be selected. For item i, let its maximum available quantity be qi. For a capacity w, we can consider selecting:

0, 1, 2, ..., qi

copies, provided that the total weight does not exceed the capacity.

The recurrence can be written as:

DP[i][w] = max0 ≤ k ≤ qi { k × pi + DP[i−1][w − k × wi] }

where k is the number of copies of item i selected.


Bounded Knapsack Algorithm

BoundedKnapsack(weights, profits, quantities, n, W) Step 1 : Create DP table of size (n+1) × (W+1). Step 2 : Initialize the first row and first column with 0. Step 3 : For every item i: Step 4 : For every capacity w: Step 5 : Consider k copies of item i, where 0 <= k <= quantity[i]. Step 6 : If k × weight[i] <= w: DP[i][w] = max( DP[i][w], k × profit[i] + DP[i-1][w-k × weight[i]] ) Step 7 : Return DP[n][W].

Complexity of Bounded Knapsack

Method Time Complexity Space Complexity
Basic Dynamic Programming O(nWQ) O(nW)

Here, Q represents the maximum quantity considered for an item.


4. Unbounded Knapsack Problem

In the Unbounded Knapsack Problem, items cannot be divided, but an item can be selected an unlimited number of times. There is no fixed upper quantity for an item.

If an item weighs 2 kg, it may be selected:

0, 1, 2, 3, 4, ... times

as long as the total weight does not exceed the knapsack capacity.


Unbounded Knapsack: Solved Example 1

Knapsack capacity = 7 kg

Item Weight Profit
A 2 3
B 3 4
C 4 5

Valid Combinations

Selection Total Weight Total Profit
A 2 3
A + A 4 6
A + A + A 6 9
B 3 4
B + B 6 8
A + B 5 7
A + A + B 7 10
C 4 5
A + C 6 8
B + C 7 9

The best selection is:

A + A + B

Weight = 2 + 2 + 3 = 7 kg

Profit = 3 + 3 + 4 = ₹10

Maximum Profit = ₹10


Unbounded Knapsack: Solved Example 2

Knapsack capacity = 20 kg

Item Weight Profit
A 5 10
B 10 30
C 15 45

Important Valid Combinations

Selection Weight Profit
A + A + A + A 20 40
A + B 15 40
B + B 20 60
A + C 20 55

The best selection is B + B:

Weight = 10 + 10 = 20 kg

Profit = 30 + 30 = ₹60

Maximum Profit = ₹60


Unbounded Knapsack: Solved Example 3

Knapsack capacity = 10 kg

Item Weight Profit
A 3 4
B 4 5
C 5 7

Important Valid Combinations

Selection Total Weight Total Profit
A + A + A 9 12
A + A + B 10 13
A + C 8 11
B + B 8 10
B + C 9 12
C + C 10 14

Maximum Profit = ₹14

Selected Items = C + C


Dynamic Programming Formula for Unbounded Knapsack

Let DP[w] represent the maximum profit that can be obtained with capacity w.

DP[w] = max(DP[w], profit[i] + DP[w − weight[i]])

The important difference from 0/1 Knapsack is that the state DP[w − weight[i]] can already contain the same item. Therefore, the same item can be selected again.


Unbounded Knapsack Algorithm

UnboundedKnapsack(weights, profits, n, W) Step 1 : Create DP array of size W + 1. Step 2 : Initialize all DP values to 0. Step 3 : For capacity w from 1 to W: Step 4 : For every item i: If weight[i] <= w: DP[w] = max( DP[w], profit[i] + DP[w - weight[i]] ) Step 5 : Return DP[W].

Complexity of Unbounded Knapsack

Operation Complexity
Dynamic Programming O(nW)
Space O(W)

Bounded vs Unbounded Knapsack

Feature Bounded Knapsack Unbounded Knapsack
Items can be divided? No No
Can an item be selected repeatedly? Yes Yes
Maximum number of copies Fixed Unlimited
Example Maximum 3 copies of an item Any number of copies
Typical technique Dynamic Programming Dynamic Programming

Complete Comparison of Knapsack Types

Feature Fractional 0/1 Bounded Unbounded
Item can be divided Yes No No No
Item selected once Yes Yes Not necessarily No
Multiple copies allowed No No Yes, limited Yes, unlimited
Typical technique Greedy Dynamic Programming Dynamic Programming Dynamic Programming
Typical time complexity O(n log n) O(nW) O(nWQ) O(nW)
Key idea Take highest value per unit weight Take or leave Take limited copies Reuse items

How to Identify the Knapsack Type

Condition in the Question Knapsack Type
Items can be divided into fractions Fractional Knapsack
Each item can be selected at most once 0/1 Knapsack
Each item has a specified maximum quantity Bounded Knapsack
Each item can be selected unlimited times Unbounded Knapsack

Fractional → Divide    |    0/1 → Take or Leave    |    Bounded → Limited Copies    |    Unbounded → Unlimited Copies


Applications of Knapsack Problem

Application Description
Cargo Loading Selecting goods to maximize value within a weight limit.
Resource Allocation Allocating limited resources among competing activities.
Investment Planning Selecting investment opportunities under a limited budget.
Project Selection Selecting projects to maximize benefit under resource constraints.
Cloud Computing Allocating computing resources among competing tasks.
Production Planning Selecting products to maximize return with limited resources.

Important Points

  • The Knapsack Problem is an optimization problem involving limited capacity.
  • Fractional Knapsack allows items to be divided and is optimally solved using the Greedy Method.
  • 0/1 Knapsack allows each item at most once.
  • Bounded Knapsack allows each item a limited number of times.
  • Unbounded Knapsack allows an item to be selected unlimited times.
  • 0/1, Bounded and Unbounded Knapsack are commonly solved using Dynamic Programming.
  • Profit/Weight ratio is central to the Greedy solution of Fractional Knapsack.
  • The standard DP solution for 0/1 and Unbounded Knapsack has O(nW) time complexity.
  • Bounded Knapsack has an additional quantity constraint for every item.

Exam Tip

First identify the type of Knapsack Problem from the restrictions given in the question. If fractions are allowed, use the Greedy Method. If every item can be selected only once, use 0/1 Knapsack. If every item has a specified maximum quantity, use Bounded Knapsack. If items can be selected any number of times, use Unbounded Knapsack.

For numerical questions, clearly write the item table, knapsack capacity, selection process or DP table, total weight and final maximum profit.


Summary

The Knapsack Problem requires selecting items to maximize profit while keeping the total weight within a fixed capacity. The four important forms are Fractional, 0/1, Bounded and Unbounded Knapsack.

In Fractional Knapsack, items can be divided and the Greedy Method based on Profit/Weight ratio provides an optimal solution. In 0/1 Knapsack, each item can be selected at most once. In Bounded Knapsack, each item can be selected only up to a specified quantity. In Unbounded Knapsack, an item can be selected unlimited times.

Fractional → Greedy → Items can be divided

0/1 → Dynamic Programming → Take once or leave

Bounded → Dynamic Programming → Limited copies

Unbounded → Dynamic Programming → Unlimited copies