Loading learning content...
Imagine you are a thief breaking into a warehouse filled with valuable commodities. You have a knapsack that can carry at most W kilograms of goods. The warehouse contains n different items, each with a specific weight and value. Your objective is clear and ruthless: maximize the total value of goods you carry away, subject to the weight limitation of your knapsack.
This scenario, while morally questionable, encapsulates one of the most important optimization problems in computer science: the Knapsack Problem. Variations of this problem appear across resource allocation, financial portfolio optimization, cargo loading, bandwidth allocation, and countless other domains where limited capacity must be allocated among competing demands to maximize overall utility.
By the end of this page, you will deeply understand the structure of the Fractional Knapsack problem: what constitutes an 'item', how weight and value interact, why this formulation differs fundamentally from simpler selection problems, and how to mathematically model the optimization objective. This foundation is essential before we explore the greedy solution strategy.
At the heart of every knapsack problem lies the concept of an item. Understanding what constitutes an item, and how items are characterized, is fundamental to formulating and solving these problems correctly.
Formal Definition:
An item i in the Fractional Knapsack problem is defined by a tuple (wᵢ, vᵢ) where:
Given n items, we have:
We require weights and values to be positive. An item with zero or negative weight would be trivially included (it 'costs' nothing or actually increases capacity). An item with zero or negative value would never be selected by any rational algorithm. These edge cases are typically excluded by definition to focus on the non-trivial optimization challenge.
The Two Fundamental Characteristics:
Every item embodies a cost (the weight it consumes) and a benefit (the value it provides). The challenge arises because:
This tension between cost and benefit, between greed and constraint, is what makes the knapsack problem both intellectually rich and practically relevant.
Concrete Interpretation:
Think of weight as 'what you pay' and value as 'what you get.' Different items offer different 'exchange rates' between these currencies. The optimization problem is essentially: given a fixed budget of weight, how do you allocate it across items to maximize total value received?
The relationship between an item's weight and its value is central to understanding knapsack problems. There is no fixed relationship — an item can be heavy and valuable, light and valuable, heavy and less valuable, or light and less valuable. This variability is what creates the optimization challenge.
Value Density — The Critical Ratio:
The value-to-weight ratio (also called value density or efficiency) of item i is:
ρᵢ = vᵢ / wᵢ
This ratio represents the value gained per unit of weight consumed. It answers the question: "How much bang do I get for each buck of capacity I spend?"
| Item | Weight (kg) | Value ($) | Value Density ($/kg) |
|---|---|---|---|
| Gold bar | 2 | 500 | 250 |
| Silver ingot | 5 | 400 | 80 |
| Bronze statue | 10 | 300 | 30 |
| Diamond ring | 0.1 | 1000 | 10,000 |
| Copper wire (spool) | 15 | 150 | 10 |
Interpreting Value Density:
Look at the table above. The diamond ring has the highest value density — every gram carried returns enormous value. The copper wire has the lowest — it takes significant capacity for modest returns.
Key Insight: Value density alone doesn't determine the optimal selection. Consider a knapsack with capacity 10kg:
This nuance — that maximizing value isn't merely about picking the highest-density items — is crucial. In the Fractional Knapsack variant, we can fill remaining capacity with fractions of items, which fundamentally changes the optimization landscape.
Value density will become the cornerstone of the greedy algorithm for Fractional Knapsack. By always selecting the highest-density available item (or fraction thereof), we ensure each 'unit' of capacity is spent as efficiently as possible. This greedy approach is optimal for the fractional variant — a remarkable and elegant result.
The capacity constraint W is what transforms an trivial selection problem into an optimization challenge. Without this constraint, the solution would be obvious: take everything.
Formal Statement of the Constraint:
For a selection of items, the total weight must not exceed capacity:
∑(selected items) wᵢ ≤ W
In the Fractional Knapsack variant, if we take a fraction xᵢ (where 0 ≤ xᵢ ≤ 1) of each item i, the constraint becomes:
∑ᵢ₌₁ⁿ (xᵢ · wᵢ) ≤ W
Why Constraints Create Complexity:
The capacity constraint forces trade-off decisions. Each unit of capacity spent on one item is unavailable for others. This creates dependencies between decisions:
The Fractional Relaxation:
In the Fractional Knapsack, we can take any fraction of an item. This means:
This relaxation is what makes the greedy approach work perfectly. In the discrete (0/1) variant, we cannot take partial items, which destroys the greedy property — but we'll explore that contrast in a later page.
If W = ∞ (infinite capacity), take everything — no optimization needed. If W ≤ 0 (zero or negative capacity), take nothing — the problem is trivially infeasible. The interesting optimization occurs when 0 < W < ∑wᵢ (capacity is positive but insufficient to carry all items).
Let us now precisely state the Fractional Knapsack problem in mathematical terms. This formal specification removes all ambiguity and provides the foundation for algorithmic solutions.
Given:
Decision Variables:
Objective Function (Maximize):
maximize ∑ᵢ₌₁ⁿ (xᵢ · vᵢ)
Subject to Constraints:
Notice that both the objective function and constraints are linear in the decision variables xᵢ. This makes Fractional Knapsack a Linear Programming (LP) problem! While general LP requires sophisticated algorithms like Simplex, the special structure of Fractional Knapsack admits a simple O(n log n) greedy solution — a beautiful example of how problem structure enables simplification.
Interpreting the Formulation:
Objective: We want to maximize total value harvested, which is the sum of (fraction taken × item value) across all items
Capacity constraint: The total weight of all fractional selections must not exceed the knapsack's capacity
Fraction bounds: We cannot take negative amounts (xᵢ ≥ 0) or more than an item exists (xᵢ ≤ 1)
Why This Is an Optimization Problem:
The problem asks for the best possible allocation — the one achieving maximum value while respecting constraints. There may be many feasible solutions (allocations that satisfy constraints), but we seek the optimal one.
Feasible Solutions vs. Optimal Solutions:
Our goal is to find an optimal solution efficiently — ideally in polynomial time.
Let's work through a concrete example to solidify understanding. This example will serve as a running case study throughout the module.
Problem Instance:
You have a knapsack with capacity W = 50 kg. Three items are available:
| Item | Weight (kg) | Value ($) | Value Density ($/kg) |
|---|---|---|---|
| Item A | 10 | 60 | 6.0 |
| Item B | 20 | 100 | 5.0 |
| Item C | 30 | 120 | 4.0 |
Observation 1: Total Weight Exceeds Capacity
Total weight of all items = 10 + 20 + 30 = 60 kg, but capacity = 50 kg. We cannot take everything — a selection must be made.
Observation 2: Items Ranked by Value Density
Item A > Item B > Item C in value density. Per unit of weight, Item A delivers the most value.
Observation 3: The Non-Obvious Optimal
What should we take? Let's explore some candidate solutions:
Candidate 1: Take only highest-density item (A)
Candidate 2: Take A entirely, then as much of B and C as possible
Candidate 3: Take only the highest-value item (C)
Candidate 2 ($240) beats Candidate 3 ($220). The greedy-by-density approach is the winner here.
In this example, sorting by value density and greedily taking from highest to lowest yields the optimal solution. This is NOT a coincidence — it is a provable property of the Fractional Knapsack problem. The next page will explore this greedy strategy and prove its optimality.
The Fractional Knapsack model appears disguised in numerous real-world optimization scenarios. Recognizing these instances is key to applying the solution pattern effectively.
Whenever you see: (1) a fixed resource/capacity, (2) multiple options each consuming some amount and providing some benefit, and (3) fractional selection is meaningful — you may have a Fractional Knapsack instance. The greedy algorithm applies directly!
The knapsack problem belongs to a family of related optimization problems. Understanding how Fractional Knapsack differs from its cousins clarifies when the greedy approach applies.
Other Related Problems:
Why Fractionality Matters:
The ability to take fractions fundamentally changes the problem's structure. In 0/1 Knapsack, you might face scenarios where the best items don't fit together optimally. Taking a high-density item might waste capacity that could be better used by smaller items.
In Fractional Knapsack, you never waste capacity (assuming total weight exceeds W). You simply fill remaining capacity with the next-best item, taking exactly as much as fits. This 'greedy stays optimal' property comes precisely from fractionality.
The difference between Fractional and 0/1 Knapsack is not cosmetic — it's fundamental. Greedy works for one and fails for the other. Confusing these variants in an interview or system design can lead to incorrect solutions. Always confirm: can items be divided, or must they be taken whole?
We have established the foundational concepts of the Fractional Knapsack problem. Let's consolidate what we've learned:
What's Next:
Now that we understand the problem structure — items, weights, values, capacity, and optimization objective — we're ready to explore the solution strategy. The next page examines taking fractions in depth: what it means algorithmically, how partial selections are computed, and why this capability is the linchpin that makes greedy algorithms optimal for this problem.
You now understand the foundational structure of the Fractional Knapsack problem: items characterized by weight and value, the capacity constraint, value density as an efficiency metric, and how this problem fits within the broader landscape of knapsack variants. Next, we'll explore how fractional selection enables optimal greedy solutions.