![](https://csdnimg.cn/release/download_crawler_static/10792979/bg10.jpg)
NetworkX Reference, Release 2.2
(e.g. nodes[n], `edges[u, v], adj[u][v]) and iteration (e.g. nodes.items(), nodes.
data('color'), nodes.data('color', default='blue') and similarly for edges) Views exist
for nodes, edges, neighbors()/adj and degree.
For details on these and other miscellaneous methods, see below.
Subclasses (Advanced):
The Graph class uses a dict-of-dict-of-dict data structure. The outer dict (node_dict) holds adjacency information
keyed by node. The next dict (adjlist_dict) represents the adjacency information and holds edge data keyed by
neighbor. The inner dict (edge_attr_dict) represents the edge data and holds edge attribute values keyed by
attribute names.
Each of these three dicts can be replaced in a subclass by a user defined dict-like object. In general, the dict-like
features should be maintained but extra features can be added. To replace one of the dicts create a new graph
class by changing the class(!) variable holding the factory for that dict-like structure. The variable names are
node_dict_factory, adjlist_inner_dict_factory, adjlist_outer_dict_factory, and edge_attr_dict_factory.
node_dict_factory [function, (default: dict)] Factory function to be used to create the dict containing node
attributes, keyed by node id. It should require no arguments and return a dict-like object
adjlist_outer_dict_factory [function, (default: dict)] Factory function to be used to create the outer-most dict
in the data structure that holds adjacency info keyed by node. It should require no arguments and return a
dict-like object.
adjlist_inner_dict_factory [function, (default: dict)] Factory function to be used to create the adjacency list
dict which holds edge data keyed by neighbor. It should require no arguments and return a dict-like object
edge_attr_dict_factory [function, (default: dict)] Factory function to be used to create the edge attribute dict
which holds attrbute values keyed by attribute name. It should require no arguments and return a dict-like
object.
Typically, if your extension doesn’t impact the data structure all methods will inherit without issue except:
to_directed/to_undirected. By default these methods create a DiGraph/Graph class and you probably
want them to create your extension of a DiGraph/Graph. To facilitate this we define two class variables that you
can set in your subclass.
to_directed_class [callable, (default: DiGraph or MultiDiGraph)] Class to create a new graph structure in the
to_directed method. If None, a NetworkX class (DiGraph or MultiDiGraph) is used.
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.
Examples
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)
(continues on next page)
10 Chapter 2. Graph types