|
| 1 | +""" |
| 2 | + AbstractMaximumWeightMaximalMatchingAlgorithm |
| 3 | +Abstract type that allows users to pass in their preferred algorithm |
| 4 | +""" |
| 5 | +abstract type AbstractMaximumWeightMaximalMatchingAlgorithm end |
| 6 | + |
| 7 | +""" |
| 8 | + LPAlgorithm <: AbstractMaximumWeightMaximalMatchingAlgorithm |
| 9 | +Forces the maximum_weight_maximal_matching function to use a linear programming formulation. |
| 10 | +""" |
| 11 | +struct LPAlgorithm <: AbstractMaximumWeightMaximalMatchingAlgorithm end |
| 12 | + |
| 13 | +function maximum_weight_maximal_matching( |
| 14 | + g::Graph, |
| 15 | + w::AbstractMatrix{T}, |
| 16 | + algorithm::LPAlgorithm, |
| 17 | + solver = nothing |
| 18 | +) where {T<:Real} |
| 19 | + if ! isa(solver, AbstractMathProgSolver) |
| 20 | + error("The keyword argument solver must be an AbstractMathProgSolver, as accepted by JuMP.") |
| 21 | + end |
| 22 | + |
| 23 | + return maximum_weight_maximal_matching_lp(g, solver, w) |
| 24 | +end |
| 25 | + |
| 26 | +""" |
| 27 | + HungarianAlgorithm <: AbstractMaximumWeightMaximalMatchingAlgorithm |
| 28 | +Forces the maximum_weight_maximal_matching function to use the Hungarian algorithm. |
| 29 | +""" |
| 30 | +struct HungarianAlgorithm <: AbstractMaximumWeightMaximalMatchingAlgorithm end |
| 31 | + |
| 32 | +function maximum_weight_maximal_matching( |
| 33 | + g::Graph, |
| 34 | + w::AbstractMatrix{T}, |
| 35 | + algorithm::HungarianAlgorithm, |
| 36 | + solver = nothing |
| 37 | +) where {T<:Real} |
| 38 | + return maximum_weight_maximal_matching_hungarian(g, w) |
| 39 | +end |
| 40 | + |
| 41 | +""" |
| 42 | + maximum_weight_maximal_matching{T<:Real}(g, w::Dict{Edge,T}) |
| 43 | +
|
| 44 | +Given a bipartite graph `g` and an edge map `w` containing weights associated to edges, |
| 45 | +returns a matching with the maximum total weight among the ones containing the |
| 46 | +greatest number of edges. |
| 47 | +
|
| 48 | +Edges in `g` not present in `w` will not be considered for the matching. |
| 49 | +
|
| 50 | +A `cutoff` keyword argument can be given, to reduce computational times |
| 51 | +excluding edges with weights lower than the cutoff. |
| 52 | +
|
| 53 | +Finally, a specific algorithm can be chosen (`algorithm` keyword argument); |
| 54 | +each algorithm has specific dependencies. For instance: |
| 55 | +
|
| 56 | +- If `algorithm=HungarianAlgorithm()` (the default), the package Hungarian.jl is used. |
| 57 | + This algorithm is always polynomial in time, with complexity O(n³). |
| 58 | +- If `algorithm=LPAlgorithm()`, the package JuMP.jl and one of its supported solvers is required. |
| 59 | + In this case, the algorithm relies on a linear relaxation on of the matching problem, which is |
| 60 | + guaranteed to have integer solution on bipartite graphs. A solver must be provided with |
| 61 | + the `solver` keyword parameter. |
| 62 | +
|
| 63 | +The returned object is of type `MatchingResult`. |
| 64 | +""" |
| 65 | +function maximum_weight_maximal_matching( |
| 66 | + g::Graph, |
| 67 | + w::AbstractMatrix{T}; |
| 68 | + cutoff = nothing, |
| 69 | + algorithm::AbstractMaximumWeightMaximalMatchingAlgorithm = HungarianAlgorithm(), |
| 70 | + solver = nothing |
| 71 | + ) where {T<:Real} |
| 72 | + |
| 73 | + if cutoff != nothing && ! isa(cutoff, Real) |
| 74 | + error("The cutoff value must be of type Real or nothing.") |
| 75 | + end |
| 76 | + |
| 77 | + if cutoff != nothing |
| 78 | + return maximum_weight_maximal_matching(g, cutoff_weights(w, cutoff), algorithm, solver) |
| 79 | + else |
| 80 | + return maximum_weight_maximal_matching(g, w, algorithm, solver) |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +""" |
| 85 | + cutoff_weights copies the weight matrix with all elements below cutoff set to 0 |
| 86 | +""" |
| 87 | +function cutoff_weights(w::AbstractMatrix{T}, cutoff::R) where {T<:Real, R<:Real} |
| 88 | + wnew = copy(w) |
| 89 | + for j in 1:size(w,2) |
| 90 | + for i in 1:size(w,1) |
| 91 | + if wnew[i,j] < cutoff |
| 92 | + wnew[i,j] = zero(T) |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + wnew |
| 97 | +end |
| 98 | + |
| 99 | +@deprecate maximum_weight_maximal_matching(g::Graph, solver::AbstractMathProgSolver, w::AbstractMatrix{T}, cutoff::R) where {T<:Real, R<:Real} maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), cutoff=cutoff, solver=solver) |
0 commit comments