Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add matrix argument for minimum_weight_perfect_matching #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
WatchJuliaBurn = "952999c0-2bc0-44cb-b633-dc1e299b187b"

[compat]
BlossomV = "0.4"
Graphs = "1.4, 1.7"
Hungarian = "0.4, 0.6"
JuMP = "1"
MathOptInterface = "1"
WatchJuliaBurn = "0.2.0"
julia = "1"

[extras]
Expand Down
32 changes: 22 additions & 10 deletions src/blossomv.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
"""
minimum_weight_perfect_matching(g, w::Dict{Edge,Real})
minimum_weight_perfect_matching(g, w::Dict{Edge,Real}, cutoff)
minimum_weight_perfect_matching(g, w::AbstractMatrix{U})

Given a graph `g` and an edgemap `w` containing weights associated to edges,
returns a matching with the mimimum total weight among the ones containing
exactly `nv(g)/2` edges.

Edges in `g` not present in `w` will not be considered for the matching.
Given a graph `g` and weights `w`, returns a matching with the mimimum total
weight among the ones containing exactly `nv(g)/2` edges.

This function relies on the BlossomV.jl package, a julia wrapper
around Kolmogorov's BlossomV algorithm.

Eventually a `cutoff` argument can be given, to the reduce computational time
excluding edges with weights higher than the cutoff.

The returned object is of type `MatchingResult`.

In case of error try to change the optional argument `tmaxscale` (default is `tmaxscale=10`).
Expand Down Expand Up @@ -68,3 +61,22 @@ function minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}) where {U<:Integ
end
return MatchingResult(totweight, mate)
end

function minimum_weight_perfect_matching(g::Graph, w::AbstractMatrix{U}, cutoff, kws...) where {U <: Real}
wnew = Dict{Edge, U}()
for e in edges(g)
c = w[src(e), dst(e)]
if c <= cutoff
wnew[e] = c
end
end
return minimum_weight_perfect_matching(g, wnew, kws...)
end

function minimum_weight_perfect_matching(g::Graph, w::AbstractMatrix{U} = default_weights(g), kws...) where {U <: Real}
wnew = Dict{Edge, U}()
for e in edges(g)
wnew[e] = w[src(e), dst(e)]
end
return minimum_weight_perfect_matching(g, wnew, kws...)
end
Loading