Skip to content

Commit

Permalink
Added W05 materials
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanSol committed May 6, 2024
1 parent 29a6bff commit ef24b7f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
| 02 | Binary search | [Slides][Slides_W02] | [Test][WarmUp_test_W02] | [Contest][ContestID_W02] | 08.05.2024 09:00 UTC+7 |
| 03 | Basic Data sturctures | [Slides][Slides_W03] | [Test][WarmUp_test_W03] | [Contest][ContestID_W03] | 09.05.2024 09:00 UTC+7 |
| 04 | Dynamic programming | [Slides][Slides_W04] | [Test][WarmUp_test_W04] | [Contest][ContestID_W04] | 10.05.2024 09:00 UTC+7 |
<!---
| 05 | Knapsack problem | [Slides][Slides_W05] | [Test][WarmUp_test_W05] | [Contest][ContestID_W05] | 13.05.2024 09:00 UTC+7 |
<!---
| 06 | KMP & Heap | [Slides][Slides_W06] | [Test][WarmUp_test_W06] | [Contest][ContestID_W06] | 14.05.2024 09:00 UTC+7 |
| 07 | DFS & BFS | [Slides][Slides_W07] | [Test][WarmUp_test_W07] | [Contest][ContestID_W07] | 15.05.2024 09:00 UTC+7 |
| 08 | Shortest paths | [Slides][Slides_W08] | [Test][WarmUp_test_W08] | [Contest][ContestID_W08] | 16.05.2024 09:00 UTC+7 |
Expand Down
Binary file added week05_knapsack/HS.BKK.2024.Algo.Class05.pdf
Binary file not shown.
47 changes: 47 additions & 0 deletions week05_knapsack/knapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
W = 8
N = 4
wi = [3, 3, 5, 6]
ci = [3, 5, 10, 14]

d = [[0] * (W + 1) for i in range(N + 1)]
for i in range(1, N + 1):
for w in range(1, W + 1):
if wi[i-1] > w:
d[i][w] = d[i-1][w]
else:
d[i][w] = max(d[i-1][w], d[i-1][w-wi[i-1]]+ci[i-1])

print('\n'.join([' '.join(map(str, d[i])) for i in range(N + 1)]))


# Python3 program to find maximum
# achievable value with a knapsack
# of weight W and multiple instances allowed.

# Returns the maximum value
# with knapsack of W capacity
def unboundedKnapsack(W, n, val, wt):
# dp[i] is going to store maximum
# value with knapsack capacity i.
dp = [0 for i in range(W + 1)]

ans = 0

# Fill dp[] using above recursive formula
for i in range(W + 1):
for j in range(n):
if (wt[j] <= i):
dp[i] = max(dp[i], dp[i - wt[j]] + val[j])
print(dp)
return dp[W]


# Driver program
W = 5
val = [10]
wt = [2]
n = len(val)

print(unboundedKnapsack(W, n, val, wt))

# This code is contributed by Anant Agarwal.

0 comments on commit ef24b7f

Please sign in to comment.