Skip to content

Commit ab37897

Browse files
committed
avoid using networkx in transversal_matroid.pyx
1 parent 665a3fa commit ab37897

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

src/sage/matroids/transversal_matroid.pxd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from sage.matroids.matroid cimport Matroid
22
from sage.matroids.basis_exchange_matroid cimport BasisExchangeMatroid
3+
from sage.graphs.generic_graph_pyx cimport GenericGraph_pyx
34

45
cdef class TransversalMatroid(BasisExchangeMatroid):
56
cdef dict _matching
67
cdef object _sets
7-
cdef object _D
8+
cdef GenericGraph_pyx _D
89
cdef list _set_labels, _sets_input, _set_labels_input
910

1011
cpdef list sets(self)

src/sage/matroids/transversal_matroid.pyx

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ REFERENCES:
4747
from collections import Counter
4848
from copy import copy
4949
from cpython.object cimport Py_EQ, Py_NE
50-
import networkx as nx
5150

5251
from sage.graphs.digraph import DiGraph
5352
from sage.graphs.bipartite_graph import BipartiteGraph
@@ -230,28 +229,27 @@ cdef class TransversalMatroid(BasisExchangeMatroid):
230229
self._matching = {self._idx[e]: matching_temp[e] for e in matching_temp}
231230

232231
# Build a DiGraph for doing basis exchange
233-
self._D = nx.DiGraph()
232+
self._D = <GenericGraph_pyx?> DiGraph()
234233
# Make sure we get isolated vertices, corresponding to loops
235-
self._D.add_nodes_from(self._idx.itervalues())
234+
self._D.add_vertices(self._idx.values())
236235
# Also get isolated vertices corresponding to empty sets
237-
self._D.add_nodes_from(self._set_labels)
236+
self._D.add_vertices(self._set_labels)
238237

239238
# For sets in the matching, orient them as starting from the collections
240-
matching_reversed = [(v, k) for k, v in self._matching.iteritems()]
241-
self._D.add_edges_from(matching_reversed)
239+
matching_reversed = ((v, k) for k, v in self._matching.items())
240+
self._D.add_edges(matching_reversed)
242241

243-
other_edges = []
244-
for i, s in enumerate(sets):
245-
for e in s:
246-
if e not in matching_temp or matching_temp[e] != set_labels[i]:
247-
other_edges.append((self._idx[e], set_labels[i]))
248-
self._D.add_edges_from(other_edges)
242+
other_edges = ((self._idx[e], set_labels[i])
243+
for i, s in enumerate(sets)
244+
for e in s
245+
if e not in matching_temp or matching_temp[e] != set_labels[i])
246+
self._D.add_edges(other_edges)
249247

250248
cdef bint _is_exchange_pair(self, long x, long y) except -1:
251249
r"""
252250
Check for `M`-alternating path from `x` to `y`.
253251
"""
254-
return nx.has_path(self._D, y, x)
252+
return x in self._D.depth_first_search(y)
255253

256254
cdef int _exchange(self, long x, long y) except -1:
257255
r"""
@@ -260,16 +258,16 @@ cdef class TransversalMatroid(BasisExchangeMatroid):
260258
Internal method, does no checks.
261259
"""
262260
# update the internal matching
263-
sh = nx.shortest_path(self._D, y, x)
261+
sh = self._D.shortest_path(y, x)
264262
del self._matching[x]
265263
for i in range(0, len(sh)-1, 2):
266264
self._matching[sh[i]] = sh[i+1]
267265

268266
# update the graph to reflect this new matching
269-
sh_edges = [(sh[i], sh[i + 1]) for i in xrange(len(sh) - 1)]
270-
sh_edges_r = [(sh[i + 1], sh[i]) for i in xrange(len(sh) - 1)]
271-
self._D.remove_edges_from(sh_edges)
272-
self._D.add_edges_from(sh_edges_r)
267+
# i.e., reverse the orientation of edges along the shortest path sh
268+
self._D.delete_edges(zip(sh, sh[1:]))
269+
sh.reverse()
270+
self._D.add_edges(zip(sh, sh[1:]))
273271

274272
BasisExchangeMatroid._exchange(self, x, y)
275273

@@ -471,16 +469,14 @@ cdef class TransversalMatroid(BasisExchangeMatroid):
471469
sage: B2.is_isomorphic(B)
472470
True
473471
"""
474-
# cast the internal networkx as a sage DiGraph
475-
D = DiGraph(self._D)
476472
# relabel the vertices, then return as a BipartiteGraph
477-
vertex_map = {i: e for i, e in enumerate(self._E)}
473+
vertex_map = dict(enumerate(self._E))
478474
for i, l in enumerate(self._set_labels):
479475
vertex_map[l] = self._set_labels_input[i]
480-
D.relabel(vertex_map)
481476
partition = [list(self._E), self._set_labels_input]
482477

483-
return BipartiteGraph(D, partition=partition)
478+
return BipartiteGraph(self._D.relabel(vertex_map, inplace=False),
479+
partition=partition)
484480

485481
cpdef _minor(self, contractions, deletions):
486482
"""

0 commit comments

Comments
 (0)