MultiDiGraph  Directed graphs with self loops and parallel edges¶
Overview¶

MultiDiGraph
(data=None, **attr)[source]¶ A directed graph class that can store multiedges.
Multiedges are multiple edges between two nodes. Each edge can hold optional data or attributes.
A MultiDiGraph holds directed edges. Self loops are allowed.
Nodes can be arbitrary (hashable) Python objects with optional key/value attributes.
Edges are represented as links between nodes with optional key/value attributes.
Parameters:  data (input graph) – Data to initialize graph. If data=None (default) an empty graph is created. The data can be an edge list, or any NetworkX graph object. If the corresponding optional Python packages are installed the data can also be a NumPy matrix or 2d ndarray, a SciPy sparse matrix, or a PyGraphviz graph.
 attr (keyword arguments, optional (default= no attributes)) – Attributes to add to graph as key=value pairs.
See also
Examples
Create an empty graph structure (a “null graph”) with no nodes and no edges.
>>> G = nx.MultiDiGraph()
G can be grown in several ways.
Nodes:
Add one node at a time:
>>> G.add_node(1)
Add the nodes from any container (a list, dict, set or even the lines from a file or the nodes from another graph).
>>> G.add_nodes_from([2,3]) >>> G.add_nodes_from(range(100,110)) >>> H=nx.Graph() >>> H.add_path([0,1,2,3,4,5,6,7,8,9]) >>> G.add_nodes_from(H)
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,
>>> G.add_edge(1, 2)
a list of edges,
>>> G.add_edges_from([(1,2),(1,3)])
or a collection of edges,
>>> 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.
>>> G.add_edges_from([(4,5,dict(route=282)), (4,5,dict(route=37))]) >>> G[4] {5: {0: {}, 1: {'route': 282}, 2: {'route': 37}}}
Attributes:
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.
>>> G = nx.MultiDiGraph(day="Friday") >>> G.graph {'day': 'Friday'}
Add node attributes using add_node(), add_nodes_from() or G.node
>>> G.add_node(1, time='5pm') >>> G.add_nodes_from([3], time='2pm') >>> G.node[1] {'time': '5pm'} >>> G.node[1]['room'] = 714 >>> del G.node[1]['room'] # remove attribute >>> G.nodes(data=True) [(1, {'time': '5pm'}), (3, {'time': '2pm'})]
Warning: adding a node to G.node does not add it to the graph.
Add edge attributes using add_edge(), add_edges_from(), subscript notation, or G.edge.
>>> G.add_edge(1, 2, weight=4.7 ) >>> G.add_edges_from([(3,4),(4,5)], color='red') >>> G.add_edges_from([(1,2,{'color':'blue'}), (2,3,{'weight':8})]) >>> G[1][2][0]['weight'] = 4.7 >>> G.edge[1][2][0]['weight'] = 4
Shortcuts:
Many common graph features allow python syntax to speed reporting.
>>> 1 in G # check if node in graph True >>> [n for n in G if n<3] # iterate through nodes [1, 2] >>> len(G) # number of nodes in graph 5 >>> G[1] # adjacency dict keyed by neighbor to edge attributes ... # Note: you should not change this dict manually! {2: {0: {'weight': 4}, 1: {'color': 'blue'}}}
The fastest way to traverse all edges of a graph is via adjacency_iter(), but the edges() method is often more convenient.
>>> for n,nbrsdict in G.adjacency_iter(): ... for nbr,keydict in nbrsdict.items(): ... for key,eattr in keydict.items(): ... if 'weight' in eattr: ... (n,nbr,eattr['weight']) (1, 2, 4) (2, 3, 8) >>> G.edges(data='weight') [(1, 2, 4), (1, 2, None), (2, 3, 8), (3, 4, None), (4, 5, None)]
Reporting:
Simple graph information is obtained using methods. Iterator versions of many reporting methods exist for efficiency. Methods exist for reporting nodes(), edges(), neighbors() and degree() as well as the number of nodes and edges.
For details on these and other miscellaneous methods, see below.
Subclasses (Advanced):
The MultiDiGraph class uses a dictofdictofdictofdict structure. The outer dict (node_dict) holds adjacency lists keyed by node. The next dict (adjlist) represents the adjacency list and holds edge_key dicts keyed by neighbor. The edge_key dict holds each edge_attr dict keyed by edge key. The inner dict (edge_attr) represents the edge data and holds edge attribute values keyed by attribute names.
Each of these four dicts in the dictofdictofdictofdict structure can be replaced by a user defined dictlike object. In general, the dictlike 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 dictlike structure. The variable names are node_dict_factory, adjlist_dict_factory, edge_key_dict_factory and edge_attr_dict_factory.
 node_dict_factory : function, (default: dict)
 Factory function to be used to create the outermost dict in the data structure that holds adjacency lists keyed by node. It should require no arguments and return a dictlike object.
 adjlist_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 dictlike 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 dictlike 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 dictlike object.
