From fc695500cd0201d22263f79094cf8d6b826ce697 Mon Sep 17 00:00:00 2001 From: johnpmarshall2000 Date: Sun, 12 Nov 2023 21:32:55 -0500 Subject: [PATCH 1/2] Cleaned up Recursive Knapsack --- 1)knapsack_recursive.py | 47 +++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/1)knapsack_recursive.py b/1)knapsack_recursive.py index f0dd81b5..d47664eb 100644 --- a/1)knapsack_recursive.py +++ b/1)knapsack_recursive.py @@ -1,14 +1,33 @@ -def knapsack_recursive(wt,val,W,n): - if n==0 or W==0: - return 0 - else: - if wt[n-1]<=W: - return max(val[n-1]+knapsack_recursive(wt,val,W-wt[n-1],n-1),knapsack_recursive(wt,val,W,n-1)) - elif wt[n-1]>W: - return knapsack_recursive(wt,val,W,n-1) -W = 6 -wt = [1,2,3,6] -val = [1,2,4,6] -n=4 -knapsack_recursive(wt,val,W,n) - +def knapsack_recursive(weights: list[int], values: list[int], capacity: int, item_index: int): + """Returns the best possible total value for the provided instance of the Knapsack Problem. + + Arguments: + weights -- the list of the weights of the items + values -- the list of the values of the items + capacity -- the maximum weight the knapsack can carry + item_index -- the index of the current item + """ + + # Base Case + if item_index == -1 or capacity == 0:return 0 + + # Check value without carrying the current item + dont_carry_value = knapsack_recursive(weights, values, capacity, item_index) + + # Current item weight is above capacity, return value without carrying + if weights[item_index] > capacity:return dont_carry_value + + # Current item weight isn't above capacity + carry_item_profit = values[item_index] + carry_value = knapsack_recursive(weights, values, capacity - weights[item_index], item_index) + + # Return the larger between the value carrying the item and the value not carrying the item + return max(carry_item_profit + carry_value, dont_carry_value) + + +if __name__ == "__main__": + capacity = 8 + weights = [3, 2, 4] + values = [6, 8, 7] + item_count = len(weights) + print(knapsack_recursive(weights, values, capacity, item_count - 1)) \ No newline at end of file From 0a441a7b69aa9531d7f189d8280b33ade1a9381e Mon Sep 17 00:00:00 2001 From: johnpmarshall2000 Date: Sun, 12 Nov 2023 21:46:59 -0500 Subject: [PATCH 2/2] Fixed an issue with remaining_items (item_offset) --- 1)knapsack_recursive.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/1)knapsack_recursive.py b/1)knapsack_recursive.py index d47664eb..f10904cc 100644 --- a/1)knapsack_recursive.py +++ b/1)knapsack_recursive.py @@ -1,25 +1,25 @@ -def knapsack_recursive(weights: list[int], values: list[int], capacity: int, item_index: int): +def knapsack_recursive(weights: list[int], values: list[int], capacity: int, remaining_items: int): """Returns the best possible total value for the provided instance of the Knapsack Problem. Arguments: - weights -- the list of the weights of the items - values -- the list of the values of the items - capacity -- the maximum weight the knapsack can carry - item_index -- the index of the current item + weights -- the list of the weights of the items + values -- the list of the values of the items + capacity -- the maximum weight the knapsack can carry + remaining_items -- the number of remaining items """ # Base Case - if item_index == -1 or capacity == 0:return 0 + if remaining_items == 0 or capacity == 0:return 0 # Check value without carrying the current item - dont_carry_value = knapsack_recursive(weights, values, capacity, item_index) + dont_carry_value = knapsack_recursive(weights, values, capacity, remaining_items - 1) # Current item weight is above capacity, return value without carrying - if weights[item_index] > capacity:return dont_carry_value + if weights[remaining_items - 1] > capacity:return dont_carry_value # Current item weight isn't above capacity - carry_item_profit = values[item_index] - carry_value = knapsack_recursive(weights, values, capacity - weights[item_index], item_index) + carry_item_profit = values[remaining_items - 1] + carry_value = knapsack_recursive(weights, values, capacity - weights[remaining_items - 1], remaining_items - 1) # Return the larger between the value carrying the item and the value not carrying the item return max(carry_item_profit + carry_value, dont_carry_value) @@ -30,4 +30,4 @@ def knapsack_recursive(weights: list[int], values: list[int], capacity: int, ite weights = [3, 2, 4] values = [6, 8, 7] item_count = len(weights) - print(knapsack_recursive(weights, values, capacity, item_count - 1)) \ No newline at end of file + print(knapsack_recursive(weights, values, capacity, item_count)) \ No newline at end of file