Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Merge b4746a9 into 6765fbf
Browse files Browse the repository at this point in the history
  • Loading branch information
matbesancon committed Apr 7, 2018
2 parents 6765fbf + b4746a9 commit 76936db
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
*.jl.cov
*.jl.*.cov
*.jl.mem
docs/build
44 changes: 44 additions & 0 deletions README.md
Expand Up @@ -6,3 +6,47 @@

[![codecov](https://codecov.io/gh/JuliaGraphs/LightGraphsMatching.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaGraphs/LightGraphsMatching.jl)

Matching algorithms on top of [LightGraphs](https://github.com/JuliaGraphs/LightGraphs).

## Usage

The results of any matching is returned as a `MatchingResult` struct
containing the `mate` and `weight` fields.

### Perfect matching

```julia
g =CompleteGraph(4)
w =Dict{Edge,Float64}()
w[Edge(1,3)] = 10
w[Edge(1,4)] = 0.5
w[Edge(2,3)] = 11
w[Edge(2,4)] = 2
w[Edge(1,2)] = 100

# find the perfect matching of minimum weight
match = minimum_weight_perfect_matching(g, w, 50)
# match.mate[1] == 4
# match.mate[4] == 1
# match.mate[2] == 3
# match.mate[3] == 2
# match.weight ≈ 11.5
```

### Maximum weight matching

A maximum weight matching is solved as a Linear Programming
problem and requires a LP solver respecting the [MathProgBase](https://github.com/JuliaOpt/MathProgBase.jl) solver
interface. See MathProgBase
[documentation](http://mathprogbasejl.readthedocs.io/en/latest/solvers.html) for more details.

```julia
using Cbc: CbcSolver #import a LP solver
g = CompleteGraph(3)
w = zeros(3,3)
w[1,2] = 1
w[3,2] = 1
w[1,3] = 1
match = maximum_weight_matching(g,CbcSolver(),w)
# match.weight ≈ 1
```
12 changes: 12 additions & 0 deletions docs/make.jl
@@ -0,0 +1,12 @@
using Documenter
using LightGraphsMatching
import LightGraphs; const lg = LightGraphs

makedocs(
modules = [LightGraphsMatching],
format = :html,
sitename = "LightGraphsMatching",
pages = Any[
"Getting started" => "index.md",
]
)
14 changes: 14 additions & 0 deletions docs/src/index.md
@@ -0,0 +1,14 @@
```@meta
CurrentModule = LightGraphsMatching
DocTestSetup = quote
using LightGraphsMatching
import LightGraphs
const lg = LightGraphs
end
```

```@autodocs
Modules = [LightGraphsMatching]
Pages = ["LightGraphsMatching.jl", "maximum_weight_matching.jl", "lp.jl", "blossomv.jl"]
Order = [:function, :type]
```
28 changes: 14 additions & 14 deletions test/runtests.jl
Expand Up @@ -26,7 +26,7 @@ w = [
]
match = maximum_weight_matching(g, CbcSolver(), w)
@test match.mate[1] == 3
@test match.weight == 3
@test match.weight 3

g = CompleteBipartiteGraph(2,2)
w = zeros(4,4)
Expand All @@ -35,7 +35,7 @@ w[1,4] = 1.
w[2,3] = 2.
w[2,4] = 11.
match = maximum_weight_maximal_matching(g, CbcSolver(), w)
@test match.weight == 21
@test match.weight 21
@test match.mate[1] == 3
@test match.mate[3] == 1
@test match.mate[2] == 4
Expand All @@ -48,7 +48,7 @@ w[1,4] = 0.5
w[2,3] = 11
w[2,4] = 1
match = maximum_weight_maximal_matching(g, CbcSolver(), w)
@test match.weight == 11.5
@test match.weight 11.5
@test match.mate[1] == 4
@test match.mate[4] == 1
@test match.mate[2] == 3
Expand All @@ -63,7 +63,7 @@ w[2,4] = 1
w[2,5] = -1
w[2,6] = -1
match = maximum_weight_maximal_matching(g,CbcSolver(),w,0)
@test match.weight == 11.5
@test match.weight 11.5
@test match.mate[1] == 4
@test match.mate[4] == 1
@test match.mate[2] == 3
Expand All @@ -78,7 +78,7 @@ w[1,6] = 1
w[1,5] = -1

match = maximum_weight_maximal_matching(g,CbcSolver(),w,0)
@test match.weight == 12
@test match.weight 12
@test match.mate[1] == 6
@test match.mate[2] == 5
@test match.mate[3] == -1
Expand All @@ -92,7 +92,7 @@ w[1,2] = 1
w[3,2] = 1
w[1,3] = 1
match = maximum_weight_matching(g,CbcSolver(),w)
@test match.weight == 1
@test match.weight 1


g = Graph(4)
Expand All @@ -106,7 +106,7 @@ w[1,4] = 3
w[2,4] = 1

match = maximum_weight_matching(g,CbcSolver(),w)
@test match.weight == 3
@test match.weight 3
@test match.mate[1] == 4
@test match.mate[2] == -1
@test match.mate[3] == -1
Expand All @@ -118,7 +118,7 @@ add_edge!(g, 2,3)
add_edge!(g, 3,1)
add_edge!(g, 3,4)
match = maximum_weight_matching(g,CbcSolver())
@test match.weight == 2
@test match.weight 2
@test match.mate[1] == 2
@test match.mate[2] == 1
@test match.mate[3] == 4
Expand All @@ -131,7 +131,7 @@ w[1,3] = 1
w[3,4] = 1

match = maximum_weight_matching(g,CbcSolver(), w)
@test match.weight == 2
@test match.weight 2
@test match.mate[1] == 2
@test match.mate[2] == 1
@test match.mate[3] == 4
Expand All @@ -144,7 +144,7 @@ w[1,3] = 5
w[3,4] = 1

match = maximum_weight_matching(g,CbcSolver(),w)
@test match.weight == 5
@test match.weight 5
@test match.mate[1] == 3
@test match.mate[2] == -1
@test match.mate[3] == 1
Expand All @@ -169,7 +169,7 @@ match = minimum_weight_perfect_matching(g, w)
@test match.mate[2] == 1
@test match.mate[3] == 4
@test match.mate[4] == 3
@test match.weight == 600
@test match.weight 600

w = Dict(
Edge(1, 2) => 500,
Expand All @@ -184,7 +184,7 @@ match = minimum_weight_perfect_matching(g, w)
@test match.mate[2] == 4
@test match.mate[3] == 1
@test match.mate[4] == 2
@test match.weight == 1400
@test match.weight 1400

g =CompleteBipartiteGraph(2,2)
w =Dict{Edge,Float64}()
Expand All @@ -198,7 +198,7 @@ match = minimum_weight_perfect_matching(g, w)
@test match.mate[4] == 1
@test match.mate[2] == 3
@test match.mate[3] == 2
@test match.weight == -11.5
@test match.weight -11.5


g =CompleteGraph(4)
Expand All @@ -214,4 +214,4 @@ match = minimum_weight_perfect_matching(g, w, 50)
@test match.mate[4] == 1
@test match.mate[2] == 3
@test match.mate[3] == 2
@test match.weight == 11.5
@test match.weight 11.5

0 comments on commit 76936db

Please sign in to comment.