Skip to content

Commit b932bcc

Browse files
authored
Merge pull request #13 from Robbybp/import
More conventional importing of dependencies
2 parents 6c21075 + 6a71fea commit b932bcc

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

src/dulmage_mendelsohn.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# This software is distributed under the 3-clause BSD license.
1818
# ___________________________________________________________________________
1919

20-
import Graphs as gjl
20+
import Graphs
2121

2222
import JuMPIn: maximum_matching, _is_valid_bipartition
2323

@@ -27,12 +27,12 @@ In this function, matching must contain a key for every matched node.
2727
I.e., Set(keys(matching)) == Set(values(matching))
2828
"""
2929
function _get_projected_digraph(
30-
graph::gjl.Graph, nodes::Vector, matching::Dict
30+
graph::Graphs.Graph, nodes::Vector, matching::Dict
3131
)
3232
# Note that we are constructing a graph in a projected space, and must
3333
# be aware of whether coordinates are in original or projected spaces.
3434
orig_proj_map = Dict(n => i for (i, n) in enumerate(nodes))
35-
n_nodes = gjl.nv(graph)
35+
n_nodes = Graphs.nv(graph)
3636
n_nodes_proj = length(nodes)
3737
matched_nodes = keys(matching)
3838
node_set = Set(nodes) # Set of nodes in the original space
@@ -41,7 +41,7 @@ function _get_projected_digraph(
4141
orig_node = nodes[proj_node]
4242
if orig_node in matched_nodes
4343
# In-edges from all (other) neighbors of matched node
44-
for nbr in gjl.neighbors(graph, matching[orig_node])
44+
for nbr in Graphs.neighbors(graph, matching[orig_node])
4545
if nbr != orig_node
4646
nbr_proj = orig_proj_map[nbr]
4747
push!(edge_set, (nbr_proj, proj_node))
@@ -50,29 +50,29 @@ function _get_projected_digraph(
5050
end
5151
# TODO: Out edges?
5252
end
53-
digraph = gjl.DiGraph(n_nodes_proj)
53+
digraph = Graphs.DiGraph(n_nodes_proj)
5454
for (n1, n2) in edge_set
55-
gjl.add_edge!(digraph, n1, n2)
55+
Graphs.add_edge!(digraph, n1, n2)
5656
end
5757
return digraph, orig_proj_map
5858
end
5959

6060

61-
function _get_reachable_from(digraph::gjl.DiGraph, nodes::Vector)
62-
n_nodes = gjl.nv(digraph)
61+
function _get_reachable_from(digraph::Graphs.DiGraph, nodes::Vector)
62+
n_nodes = Graphs.nv(digraph)
6363
source_set = Set(nodes)
64-
gjl.add_vertex!(digraph)
64+
Graphs.add_vertex!(digraph)
6565
# Note that root needs to be in this scope so it can be accessed in
6666
# finally block
6767
root = n_nodes + 1
6868
bfs_parents = Vector{Int64}()
6969
try
7070
for node in nodes
71-
gjl.add_edge!(digraph, root, node)
71+
Graphs.add_edge!(digraph, root, node)
7272
end
73-
bfs_parents = gjl.bfs_parents(digraph, root)
73+
bfs_parents = Graphs.bfs_parents(digraph, root)
7474
finally
75-
gjl.rem_vertex!(digraph, root)
75+
Graphs.rem_vertex!(digraph, root)
7676
end
7777
reachable = [
7878
node for (node, par) in enumerate(bfs_parents)
@@ -82,11 +82,11 @@ function _get_reachable_from(digraph::gjl.DiGraph, nodes::Vector)
8282
end
8383

8484

85-
function dulmage_mendelsohn(graph::gjl.Graph, set1::Set)
85+
function dulmage_mendelsohn(graph::Graphs.Graph, set1::Set)
8686
if !_is_valid_bipartition(graph, set1)
8787
throw(Exception)
8888
end
89-
n_nodes = gjl.nv(graph)
89+
n_nodes = Graphs.nv(graph)
9090
set2 = setdiff(Set(1:n_nodes), set1)
9191

9292
# Compute maximum matching between bipartite sets

src/interface.jl

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ import JuMP
2626

2727
import JuMPIn: get_bipartite_incidence_graph, maximum_matching, GraphDataTuple
2828

29-
import Graphs as gjl
30-
import BipartiteMatching as bpm
31-
29+
import Graphs
3230

3331
"""
3432
Utility function to convert a tuple of nodes and edges into a
@@ -39,17 +37,16 @@ function _tuple_to_graphs_jl(bip_graph)
3937
# Assumption here is that A and B are disjoint. And also form
4038
# a partition of 1:nv.
4139
nv = length(A) + length(B)
42-
graph = gjl.Graph(gjl.Edge.(E))
40+
graph = Graphs.Graph(Graphs.Edge.(E))
4341
# If E does not cover all vertices, some vertices may not appear in the
4442
# graph. Add these missing vertices.
45-
n_missing = nv - gjl.nv(graph)
46-
gjl.add_vertices!(graph, n_missing)
43+
n_missing = nv - Graphs.nv(graph)
44+
Graphs.add_vertices!(graph, n_missing)
4745
# Note that by constructing this graph, we have lost our particular
4846
# bipartition.
4947
return graph
5048
end
5149

