3.5. MultiDiGraph - Directed graphs with self loops and parallel edges

3.5.1. MultiDiGraph()

networkx.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 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.

attr : keyword arguments, optional (default= no attributes)

Attributes to add to graph as key=value pairs.

See also

Graph, DiGraph, MultiGraph

Examples

Create a multigraph subclass 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) )
>>> list(G.nodes())
[2, 1]
>>> G.add_edges_from( ((2,2), (2,1), (2,1), (1,1)) )
>>> list(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) )
>>> list(G.nodes())
[2, 1]
>>> G.add_edges_from( ((2,2), (2,1,2,{'weight':0.1}), (2,1,1,{'weight':0.2}), (1,1)) )
>>> list(G.edges(keys=True))
[(2, 2, 0), (2, 1, 2), (2, 1, 1), (1, 1, 0)]

3.5.2. 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.new_edge_key
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.clear() Remove all nodes and edges from the graph.

3.5.3. Iterating over nodes and edges

MultiDiGraph.nodes([data, default]) Returns an iterator over the nodes.
MultiDiGraph.__iter__() Iterate over the nodes.
MultiDiGraph.edges([nbunch, data, keys, default]) Return an iterator over the edges.
MultiDiGraph.out_edges([nbunch, data, keys, ...]) Return an iterator over the edges.
MultiDiGraph.in_edges([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 an iterator over successor nodes of n.
MultiDiGraph.__getitem__(n) Return a dict of neighbors of node n.
MultiDiGraph.successors(n) Return an iterator over successor nodes of n.
MultiDiGraph.predecessors(n) Return an iterator over predecessor nodes of n.
MultiDiGraph.adjacency() Return an iterator over (node, adjacency dict) tuples for all nodes.
MultiDiGraph.nbunch_iter([nbunch]) Return an iterator over nodes contained in nbunch that are also in the graph.

3.5.4. 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 an iterator for (node, degree) or degree for single node.
MultiDiGraph.in_degree([nbunch, weight]) Return an iterator for (node, in-degree) or in-degree for single node.
MultiDiGraph.out_degree([nbunch, weight]) Return an iterator for (node, out-degree) or out-degree for single node.
MultiDiGraph.size([weight]) Return the number of edges or total of all edge weights.
MultiDiGraph.number_of_edges([u, v]) Return the number of edges between two nodes.
MultiDiGraph.nodes_with_selfloops() Returns an iterator over 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.

3.5.5. Making copies and subgraphs

MultiDiGraph.copy([with_data]) 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.edge_subgraph(edges) Returns the subgraph induced by the specified edges.
MultiDiGraph.subgraph(nbunch) Return the subgraph induced on nodes in nbunch.
MultiDiGraph.reverse([copy]) Return the reverse of the graph.