Package networkx :: Module operators
[hide private]
[frames] | no frames]

Module operators

source code

Operations on graphs; including union, complement, subgraph.


Date: $Date: 2007-07-18 15:23:23 -0600 (Wed, 18 Jul 2007) $

Author: Aric Hagberg (hagberg@lanl.gov) Pieter Swart (swart@lanl.gov) Dan Schult(dschult@colgate.edu)

Functions [hide private]
 
subgraph(G, nbunch, inplace=False, create_using=None)
Return the subgraph induced on nodes in nbunch.
source code
 
union(G, H, create_using=None, rename=False, name=None)
Return the union of graphs G and H.
source code
 
disjoint_union(G, H)
Return the disjoint union of graphs G and H, forcing distinct integer node labels.
source code
 
cartesian_product(G, H)
Return the Cartesian product of G and H.
source code
 
compose(G, H, create_using=None, name=None)
Return a new graph of G composed with H.
source code
 
complement(G, create_using=None, name=None)
Return graph complement of G.
source code
 
create_empty_copy(G, with_nodes=True)
Return a copy of the graph G with all of the edges removed.
source code
 
convert_to_undirected(G)
Return a new undirected representation of the graph G.
source code
 
convert_to_directed(G)
Return a new directed representation of the graph G.
source code
 
relabel_nodes(G, mapping)
Return a copy of G with node labels transformed by mapping.
source code
 
relabel_nodes_with_function(G, func)
Deprecated: call relabel_nodes(G,func).
source code
 
convert_node_labels_to_integers(G, first_label=0, ordering='default', discard_old_labels=True)
Return a copy of G, with n node labels replaced with integers, starting at first_label.
source code
 
_test_suite() source code
Variables [hide private]
  __credits__ = ''
  __revision__ = '$Revision: 1024 $'
Function Details [hide private]

subgraph(G, nbunch, inplace=False, create_using=None)

source code 

Return the subgraph induced on nodes in nbunch.

nbunch: can be a singleton node, a string (which is treated as a singleton 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 the induced subgraph in the original graph by deleting nodes not in nbunch. This overrides create_using. Warning: this can 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 is a call to a graph object, e.g. create_using=DiGraph(). See documentation for empty_graph.

Implemented for Graph, DiGraph, XGraph, XDiGraph

Note: subgraph(G) calls G.subgraph()

union(G, H, create_using=None, rename=False, name=None)

source code 

Return the union of graphs G and H.

Graphs G and H must be disjoint, otherwise an exception is raised.

Node names of G and H can be changed be specifying the tuple rename=('G-','H-') (for example). Node u in G is then renamed "G-u" and v in H is renamed "H-v".

To force a disjoint union with node relabeling, use disjoint_union(G,H) or convert_node_labels_to integers().

Optional create_using=R returns graph R filled in with the union of G and H. Otherwise a new graph is created, of the same class as G. It is recommended that G and H be either both directed or both undirected.

A new name can be specified in the form X=graph_union(G,H,name="new_name")

Implemented for Graph, DiGraph, XGraph, XDiGraph.

disjoint_union(G, H)

source code 

Return the disjoint union of graphs G and H, forcing distinct integer node labels.

A new graph is created, of the same class as G. It is recommended that G and H be either both directed or both undirected.

Implemented for Graph, DiGraph, XGraph, XDiGraph.

cartesian_product(G, H)

source code 

Return the Cartesian product of G and H.

Tested only on Graph class.

compose(G, H, create_using=None, name=None)

source code 

Return a new graph of G composed with H.

The node sets of G and H need not be disjoint.

A new graph is returned, of the same class as G. It is recommended that G and H be either both directed or both undirected.

Optional create_using=R returns graph R filled in with the compose(G,H). Otherwise a new graph is created, of the same class as G. It is recommended that G and H be either both directed or both undirected.

Implemented for Graph, DiGraph, XGraph, XDiGraph

complement(G, create_using=None, name=None)

source code 

Return graph complement of G.

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

Implemented for Graph, DiGraph, XGraph, XDiGraph. Note that complement() is not well-defined for XGraph and XDiGraph objects that allow multiple edges or self-loops.

convert_to_undirected(G)

source code 

Return a new undirected representation of the graph G.

Works for Graph, DiGraph, XGraph, XDiGraph.

Note: convert_to_undirected(G)=G.to_undirected()

convert_to_directed(G)

source code 

Return a new directed representation of the graph G.

Works for Graph, DiGraph, XGraph, XDiGraph.

Note: convert_to_directed(G)=G.to_directed()

relabel_nodes(G, mapping)

source code 

Return a copy of G with node labels transformed by mapping.

mapping is either
  • a dictionary with the old labels as keys and new labels as values
  • a function transforming an old label with a new label

In either case, the new labels must be hashable Python objects.

mapping as dictionary:

>>> G=path_graph(3)  # nodes 0-1-2
>>> mapping={0:'a',1:'b',2:'c'}
>>> H=relabel_nodes(G,mapping)
>>> print H.nodes()
['a', 'c', 'b']
>>> G=path_graph(26) # nodes 0..25
>>> mapping=dict(zip(G.nodes(),"abcdefghijklmnopqrstuvwxyz"))
>>> H=relabel_nodes(G,mapping) # nodes a..z
>>> mapping=dict(zip(G.nodes(),xrange(1,27)))
>>> G1=relabel_nodes(G,mapping) # nodes 1..26

mapping as function

>>> G=path_graph(3)
>>> def mapping(x):
...    return x**2
>>> H=relabel_nodes(G,mapping)
>>> print H.nodes()
[0, 1, 4]

Also see convert_node_labels_to_integers.

convert_node_labels_to_integers(G, first_label=0, ordering='default', discard_old_labels=True)

source code 

Return a copy of G, with n node labels replaced with integers, starting at first_label.

first_label: (optional, default=0)

An integer specifying the offset in numbering nodes. The n new integer labels are numbered first_label, ..., n+first_label.

ordering: (optional, default="default")

A string specifying how new node labels are ordered. Possible values are:

"default" : inherit node ordering from G.nodes() "sorted" : inherit node ordering from sorted(G.nodes()) "increasing degree" : nodes are sorted by increasing degree "decreasing degree" : nodes are sorted by decreasing degree
discard_old_labels
if True (default) discard old labels if False, create a dict self.node_labels that maps new labels to old labels

Works for Graph, DiGraph, XGraph, XDiGraph