networkx.DiGraph

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

Examples

Create a graph subclass that tracks the order nodes are added.

>>> from collections import OrderedDict
>>> class OrderedNodeGraph(nx.Graph):
...     node_dict_factory=OrderedDict
>>> G=OrderedNodeGraph()
>>> G.add_nodes_from( (2,1) )
>>> list(G.nodes())
[2, 1]
>>> G.add_edges_from( ((2,2), (2,1), (1,1)) )
>>> list(G.edges())
[(2, 1), (2, 2), (1, 1)]

Create a graph object that tracks the order nodes are added and for each node track the order that neighbors are added.

>>> class OrderedGraph(nx.Graph):
...    node_dict_factory = OrderedDict
...    adjlist_dict_factory = OrderedDict
>>> G = OrderedGraph()
>>> G.add_nodes_from( (2,1) )
>>> list(G.nodes())
[2, 1]
>>> G.add_edges_from( ((2,2), (2,1), (1,1)) )
>>> list(G.edges())
[(2, 2), (2, 1), (1, 1)]

Create a low memory graph class that effectively disallows edge attributes by using a single attribute dict for all edges. This reduces the memory used, but you lose edge attributes.

>>> class ThinGraph(nx.Graph):
...     all_edge_dict = {'weight': 1}
...     def single_edge_dict(self):
...         return self.all_edge_dict
...     edge_attr_dict_factory = single_edge_dict
>>> G = ThinGraph()
>>> G.add_edge(2,1)
>>> list(G.edges(data= True))
[(1, 2, {'weight': 1})]
>>> G.add_edge(2,2)
>>> G[2][1] is G[2][2]
True
__init__(data=None, **attr)[source]

Initialize a graph with edges, name, graph 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.

name : string, optional (default=’‘)

An optional name for the graph.

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

Attributes to add to graph as key=value pairs.

See also

convert

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G = nx.Graph(name='my graph')
>>> e = [(1,2),(2,3),(3,4)] # list of edges
>>> G = nx.Graph(e)

Arbitrary graph attribute pairs (key=value) may be assigned

>>> G=nx.Graph(e, day="Friday")
>>> G.graph
{'day': 'Friday'}

Methods

__init__([data]) Initialize a graph with edges, name, graph attributes.
add_edge(u, v[, 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, default]) Return an iterator over the edges.
get_edge_data(u, v[, default]) Return the attribute dictionary associated with edge (u,v).
has_edge(u, v) Return True if the edge (u,v) is in the graph.
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]) 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, default]) Return an iterator over the edges.
predecessors(n) Return an iterator over predecessor nodes of n.
remove_edge(u, v) Remove the 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, default]) Returns an iterator over 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