Package networkx :: Module xbase :: Class XDiGraph

[frames | no frames]

## Type XDiGraph

    object --+
             |
         Graph --+
                 |
           DiGraph --+
                     |
                    XDiGraph

A class implementing general undirected digraphs, allowing (optional) self-loops, multiple edges, arbitrary (hashable) objects as nodes and arbitrary objects associated with edges.

As in XGraph, 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.

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:

- __init__: read multiedges and selfloops kwds.
- add_edge
- add_edges_from
- delete_edge
- delete_edges_from
- has_edge
- edges_iter
- degree_iter
- degree
- copy
- clear
- subgraph
- is_directed
- to_directed

XDiGraph also adds the following methods to those of DiGraph:

- allow_selfloops
- remove_selfloops
- ban_selfloops
- allow_multiedges
- remove_multiedges
- ban_multiedges

XDigraph adds the following methods to those of XGraph:

- has_successor
- successors
- successors_iter
- has_predecessor
- predecessors
- predecessors_iter
- out_degree
- out_degree_iter
- in_degree
- in_degree_iter
- to_undirected
- is_directed

Method Summary
| ``__init__`(self, **kwds)` Initialize XDiGraph.
| ``add_edge`(self, n1, n2, x)` Add a single directed edge to the digraph.
| ``add_edges_from`(self, ebunch)` Add multiple directed edges to the digraph.
| ``allow_multiedges`(self)` Henceforth allow addition of multiedges (more than one edge between two nodes).
| ``allow_selfloops`(self)` Henceforth allow addition of self-loops (edges from a node to itself).
| ``ban_multiedges`(self)` Remove multiedges retaining the data from the first edge.
| ``ban_selfloops`(self)` Remove self-loops from the graph and henceforth do not allow their creation.
| ``copy`(self)` Return a (shallow) copy of the digraph.
| ``degree`(self, nbunch, with_labels)` Return the out-degree of single node or of nbunch of nodes.
| ``delete_edge`(self, n1, n2, x)` Delete the directed edge (n1,n2,x) from the graph.
| ``delete_edges_from`(self, ebunch, data)` Delete edges in ebunch from the graph.
| ``delete_multiedges`(self)` Remove multiedges retaining the data from the first edge
| ``delete_selfloops`(self)` Remove self-loops from the graph (edges from a node to itself).
| ``edges_iter`(self, nbunch, with_labels)` Return iterator that iterates once over each edge adjacent to nodes in nbunch, or over all edges in digraph if no nodes are specified.
| ``get_edge`(self, n1, n2)` Return the objects associated with each edge between n1 and n2.
| ``has_edge`(self, n1, n2, x)` Return True if digraph contains directed edge (n1,n2,x).
| ``has_neighbor`(self, n1, n2)` Return True if node n1 and n2 are connected.
| ``has_predecessor`(self, n1, n2)` Return True if node n1 has a predecessor n2.
| ``has_successor`(self, n1, n2)` Return True if node n1 has a successor n2.
| ``in_degree`(self, nbunch, with_labels)` Return the in-degree of single node or of nbunch of nodes.
| ``neighbors`(self, n, with_labels)` Return a list of all nodes connected to node n.
| ``neighbors_iter`(self, n, with_labels)` Return an iterator for neighbors of n.
| ``nodes_with_selfloops`(self)` Return list of all nodes having self-loops.
| ``number_of_selfloops`(self)` Return number of self-loops in graph.
| ``out_degree`(self, nbunch, with_labels)` Return the out-degree of single node or of nbunch of nodes.
| ``predecessors`(self, n, with_labels)` Return a list of predecessor nodes of node n.
| ``selfloop_edges`(self)` Return all edges that are self-loops.
| ``subgraph`(self, nbunch, inplace, create_using)` Return the subgraph induced on nodes in nbunch.
| ``successors`(self, n, with_labels)` Return a list of all successor nodes of node n.
| ``to_undirected`(self)` Return the underlying graph of G.

