Skip to content

Commit

Permalink
adding knapsack (TheAlgorithms#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rratic committed Jun 13, 2021
1 parent a488637 commit 2fd53b8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/TheAlgorithms.jl
Expand Up @@ -12,6 +12,10 @@ using Random
## Exports
# Please keep the folders/functions sorted

# Exports: knapsack
export ZeroOnePack!
export CompletePack!

# Exports: math
export collatz_sequence
export euler_method
Expand Down Expand Up @@ -42,6 +46,9 @@ export is_palindrome
## Includes
# Please keep the folders/files sorted (by dependencies then alphabetical order)

# Includes: knapsack
include("knapsack/knapsack.jl")

# Includes: math
include("math/area_under_curve.jl")
include("math/collatz_sequence.jl")
Expand Down
47 changes: 47 additions & 0 deletions src/knapsack/knapsack.jl
@@ -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
17 changes: 17 additions & 0 deletions test/knapsack.jl
@@ -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
1 change: 1 addition & 0 deletions test/runtests.jl
Expand Up @@ -10,6 +10,7 @@ using Random

@testset "TheAlgorithms" begin

include("knapsack.jl")
include("math.jl")
include("matrix.jl")
include("project-rosalind.jl")
Expand Down

0 comments on commit 2fd53b8

Please sign in to comment.