Skip to content

Commit 76286e2

Browse files
thchretiennedeg
andauthored
Fix same-weight bug (issue #5) (#6)
* fix same-weight bug (issue #5) * Fix weight * Fix typos * change to first strategy * revert to spaces * correct formatting using blue style --------- Co-authored-by: etienneINSA <[email protected]>
1 parent 7d14c0b commit 76286e2

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

src/blossomv.jl

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
minimum_weight_perfect_matching(g, w::Dict{Edge,Real})
3-
minimum_weight_perfect_matching(g, w::Dict{Edge,Real}, cutoff)
2+
minimum_weight_perfect_matching(g, w::Dict{Edge,Real})
3+
minimum_weight_perfect_matching(g, w::Dict{Edge,Real}, cutoff)
44
55
Given a graph `g` and an edgemap `w` containing weights associated to edges,
66
returns a matching with the mimimum total weight among the ones containing
@@ -20,8 +20,10 @@ In case of error try to change the optional argument `tmaxscale` (default is `tm
2020
"""
2121
function minimum_weight_perfect_matching end
2222

23-
function minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}, cutoff, kws...) where {U<:Real, E<:Edge}
24-
wnew = Dict{E, U}()
23+
function minimum_weight_perfect_matching(
24+
g::Graph, w::Dict{E,U}, cutoff, kws...
25+
) where {U<:Real,E<:Edge}
26+
wnew = Dict{E,U}()
2527
for (e, c) in w
2628
if c <= cutoff
2729
wnew[e] = c
@@ -30,40 +32,43 @@ function minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}, cutoff, kws...)
3032
return minimum_weight_perfect_matching(g, wnew; kws...)
3133
end
3234

33-
function minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}; tmaxscale=10.) where {U<:AbstractFloat, E<:Edge}
34-
wnew = Dict{E, Int32}()
35+
function minimum_weight_perfect_matching(
36+
g::Graph, w::Dict{E,U}; tmaxscale=10.0
37+
) where {U<:AbstractFloat,E<:Edge}
38+
wnew = Dict{E,Int32}()
3539
cmax = maximum(values(w))
3640
cmin = minimum(values(w))
37-
tmax = typemax(Int32) / tmaxscale # /10 is kinda arbitrary,
38-
# hopefully high enough to not occur in overflow problems
41+
42+
tmax = typemax(Int32) / tmaxscale # /10 is kinda arbitrary,
43+
# hopefully high enough to not occur in overflow problems
3944
for (e, c) in w
40-
wnew[e] = round(Int32, (c-cmin) / (cmax-cmin) * tmax)
45+
wnew[e] = round(Int32, (c - cmin) / max(cmax - cmin, 1) * tmax)
4146
end
4247
match = minimum_weight_perfect_matching(g, wnew)
4348
weight = zero(U)
44-
for i=1:nv(g)
49+
for i in 1:nv(g)
4550
j = match.mate[i]
4651
if j > i
47-
weight += w[E(i,j)]
52+
weight += w[E(i, j)]
4853
end
4954
end
5055
return MatchingResult(weight, match.mate)
5156
end
5257

53-
function minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}) where {U<:Integer, E<:Edge}
58+
function minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}) where {U<:Integer,E<:Edge}
5459
m = BlossomV.Matching(nv(g))
5560
for (e, c) in w
56-
BlossomV.add_edge(m, src(e)-1, dst(e)-1, c)
61+
BlossomV.add_edge(m, src(e) - 1, dst(e) - 1, c)
5762
end
5863
BlossomV.solve(m)
5964

6065
mate = fill(-1, nv(g))
6166
totweight = zero(U)
62-
for i=1:nv(g)
63-
j = BlossomV.get_match(m, i-1) + 1
67+
for i in 1:nv(g)
68+
j = BlossomV.get_match(m, i - 1) + 1
6469
mate[i] = j <= 0 ? -1 : j
6570
if i < j
66-
totweight += w[Edge(i,j)]
71+
totweight += w[Edge(i, j)]
6772
end
6873
end
6974
return MatchingResult(totweight, mate)

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,21 @@ end
254254
@test match.mate[2] == 3
255255
@test match.mate[3] == 2
256256
@test match.weight 11.5
257+
258+
# Issue #5
259+
g = Graph(Graph([Edge(1,2), Edge(2,3), Edge(4,1)]))
260+
w = Dict(Edge(1,2) => 2.0, Edge(2,3) => 2.0, Edge(1,4) => 2.0)
261+
match = minimum_weight_perfect_matching(g, w)
262+
@test match.mate == [4,3,2,1]
263+
@test match.weight == 4.0
264+
265+
g = Graph([Edge(1,2)])
266+
wFloat = Dict(Edge(1,2) => 2.0)
267+
wInt = Dict(Edge(1,2) => 2)
268+
matchFloat = minimum_weight_perfect_matching(g, wFloat)
269+
matchInt = minimum_weight_perfect_matching(g, wInt)
270+
@test matchFloat.mate == matchInt.mate
271+
@test matchFloat.weight == matchInt.weight
257272
end
258273

259274

0 commit comments

Comments
 (0)