Skip to content

Commit

Permalink
fix same-weight bug (issue JuliaGraphs#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
thchr authored Feb 28, 2023
1 parent 7d14c0b commit f9414ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/blossomv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ function minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}; tmaxscale=10.)
wnew = Dict{E, Int32}()
cmax = maximum(values(w))
cmin = minimum(values(w))

if cmin == cmax
# all weights are identical: _any_ pairing is a minimum weight perfect matching
weight = nv(g) * cmin
mate = collect(reverse(vertices(g)))
return MatchingResult(weight, mate)
end

tmax = typemax(Int32) / tmaxscale # /10 is kinda arbitrary,
# hopefully high enough to not occur in overflow problems
for (e, c) in w
Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ end
@test match.mate[2] == 3
@test match.mate[3] == 2
@test match.weight 11.5

# Issue #5
g = Graph(Graph([Edge(1,2), Edge(2,3), Edge(4,1)]))
w = Dict(Edge(1,2)=>2.0, Edge(2,3)=>2.0, Edge(1,4)=>2.0)
match = minimum_weight_perfect_matching(g, w)
@test match.mate == [4,3,2,1]
@test match.weight == 4.0

g = Graph([Edge(1,2)])
wFloat = Dict(Edge(1,2) = 2.0)
wInt = Dict(Edge(1,2) = 2)
matchFloat = minimum_weight_perfect_matching(g, wFloat)
matchInt = minimum_weight_perfect_matching(g, wInt)
@test matchFloat.mate == matchInt.mate
@test matchFloat.weight == matchInt.weight
end


Expand Down

0 comments on commit f9414ee

Please sign in to comment.