Examples
Create a multigraph object that tracks the order nodes are added.
>>> from collections import OrderedDict >>> class OrderedGraph(nx.MultiDiGraph): ... node_dict_factory = OrderedDict >>> G = OrderedGraph() >>> G.add_nodes_from( (2,1) ) >>> G.nodes() [2, 1] >>> G.add_edges_from( ((2,2), (2,1), (2,1), (1,1)) ) >>> G.edges() [(2, 1), (2, 1), (2, 2), (1, 1)]
Create a multdigraph object that tracks the order nodes are added and for each node track the order that neighbors are added and for each neighbor tracks the order that multiedges are added.
>>> class OrderedGraph(nx.MultiDiGraph): ... node_dict_factory = OrderedDict ... adjlist_dict_factory = OrderedDict ... edge_key_dict_factory = OrderedDict >>> G = OrderedGraph() >>> G.add_nodes_from( (2,1) ) >>> G.nodes() [2, 1] >>> G.add_edges_from( ((2,2), (2,1,2,{'weight':0.1}), (2,1,1,{'weight':0.2}), (1,1)) ) >>> G.edges(keys=True) [(2, 2, 0), (2, 1, 2), (2, 1, 1), (1, 1, 0)]
Adding and Removing Nodes and Edges¶
MultiDiGraph.__init__ ([data]) 

