Home | Trees | Indices | Help |
---|
|
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:
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
Inherited from Inherited from |
|
|||
Inherited from |
|
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)
|
Return the neighbors of node n as a list. This provides graph G the natural property that G[n] returns the neighbors of G.
|
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().
|
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.
|
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))
|
Return True if node n1 has neighbor n2. Note that this returns True if there exists ANY edge (n1,n2,x) for some x.
|
Return an iterator of nodes connected to node n. Returns the same data as edges(n) but in a different format.
|
|
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.
|
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.
|
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 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)
|
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.
|
|
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.
|
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).
|
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. |
Henceforth allow addition of multiedges (more than one edge between two nodes). Warning: This causes all edge data to be converted to lists. |
|
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.
|
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)
|
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0beta1 on Sun Aug 17 12:04:48 2008 | http://epydoc.sourceforge.net |