Saltar al contenido principal
Interactive Algorithm Education

Visualize & Master Algorithms & Data Structures

Explore classic & modern sorting algorithms, efficient searching techniques, and interactive data structure visualizations — all with real-time step-by-step animation, comparisons, swaps, and Big-O metrics.

Greedy Visualizer

Fractional Knapsack

Paso 0 / 0
Speed 100ms
Step Progress 0 / 0
Selected 0
Rejected 0
Status Ready
Unconsidered
Considering
Selected
Rejected
Step Explanation

Select an algorithm and press Play to watch the greedy choices unfold.

—
Pseudocode
 

Fractional Knapsack

Elementary (2/5) ~30 minutes Value/weight ratio Greedy is optimal for divisible items Contrast with 0/1 Knapsack Sorting as preprocessing Prereqs: Sorting, Ratio reasoning, 0/1 Knapsack (helpful contrast)
Quick Reference

Fractional Knapsack

Fractional Knapsack allows taking fractions of items, so the greedy rule — sort by value/weight ratio and take the best first — achieves the optimal value. Unlike 0/1 Knapsack, greedy is optimal here.

Difficulty: Elementary (2/5) greedy

Complexity

Best Time
O(n log n)
Average Time
O(n log n)
Worst Time
O(n log n)
Space
O(n)

When to Use

For divisible-resource allocation: hauling bulk goods, time-budgeting, and any scenario where resources can be split.

Pros

  • Greedy is optimal for fractional items
  • O(n log n) dominated by sorting
  • Intuitive ratio-based reasoning

Cons

  • Not valid when items are indivisible (use 0/1 Knapsack)
  • Ratio tie-breaking can change selections
  • Requires fractional units to exist

History

The fractional knapsack problem is a classic greedy-algorithm example dating back to the 1950s, often contrasted with the DP-solved 0/1 knapsack in algorithms courses.

Fill a knapsack with items you can slice — gold bars you can cut, grain you can scoop, bandwidth you can split.

Because every item is infinitely divisible, you never face painful either/or decisions: take the best item, and if the bag runs low, take part of the next best. That makes the greedy rule — take the best value-per-weight first — not just good, but provably optimal.

How It Works

  1. Compute the value-per-weight ratio for every item.
  2. Sort items by ratio, descending.
  3. Take items in that order, filling the knapsack completely.
  4. If the next item would overflow, take only the fraction that fits.
take full items by ratio; on overflow, take (remaining capacity / weight) of the next item

Key Insight

The natural unit of comparison is value per unit weight — that’s what the greedy measures.

Divisibility is what makes the strategy airtight: there’s no hidden “combination” to discover, because any unused capacity can always be filled by a fraction of the next-best item.

This is exactly why the same greedy fails for 0/1 Knapsack, where indivisibility forces choices that greedy can’t undo.

Worked Example

The visualizer packs capacity 50 with three items:

ItemWeightValueRatio (v/w)
Gold10606.0
Silver201005.0
Bronze301204.0
  • Take all Gold (10w → 60), then all Silver (20w → 100): 30w used, value 160.
  • Remaining capacity 20 of Bronze: take ²⁄₃ of Bronze → 80.
  • Total value = 240 — the visualizer’s final answer.

Notice how the last item is partial. If these items were indivisible (0/1), this exact greedy would fail — it would waste the 20 leftover and you’d instead want a clever combination.

Edge Cases & Pitfalls

  • Ties in ratio — order among equal ratios doesn’t affect the total.
  • Zero-value items — ratio 0: never worth taking.
  • Zero-weight items — avoid division by zero; they contribute infinite ratio and are usually excluded.
  • Capacity ≥ total weight — take everything: no fraction needed.
  • Indivisible items — greedy is not optimal: use 0/1 Knapsack DP instead.

Comparison: Fractional vs 0/1 Knapsack

AspectFractional0/1
Items divisibleYesNo
Greedy optimal?YesNo
ComplexityO(n log n)O(nW)
ObjectiveFill by best ratioChoose subset

Applications

  • Bulk cargo — hauling divisible goods (fuel, grain, ore)
  • Time budgeting — allocating hours to tasks by value/hour
  • Network bandwidth — proportional resource sharing

Practice Trajectory

  1. Hand-compute the ratios for the visualizer’s three items and reproduce the fill order.
  2. Confirm the ²⁄₃ Bronze cut: why 20 units, and why value 80?
  3. Change capacity to 100 and recompute — no fraction is needed.
  4. Make Gold indivisible and find a counterexample where greedy fails.
  5. Explain in one sentence why divisibility is the crux of greedy’s optimality here.