MultiDiGraph.add_node (n[, attr_dict]) 
Add a single node n and update node attributes. 
MultiDiGraph.add_nodes_from (nodes, **attr) 
Add multiple nodes. 
MultiDiGraph.remove_node (n) 
Remove node n. 
MultiDiGraph.remove_nodes_from (nbunch) 
Remove multiple nodes. 
MultiDiGraph.add_edge (u, v[, key, attr_dict]) 
Add an edge between u and v. 
MultiDiGraph.add_edges_from (ebunch[, attr_dict]) 
Add all the edges in ebunch. 
MultiDiGraph.add_weighted_edges_from (ebunch) 
Add all the edges in ebunch as weighted edges with specified weights. 
MultiDiGraph.remove_edge (u, v[, key]) 
Remove an edge between u and v. 
MultiDiGraph.remove_edges_from (ebunch) 
Remove all edges specified in ebunch. 
MultiDiGraph.add_star (nodes, **attr) 
Add a star. 
MultiDiGraph.add_path (nodes, **attr) 
Add a path. 
MultiDiGraph.add_cycle (nodes, **attr) 
Add a cycle. 
MultiDiGraph.clear () 
Remove all nodes and edges from the graph. 
Iterating over nodes and edges¶
MultiDiGraph.nodes ([data]) 
Return a list of the nodes in the graph. 
MultiDiGraph.nodes_iter ([data]) 
Return an iterator over the nodes. 
MultiDiGraph.__iter__ () 
Iterate over the nodes. 
MultiDiGraph.edges ([nbunch, data, keys, default]) 
Return a list of edges. 
MultiDiGraph.edges_iter ([nbunch, data, ...]) 
Return an iterator over the edges. 
MultiDiGraph.out_edges ([nbunch, keys, data]) 
Return a list of the outgoing edges. 
MultiDiGraph.out_edges_iter ([nbunch, data, ...]) 
Return an iterator over the edges. 
MultiDiGraph.in_edges ([nbunch, keys, data]) 
Return a list of the incoming edges. 
MultiDiGraph.in_edges_iter ([nbunch, data, keys]) 
Return an iterator over the incoming edges. 
MultiDiGraph.get_edge_data (u, v[, key, default]) 
Return the attribute dictionary associated with edge (u,v). 
MultiDiGraph.neighbors (n) 
Return a list of successor nodes of n. 
MultiDiGraph.neighbors_iter (n) 
Return an iterator over successor nodes of n. 
MultiDiGraph.__getitem__ (n) 
Return a dict of neighbors of node n. 
MultiDiGraph.successors (n) 
Return a list of successor nodes of n. 
MultiDiGraph.successors_iter (n) 
Return an iterator over successor nodes of n. 
MultiDiGraph.predecessors (n) 
Return a list of predecessor nodes of n. 
MultiDiGraph.predecessors_iter (n) 
Return an iterator over predecessor nodes of n. 
MultiDiGraph.adjacency_list () 
Return an adjacency list representation of the graph. 
MultiDiGraph.adjacency_iter () 
Return an iterator of (node, adjacency dict) tuples for all nodes. 
MultiDiGraph.nbunch_iter ([nbunch]) 
Return an iterator of nodes contained in nbunch that are also in the graph. 
Information about graph structure¶
MultiDiGraph.has_node (n) 
Return True if the graph contains the node n. 
MultiDiGraph.__contains__ (n) 
Return True if n is a node, False otherwise. 
MultiDiGraph.has_edge (u, v[, key]) 
Return True if the graph has an edge between nodes u and v. 
MultiDiGraph.order () 
Return the number of nodes in the graph. 
MultiDiGraph.number_of_nodes () 
Return the number of nodes in the graph. 
MultiDiGraph.__len__ () 
Return the number of nodes. 
MultiDiGraph.degree ([nbunch, weight]) 
Return the degree of a node or nodes. 
MultiDiGraph.degree_iter ([nbunch, weight]) 
Return an iterator for (node, degree). 
MultiDiGraph.in_degree ([nbunch, weight]) 
Return the indegree of a node or nodes. 
MultiDiGraph.in_degree_iter ([nbunch, weight]) 
Return an iterator for (node, indegree). 
MultiDiGraph.out_degree ([nbunch, weight]) 
Return the outdegree of a node or nodes. 
MultiDiGraph.out_degree_iter ([nbunch, weight]) 
Return an iterator for (node, outdegree). 
MultiDiGraph.size ([weight]) 
Return the number of edges. 
MultiDiGraph.number_of_edges ([u, v]) 
Return the number of edges between two nodes. 
MultiDiGraph.nodes_with_selfloops () 
Return a list of nodes with self loops. 
MultiDiGraph.selfloop_edges ([data, keys, ...]) 
Return a list of selfloop edges. 
MultiDiGraph.number_of_selfloops () 
Return the number of selfloop edges. 
Making copies and subgraphs¶
MultiDiGraph.copy () 
Return a copy of the graph. 
MultiDiGraph.to_undirected ([reciprocal]) 
Return an undirected representation of the digraph. 
MultiDiGraph.to_directed () 
Return a directed copy of the graph. 
MultiDiGraph.subgraph (nbunch) 
Return the subgraph induced on nodes in nbunch. 
MultiDiGraph.reverse ([copy]) 
Return the reverse of the graph. 