Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hw1/LinZhu/9.6
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
code is in the .py file with explaination
40 changes: 40 additions & 0 deletions hw1/LinZhu/knapsack 9.6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def knapsack_value_dp(weights, values, capacity):
l = len(weights)
Vmax = max(values)
total_value = sum(values)

# Initialize DP table with infinity
INF = float('inf')
dp = [[INF] * (total_value + 1) for _ in range(l + 2)]

# Base case: 0 value requires 0 weight
dp[l + 1][0] = 0

# Build table from item l down to 1
for j in range(l, 0, -1):
for v in range(total_value + 1):
# Option 1: exclude item j-1
dp[j][v] = dp[j + 1][v]
# Option 2: include item j-1, if value allows
if v >= values[j - 1]:
dp[j][v] = min(dp[j][v], weights[j - 1] + dp[j + 1][v - values[j - 1]])

# Find the maximum value v such that dp[1][v] <= capacity
for v in range(total_value, -1, -1):
if dp[1][v] <= capacity:
return v # maximum value achievable within capacity

return 0 # no items can be included

# Example usage
weights = [2, 3, 4, 5]
values = [3, 4, 5, 6]
capacity = 7

result = knapsack_value_dp(weights, values, capacity)
print("Maximum value within capacity:", result)


#If P = NP, the NP problem will be solved in polynomial time,
#O(l^2 * v_max) where v_max is a constant, so bits required for v_max is log2(v_max), which is not a polynomial
#P != NP