52-
5350
function _maps_to_nodes(con_map, var_map)
5451
n_nodes = length(con_map) + length(var_map)
5552
nodes = Vector{Any}([nothing for _ in 1:n_nodes])
@@ -65,7 +62,6 @@ function _maps_to_nodes(con_map, var_map)
6562
return nodes
6663
end
6764

68-
6965
"""
7066
IncidenceGraphInterface(model; include_inequality = false)
7167
@@ -99,7 +95,6 @@ struct IncidenceGraphInterface
9995
_nodes
10096
end
10197

102-
10398
IncidenceGraphInterface(
10499
args::GraphDataTuple
105100
) = IncidenceGraphInterface(
@@ -109,23 +104,20 @@ IncidenceGraphInterface(
109104
_maps_to_nodes(args[2], args[3]),
110105
)
111106

112-
113107
IncidenceGraphInterface(
114108
m::JuMP.Model;
115109
include_inequality::Bool = false,
116110
) = IncidenceGraphInterface(
117111
get_bipartite_incidence_graph(m, include_inequality = include_inequality)
118112
)
119113

120-
121114
IncidenceGraphInterface(
122115
constraints::Vector{<:JuMP.ConstraintRef},
123116
variables::Vector{JuMP.VariableRef},
124117
) = IncidenceGraphInterface(
125118
get_bipartite_incidence_graph(constraints, variables)
126119
)
127120

128-
129121
"""
130122
get_adjacent(
131123
igraph::IncidenceGraphInterface,
@@ -140,12 +132,11 @@ function get_adjacent(
140132
constraint::JuMP.ConstraintRef,
141133
)::Vector{JuMP.VariableRef}
142134
con_node = igraph._con_node_map[constraint]
143-
var_nodes = gjl.neighbors(igraph._graph, con_node)
135+
var_nodes = Graphs.neighbors(igraph._graph, con_node)
144136
variables = [igraph._nodes[n] for n in var_nodes]
145137
return variables
146138
end
147139

148-
149140
"""
150141
get_adjacent(
151142
igraph::IncidenceGraphInterface,
@@ -176,12 +167,11 @@ function get_adjacent(
176167
variable::JuMP.VariableRef,
177168
)::Vector{JuMP.ConstraintRef}
178169
var_node = igraph._var_node_map[variable]
179-
con_nodes = gjl.neighbors(igraph._graph, var_node)
170+
con_nodes = Graphs.neighbors(igraph._graph, var_node)
180171
constraints = [igraph._nodes[n] for n in con_nodes]
181172
return constraints
182173
end
183174

184-
185175
"""
186176
maximum_matching(igraph::IncidenceGraphInterface)::Dict
187177
@@ -351,7 +341,6 @@ function dulmage_mendelsohn(
351341
return con_dmp, var_dmp
352342
end
353343

354-
355344
function dulmage_mendelsohn(
356345
constraints::Vector{<:JuMP.ConstraintRef},
357346
variables::Vector{JuMP.VariableRef},

src/maximum_matching.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,46 @@
1717
# This software is distributed under the 3-clause BSD license.
1818
# ___________________________________________________________________________
1919

20-
import Graphs as gjl
21-
import BipartiteMatching as bpm
20+
import Graphs
21+
import BipartiteMatching as BM
2222

2323

2424
"""
2525
TODO: This should probably be promoted to Graphs.jl
2626
"""
27-
function _is_valid_bipartition(graph::gjl.Graph, set1::Set)
28-
n_nodes = gjl.nv(graph)
27+
function _is_valid_bipartition(graph::Graphs.Graph, set1::Set)
28+
n_nodes = Graphs.nv(graph)
2929
all_nodes = Set(1:n_nodes)
3030
if !issubset(set1, all_nodes)
3131
throw(Exception)
3232
end
3333
set2 = setdiff(all_nodes, set1)
3434
for node in set1
35-
if !issubset(gjl.neighbors(graph, node), set2)
35+
if !issubset(Graphs.neighbors(graph, node), set2)
3636
return false
3737
end
3838
end
3939
for node in set2
40-
if !issubset(gjl.neighbors(graph, node), set1)
40+
if !issubset(Graphs.neighbors(graph, node), set1)
4141
return false
4242
end
4343
end
4444
return true
4545
end
4646

4747

48-
function maximum_matching(graph::gjl.Graph, set1::Set)
48+
function maximum_matching(graph::Graphs.Graph, set1::Set)
4949
if !_is_valid_bipartition(graph, set1)
5050
throw(Exception)
5151
end
52-
n_nodes = gjl.nv(graph)
52+
n_nodes = Graphs.nv(graph)
5353
card1 = length(set1)
5454
nodes1 = sort([node for node in set1])
5555
set2 = setdiff(Set(1:n_nodes), set1)
5656
nodes2 = sort([node for node in set2])
57-
edge_set = Set((n1, n2) for n1 in nodes1 for n2 in gjl.neighbors(graph, n1))
57+
edge_set = Set((n1, n2) for n1 in nodes1 for n2 in Graphs.neighbors(graph, n1))
5858
amat = BitArray{2}((r, c) in edge_set for r in nodes1, c in nodes2)
59-
matching, _ = bpm.findmaxcardinalitybipartitematching(amat)
59+
matching, _ = BM.findmaxcardinalitybipartitematching(amat)
6060
# Translate row/column coordinates back into nodes of the graph
6161
graph_matching = Dict(nodes1[r] => nodes2[c] for (r, c) in matching)
6262
return graph_matching

0 commit comments

Comments
 (0)