Inherited from `DiGraph`: `__getitem__`, `add_node`, `add_nodes_from`, `clear`, `degree_iter`, `delete_node`, `delete_nodes_from`, `foo`, `in_degree_iter`, `is_directed`, `out_degree_iter`, `predecessors_iter`, `reverse`, `successors_iter`, `to_directed`
Inherited from `Graph`: `__contains__`, `__iter__`, `__len__`, `__str__`, `add_cycle`, `add_path`, `edge_boundary`, `edges`, `has_node`, `node_boundary`, `nodes`, `nodes_iter`, `number_of_edges`, `number_of_nodes`, `order`, `print_dna`, `size`
Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`

Method Details

### __init__(self, **kwds)
(Constructor)

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:
    `networkx.base.DiGraph.__init__`

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

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,1), so as to be compatible with the Graph and DiGraph classes.

n1,n2 are (hashable) node objects, and are added silently to the Graph if not already present.

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.

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:
    `networkx.base.DiGraph.add_edge`

### add_edges_from(self, ebunch)

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:
    `networkx.base.DiGraph.add_edges_from`

### allow_multiedges(self)

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

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

### allow_selfloops(self)

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.

### ban_multiedges(self)

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

### ban_selfloops(self)

Remove self-loops from the graph and henceforth do not allow their creation.

### copy(self)

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:
    `networkx.base.DiGraph.copy`

### degree(self, nbunch=None, with_labels=False)

Return the out-degree of single node or of nbunch of nodes. If nbunch is omitted or nbunch=None, then return out-degrees of all nodes.

If with_labels=True, then return a dict that maps each n in nbunch to out_degree(n).

Any nodes in nbunch that are not in the graph are (quietly) ignored.

  Overrides:
    `networkx.base.DiGraph.degree`

### delete_edge(self, n1, n2=None, x=None)

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 x is unspecified, i.e. if called with an edge e=(n1,n2), or as G.delete_edge(n1,n2), then delete all edges between n1 and n2.

If the edge does not exist, do nothing.

  Overrides:
    `networkx.base.DiGraph.delete_edge`

### delete_edges_from(self, ebunch, data=None)

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:
    `networkx.base.DiGraph.delete_edges_from`

### delete_multiedges(self)

Remove multiedges retaining the data from the first edge

### delete_selfloops(self)

Remove self-loops from the graph (edges from a node to itself).

### edges_iter(self, nbunch=None, with_labels=False)

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 add_node for definition of nbunch.

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

with_labels=True is not supported. (In that case you should probably use neighbors().)

  Overrides:
    `networkx.base.DiGraph.edges_iter`

### get_edge(self, n1, n2)

Return the objects associated with each edge between n1 and n2.

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

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

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:
    `networkx.base.Graph.has_edge`

### has_neighbor(self, n1, n2)

Return True if node n1 and n2 are connected.

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

  Overrides:
    `networkx.base.Graph.has_neighbor`

### has_predecessor(self, n1, n2)

Return True if node n1 has a predecessor n2.

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

### has_successor(self, n1, n2)

Return True if node n1 has a successor n2.

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

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

Return the in-degree of single node or of nbunch of nodes. If nbunch is omitted or nbunch=None, then return in-degrees of all nodes.

If with_labels=True, then return a dict that maps each n in nbunch to in_degree(n).

Any nodes in nbunch that are not in the graph are (quietly) ignored.

  Overrides:
    `networkx.base.DiGraph.in_degree`

### neighbors(self, n, with_labels=False)

Return a list of all nodes connected to node n.

If with_labels=True, return a dict keyed by neighbors to edge data for that edge. If neighbor has both in and out edge, the edge data is returned as the list [indata, outdata]

The node n will be a neighbor of itself if a selfloop exists.

  Overrides:
    `networkx.base.DiGraph.neighbors`

### neighbors_iter(self, n, with_labels=False)

Return an iterator for neighbors of n.

If with_labels=True, the iterator returns (neighbor, edge_data) tuples for each edge. If neighbor has both in and out edges, the edge data is either: 1) concatenated as lists if multiedges==True, or 2) returned as the list [indata, outdata] if multiedges==False.

The node n will be a neighbor of itself if a selfloop exists.

  Overrides:
    `networkx.base.DiGraph.neighbors_iter`

### nodes_with_selfloops(self)

Return list of all nodes having self-loops.

### number_of_selfloops(self)

Return number of self-loops in graph.

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

Return the out-degree of single node or of nbunch of nodes. If nbunch is omitted or nbunch=None, then return out-degrees of all nodes.

If with_labels=True, then return a dict that maps each n in nbunch to out_degree(n).

Any nodes in nbunch that are not in the graph are (quietly) ignored.

  Overrides:
    `networkx.base.DiGraph.out_degree`

### predecessors(self, n, with_labels=False)

Return a list of predecessor nodes of node n.

If with_labels=True, return a dict keyed by predecessors to edge data for that edge.

  Overrides:
    `networkx.base.DiGraph.predecessors`

### selfloop_edges(self)

Return all edges that are self-loops.

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

Return the subgraph induced on nodes in nbunch.

nbunch: either a singleton node, a string (which is treated as a singleton node), or any non-string iterable or iterator. For example, a list, dict, set, Graph, numeric array, or user-defined iterable object.

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:
    `networkx.base.Graph.subgraph`

### successors(self, n, with_labels=False)

Return a list of all successor nodes of node n.

If with_labels=True, return a dict keyed by successors to edge data for that edge.

  Overrides:
    `networkx.base.DiGraph.successors`

### to_undirected(self)

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 garantee which one it is.

  Overrides:
    `networkx.base.DiGraph.to_undirected`

Generated by Epydoc 2.1 on Sun Aug 21 08:06:58 2005 | http://epydoc.sf.net