struct Item {
value: f64,
weight: f64,
}
fn fractional_knapsack(items: &mut [Item], capacity: f64) -> f64 {
items.sort_by(|a, b| (b.value / b.weight).partial_cmp(&(a.value / a.weight)).unwrap());
let mut total_value = 0.0;
let mut remaining_capacity = capacity;
for item in items {
if item.weight <= remaining_capacity {
total_value += item.value;
remaining_capacity -= item.weight;
} else {
total_value += item.value * (remaining_capacity / item.weight);
break;
}
}
total_value
}
fn main() {
let mut items = vec![
Item { value: 60.0, weight: 10.0 },
Item { value: 100.0, weight: 20.0 },
Item { value: 120.0, weight: 30.0 },
];
let capacity = 50.0;
let max_value = fractional_knapsack(&mut items, capacity);
println!("{:.2}", max_value);
}The fractional knapsack algorithm sorts items by their value-to-weight ratio in descending order. It then takes whole items until the capacity is reached, and finally takes a fraction of the next item if needed.
For the given items, the ratios are:
- Item 1: 60/10 = 6
- Item 2: 100/20 = 5
- Item 3: 120/30 = 4
We take all of item 1 (weight 10, value 60), all of item 2 (weight 20, value 100), and 20 weight units of item 3 (since capacity left is 20), which gives 120 * (20/30) = 80 value.
Total value = 60 + 100 + 80 = 240.
The fractional knapsack problem allows taking fractions of items, which means we can always take the most valuable part first. This property makes the greedy approach optimal.
In contrast, the 0/1 knapsack problem requires whole items, which is not solvable optimally by a simple greedy method.
struct Item {
value: f64,
weight: f64,
}
fn fractional_knapsack(items: &mut [Item], capacity: f64) -> f64 {
items.sort_by(|a, b| (a.value / a.weight).partial_cmp(&(b.value / b.weight)).unwrap());
let mut total_value = 0.0;
let mut remaining_capacity = capacity;
for item in items {
if item.weight <= remaining_capacity {
total_value += item.value;
remaining_capacity -= item.weight;
} else {
total_value += item.value * (remaining_capacity / item.weight);
break;
}
}
total_value
}
fn main() {
let mut items = vec![
Item { value: 60.0, weight: 10.0 },
Item { value: 100.0, weight: 20.0 },
Item { value: 120.0, weight: 30.0 },
];
let capacity = 50.0;
let max_value = fractional_knapsack(&mut items, capacity);
println!("{:.2}", max_value);
}The code sorts items by ascending order of value-to-weight ratio because it compares a.value/a.weight to b.value/b.weight directly without reversing the order.
This causes the algorithm to pick the least valuable items first, resulting in a suboptimal total value.
struct Item {
value: f64,
weight: f64,
}
fn fractional_knapsack(items: &mut [Item], capacity: f64) -> f64 {
items.sort_by(|a, b| (b.value / b.weight).partial_cmp(&(a.value / a.weight)).unwrap());
let mut total_value = 0.0;
let mut remaining_capacity = capacity;
for item in items {
if item.weight <= remaining_capacity {
total_value += item.value;
remaining_capacity -= item.weight;
} else {
total_value += item.value * (remaining_capacity / item.weight);
break;
}
}
total_value
}
fn main() {
let mut items = vec![
Item { value: 60.0, weight: 10.0 },
Item { value: 100.0, weight: 20.0 },
Item { value: 120.0, weight: 30.0 },
];
let capacity = 50.0;
let max_value = fractional_knapsack(&mut items, capacity);
println!("{:.2}", max_value);
}The line 'let mut total_value = 0.0' is missing a semicolon at the end, causing a syntax error.
All other parts have correct syntax.
struct Item {
value: f64,
weight: f64,
}
fn fractional_knapsack(items: &mut [Item], capacity: f64) -> f64 {
items.sort_by(|a, b| (b.value / b.weight).partial_cmp(&(a.value / a.weight)).unwrap());
let mut total_value = 0.0;
let mut remaining_capacity = capacity;
for item in items {
if item.weight <= remaining_capacity {
total_value += item.value;
remaining_capacity -= item.weight;
} else {
total_value += item.value * (remaining_capacity / item.weight);
break;
}
}
total_value
}
fn main() {
let mut items = vec![
Item { value: 500.0, weight: 50.0 },
Item { value: 400.0, weight: 40.0 },
Item { value: 300.0, weight: 30.0 },
Item { value: 200.0, weight: 20.0 },
];
let capacity = 100.0;
let max_value = fractional_knapsack(&mut items, capacity);
println!("{:.2}", max_value);
}Calculate value-to-weight ratios:
- Item 1: 500/50 = 10
- Item 2: 400/40 = 10
- Item 3: 300/30 = 10
- Item 4: 200/20 = 10
All ratios are equal at 10, so the maximum value is 10 * 100 = 1000.00 regardless of selection order.
The code sorts by descending ratio (stable for equals), takes Item 1 (50w, 500v), Item 2 (40w, 400v), then 10/30 of Item 3 (100v), totaling 1000.00.
