Package networkx :: Module digraph :: Class DiGraph
[hide private]
[frames] | no frames]

Class DiGraph

source code

 object --+    
          |    
graph.Graph --+
              |
             DiGraph
Known Subclasses:
tree.DirectedForest, tree.DirectedTree, xdigraph.XDiGraph

A graph with directed edges. Subclass of Graph.

DiGraph inherits from Graph, overriding the following methods:

Digraph adds the following methods to those of Graph:



Instance Methods [hide private]
 
__init__(self, data=None, name='')
Initialize Graph.
source code
 
add_node(self, n)
Add a single node to the digraph.
source code
 
add_nodes_from(self, nlist)
Add multiple nodes to the digraph.
source code
 
delete_node(self, n)
Delete node n from the digraph.
source code
 
delete_nodes_from(self, nlist)
Remove nodes in nlist from the digraph.
source code
 
add_edge(self, u, v=None)
Add a single directed edge (u,v) to the digraph.
source code
 
add_edges_from(self, ebunch)
Add all the edges in ebunch to the graph.
source code
 
delete_edge(self, u, v=None)
Delete the single directed edge (u,v) from the digraph.
source code
 
delete_edges_from(self, ebunch)
Delete the directed edges in ebunch from the digraph.
source code
 
out_edges_iter(self, nbunch=None)
Return iterator that iterates once over each edge pointing out of nodes in nbunch, or over all edges in digraph if no nodes are specified.
source code
 
in_edges_iter(self, nbunch=None)
Return iterator that iterates once over each edge adjacent to nodes in nbunch, or over all edges in digraph if no nodes are specified.
source code
 
edges_iter(self, nbunch=None)
Return iterator that iterates once over each edge pointing out of nodes in nbunch, or over all edges in digraph if no nodes are specified.
source code
 
out_edges(self, nbunch=None)
Return list of all edges that point out of nodes in nbunch, or a list of all edges in graph if no nodes are specified.
source code
 
in_edges(self, nbunch=None)
Return list of all edges that point in to nodes in nbunch, or a list of all edges in graph if no nodes are specified.
source code
 
successors_iter(self, n)
Return an iterator for successor nodes of n.
source code
 
predecessors_iter(self, n)
Return an iterator for predecessor nodes of n.
source code
 
successors(self, n)
Return sucessor nodes of n.
source code
 
predecessors(self, n)
Return predecessor nodes of n.
source code
 
out_neighbors(self, n)
Return sucessor nodes of n.
source code
 
in_neighbors(self, n)
Return predecessor nodes of n.
source code
 
neighbors(self, n)
Return sucessor nodes of n.
source code
 
neighbors_iter(self, n)
Return an iterator for successor nodes of n.
source code
 
degree_iter(self, nbunch=None, with_labels=False)
Return iterator that returns in_degree(n)+out_degree(n) or (n,in_degree(n)+out_degree(n)) for all n in nbunch.
source code
 
in_degree_iter(self, nbunch=None, with_labels=False)
Return iterator for in_degree(n) or (n,in_degree(n)) for all n in nbunch.
source code
 
out_degree_iter(self, nbunch=None, with_labels=False)
Return iterator for out_degree(n) or (n,out_degree(n)) for all n in nbunch.
source code
 
out_degree(self, nbunch=None, with_labels=False)
Return out-degree of single node or of nbunch of nodes.
source code
 
in_degree(self, nbunch=None, with_labels=False)
Return in-degree of single node or of nbunch of nodes.
source code
 
clear(self)
Remove name and delete all nodes and edges from digraph.
source code
 
copy(self)
Return a (shallow) copy of the digraph.
source code
 
subgraph(self, nbunch, inplace=False, create_using=None)
Return the subgraph induced on nodes in nbunch.
source code
 
is_directed(self)
Return True if a directed graph.
source code
 
to_undirected(self)
Return the undirected representation of the digraph.
source code
 
to_directed(self)
Return a directed representation of the digraph.
source code
 
reverse(self)
Return a new digraph with the same vertices and edges as G but with the directions of the edges reversed.
source code

