DiGraph  Directed graphs with self loops¶
Overview¶

DiGraph
(data=None, **attr)[source]¶ Base class for directed graphs.
A DiGraph stores nodes and edges with optional data, or attributes.
DiGraphs hold directed edges. Self loops are allowed but multiple (parallel) edges are not.
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.DiGraph()
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. There are no errors when adding nodes or edges that already exist.
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.DiGraph(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]['weight'] = 4.7 >>> G.edge[1][2]['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
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,eattr in nbrsdict.items(): ... if 'weight' in eattr: ... (n,nbr,eattr['weight']) (1, 2, 4) (2, 3, 8) >>> [ (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.
Adding and removing nodes and edges¶
DiGraph.__init__ ([data]) 
Initialize a graph with edges, name, graph attributes. 
DiGraph.add_node (n[, attr_dict]) 
Add a single node n and update node attributes. 
DiGraph.add_nodes_from (nodes, **attr) 
Add multiple nodes. 
DiGraph.remove_node (n) 
Remove node n. 
DiGraph.remove_nodes_from (nbunch) 
Remove multiple nodes. 
DiGraph.add_edge (u, v[, attr_dict]) 
Add an edge between u and v. 
DiGraph.add_edges_from (ebunch[, attr_dict]) 
Add all the edges in ebunch. 
DiGraph.add_weighted_edges_from (ebunch[, weight]) 
Add all the edges in ebunch as weighted edges with specified weights. 
DiGraph.remove_edge (u, v) 
Remove the edge between u and v. 
DiGraph.remove_edges_from (ebunch) 
Remove all edges specified in ebunch. 
DiGraph.add_star (nodes, **attr) 
Add a star. 
DiGraph.add_path (nodes, **attr) 
Add a path. 
DiGraph.add_cycle (nodes, **attr) 
Add a cycle. 
DiGraph.clear () 
Remove all nodes and edges from the graph. 
Iterating over nodes and edges¶
DiGraph.nodes ([data]) 
Return a list of the nodes in the graph. 
DiGraph.nodes_iter ([data]) 
Return an iterator over the nodes. 
DiGraph.__iter__ () 
Iterate over the nodes. 
DiGraph.edges ([nbunch, data]) 
Return a list of edges. 
DiGraph.edges_iter ([nbunch, data]) 
Return an iterator over the edges. 
DiGraph.out_edges ([nbunch, data]) 
Return a list of edges. 
DiGraph.out_edges_iter ([nbunch, data]) 
Return an iterator over the edges. 
DiGraph.in_edges ([nbunch, data]) 
Return a list of the incoming edges. 
DiGraph.in_edges_iter ([nbunch, data]) 
Return an iterator over the incoming edges. 
DiGraph.get_edge_data (u, v[, default]) 
Return the attribute dictionary associated with edge (u,v). 
DiGraph.neighbors (n) 
Return a list of successor nodes of n. 
DiGraph.neighbors_iter (n) 
Return an iterator over successor nodes of n. 
DiGraph.__getitem__ (n) 
Return a dict of neighbors of node n. 
DiGraph.successors (n) 
Return a list of successor nodes of n. 
DiGraph.successors_iter (n) 
Return an iterator over successor nodes of n. 
DiGraph.predecessors (n) 
Return a list of predecessor nodes of n. 
DiGraph.predecessors_iter (n) 
Return an iterator over predecessor nodes of n. 
DiGraph.adjacency_list () 
Return an adjacency list representation of the graph. 
DiGraph.adjacency_iter () 
Return an iterator of (node, adjacency dict) tuples for all nodes. 
DiGraph.nbunch_iter ([nbunch]) 
Return an iterator of nodes contained in nbunch that are also in the graph. 
Information about graph structure¶
DiGraph.has_node (n) 
Return True if the graph contains the node n. 
DiGraph.__contains__ (n) 
Return True if n is a node, False otherwise. 
DiGraph.has_edge (u, v) 
Return True if the edge (u,v) is in the graph. 
DiGraph.order () 
Return the number of nodes in the graph. 
DiGraph.number_of_nodes () 
Return the number of nodes in the graph. 
DiGraph.__len__ () 
Return the number of nodes. 
DiGraph.degree ([nbunch, weight]) 
Return the degree of a node or nodes. 
DiGraph.degree_iter ([nbunch, weight]) 
Return an iterator for (node, degree). 
DiGraph.in_degree ([nbunch, weight]) 
Return the indegree of a node or nodes. 
DiGraph.in_degree_iter ([nbunch, weight]) 
Return an iterator for (node, indegree). 
DiGraph.out_degree ([nbunch, weight]) 
Return the outdegree of a node or nodes. 
DiGraph.out_degree_iter ([nbunch, weight]) 
Return an iterator for (node, outdegree). 
DiGraph.size ([weight]) 
Return the number of edges. 
DiGraph.number_of_edges ([u, v]) 
Return the number of edges between two nodes. 
DiGraph.nodes_with_selfloops () 
Return a list of nodes with self loops. 
DiGraph.selfloop_edges ([data]) 
Return a list of selfloop edges. 
DiGraph.number_of_selfloops () 
Return the number of selfloop edges. 
Making copies and subgraphs¶
DiGraph.copy () 
Return a copy of the graph. 
DiGraph.to_undirected ([reciprocal]) 
Return an undirected representation of the digraph. 
DiGraph.to_directed () 
Return a directed copy of the graph. 
DiGraph.subgraph (nbunch) 
Return the subgraph induced on nodes in nbunch. 
DiGraph.reverse ([copy]) 
Return the reverse of the graph. 