What if you could take just the right part of each item to get the most value in your bag?
Why Fractional Knapsack Problem in DSA Typescript?
Imagine you have a backpack with limited space and many items to carry. Each item has a weight and a value. You want to fill your backpack to get the most value possible. But if you try to pick items one by one without a plan, you might fill the bag with heavy, low-value items and miss out on better choices.
Trying to pick items manually is slow and confusing. You might pick heavy items first and run out of space quickly. It's easy to make mistakes and miss the best combination. Also, if you can only take whole items, you might waste space that could be used for parts of valuable items.
The Fractional Knapsack Problem lets you take parts of items, not just whole ones. By sorting items by value per weight and taking the best parts first, you fill your backpack with the highest value possible. This method is fast, simple, and guarantees the best result.
let totalWeight = 0; let totalValue = 0; for (let item of items) { if (totalWeight + item.weight <= maxWeight) { totalWeight += item.weight; totalValue += item.value; } }
items.sort((a, b) => (b.value / b.weight) - (a.value / a.weight)); let totalWeight = 0; let totalValue = 0; for (let item of items) { if (totalWeight + item.weight <= maxWeight) { totalWeight += item.weight; totalValue += item.value; } else { let remain = maxWeight - totalWeight; totalValue += item.value * (remain / item.weight); break; } }
This concept lets you maximize value by smartly choosing whole or parts of items to perfectly fill your limited space.
Imagine packing snacks for a trip with a small bag. You want to carry the tastiest and most energy-packed snacks. Fractional Knapsack helps you decide how much of each snack to pack to get the best energy without overloading your bag.
Manual picking wastes space and value.
Fractional Knapsack sorts items by value per weight.
It allows taking parts of items for maximum value.
Practice
Fractional Knapsack Problem?Solution
Step 1: Understand problem goal
The fractional knapsack problem allows taking parts of items to maximize value within a weight limit.Step 2: Compare options
Maximize total value by taking fractions of items within weight limit correctly states maximizing total value by taking fractions. Others focus on minimizing weight or whole items only, which is incorrect.Final Answer:
Maximize total value by taking fractions of items within weight limit -> Option AQuick Check:
Fractional knapsack = maximize value with fractions [OK]
- Thinking only whole items can be taken
- Confusing minimization with maximization
- Ignoring fractional parts of items
Solution
Step 1: Check array and object syntax
const items = [{weight: 10, value: 60}, {weight: 20, value: 100}]; uses correct array of objects syntax with curly braces and key-value pairs.Step 2: Identify syntax errors in other options
Options A, B, and D have invalid TypeScript syntax for arrays or objects.Final Answer:
const items = [{weight: 10, value: 60}, {weight: 20, value: 100}]; -> Option DQuick Check:
Array of objects syntax = const items = [{weight: 10, value: 60}, {weight: 20, value: 100}]; [OK]
- Using parentheses instead of square brackets for arrays
- Missing key names in objects
- Incorrect object literal syntax
const items = [
{weight: 10, value: 60},
{weight: 20, value: 100},
{weight: 30, value: 120}
];
const capacity = 50;
function fractionalKnapsack(items, capacity) {
items.sort((a, b) => (b.value / b.weight) - (a.value / a.weight));
let totalValue = 0;
let remaining = capacity;
for (const item of items) {
if (item.weight <= remaining) {
totalValue += item.value;
remaining -= item.weight;
} else {
totalValue += item.value * (remaining / item.weight);
break;
}
}
return totalValue;
}
console.log(fractionalKnapsack(items, capacity));Solution
Step 1: Sort items by value/weight ratio
Ratios: 60/10=6, 100/20=5, 120/30=4. Sorted order: item1, item2, item3.Step 2: Add items until capacity 50
Take item1 fully (weight 10, value 60), remaining 40.
Take item2 fully (weight 20, value 100), remaining 20.
Take 20/30 fraction of item3: value = 120 * (20/30) = 80.
Total value = 60 + 100 + 80 = 240.Step 3: Check options
Matches 240.Final Answer:
240 -> Option CQuick Check:
Sum values with fractions = 240 [OK]
- Not sorting items by value/weight ratio
- Taking items without fractions
- Incorrect fraction calculation
function fractionalKnapsack(items, capacity) {
items.sort((a, b) => (a.value / a.weight) - (b.value / b.weight));
let totalValue = 0;
let remaining = capacity;
for (const item of items) {
if (item.weight <= remaining) {
totalValue += item.value;
remaining -= item.weight;
} else {
totalValue += item.value * (remaining / item.weight);
break;
}
}
return totalValue;
}Solution
Step 1: Analyze sorting function
The sort compares (a.value/a.weight) - (b.value/b.weight), which sorts ascending by ratio.Step 2: Understand impact on knapsack
Fractional knapsack requires descending order to pick highest value/weight first. Ascending order causes wrong selection.Final Answer:
Sorting order is ascending instead of descending by value/weight -> Option BQuick Check:
Sort descending by ratio [OK]
- Sorting ascending instead of descending
- Ignoring sorting impact on result
- Confusing variable declarations
Solution
Step 1: Calculate value/weight ratios and sort descending
Ratios: 30/5=6, 40/10=4, 140/20=7.
Sorted order: item3 (7), item1 (6), item2 (4).Step 2: Fill knapsack capacity 15
Take item3 partially: since 20 > 15, value = 140 * (15/20) = 105.
Total value = 105.
Remaining capacity 0, stop.Final Answer:
105 -> Option AQuick Check:
Fractional knapsack max value = 105 [OK]
- Not sorting by value/weight ratio
- Trying to take full items exceeding capacity
- Miscomputing fractional values
Ready to prove it?
Pass the test and this pattern moves unknown
