Multiedges are multiple edges between two nodes. Each edge
can hold optional data or attributes.

A MultiGraph holds undirected edges. Self loops are allowed.

Nodes can be arbitrary (hashable) Python objects with optional
key/value attributes. By convention None is not used as a node.

Edges are represented as links between nodes with optional
key/value attributes.

Parameters:

incoming_graph_data (input graph (optional, default: None)) – Data to initialize graph. If None (default) an empty
graph is created. The data can be any format that is supported
by the to_networkx_graph() function, currently including edge list,
dict of dicts, dict of lists, NetworkX graph, NumPy matrix
or 2d ndarray, SciPy sparse matrix, or PyGraphviz graph.

In addition to strings and integers any hashable Python object
(except None) can represent a node, e.g. a customized node object,
or even another Graph.

>>> G.add_node(H)

Edges:

G can also be grown by adding edges.

Add one edge,

>>> key=G.add_edge(1,2)

a list of edges,

>>> keys=G.add_edges_from([(1,2),(1,3)])

or a collection of edges,

>>> keys=G.add_edges_from(H.edges)

If some edges connect nodes not yet in the graph, the nodes
are added automatically. If an edge already exists, an additional
edge is created and stored using a key to identify the edge.
By default the key is the lowest unused integer.

Each graph, node, and edge can hold key/value attribute pairs
in an associated attribute dictionary (the keys must be hashable).
By default these are empty, but can be added or changed using
add_edge, add_node or direct manipulation of the attribute
dictionaries named graph, node and edge respectively.

Warning: we protect the graph data structure by making G.edges[1,2] a
read-only dict-like structure. However, you can assign to attributes
in e.g. G.edges[1,2]. Thus, use 2 sets of brackets to add/change
data attributes: G.edges[1,2]['weight']=4
(For multigraphs: MG.edges[u,v,key][name]=value).

Each of these four dicts in the dict-of-dict-of-dict-of-dict
structure can be replaced 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, node_attr_dict_factory, adjlist_inner_dict_factory,
adjlist_outer_dict_factory, edge_key_dict_factory, edge_attr_dict_factory
and graph_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

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 multiedge key dicts keyed by neighbor.
It should require no arguments and return a dict-like object.

edge_key_dict_factory :function, (default: dict)

Factory function to be used to create the edge key dict
which holds edge data keyed by edge key.
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 inherited 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

Please see ordered for examples of
creating graph subclasses by overwriting the base class dict with
a dictionary-like object.