Skip to content

Commit eaec345

Browse files
committed
move BlossomV to an extension to permit GraphsMatching install on any system
1 parent 20efab0 commit eaec345

File tree

6 files changed

+57
-23
lines changed

6 files changed

+57
-23
lines changed

Project.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ authors = ["JuliaGraphs"]
44
version = "0.2.1"
55

66
[deps]
7-
BlossomV = "6c721016-9dae-5d90-abf6-67daaccb2332"
87
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
98
Hungarian = "e91730f6-4275-51fb-a7a0-7064cfbd3b39"
109
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
@@ -13,14 +12,20 @@ LEMONGraphs = "14b1564f-c77f-4800-9e89-efd961faef7c"
1312
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
1413
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1514

15+
[weakdeps]
16+
BlossomV = "6c721016-9dae-5d90-abf6-67daaccb2332"
17+
18+
[extensions]
19+
GraphsMatchingBlossomVExt = "BlossomV"
20+
1621
[compat]
1722
BlossomV = "0.4"
1823
Graphs = "1.4, 1.7"
1924
Hungarian = "0.4, 0.6, 0.7"
2025
JuMP = "1"
2126
LEMONGraphs = "0.1.0"
2227
MathOptInterface = "1"
23-
julia = "1"
28+
julia = "1.10"
2429

2530
[extras]
2631
Cbc = "9961bab8-2fa3-5c5a-9d89-47fab24efd76"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module GraphsMatchingBlossomVExt
2+
3+
using Graphs
4+
using GraphsMatching
5+
import BlossomV
6+
7+
function GraphsMatching.minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}, ::BlossomVAlgorithm) where {U<:Integer,E<:Edge}
8+
m = BlossomV.Matching(nv(g))
9+
for (e, c) in w
10+
BlossomV.add_edge(m, src(e) - 1, dst(e) - 1, c)
11+
end
12+
BlossomV.solve(m)
13+
14+
mate = fill(-1, nv(g))
15+
totweight = zero(U)
16+
for i in 1:nv(g)
17+
j = BlossomV.get_match(m, i - 1) + 1
18+
mate[i] = j <= 0 ? -1 : j
19+
if i < j
20+
totweight += w[Edge(i, j)]
21+
end
22+
end
23+
return MatchingResult(totweight, mate)
24+
end
25+
26+
end

src/GraphsMatching.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ using SparseArrays: spzeros
77
using JuMP
88
using MathOptInterface
99
const MOI = MathOptInterface
10-
import BlossomV
1110
import LEMONGraphs
1211
using Hungarian
1312

@@ -21,6 +20,16 @@ export MatchingResult,
2120
BlossomVAlgorithm,
2221
LEMONMWPMAlgorithm
2322

23+
function __init__()
24+
if isdefined(Base.Experimental, :register_error_hint)
25+
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
26+
if BlossomVAlgorithm in argtypes
27+
print(io,"""\nPlease first import `BlossomV` to make BlossomV-based matching available.""")
28+
end
29+
end
30+
end
31+
end
32+
2433
"""
2534
struct MatchingResult{U}
2635
weight::U

src/minimum_weight_perfect_matching.jl

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function minimum_weight_perfect_matching(
7777
end
7878

7979
function minimum_weight_perfect_matching(
80-
g::Graph, w::Dict{E,U}, cutoff, algorithm::AbstractMinimumWeightPerfectMatchingAlgorithm=LEMONMWPMAlgorithm(); kws...
80+
g::Graph, w::Dict{E,U}, cutoff::Real, algorithm::AbstractMinimumWeightPerfectMatchingAlgorithm=LEMONMWPMAlgorithm(); kws...
8181
) where {U<:Real,E<:Edge}
8282
wnew = Dict{E,U}()
8383
for (e, c) in w
@@ -111,25 +111,6 @@ function minimum_weight_perfect_matching(
111111
return MatchingResult(weight, match.mate)
112112
end
113113

114-
function minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}, ::BlossomVAlgorithm) where {U<:Integer,E<:Edge}
115-
m = BlossomV.Matching(nv(g))
116-
for (e, c) in w
117-
BlossomV.add_edge(m, src(e) - 1, dst(e) - 1, c)
118-
end
119-
BlossomV.solve(m)
120-
121-
mate = fill(-1, nv(g))
122-
totweight = zero(U)
123-
for i in 1:nv(g)
124-
j = BlossomV.get_match(m, i - 1) + 1
125-
mate[i] = j <= 0 ? -1 : j
126-
if i < j
127-
totweight += w[Edge(i, j)]
128-
end
129-
end
130-
return MatchingResult(totweight, mate)
131-
end
132-
133114
function minimum_weight_perfect_matching(g::Graph, w::Dict{E,U}, ::LEMONMWPMAlgorithm) where {U<:Integer,E<:Edge}
134115
max = 2*abs(maximum(values(w)))
135116
weights = [-get(w, e, max) for e in edges(g)]

test/Project.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[deps]
2+
BlossomV = "6c721016-9dae-5d90-abf6-67daaccb2332"
3+
Cbc = "9961bab8-2fa3-5c5a-9d89-47fab24efd76"
4+
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
5+
Hungarian = "e91730f6-4275-51fb-a7a0-7064cfbd3b39"
6+
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
7+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
8+
LEMONGraphs = "14b1564f-c77f-4800-9e89-efd961faef7c"
9+
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
10+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
11+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ using Cbc
55
using JuMP
66
using LinearAlgebra: I
77

8+
import BlossomV # to test the extension
9+
810
@testset "GraphsMatching" begin
911

1012
@testset "maximum_weight_matching_reduction" begin

0 commit comments

Comments
 (0)