Package networkx :: Module xbase :: Class XGraph
[frames | no frames]

Type XGraph

object --+    
         |    
     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 (hashable) objects (nodes) 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="No Name",selfloops=False,multiedges=False)
>>> G=XGraph(name="empty",multiedges=True)

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

See also the XDiGraph class below.


Method Summary
  __init__(self, **kwds)
Initialize XGraph.
  add_cycle(self, nlist)
Add the cycle of nodes in nlist to graph
  add_edge(self, n1, n2, x)
Add a single edge to the graph.
  add_edges_from(self, ebunch)
Add multiple edges to the graph.
  add_path(self, nlist)
Add the path through the nodes in nlist to graph
  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 graph.
  degree(self, nbunch, with_labels)
Return degree of single node or of nbunch of nodes.
  degree_iter(self, nbunch, with_labels)
This is the degree() method returned in interator form.
  delete_edge(self, n1, n2, x)
Delete the edge (n1,n2,x) from the graph.
  delete_edges_from(self, ebunch)
Delete edges in ebunch from the graph.
  delete_multiedges(self)
Remove multiedges retaining the data from the first edge
  delete_node(self, n)
Delete node n from graph.
  delete_nodes_from(self, nbunch)
Remove nodes in nbunch from graph.
  delete_selfloops(self)
Remove self-loops from the graph (edges from a node to itself).
  edges(self, nbunch, with_labels)
Return a list of all edges that originate at a node in nbunch, or a list of all edges if nbunch=None.
  edges_iter(self, nbunch, with_labels)
Return iterator that iterates once over each edge adjacent to nodes in nbunch, or over all nodes in graph if nbunch=None.
  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 graph contains edge (n1,n2,x).
  has_neighbor(self, n1, n2)
Return True if node n1 has neighbor n2.
  neighbors(self, n, with_labels)
Return a list of 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_edges(self)
Return number of edges
  number_of_selfloops(self)
Return number of self-loops in graph.
  selfloop_edges(self)
Return all edges that are self-loops.
  size(self)
Return the size of a graph = number of edges.
  subgraph(self, nbunch, inplace, create_using)
Return the subgraph induced on nodes in nbunch.
  to_directed(self)
Return a directed representation of the XGraph G.
Inherited from Graph: __contains__, __getitem__, __iter__, __len__, __str__, add_node, add_nodes_from, clear, edge_boundary, has_node, is_directed, node_boundary, nodes, nodes_iter, number_of_nodes, order, print_dna, to_undirected
Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__

Method Details

__init__(self, **kwds)
(Constructor)

Initialize XGraph.

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

Overrides:
networkx.base.Graph.__init__

add_cycle(self, nlist)

Add the cycle of nodes in nlist to graph

Overrides:
networkx.base.Graph.add_cycle

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

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 (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=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:
networkx.base.Graph.add_edge

add_edges_from(self, ebunch)

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

add_path(self, nlist)

Add the path through the nodes in nlist to graph

Overrides:
networkx.base.Graph.add_path

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

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

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

The degree of a node is the number of edges attached to that node.

Can be called in three ways:
  • G.degree(n): return the degree of node n

  • G.degree(nbunch): return a list of values,

    one for each n in nbunch (nbunch is any iterable container of nodes.)

  • G.degree(): same as nbunch = all nodes in graph.

    Always return a list.

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

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

Overrides:
networkx.base.Graph.degree

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

This is the degree() method returned in interator form. If with_labels=True, iterator yields 2-tuples of form (n,degree(n)) (like iteritems() on a dict.)

Overrides:
networkx.base.Graph.degree_iter

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

Delete the 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.Graph.delete_edge

delete_edges_from(self, ebunch)

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

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.Graph.delete_edges_from

delete_multiedges(self)

Remove multiedges retaining the data from the first edge

delete_node(self, n)

Delete node n from graph.

Attempting to delete a non-existent node will raise an exception.

Overrides:
networkx.base.Graph.delete_node

delete_nodes_from(self, nbunch)

Remove nodes in nbunch from graph.

nbunch: an iterable or iterator containing valid(hashable) node names.

Attempting to delete a non-existent node will raise an exception. This could result in a partial deletion of those nodes both in nbunch and in the graph.

Overrides:
networkx.base.Graph.delete_nodes_from

delete_selfloops(self)

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

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

Return a list of all edges that originate at a node in nbunch, or a list of all edges if nbunch=None.

See add_node for definition of nbunch.

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

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

Overrides:
networkx.base.Graph.edges

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

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

See add_node for definition of nbunch.

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

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

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

has_neighbor(self, 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.

Overrides:
networkx.base.Graph.has_neighbor

neighbors(self, n, with_labels=False)

Return a list of nodes connected to node n.

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

Overrides:
networkx.base.Graph.neighbors

neighbors_iter(self, n, with_labels=False)

Return an iterator for neighbors of n.

Overrides:
networkx.base.Graph.neighbors_iter

nodes_with_selfloops(self)

Return list of all nodes having self-loops.

number_of_edges(self)

Return number of edges

Overrides:
networkx.base.Graph.number_of_edges

number_of_selfloops(self)

Return number of self-loops in graph.

selfloop_edges(self)

Return all edges that are self-loops.

size(self)

Return the size of a graph = number of edges.

Overrides:
networkx.base.Graph.size

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

to_directed(self)

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

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