networkx.MultiDiGraph

class 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)]
__init__(data=None, **attr)[source]

Methods

__init__([data])
add_edge(u, v[, key, attr_dict]) Add an edge between u and v.
add_edges_from(ebunch[, attr_dict]) Add all the edges in ebunch.
add_node(n[, attr_dict]) Add a single node n and update node attributes.
add_nodes_from(nodes, **attr) Add multiple nodes.
add_weighted_edges_from(ebunch[, weight]) Add all the edges in ebunch as weighted edges with specified weights.
adjacency() Return an iterator over (node, adjacency dict) tuples for all nodes.
clear() Remove all nodes and edges from the graph.
copy([with_data]) Return a copy of the graph.
degree([nbunch, weight]) Return an iterator for (node, degree) or degree for single node.
edge_subgraph(edges) Returns the subgraph induced by the specified edges.
edges([nbunch, data, keys, default]) Return an iterator over the edges.
get_edge_data(u, v[, key, default]) Return the attribute dictionary associated with edge (u,v).
has_edge(u, v[, key]) Return True if the graph has an edge between nodes u and v.
has_node(n) Return True if the graph contains the node n.
has_predecessor(u, v) Return True if node u has predecessor v.
has_successor(u, v) Return True if node u has successor v.
in_degree([nbunch, weight]) Return an iterator for (node, in-degree) or in-degree for single node.
in_edges([nbunch, data, keys]) Return an iterator over the incoming edges.
is_directed() Return True if graph is directed, False otherwise.
is_multigraph() Return True if graph is a multigraph, False otherwise.
nbunch_iter([nbunch]) Return an iterator over nodes contained in nbunch that are also in the graph.
neighbors(n) Return an iterator over successor nodes of n.
nodes([data, default]) Returns an iterator over the nodes.
nodes_with_selfloops() Returns an iterator over nodes with self loops.
number_of_edges([u, v]) Return the number of edges between two nodes.
number_of_nodes() Return the number of nodes in the graph.
number_of_selfloops() Return the number of selfloop edges.
order() Return the number of nodes in the graph.
out_degree([nbunch, weight]) Return an iterator for (node, out-degree) or out-degree for single node.
out_edges([nbunch, data, keys, default]) Return an iterator over the edges.
predecessors(n) Return an iterator over predecessor nodes of n.
remove_edge(u, v[, key]) Remove an edge between u and v.
remove_edges_from(ebunch) Remove all edges specified in ebunch.
remove_node(n) Remove node n.
remove_nodes_from(nbunch) Remove multiple nodes.
reverse([copy]) Return the reverse of the graph.
selfloop_edges([data, keys, default]) Return a list of selfloop edges.
size([weight]) Return the number of edges or total of all edge weights.
subgraph(nbunch) Return the subgraph induced on nodes in nbunch.
successors(n) Return an iterator over successor nodes of n.
to_directed() Return a directed copy of the graph.
to_undirected([reciprocal]) Return an undirected representation of the digraph.

Attributes

name