def add_node(self, n, attr_dict=None, **attr): """Add a single node n and update node attributes.""" if misc.nx_version() == '1': return super(Graph, self).add_node(n, attr_dict=attr_dict, **attr) if attr_dict is not None: return super(Graph, self).add_node(n, **attr_dict) return super(Graph, self).add_node(n, **attr)
def add_edge(self, u, v, attr_dict=None, **attr): """Add an edge between u and v.""" if misc.nx_version() == '1': return super(Graph, self).add_edge(u, v, attr_dict=attr_dict, **attr) if attr_dict is not None: return super(Graph, self).add_edge(u, v, **attr_dict) return super(Graph, self).add_edge(u, v, **attr)
def nodes_iter(self, data=False): """Returns an iterable object over the nodes. Type of iterable returned object depends on which version of networkx is used. When networkx < 2.0 is used , method returns an iterator, but if networkx > 2.0 is used, it returns NodeView of the Graph which is also iterable. """ if misc.nx_version() == '1': return super(Graph, self).nodes_iter(data=data) return super(Graph, self).nodes(data=data)
def edges_iter(self, nbunch=None, data=False, default=None): """Returns an iterable object over the edges. Type of iterable returned object depends on which version of networkx is used. When networkx < 2.0 is used , method returns an iterator, but if networkx > 2.0 is used, it returns EdgeView of the Graph which is also iterable. """ if misc.nx_version() == '1': return super(Graph, self).edges_iter(nbunch=nbunch, data=data, default=default) return super(Graph, self).edges(nbunch=nbunch, data=data, default=default)
class OrderedGraph(Graph): """A graph subclass with useful utility functions. This derivative retains node, edge, insertion and iteration ordering (so that the iteration order matches the insertion order). """ node_dict_factory = collections.OrderedDict if misc.nx_version() == '1': adjlist_dict_factory = collections.OrderedDict else: adjlist_outer_dict_factory = collections.OrderedDict adjlist_inner_dict_factory = collections.OrderedDict edge_attr_dict_factory = collections.OrderedDict def fresh_copy(self): """Return a fresh copy graph with the same data structure. A fresh copy has no nodes, edges or graph attributes. It is the same data structure as the current graph. This method is typically used to create an empty version of the graph. """ return OrderedGraph()
def __init__(self, data=None, name=''): if misc.nx_version() == '1': super(Graph, self).__init__(name=name, data=data) else: super(Graph, self).__init__(name=name, incoming_graph_data=data) self.frozen = False
def predecessors_iter(self, n): """Return an iterator over predecessor nodes of n.""" if misc.nx_version() == '1': return super(DiGraph, self).predecessors_iter(n) return super(DiGraph, self).predecessors(n)