diff options
-rw-r--r-- | networkx/classes/multidigraph.py | 21 | ||||
-rw-r--r-- | networkx/classes/multigraph.py | 21 |
2 files changed, 42 insertions, 0 deletions
diff --git a/networkx/classes/multidigraph.py b/networkx/classes/multidigraph.py index 31c33f67..81d2942f 100644 --- a/networkx/classes/multidigraph.py +++ b/networkx/classes/multidigraph.py @@ -269,6 +269,27 @@ class MultiDiGraph(MultiGraph, DiGraph): to_undirected_class : callable, (default: Graph or MultiGraph) Class to create a new graph structure in the `to_undirected` method. If `None`, a NetworkX class (Graph or MultiGraph) is used. + + **Subclassing Example** + + Create a low memory graph class that effectively disallows edge + attributes by using a single attribute dict for all edges. + This reduces the memory used, but you lose edge attributes. + + >>> class ThinGraph(nx.Graph): + ... all_edge_dict = {"weight": 1} + ... + ... def single_edge_dict(self): + ... return self.all_edge_dict + ... + ... edge_attr_dict_factory = single_edge_dict + >>> G = ThinGraph() + >>> G.add_edge(2, 1) + >>> G[2][1] + {'weight': 1} + >>> G.add_edge(2, 2) + >>> G[2][1] is G[2][2] + True """ # node_dict_factory = dict # already assigned in Graph diff --git a/networkx/classes/multigraph.py b/networkx/classes/multigraph.py index 308c20cc..3d9e69b3 100644 --- a/networkx/classes/multigraph.py +++ b/networkx/classes/multigraph.py @@ -262,6 +262,27 @@ class MultiGraph(Graph): to_undirected_class : callable, (default: Graph or MultiGraph) Class to create a new graph structure in the `to_undirected` method. If `None`, a NetworkX class (Graph or MultiGraph) is used. + + **Subclassing Example** + + Create a low memory graph class that effectively disallows edge + attributes by using a single attribute dict for all edges. + This reduces the memory used, but you lose edge attributes. + + >>> class ThinGraph(nx.Graph): + ... all_edge_dict = {"weight": 1} + ... + ... def single_edge_dict(self): + ... return self.all_edge_dict + ... + ... edge_attr_dict_factory = single_edge_dict + >>> G = ThinGraph() + >>> G.add_edge(2, 1) + >>> G[2][1] + {'weight': 1} + >>> G.add_edge(2, 2) + >>> G[2][1] is G[2][2] + True """ # node_dict_factory = dict # already assigned in Graph |