codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

35 / 65 topics
32Greedy Algorithms Basics33Activity Selection Problem34Huffman Coding35Fractional Knapsack Problem
Tutorials/Data Structures & Algorithms/Fractional Knapsack Problem
🧮Data Structures & Algorithms

Fractional Knapsack Problem

Updated 2026-04-20
3 min read

Fractional Knapsack Problem

Introduction

The Fractional Knapsack Problem is a classic optimization problem that involves maximizing the total value of items placed inside a knapsack without exceeding its weight capacity. Unlike the 0/1 Knapsack Problem, where each item must be taken or left behind entirely, the Fractional Knapsack Problem allows for taking fractions of an item.

This tutorial will guide you through understanding the problem, implementing a solution using a greedy algorithm, and discussing best practices and real-world applications.

Understanding the Problem

Problem Statement

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. Unlike the 0/1 Knapsack Problem, you can take fractions of an item.

Example

Consider the following items:

ItemWeight (kg)Value ($)
A23
B34
C45

Suppose the knapsack has a weight limit of 7 kg. The goal is to maximize the total value without exceeding the weight limit.

Greedy Algorithm Approach

The Fractional Knapsack Problem can be efficiently solved using a greedy algorithm. The idea is to select items based on their value-to-weight ratio, starting with the highest ratio until the knapsack is full or no more items can be added.

Steps

  1. Calculate Value-to-Weight Ratio: For each item, compute its value-to-weight ratio.
  2. Sort Items by Ratio: Sort the items in descending order based on their value-to-weight ratio.
  3. Select Items Greedily: Start adding items to the knapsack from the highest ratio until the weight limit is reached or no more items can be added.

Pseudocode

function fractionalKnapsack(weights, values, capacity):
    n = length(weights)
    ratios = []
    
    for i from 0 to n-1:
        ratios.append((values[i] / weights[i], weights[i], values[i]))
    
    sort ratios in descending order by value-to-weight ratio
    
    totalValue = 0
    remainingCapacity = capacity
    
    for item in ratios:
        if remainingCapacity == 0:
            break
        
        weight, value = item[1], item[2]
        
        if weight <= remainingCapacity:
            totalValue += value
            remainingCapacity -= weight
        else:
            fraction = remainingCapacity / weight
            totalValue += value * fraction
            remainingCapacity = 0
    
    return totalValue

Implementation in JavaScript

Here's a complete implementation of the Fractional Knapsack Problem using JavaScript:

function fractionalKnapsack(weights, values, capacity) {
    const n = weights.length;
    const ratios = [];

    for (let i = 0; i < n; i++) {
        ratios.push({
            ratio: values[i] / weights[i],
            weight: weights[i],
            value: values[i]
        });
    }

    // Sort items by value-to-weight ratio in descending order
    ratios.sort((a, b) => b.ratio - a.ratio);

    let totalValue = 0;
    let remainingCapacity = capacity;

    for (const item of ratios) {
        if (remainingCapacity === 0) break;

        const weight = item.weight;
        const value = item.value;

        if (weight <= remainingCapacity) {
            totalValue += value;
            remainingCapacity -= weight;
        } else {
            const fraction = remainingCapacity / weight;
            totalValue += value * fraction;
            remainingCapacity = 0;
        }
    }

    return totalValue;
}

// Example usage
const weights = [2, 3, 4];
const values = [3, 4, 5];
const capacity = 7;

console.log(fractionalKnapsack(weights, values, capacity)); // Output: 8.333333333333334

Best Practices

  1. Input Validation: Ensure that the input arrays for weights and values are of the same length and contain valid numbers.
  2. Edge Cases: Handle cases where the knapsack capacity is zero or when all items have a weight greater than the capacity.
  3. Performance Considerations: The algorithm has a time complexity of \(O(n \log n)\) due to sorting, which is efficient for moderate-sized inputs.

Real-World Applications

  1. Resource Allocation: Allocating resources like bandwidth, memory, or CPU time in computing environments.
  2. Investment Portfolio Optimization: Determining the optimal allocation of funds across different assets.
  3. Production Scheduling: Deciding how to allocate production capacity among various products.

Conclusion

The Fractional Knapsack Problem is a practical example of how greedy algorithms can be used to solve optimization problems efficiently. By understanding the problem, implementing the solution, and considering best practices, you can apply this algorithm to real-world scenarios where fractional selection is allowed.

Feel free to experiment with different inputs and scenarios to deepen your understanding of this classic problem.


PreviousHuffman CodingNext Backtracking Basics

Recommended Gear

Huffman CodingBacktracking Basics