From 5129e5e13321b6d78d475a3afd254f87134223ef Mon Sep 17 00:00:00 2001 From: jrtechs Date: Fri, 11 Jan 2019 13:55:20 -0500 Subject: [PATCH] Python implementation of the knapsack problem. --- other/knapSack.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 other/knapSack.py diff --git a/other/knapSack.py b/other/knapSack.py new file mode 100644 index 0000000..4bfdece --- /dev/null +++ b/other/knapSack.py @@ -0,0 +1,52 @@ +""" +Jeffery Russell +12-1-18 +""" + + +def knapsack(V, W, capacity): + """ + Dynamic programming implementation of the knapsack problem + + :param V: List of the values + :param W: List of weights + :param capacity: max capacity of knapsack + :return: List of tuples of objects stolen in form (w, v) + """ + choices = [[[] for i in range(capacity + 1)] for j in range(len(V) + 1)] + cost = [[0 for i in range(capacity + 1)] for j in range(len(V) + 1)] + + for i in range(0, len(V)): + for j in range(0, capacity + 1): + if W[i] > j: # don't include another item + cost[i][j] = cost[i -1][j] + choices[i][j] = choices[i - 1][j] + else: # Adding another item + cost[i][j] = max(cost[i-1][j], cost[i-1][j - W[i]] + V[i]) + if cost[i][j] != cost[i-1][j]: + choices[i][j] = choices[i - 1][j - W[i]] + [(W[i], V[i])] + else: + choices[i][j] = choices[i - 1][j] + return choices[len(V) -1][capacity] + + +def printSolution(S): + """ + Takes the output of knapsack and prints it in a + pretty format. + + :param S: list of tuples representing items stolen + :return: None + """ + print("Thief Took:") + for i in S: + print("Weight: " + str(i[0]) + "\tValue: \t" + str(i[1])) + + print() + print("Total Value Stolen: " + str(sum(int(v[0]) for v in S))) + print("Total Weight in knapsack: " + str(sum(int(v[1]) for v in S))) + + +values = [99,1,1,1,1, 1,1] +weights = [5,1,2,3,4,5,1] +printSolution(knapsack(values, weights, 6)) \ No newline at end of file