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
name : string, optional (default=’‘)
attr : keyword arguments, optional (default= no attributes)
|
---|
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.path_graph(10)
>>> 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
>>> G.nodes(data=True)
[(1, {'room': 714, '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
>>> print [n for n in G if n<3] # iterate through nodes
[1, 2]
>>> print len(G) # number of nodes in graph
5
>>> print 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.iteritems():
... for key,eattr in keydict.iteritems():
... if 'weight' in eattr:
... print (n,nbr,eattr['weight'])
(1, 2, 4)
(2, 3, 8)
>>> print [ (u,v,edata['weight']) for u,v,edata in G.edges(data=True) if 'weight' in edata ]
[(1, 2, 4), (2, 3, 8)]
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.
MultiDiGraph.__init__(**attr[, data, name]) | Initialize a graph with edges, name, graph attributes. |
MultiDiGraph.add_node(n, **attr[, 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, **attr[, key, ...]) | Add an edge between u and v. |
MultiDiGraph.add_edges_from(ebunch, **attr) | 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(nlist, **attr) | Add a star. |
MultiDiGraph.add_path(nlist, **attr) | Add a path. |
MultiDiGraph.add_cycle(nlist, **attr) | Add a cycle. |
MultiDiGraph.clear() | Remove all nodes and edges from the graph. |
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]) | Return a list of edges. |
MultiDiGraph.edges_iter([nbunch, data, keys]) | Return an iterator over the edges. |
MultiDiGraph.out_edges([nbunch, data]) | Return a list of edges. |
MultiDiGraph.out_edges_iter([nbunch, data, keys]) | Return an iterator over the edges. |
MultiDiGraph.in_edges([nbunch, 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. |
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. Use the expression |
MultiDiGraph.has_edge(u, v[, key]) | Return True if the graph has an edge between nodes u and v. |
MultiDiGraph.nodes_with_selfloops() | Return a list of nodes with self loops. |
MultiDiGraph.selfloop_edges([data, keys]) | Return a list of selfloop edges. |
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.size([weighted]) | Return the number of edges. |
MultiDiGraph.number_of_edges([u, v]) | Return the number of edges between two nodes. |
MultiDiGraph.number_of_selfloops() | Return the number of selfloop edges. |
MultiDiGraph.degree([nbunch, with_labels, ...]) | Return the degree of a node or nodes. |
MultiDiGraph.degree_iter([nbunch, weighted]) | Return an iterator for (node, degree). |
MultiDiGraph.in_degree([nbunch, ...]) | Return the in-degree of a node or nodes. |
MultiDiGraph.in_degree_iter([nbunch, weighted]) | Return an iterator for (node, in-degree). |
MultiDiGraph.out_degree([nbunch, ...]) | Return the out-degree of a node or nodes. |
MultiDiGraph.out_degree_iter([nbunch, weighted]) | Return an iterator for (node, out-degree). |
MultiDiGraph.copy() | Return a copy of the graph. |
MultiDiGraph.to_undirected() | Return an undirected representation of the digraph. |
MultiDiGraph.subgraph(nbunch[, copy]) | Return the subgraph induced on nodes in nbunch. |
MultiDiGraph.reverse([copy]) | Return the reverse of the graph. |