From d2c4c059b0d05ac26532f094b518fa92cd9a34e9 Mon Sep 17 00:00:00 2001 From: Anandha Kumar J Date: Sat, 5 Oct 2024 13:21:36 +0530 Subject: [PATCH 1/2] Create fractional_knapsack.py --- knapsack/fractional_knapsack.py | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 knapsack/fractional_knapsack.py diff --git a/knapsack/fractional_knapsack.py b/knapsack/fractional_knapsack.py new file mode 100644 index 000000000000..a236232fe2e4 --- /dev/null +++ b/knapsack/fractional_knapsack.py @@ -0,0 +1,38 @@ +"given a set of items, each with a weight and a value, +"determine how to best fill a knapsack of capacity C such that the total value of the items in the knapsack is maximised." + +class Item: + def __init__(self, value, weight): + self.value = value + self.weight = weight + self.cost_per_weight = value / weight # Value per unit weight + +def fractional_knapsack(capacity, items): + # Sort items by cost per weight in descending order + items.sort(key=lambda item: item.cost_per_weight, reverse=True) + + total_value = 0.0 + for item in items: + if capacity == 0: # No more capacity in the knapsack + break + if item.weight <= capacity: + # Take the whole item + capacity -= item.weight + total_value += item.value + else: + # Take the fraction of the item + total_value += item.cost_per_weight * capacity + capacity = 0 # The knapsack is now full + + return total_value + +if __name__ == "__main__": + items = [ + Item(,), #Value and weight inputs can be changed + Item(,) + ] + + c = int(input("Capacity: ")) + capacity = c + max_value = fractional_knapsack(capacity, items) + print(f"Maximum value : {max_value}") From c26d818acd21a5906f44ac68556c177ed296357a Mon Sep 17 00:00:00 2001 From: Anandha Kumar J Date: Sat, 5 Oct 2024 15:15:13 +0530 Subject: [PATCH 2/2] Updated Fractional Knapsack --- knapsack/fractional_knapsack.py | 54 ++++++++++++++++----------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/knapsack/fractional_knapsack.py b/knapsack/fractional_knapsack.py index a236232fe2e4..d52ea699c26d 100644 --- a/knapsack/fractional_knapsack.py +++ b/knapsack/fractional_knapsack.py @@ -1,38 +1,36 @@ -"given a set of items, each with a weight and a value, -"determine how to best fill a knapsack of capacity C such that the total value of the items in the knapsack is maximised." +def fractional_knapsack(capacity, values, weights): -class Item: - def __init__(self, value, weight): - self.value = value - self.weight = weight - self.cost_per_weight = value / weight # Value per unit weight - -def fractional_knapsack(capacity, items): - # Sort items by cost per weight in descending order - items.sort(key=lambda item: item.cost_per_weight, reverse=True) + """ + >>> capacity = 50 + >>> values = [60, 100, 120] + >>> weight = [10, 20, 30] + >>> fractional_knapsack(capacity, values, weight) + 240.0 + """ + # Calculate value-to-weight ratio for each item + + items = [(values[i] / weights[i], values[i], weights[i]) for i in range(len(values))] + + # Sort items by their value-to-weight ratio in descending order + items.sort(key=lambda x: x[0], reverse=True) - total_value = 0.0 - for item in items: - if capacity == 0: # No more capacity in the knapsack + total_value = 0 # Total value of items taken + + for ratio, value, weight in items: + if capacity == 0: # If the knapsack is full, break the loop break - if item.weight <= capacity: + + if weight <= capacity: # Take the whole item - capacity -= item.weight - total_value += item.value + total_value += value + capacity -= weight else: - # Take the fraction of the item - total_value += item.cost_per_weight * capacity + total_value += ratio * capacity # Value of the fraction taken capacity = 0 # The knapsack is now full return total_value + if __name__ == "__main__": - items = [ - Item(,), #Value and weight inputs can be changed - Item(,) - ] - - c = int(input("Capacity: ")) - capacity = c - max_value = fractional_knapsack(capacity, items) - print(f"Maximum value : {max_value}") + import doctest + doctest.testmod() \ No newline at end of file