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
55Given a graph `g` and an edgemap `w` containing weights associated to edges,
66returns 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"""
2121function 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... )
3133end
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)
5156end
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)
0 commit comments