Skip to content

Commit

Permalink
Merge pull request #12 from Gnimuc/speed-up
Browse files Browse the repository at this point in the history
1. Improve scalability
2. Reduce memory reallocation
3. Bugfixes
  • Loading branch information
Gnimuc authored Feb 12, 2018
2 parents 04d0fed + cc0c99c commit d3a4acb
Show file tree
Hide file tree
Showing 5 changed files with 1,187 additions and 124 deletions.
31 changes: 21 additions & 10 deletions src/Hungarian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,37 @@ julia> assignment, cost = hungarian(A')
julia> using Missings
julia> costMat = [ missing 1 1
1 0 1
1 1 0 ]
1 0 1
1 1 0 ]
3×3 Array{Union{Float64, Missings.Missing},2}:
missing 1.0 1.0
1.0 0.0 1.0
1.0 1.0 0.0
missing 1.0 1.0
1.0 0.0 1.0
1.0 1.0 0.0
julia> hungarian(costMat)
([2, 1, 3], 2)
```
"""
function hungarian(costMat::AbstractMatrix)
r, c = size(costMat)
# r != c && warn("Currently, the function `hungarian` automatically transposes `cost matrix` when there are more workers than jobs.")
costMatrix = r c ? costMat : costMat'
rowNum, colNum = size(costMat)
# currently, the function `hungarian` automatically transposes `cost matrix` when there are more workers than jobs.
costMatrix = rowNum colNum ? costMat : costMat'
matching = munkres(costMatrix)
assignment = r c ? findn(matching' .== STAR)[1] : [findfirst(matching[:,i] .== STAR) for i = 1:r]
assignment = zeros(Int, rowNum)
rows = rowvals(matching)
for c = 1:size(matching,2), i in nzrange(matching, c)
r = rows[i]
if matching[r,c] == STAR
if rowNum colNum
assignment[r] = c
else
assignment[c] = r
end
end
end
# calculate minimum cost
cost = sum(costMat[i...] for i in zip(1:r, assignment) if i[2] != 0)
cost = sum(costMat[i...] for i in zip(1:rowNum, assignment) if i[2] != 0)
return assignment, cost
end

Expand Down
Loading

0 comments on commit d3a4acb

Please sign in to comment.