NetworkX Reference, Release 2.8.8
Simple graph information is obtained using object-attributes and methods. Reporting typically provides views in-
stead of containers to reduce memory usage. The views update as the graph is updated similarly to dict-views. The
objects nodes, edges and adj provide access to data attributes via lookup (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 de-
gree.
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 dened 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.
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
node_attr_dict_factory: function, (default: dict)
Factory function to be used to create the node attribute dict which holds attribute values keyed by attribute
name. 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 attribute
values keyed by attribute name. It should require no arguments and return a dict-like object.
graph_attr_dict_factory
[function, (default: dict)] Factory function to be used to create the graph attribute dict which holds attribute
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 proba-
bly want them to create your extension of a DiGraph/Graph. To facilitate this we dene 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.
Subclassing Example
Create a low memory graph class that eectively disallows edge attributes by using a single attribute dict for all
edges. This reduces the memory used, but you lose edge attributes.
10 Chapter 2. Graph types