Inherited from graph.Graph: __contains__, __getitem__, __iter__, __len__, __str__, add_cycle, add_path, degree, edge_boundary, edges, get_edge, has_edge, has_neighbor, has_node, info, node_boundary, nodes, nodes_iter, number_of_edges, number_of_nodes, order, prepare_nbunch, size

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, data=None, name='')
(Constructor)

source code 

Initialize Graph.

>>> G=Graph(name="empty")

creates empty graph G with G.name="empty"

Overrides: graph.Graph.__init__
(inherited documentation)

add_node(self, n)

source code 

Add a single node to the digraph.

The node n can be any hashable object except None.

A hashable object is one that can be used as a key in a Python dictionary. This includes strings, numbers, tuples of strings and numbers, etc. On many platforms this also includes mutables such as Graphs, though one should be careful that the hash doesn't change on mutables.

>>> from networkx import *
>>> G=DiGraph()
>>> K3=complete_graph(3)
>>> G.add_nodes_from(K3)    # add the nodes from K3 to G
>>> G.nodes()
[0, 1, 2]
>>> G.clear()
>>> G.add_node(K3)          # add the graph K3 as a node in G.
>>> G.number_of_nodes()
1
Overrides: graph.Graph.add_node

add_nodes_from(self, nlist)

source code 

Add multiple nodes to the digraph.

nlist: A container of nodes that will be iterated through once (thus it should be an iterator or be iterable). Each element of the container should be a valid node type: any hashable type except None. See add_node for details.

Overrides: graph.Graph.add_nodes_from

delete_node(self, n)

source code 
Delete node n from the digraph. Attempting to delete a non-existent node will raise a NetworkXError.
Overrides: graph.Graph.delete_node

delete_nodes_from(self, nlist)

source code 

Remove nodes in nlist from the digraph.

nlist: an iterable or iterator containing valid node names.

Attempting to delete a non-existent node will raise an exception. This could mean some nodes in nlist were deleted and some valid nodes were not!

Overrides: graph.Graph.delete_nodes_from

add_edge(self, u, v=None)

source code 

Add a single directed edge (u,v) to the digraph.

>> G.add_edge(u,v) and >>> G.add_edge( (u,v) ) are equivalent forms of adding a single edge between nodes u and v. The nodes u and v will be automatically added if not already in the graph. They must be a hashable (except None) Python object.

For example, the following examples all add the edge (1,2) to the digraph G.

>>> G=DiGraph()
>>> G.add_edge( 1, 2 )          # explicit two node form
>>> G.add_edge( (1,2) )         # single edge as tuple of two nodes
>>> G.add_edges_from( [(1,2)] ) # list of edges form
Overrides: graph.Graph.add_edge

add_edges_from(self, ebunch)

source code 

Add all the edges in ebunch to the graph.

ebunch: Container of 2-tuples (u,v). The container must be iterable or an iterator. It is iterated over once. Adding the same edge twice has no effect and does not raise an exception.

See add_edge for an example.

Overrides: graph.Graph.add_edges_from

delete_edge(self, u, v=None)

source code 

Delete the single directed edge (u,v) from the digraph.

Can be used in two basic forms >>> G.delete_edge(u,v) and G.delete_edge( (u,v) ) are equivalent ways of deleting a directed edge u->v.

If the edge does not exist return without complaining.

Overrides: graph.Graph.delete_edge

delete_edges_from(self, ebunch)

source code 

Delete the directed edges in ebunch from the digraph.

ebunch: Container of 2-tuples (u,v). The container must be iterable or an iterator. It is iterated over once.

Edges that are not in the digraph are ignored.

Overrides: graph.Graph.delete_edges_from

out_edges_iter(self, nbunch=None)

source code 

Return iterator that iterates once over each edge pointing out of nodes in nbunch, or over all edges in digraph if no nodes are specified.

See edges() for definition of nbunch.

Nodes in nbunch that are not in the graph will be (quietly) ignored.

in_edges_iter(self, nbunch=None)

source code 

Return iterator that iterates once over each edge adjacent to nodes in nbunch, or over all edges in digraph if no nodes are specified.

See edges() for definition of nbunch.

Nodes in nbunch that are not in the graph will be (quietly) ignored.

edges_iter(self, nbunch=None)

source code 

Return iterator that iterates once over each edge pointing out of nodes in nbunch, or over all edges in digraph if no nodes are specified.

See edges() for definition of nbunch.

Nodes in nbunch that are not in the graph will be (quietly) ignored.

Overrides: graph.Graph.edges_iter

out_edges(self, nbunch=None)

source code 

Return list of all edges that point out of nodes in nbunch, or a list of all edges in graph if no nodes are specified.

See edges() for definition of nbunch.

Nodes in nbunch that are not in the graph will be (quietly) ignored.

in_edges(self, nbunch=None)

source code 

Return list of all edges that point in to nodes in nbunch, or a list of all edges in graph if no nodes are specified.

See edges() for definition of nbunch.

Nodes in nbunch that are not in the graph will be (quietly) ignored.

neighbors(self, n)

source code 
Return sucessor nodes of n.
Overrides: graph.Graph.neighbors

neighbors_iter(self, n)

source code 
Return an iterator for successor nodes of n.
Overrides: graph.Graph.neighbors_iter

degree_iter(self, nbunch=None, with_labels=False)

source code 

Return iterator that returns in_degree(n)+out_degree(n) or (n,in_degree(n)+out_degree(n)) for all n in nbunch. If nbunch is ommitted, then iterate over all nodes.

Can be called in three ways: G.degree_iter(n): return iterator the degree of node n G.degree_iter(nbunch): return a list of values, one for each n in nbunch (nbunch is any iterable container of nodes.) G.degree_iter(): same as nbunch = all nodes in graph.

If with_labels=True, iterator will return an (n,in_degree(n)+out_degree(n)) tuple of node and degree.

Any nodes in nbunch but not in the graph will be (quietly) ignored.

Overrides: graph.Graph.degree_iter

in_degree_iter(self, nbunch=None, with_labels=False)

source code 

Return iterator for in_degree(n) or (n,in_degree(n)) for all n in nbunch.

If nbunch is ommitted, then iterate over all nodes.

See degree_iter method for more details.

out_degree_iter(self, nbunch=None, with_labels=False)

source code 

Return iterator for out_degree(n) or (n,out_degree(n)) for all n in nbunch.

If nbunch is ommitted, then iterate over all nodes.

See degree_iter method for more details.

out_degree(self, nbunch=None, with_labels=False)

source code 

Return out-degree of single node or of nbunch of nodes.

If nbunch is omitted or nbunch=None, then return out-degrees of all nodes.

in_degree(self, nbunch=None, with_labels=False)

source code 

Return in-degree of single node or of nbunch of nodes.

If nbunch is omitted or nbunch=None, then return in-degrees of all nodes.

clear(self)

source code 
Remove name and delete all nodes and edges from digraph.
Overrides: graph.Graph.clear

copy(self)

source code 

Return a (shallow) copy of the digraph.

Identical to dict.copy() of adjacency dicts pred and succ, with name copied as well.

Overrides: graph.Graph.copy

subgraph(self, nbunch, inplace=False, create_using=None)

source code 

Return the subgraph induced on nodes in nbunch.

nbunch: can be a single node or any iterable container of of nodes. (It can be an iterable or an iterator, e.g. a list, set, graph, file, numeric array, etc.)

Setting inplace=True will return the induced subgraph in original graph by deleting nodes not in nbunch. This overrides create_using. Warning: this can destroy the graph.

Unless otherwise specified, return a new graph of the same type as self. Use (optional) create_using=R to return the resulting subgraph in R. R can be an existing graph-like object (to be emptied) or R can be a call to a graph object, e.g. create_using=DiGraph(). See documentation for empty_graph()

Note: use subgraph(G) rather than G.subgraph() to access the more general subgraph() function from the operators module.

Overrides: graph.Graph.subgraph

is_directed(self)

source code 
Return True if a directed graph.
Overrides: graph.Graph.is_directed

to_undirected(self)

source code 

Return the undirected representation of the digraph.

A new graph is returned (the underlying graph). The edge u-v is in the underlying graph if either u->v or v->u is in the digraph.

Overrides: graph.Graph.to_undirected

to_directed(self)

source code 

Return a directed representation of the digraph.

This is already directed, so merely return a copy.

Overrides: graph.Graph.to_directed