@@ -47,7 +47,6 @@ REFERENCES:
47
47
from collections import Counter
48
48
from copy import copy
49
49
from cpython.object cimport Py_EQ, Py_NE
50
- import networkx as nx
51
50
52
51
from sage.graphs.digraph import DiGraph
53
52
from sage.graphs.bipartite_graph import BipartiteGraph
@@ -230,28 +229,27 @@ cdef class TransversalMatroid(BasisExchangeMatroid):
230
229
self ._matching = {self ._idx[e]: matching_temp[e] for e in matching_temp}
231
230
232
231
# Build a DiGraph for doing basis exchange
233
- self ._D = nx. DiGraph()
232
+ self ._D = < GenericGraph_pyx? > DiGraph()
234
233
# 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 ())
236
235
# 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)
238
237
239
238
# 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)
242
241
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)
249
247
250
248
cdef bint _is_exchange_pair(self , long x, long y) except - 1 :
251
249
r """
252
250
Check for `M`-alternating path from `x` to `y`.
253
251
"""
254
- return nx.has_path( self ._D, y, x )
252
+ return x in self ._D.depth_first_search(y )
255
253
256
254
cdef int _exchange(self , long x, long y) except - 1 :
257
255
r """
@@ -260,16 +258,16 @@ cdef class TransversalMatroid(BasisExchangeMatroid):
260
258
Internal method, does no checks.
261
259
"""
262
260
# update the internal matching
263
- sh = nx.shortest_path( self ._D, y, x)
261
+ sh = self ._D.shortest_path( y, x)
264
262
del self ._matching[x]
265
263
for i in range (0 , len (sh)- 1 , 2 ):
266
264
self ._matching[sh[i]] = sh[i+ 1 ]
267
265
268
266
# 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 :]) )
273
271
274
272
BasisExchangeMatroid._exchange(self , x, y)
275
273
@@ -471,16 +469,14 @@ cdef class TransversalMatroid(BasisExchangeMatroid):
471
469
sage: B2.is_isomorphic(B)
472
470
True
473
471
"""
474
- # cast the internal networkx as a sage DiGraph
475
- D = DiGraph(self ._D)
476
472
# 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))
478
474
for i, l in enumerate (self ._set_labels):
479
475
vertex_map[l] = self ._set_labels_input[i]
480
- D.relabel(vertex_map)
481
476
partition = [list (self ._E), self ._set_labels_input]
482
477
483
- return BipartiteGraph(D, partition = partition)
478
+ return BipartiteGraph(self ._D.relabel(vertex_map, inplace = False ),
479
+ partition = partition)
484
480
485
481
cpdef _minor(self , contractions, deletions):
486
482
"""
0 commit comments