forked from TheAlgorithms/Julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
This does 0-1 (each item can be chosen only once) knapsack : | ||
pack capacity = capacity | ||
weight of each item = weights | ||
value of each item = values | ||
dp array is what the function works on | ||
It returns the ans (dp[capacity]) | ||
``` | ||
julia> dp=zeros(Int,30) | ||
julia> ZeroOnePack!(20,[1,3,11],[2,5,30],dp) | ||
37 | ||
``` | ||
""" | ||
function ZeroOnePack!(capacity::N,weights::V,values::V,dp::V)where N<:Number where V<:AbstractVector | ||
for i in 1:length(weights) | ||
j=capacity | ||
while j>weights[i] # reversed loop | ||
dp[j]=max(dp[j],dp[j-weights[i]]+values[i]) | ||
j-=1 | ||
end | ||
dp[weights[i]]=max(dp[weights[i]],values[i]) # dp[j]=max(dp[j],dp[0]+values[i]) | ||
end | ||
return dp[capacity] | ||
end | ||
|
||
""" | ||
This does complete/infinite (each item can be chosen infinite times) knapsack : | ||
pack capacity = capacity | ||
weight of each item = weights | ||
value of each item = values | ||
dp array is what the function works on | ||
It returns the ans (dp[capacity]) | ||
``` | ||
julia> dp=zeros(Int,30) | ||
julia> CompletePack!(20,[1,2,9],[1,3,20],dp) | ||
43 | ||
``` | ||
""" | ||
function CompletePack!(capacity::N,weights::V,values::V,dp::V)where N<:Number where V<:AbstractVector | ||
for i in 1:length(weights) | ||
dp[weights[i]]=max(dp[weights[i]],values[i]) # dp[j]=max(dp[j],dp[0]+values[i]) | ||
for j in weights[i]+1:capacity | ||
dp[j]=max(dp[j],dp[j-weights[i]]+values[i]) | ||
end | ||
end | ||
return dp[capacity] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@testset "Knapsack" begin | ||
|
||
@testset "Knapsack: ZeroOnePack!" begin | ||
dp=zeros(Int,30) | ||
@test ZeroOnePack!(20,[1,3,11],[2,5,30],dp) == 37 | ||
dp=zeros(Int,30) | ||
@test ZeroOnePack!(10,[1,3,11],[20,5,80],dp) == 25 | ||
end | ||
|
||
@testset "Knapsack: CompletePack!" begin | ||
dp=zeros(Int,30) | ||
@test CompletePack!(20,[1,2,9],[1,3,20],dp) == 43 | ||
dp=zeros(Int,30) | ||
@test CompletePack!(10,[1,3,11],[20,5,80],dp) == 200 | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters