Package networkx :: Module xdigraph :: Class XDiGraph
[hide private]
[frames] | no frames]

Class XDiGraph

source code

 object --+        
          |        
graph.Graph --+    
              |    
digraph.DiGraph --+
                  |
                 XDiGraph

Digraphs with (optional) self-loops, (optional) multiple edges, arbitrary (hashable) objects as nodes, and arbitrary objects associated with edges.

An XDiGraph edge is uniquely specified by a 3-tuple e=(n1,n2,x), where n1 and n2 are (hashable) objects (nodes) and x is an arbitrary (and not necessarily unique) object associated with that edge.

See the documentation of XGraph for the use of the optional parameters selfloops (defaults is False) and multiedges (default is False).

XDiGraph inherits from DiGraph, with all purely node-specific methods identical to those of DiGraph. XDiGraph edges are identical to XGraph edges, except that they are directed rather than undirected. XDiGraph replaces the following DiGraph methods:

XDiGraph also adds the following methods to those of DiGraph:

While XDiGraph does not inherit from XGraph, we compare them here. XDigraph adds the following methods to those of XGraph:



Instance Methods [hide private]
 
__init__(self, data=None, name='', selfloops=False, multiedges=False)
Initialize XDiGraph.
source code
 
add_edge(self, n1, n2=None, x=None)
Add a single directed edge to the digraph.
source code
 
add_edges_from(self, ebunch)
Add multiple directed edges to the digraph.
source code
 
has_edge(self, n1, n2=None, x=None)
Return True if digraph contains directed edge (n1,n2,x).
source code
 
has_successor(self, n1, n2)
Return True if node n1 has a successor n2.
source code
 
has_predecessor(self, n1, n2)
Return True if node n1 has a predecessor n2.
source code
 
get_edge_iter(self, u, v=None)
Return an iterator over the objects associated with each edge from node u to node v.
source code
 
get_edge(self, u, v=None)
Return the objects associated with each edge from node u to node v.
source code
 
delete_multiedge(self, n1, n2)
Delete all edges between nodes n1 and n2.
source code
 
delete_edge(self, n1, n2=None, x=None, all=False)
Delete the directed edge (n1,n2,x) from the graph.
source code
 
delete_edges_from(self, ebunch)
Delete edges in ebunch from the graph.
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 pointing in to nodes in nbunch, or over all edges in digraph if no nodes are specified.
source code
 
successors_iter(self, n)
Return an iterator of nodes pointing out of node n.
source code
 
predecessors_iter(self, n)
Return an iterator of nodes pointing in to node n.
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
 
neighbors_iter(self, n)
Return an iterator of nodes pointing out of node n.
source code
 
predecessors(self, n)
Return predecessor nodes of n.
source code
 
successors(self, n)
Return sucessor nodes of n.
source code
 
neighbors(self, n)
Return sucessor nodes of n.
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
 
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
 
nodes_with_selfloops(self)
Return list of all nodes having self-loops.
source code
 
selfloop_edges(self)
Return all edges that are self-loops.
source code
 
number_of_selfloops(self)
Return number of self-loops in graph.
source code
 
allow_selfloops(self)
Henceforth allow addition of self-loops (edges from a node to itself).
source code
 
remove_all_selfloops(self)
Remove self-loops from the graph (edges from a node to itself).
source code
 
ban_selfloops(self)
Remove self-loops from the graph and henceforth do not allow their creation.
source code
 
allow_multiedges(self)
Henceforth allow addition of multiedges (more than one edge between two nodes).
source code
 
remove_all_multiedges(self)
Remove multiedges retaining the data from the first edge
source code
 
ban_multiedges(self)
Remove multiedges retaining the data from the first edge.
source code
 
subgraph(self, nbunch, inplace=False, create_using=None)
Return the subgraph induced on nodes in nbunch.
source code
 
copy(self)
Return a (shallow) copy of the digraph.
source code
 
to_undirected(self)
Return the underlying graph of G.
source code
 
reverse(self)
Return a new digraph with the same vertices and edges as self but with the directions of the edges reversed.
source code
 
number_of_edges(self, u=None, v=None, x=None)
Return the number of edges between nodes u and v.
source code

Inherited from digraph.DiGraph: add_node, add_nodes_from, clear, delete_node, delete_nodes_from, in_degree, in_edges, in_neighbors, is_directed, out_degree, out_edges, out_neighbors, to_directed

Inherited from graph.Graph: __contains__, __getitem__, __iter__, __len__, __str__, add_cycle, add_path, degree, edge_boundary, edges, has_neighbor, has_node, info, node_boundary, nodes, nodes_iter, 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='', selfloops=False, multiedges=False)
(Constructor)

source code 

Initialize XDiGraph.

Optional arguments:: name: digraph name (default="No Name") selfloops: if True then selfloops are allowed (default=False) multiedges: if True then multiple edges are allowed (default=False)

Overrides: digraph.DiGraph.__init__

add_edge(self, n1, n2=None, x=None)

source code 

Add a single directed edge to the digraph.

Can be called as G.add_edge(n1,n2,x) or as G.add_edge(e), where e=(n1,n2,x).

If called as G.add_edge(n1,n2) or G.add_edge(e), with e=(n1,n2), then this is interpreted as adding the edge (n1,n2,None) to be compatible with the Graph and DiGraph classes.

n1,n2 are node objects, and are added to the Graph if not already present. Nodes must be hashable Python objects (except None).

x is an arbitrary (not necessarily hashable) object associated with this edge. It can be used to associate one or more, labels, data records, weights or any arbirary objects to edges. The default is the Python None.

For example, if the graph G was created with

>>> G=XDiGraph()

then G.add_edge(1,2,"blue") will add the directed edge (1,2,"blue").

If G.multiedges=False, then a subsequent G.add_edge(1,2,"red") will change the above edge (1,2,"blue") into the edge (1,2,"red").

On the other hand, if G.multiedges=True, then two successive calls to G.add_edge(1,2,"red") will result in 2 edges of the form (1,2,"red") that can not be distinguished from one another.

If self.selfloops=False, then any attempt to create a self-loop with add_edge(n1,n1,x) will have no effect on the digraph and will not elicit a warning.

Objects imbedded in the edges from n1 to n2 (if any), can be retrieved using get_edge(n1,n2), or calling edges(n1) or edge_iter(n1) to return all edges attached to n1.

Overrides: digraph.DiGraph.add_edge

add_edges_from(self, ebunch)

source code 
Add multiple directed edges to the digraph. ebunch: Container of edges. Each edge e in container will be added using add_edge(e). See add_edge documentation. The container must be iterable or an iterator. It is iterated over once.
Overrides: digraph.DiGraph.add_edges_from

has_edge(self, n1, n2=None, x=None)

source code 

Return True if digraph contains directed edge (n1,n2,x).

Can be called as G.has_edge(n1,n2,x) or as G.has_edge(e), where e=(n1,n2,x).

If x is unspecified, i.e. if called with an edge of the form e=(n1,n2), then return True if there exists ANY edge from n1 to n2 (equivalent to has_successor(n1,n2)).

Overrides: graph.Graph.has_edge

has_successor(self, n1, n2)

source code 

Return True if node n1 has a successor n2.

Return True if there exists ANY edge (n1,n2,x) for some x.

has_predecessor(self, n1, n2)

source code 

Return True if node n1 has a predecessor n2.

Return True if there exists ANY edge (n2,n1,x) for some x.

get_edge(self, u, v=None)

source code 

Return the objects associated with each edge from node u to node v.

If multiedges=False, a single object is returned. If multiedges=True, a list of objects is returned. If no edge exists, None is returned.

Overrides: graph.Graph.get_edge

delete_multiedge(self, n1, n2)

source code 

Delete all edges between nodes n1 and n2.

When there is only a single edge allowed between nodes (multiedges=False), this just calls delete_edge(n1,n2), otherwise (multiedges=True) all edges between n1 and n2 are deleted.

delete_edge(self, n1, n2=None, x=None, all=False)

source code 

Delete the directed edge (n1,n2,x) from the graph.

Can be called either as >>> G.delete_edge(n1,n2,x) or as >>> G.delete_edge(e) where e=(n1,n2,x).

If called with an edge e=(n1,n2), or as G.delete_edge(n1,n2) then the edge (n1,n2,None) will be deleted.

If the edge does not exist, do nothing.

To delete all edges between n1 and n2 use >>> G.delete_multiedges(n1,n2)

Overrides: digraph.DiGraph.delete_edge

delete_edges_from(self, ebunch)

source code 

Delete edges in ebunch from the graph.

ebunch: Container of edges. Each edge must be a 3-tuple (n1,n2,x) or a 2-tuple (n1,n2). The container must be iterable or an iterator, and is iterated over once.

Edges that are not in the graph are ignored.

Overrides: digraph.DiGraph.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.

Overrides: digraph.DiGraph.out_edges_iter

in_edges_iter(self, nbunch=None)

source code 

Return iterator that iterates once over each edge pointing in 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.

Overrides: digraph.DiGraph.in_edges_iter

successors_iter(self, n)

source code 

Return an iterator of nodes pointing out of node n.

Returns the same data as out_edges(n) but in a different format.

Overrides: digraph.DiGraph.successors_iter

predecessors_iter(self, n)

source code 

Return an iterator of nodes pointing in to node n.

Returns the same data as in_edges(n) but in a different format.

Overrides: digraph.DiGraph.predecessors_iter

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: digraph.DiGraph.out_edges_iter

neighbors_iter(self, n)

source code 

Return an iterator of nodes pointing out of node n.

Returns the same data as out_edges(n) but in a different format.

Overrides: digraph.DiGraph.successors_iter

predecessors(self, n)

source code 
Return predecessor nodes of n.
Overrides: digraph.DiGraph.predecessors

successors(self, n)

source code 
Return sucessor nodes of n.
Overrides: digraph.DiGraph.successors

neighbors(self, n)

source code 
Return sucessor nodes of n.
Overrides: digraph.DiGraph.successors

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.

Overrides: digraph.DiGraph.in_degree_iter

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.

Overrides: digraph.DiGraph.out_degree_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: digraph.DiGraph.degree_iter

allow_selfloops(self)

source code 

Henceforth allow addition of self-loops (edges from a node to itself).

This doesn't change the graph structure, only what you can do to it.

allow_multiedges(self)

source code 

Henceforth allow addition of multiedges (more than one edge between two nodes).

Warning: This causes all edge data to be converted to lists.

ban_multiedges(self)

source code 
Remove multiedges retaining the data from the first edge. Henceforth do not allow multiedges.

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 induced subgraph in original graph by deleting nodes not in nbunch. It overrides any setting of create_using.

WARNING: specifying inplace=True makes it easy to 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: digraph.DiGraph.subgraph

copy(self)

source code 

Return a (shallow) copy of the digraph.

Return a new XDiGraph with same name and same attributes for selfloop and multiededges. Each node and each edge in original graph are added to the copy.

Overrides: digraph.DiGraph.copy

to_undirected(self)

source code 

Return the underlying graph of G.

The underlying graph is its undirected representation: each directed edge is replaced with an undirected edge.

If multiedges=True, then an XDiGraph with only two directed edges (1,2,"red") and (2,1,"blue") will be converted into an XGraph with two undirected edges (1,2,"red") and (1,2,"blue"). Two directed edges (1,2,"red") and (2,1,"red") will result in in two undirected edges (1,2,"red") and (1,2,"red").

If multiedges=False, then two directed edges (1,2,"red") and (2,1,"blue") can only result in one undirected edge, and there is no guarantee which one it is.

Overrides: digraph.DiGraph.to_undirected

reverse(self)

source code 
Return a new digraph with the same vertices and edges as self but with the directions of the edges reversed.
Overrides: digraph.DiGraph.reverse

number_of_edges(self, u=None, v=None, x=None)

source code 

Return the number of edges between nodes u and v.

If u and v are not specified return the number of edges in the entire graph.

The edge argument e=(u,v) can be specified as G.number_of_edges(u,v) or G.number_of_edges(e)

Overrides: graph.Graph.number_of_edges