Package networkx :: Module xgraph :: Class XGraph
[hide private]
[frames] | no frames]

Class XGraph

source code

 object --+    
          |    
graph.Graph --+
              |
             XGraph

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

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

>>> G=XGraph()

creates an empty simple and undirected graph (no self-loops or multiple edges allowed). It is equivalent to the expression:

>>> G=XGraph(name='',selfloops=False,multiedges=False)
>>> G=XGraph(name="empty",multiedges=True)

creates an empty graph with G.name="empty", that does not allow the addition of self-loops but does allow for multiple edges.

See also the XDiGraph class.

XGraph inherits from Graph, overriding the following methods:

XGraph adds the following methods to those of Graph:



Instance Methods [hide private]
 
__init__(self, data=None, name='', selfloops=False, multiedges=False)
Initialize XGraph.
source code
 
__getitem__(self, n)
Return the neighbors of node n as a list.
source code
 
add_edge(self, n1, n2=None, x=None)
Add a single edge to the graph.
source code
 
add_edges_from(self, ebunch)
Add multiple edges to the graph.
source code
 
has_edge(self, n1, n2=None, x=None)
Return True if graph contains edge (n1,n2,x).
source code
 
has_neighbor(self, n1, n2)
Return True if node n1 has neighbor n2.
source code
 
neighbors_iter(self, n)
Return an iterator of nodes connected to node n.
source code
 
neighbors(self, n)
Return a list of nodes connected to node n.
source code
 
get_edge_iter(self, u, v)
Return an iterator over the objects associated with each edge from node u to node v.
source code
 
get_edge(self, u, v)
Return the objects associated with each edge from node u to node v.
source code
 
edges_iter(self, nbunch=None)
Return iterator that iterates once over each edge adjacent to nodes in nbunch, or over all nodes in graph if nbunch=None.
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)
Delete the edge (n1,n2,x) from the graph.
source code
 
delete_edges_from(self, ebunch)
Delete edges in ebunch from the graph.
source code
 
degree_iter(self, nbunch=None, with_labels=False)
This is the degree() method returned in iterator form.
source code
 
copy(self)
Return a (shallow) copy of the graph.
source code
 
to_directed(self)
Return a directed representation of the XGraph G.
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
 
number_of_edges(self, u=None, v=None, x=None)
Return the number of edges between nodes u and v.
source code

Inherited from graph.Graph: __contains__, __iter__, __len__, __str__, add_cycle, add_node, add_nodes_from, add_path, clear, degree, delete_node, delete_nodes_from, edge_boundary, edges, has_node, info, is_directed, node_boundary, nodes, nodes_iter, number_of_nodes, order, prepare_nbunch, size, to_undirected

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 XGraph.

Optional arguments:: name: graph name (default='') selfloops: if True selfloops are allowed (default=False) multiedges: if True multiple edges are allowed (default=False)

Overrides: graph.Graph.__init__

__getitem__(self, n)
(Indexing operator)

source code 

Return the neighbors of node n as a list.

This provides graph G the natural property that G[n] returns the neighbors of G.

Overrides: graph.Graph.__getitem__

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

source code 

Add a single edge to the graph.

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

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=XGraph()

then G.add_edge(1,2,"blue") will add the 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").

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.

G.add_edge(1,2,"green") will add both edges (1,2,X) and (2,1,X).

If self.selfloops=False, then calling add_edge(n1,n1,x) will have no effect on the Graph.

Objects associated to an edge can be retrieved using edges(), edge_iter(), or get_edge().

Overrides: graph.Graph.add_edge

add_edges_from(self, ebunch)

source code 

Add multiple edges to the graph.

ebunch: Container of edges. Each edge must be a 3-tuple (n1,n2,x) or a 2-tuple (n1,n2). See add_edge documentation.

The container must be iterable or an iterator. It is iterated over once.

Overrides: graph.Graph.add_edges_from

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

source code 

Return True if graph contains 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 or None, i.e. if called with an edge of the form e=(n1,n2), then return True if there exists ANY edge between n1 and n2 (equivalent to has_neighbor(n1,n2))

Overrides: graph.Graph.has_edge

has_neighbor(self, n1, n2)

source code 

Return True if node n1 has neighbor n2.

Note that this returns True if there exists ANY edge (n1,n2,x) for some x.

Overrides: graph.Graph.has_neighbor

neighbors_iter(self, n)

source code 

Return an iterator of nodes connected to node n.

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

Overrides: graph.Graph.neighbors_iter

neighbors(self, n)

source code 
Return a list of nodes connected to node n.
Overrides: graph.Graph.neighbors

get_edge(self, u, v)

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

edges_iter(self, nbunch=None)

source code 

Return iterator that iterates once over each edge adjacent to nodes in nbunch, or over all nodes in graph if nbunch=None.

If nbunch is None return all edges in the graph. The argument nbunch can be any single node, or any sequence or iterator of nodes. Nodes in nbunch that are not in the graph will be (quietly) ignored.

Overrides: graph.Graph.edges_iter

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)

source code 

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

Can be called either as

>>> G.delete_edge(n1,n2,x)
or
>>> G.delete_edge(e)

where e=(n1,n2,x).

The default edge data is x=None

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: graph.Graph.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). In the latter case all edges between n1 and n2 will be deleted. See delete_edge.

The container must be iterable or an iterator, and is iterated over once. Edges that are not in the graph are ignored.

Overrides: graph.Graph.delete_edges_from

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

source code 
This is the degree() method returned in iterator form. If with_labels=True, iterator yields 2-tuples of form (n,degree(n)) (like iteritems() on a dict.)
Overrides: graph.Graph.degree_iter

copy(self)

source code 

Return a (shallow) copy of the graph.

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

Overrides: graph.Graph.copy

to_directed(self)

source code 

Return a directed representation of the XGraph G.

A new XDigraph is returned with the same name, same nodes and with each edge (u,v,x) replaced by two directed edges (u,v,x) and (v,u,x).

Overrides: graph.Graph.to_directed

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: graph.Graph.subgraph

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