1
1
"""
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)
4
4
5
5
Given a graph `g` and an edgemap `w` containing weights associated to edges,
6
6
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
20
20
"""
21
21
function minimum_weight_perfect_matching end
22
22
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} ()
25
27
for (e, c) in w
26
28
if c <= cutoff
27
29
wnew[e] = c
@@ -30,40 +32,43 @@ function minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}, cutoff, kws...)
30
32
return minimum_weight_perfect_matching (g, wnew; kws... )
31
33
end
32
34
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} ()
35
39
cmax = maximum (values (w))
36
40
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
39
44
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)
41
46
end
42
47
match = minimum_weight_perfect_matching (g, wnew)
43
48
weight = zero (U)
44
- for i= 1 : nv (g)
49
+ for i in 1 : nv (g)
45
50
j = match. mate[i]
46
51
if j > i
47
- weight += w[E (i,j)]
52
+ weight += w[E (i, j)]
48
53
end
49
54
end
50
55
return MatchingResult (weight, match. mate)
51
56
end
52
57
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 }
54
59
m = BlossomV. Matching (nv (g))
55
60
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)
57
62
end
58
63
BlossomV. solve (m)
59
64
60
65
mate = fill (- 1 , nv (g))
61
66
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
64
69
mate[i] = j <= 0 ? - 1 : j
65
70
if i < j
66
- totweight += w[Edge (i,j)]
71
+ totweight += w[Edge (i, j)]
67
72
end
68
73
end
69
74
return MatchingResult (totweight, mate)
0 commit comments