1
1
"""
2
- maximum_weight_matching{T <:Real} (g::Graph, w::Dict{Edge,T} = Dict{Edge,Int64}())
2
+ maximum_weight_matching(g::Graph, w::Dict{Edge,Real} -> Dict{Edge,Int64}
3
3
4
4
Given a graph `g` and an edgemap `w` containing weights associated to edges,
5
5
returns a matching with the maximum total weight.
@@ -23,7 +23,7 @@ function maximum_weight_matching end
23
23
24
24
function maximum_weight_matching (g:: Graph ,
25
25
solver:: AbstractMathProgSolver ,
26
- w:: AbstractMatrix{T } = default_weights (g)) where {T <: Real }
26
+ w:: AbstractMatrix{U } = default_weights (g)) where {U <: Real }
27
27
28
28
model = Model (solver = solver)
29
29
n = nv (g)
@@ -32,11 +32,11 @@ function maximum_weight_matching(g::Graph,
32
32
# put the edge weights in w in the right order to be compatible with edge_list
33
33
for j in 1 : n
34
34
for i in 1 : n
35
- if i > j && w[i,j] > zero (T ) && w[j,i] < w[i,j]
35
+ if i > j && w[i,j] > zero (U ) && w[j,i] < w[i,j]
36
36
w[j,i] = w[i,j]
37
37
end
38
38
if Edge (i,j) ∉ edge_list
39
- w[i,j] = zero (T )
39
+ w[i,j] = zero (U )
40
40
end
41
41
end
42
42
end
@@ -47,26 +47,22 @@ function maximum_weight_matching(g::Graph,
47
47
@variable (model, x[edge_list] >= 0 , Int) # requires MIP solver
48
48
end
49
49
@objective (model, Max, sum (x[e]* w[src (e),dst (e)] for e in edge_list))
50
- @constraint (model, c1[i= 1 : n],
51
- sum (x[Edge (i,j)] for j= filter (l -> l > i, neighbors (g,i))) +
52
- sum (x[Edge (j,i)] for j= filter (l -> l <= i, neighbors (g,i)))
53
- <= 1 )
54
50
51
+ @constraint (model, c1[i= 1 : n], sum (x[Edge (minmax (i,j))] for j in neighbors (g,i)) <= 1 )
55
52
status = solve (model)
56
53
solution = getvalue (x)
57
54
cost = getobjectivevalue (model)
58
55
# # TODO : add the option of returning the solve status as part of the MatchingResult type.
59
- return MatchingResult (cost, dict_to_arr (n, solution))
56
+ return MatchingResult (cost, dict_to_arr (n, solution, edge_list ))
60
57
end
61
58
62
59
""" Returns an array of mates from a dictionary that maps edges to {0,1} """
63
- function dict_to_arr (n:: Int64 , solution:: JuMP.JuMPArray{T ,1,Tuple{Array{E,1}}} ) where {T <: Real , E<: Edge }
60
+ function dict_to_arr (n:: Int64 , solution:: JuMP.JuMPArray{U ,1,Tuple{Array{E,1}}} , edge_list :: AbstractVector{E} ) where {U <: Real , E<: Edge }
64
61
mate = fill (- 1 ,n)
65
- for i in keys (solution)
66
- key = i[1 ] # i is a tuple with 1 element.
67
- if solution[key] >= 1 - 1e-5 # Some tolerance to numerical approximations by the solver.
68
- mate[src (key)] = dst (key)
69
- mate[dst (key)] = src (key)
62
+ for e in edge_list
63
+ if solution[e] >= 1 - 1e-5 # Some tolerance to numerical approximations by the solver.
64
+ mate[src (e)] = dst (e)
65
+ mate[dst (e)] = src (e)
70
66
end
71
67
end
72
68
return mate
@@ -79,4 +75,4 @@ function default_weights(g::G) where {G<:AbstractGraph}
79
75
m[src (e),dst (e)] = 1
80
76
end
81
77
return m
82
- end
78
+ end
0 commit comments