Package networkx :: Module graph :: Class Graph
[hide private]
[frames] | no frames]

Class Graph

source code

object --+
         |
        Graph
Known Subclasses:
digraph.DiGraph, tree.Tree, tree.Forest, tree.RootedTree, xgraph.XGraph

Graph is a simple graph without any multiple (parallel) edges or self-loops. Attempting to add either will not change the graph and will not report an error.

Instance Methods [hide private]
 
__init__(self, data=None, name='')
Initialize Graph.
source code
 
__str__(self)
str(x)
source code
 
__iter__(self)
Return an iterator over the nodes in G.
source code
 
__contains__(self, n)
Return True if n is a node in graph.
source code
 
__len__(self)
Return the number of nodes in graph.
source code
 
__getitem__(self, n)
Return the neighbors of node n as a list.
source code
 
prepare_nbunch(self, nbunch=None)
Return a sequence (or iterator) of nodes contained in nbunch which are also in the graph.
source code
 
info(self, n=None)
Print short info for graph G or node n.
source code
 
add_node(self, n)
Add a single node n to the graph.
source code
 
add_nodes_from(self, nlist)
Add multiple nodes to the graph.
source code
 
delete_node(self, n)
Delete node n from graph.
source code
 
delete_nodes_from(self, nlist)
Remove nodes in nlist from graph.
source code
 
nodes_iter(self)
Return an iterator over the graph nodes.
source code
 
nodes(self)
Return a copy of the graph nodes in a list.
source code
 
number_of_nodes(self)
Return number of nodes.
source code
 
has_node(self, n)
Return True if graph has node n.
source code
 
order(self)
Return the order of a graph = number of nodes.
source code
 
add_edge(self, u, v=None)
Add a single edge (u,v) to the graph.
source code
 
add_edges_from(self, ebunch)
Add all the edges in ebunch to the graph.
source code
 
delete_edge(self, u, v=None)
Delete the single edge (u,v).
source code
 
delete_edges_from(self, ebunch)
Delete the edges in ebunch from the graph.
source code
 
has_edge(self, u, v=None)
Return True if graph contains the edge u-v, return False otherwise.
source code
 
has_neighbor(self, u, v)
Return True if node u has neighbor v.
source code
 
get_edge(self, u, v=None)
Return 1 if graph contains the edge u-v.
source code
 
neighbors_iter(self, n)
Return an iterator over all neighbors of node n.
source code
 
neighbors(self, n)
Return a list of nodes connected to node n.
source code
 
edges_iter(self, nbunch=None)
Return iterator that iterates once over each edge adjacent to nodes in nbunch, or over all edges in graph if no nodes are specified.
source code
 
edges(self, nbunch=None)
Return list of all edges that are adjacent to a node in nbunch, or a list of all edges in graph if no nodes are specified.
source code
 
edge_boundary(self, nbunch1, nbunch2=None)
Return list of edges (n1,n2) with n1 in nbunch1 and n2 in nbunch2.
source code
 
node_boundary(self, nbunch1, nbunch2=None)
Return list of all nodes on external boundary of nbunch1 that are in nbunch2.
source code
 
degree(self, nbunch=None, with_labels=False)
Return degree of single node or of nbunch of nodes.
source code
 
degree_iter(self, nbunch=None, with_labels=False)
Return iterator that return degree(n) or (n,degree(n)) for all n in nbunch.
source code
 
clear(self)
Remove name and delete all nodes and edges from graph.
source code
 
copy(self)
Return a (shallow) copy of the graph.
source code
 
to_undirected(self)
Return the undirected representation of the graph G.
source code
 
to_directed(self)
Return a directed representation of the graph G.
source code
 
subgraph(self, nbunch, inplace=False, create_using=None)
Return the subgraph induced on nodes in nbunch.
source code
 
add_path(self, nlist)
Add the path through the nodes in nlist to graph
source code
 
add_cycle(self, nlist)
Add the cycle of nodes in nlist to graph
source code
 
is_directed(self)
Return True if graph is directed.
source code
 
size(self)
Return the size of a graph = number of edges.
source code
 
number_of_edges(self, u=None, v=None)
Return the number of edges between nodes u and v.
source code

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='')
(Constructor)

source code 

Initialize Graph.

>>> G=Graph(name="empty")

creates empty graph G with G.name="empty"

Overrides: object.__init__

__str__(self)
(Informal representation operator)

source code 
str(x)
Overrides: object.__str__
(inherited documentation)

__iter__(self)

source code 

Return an iterator over the nodes in G.

This is the iterator for the underlying adjacency dict. (Allows the expression 'for n in G')

__contains__(self, n)
(In operator)

source code 

Return True if n is a node in graph.

Allows the expression 'n in G'.

Testing whether an unhashable object, such as a list, is in the dict datastructure (self.adj) will raise a TypeError. Rather than propagate this to the calling method, just return False.

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

prepare_nbunch(self, nbunch=None)

source code 

Return a sequence (or iterator) of nodes contained in nbunch which are also in the graph.

The input nbunch can be a single node, a sequence or iterator of nodes or None (omitted). If None, all nodes in the graph are returned.

Note: This routine exhausts any iterator nbunch.

Note: To test whether nbunch is a single node, one can use "if nbunch in self:", even after processing with this routine.

Note: This routine returns an empty list if nbunch is not either a node, sequence, iterator, or None. You can catch this exception if you want to change this behavior.

add_node(self, n)

source code 

Add a single node n to the graph.

The node n can be any hashable object except None.

A hashable object is one that can be used as a key in a Python dictionary. This includes strings, numbers, tuples of strings and numbers, etc. On many platforms this also includes mutables such as Graphs e.g., though one should be careful the hash doesn't change on mutables.

Example:

>>> from networkx import *
>>> G=Graph()
>>> K3=complete_graph(3)
>>> G.add_node(1)
>>> G.add_node('Hello')
>>> G.add_node(K3)
>>> G.number_of_nodes()
3

add_nodes_from(self, nlist)

source code 

Add multiple nodes to the graph.

nlist: A container of nodes that will be iterated through once (thus it should be an iterator or be iterable). Each element of the container should be a valid node type: any hashable type except None. See add_node for details.

Examples:

>>> from networkx import *
>>> G=Graph()
>>> K3=complete_graph(3)
>>> G.add_nodes_from('Hello')
>>> G.add_nodes_from(K3)
>>> sorted(G.nodes())
[0, 1, 2, 'H', 'e', 'l', 'o']

delete_node(self, n)

source code 
Delete node n from graph. Attempting to delete a non-existent node will raise an exception.

delete_nodes_from(self, nlist)

source code 

Remove nodes in nlist from graph.

nlist: an iterable or iterator containing valid node names.

Attempting to delete a non-existent node will raise an exception. This could mean some nodes got deleted and other valid nodes did not.

has_node(self, n)

source code 

Return True if graph has node n.

(duplicates self.__contains__) "n in G" is a more readable version of "G.has_node(n)"?

add_edge(self, u, v=None)

source code 

Add a single edge (u,v) to the graph.

>> G.add_edge(u,v) and >>> G.add_edge( (u,v) ) are equivalent forms of adding a single edge between nodes u and v. The nodes u and v will be automatically added if not already in the graph. They must be a hashable (except None) Python object.

The following examples all add the edge (1,2) to graph G.

>>> G=Graph()
>>> G.add_edge( 1, 2 )          # explicit two node form
>>> G.add_edge( (1,2) )         # single edge as tuple of two nodes
>>> G.add_edges_from( [(1,2)] ) # add edges from iterable container

add_edges_from(self, ebunch)

source code 

Add all the edges in ebunch to the graph.

ebunch: Container of 2-tuples (u,v). The container must be iterable or an iterator. It is iterated over once. Adding the same edge twice has no effect and does not raise an exception.

delete_edge(self, u, v=None)

source code 

Delete the single edge (u,v).

Can be used in two basic forms: >>> G.delete_edge(u,v) and >> G.delete_edge( (u,v) ) are equivalent ways of deleting a single edge between nodes u and v.

Return without complaining if the nodes or the edge do not exist.

delete_edges_from(self, ebunch)

source code 

Delete the edges in ebunch from the graph.

ebunch: an iterator or iterable of 2-tuples (u,v).

Edges that are not in the graph are ignored.

has_neighbor(self, u, v)

source code 

Return True if node u has neighbor v.

This is equivalent to has_edge(u,v).

get_edge(self, u, v=None)

source code 
Return 1 if graph contains the edge u-v. Raise an exception otherwise.

edges_iter(self, nbunch=None)

source code 

Return iterator that iterates once over each edge adjacent to nodes in nbunch, or over all edges in graph if no nodes are specified.

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.

edges(self, nbunch=None)

source code 

Return list of all edges that are adjacent to a node in nbunch, or a list of all edges in graph if no nodes are specified.

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.

For digraphs, edges=out_edges

edge_boundary(self, nbunch1, nbunch2=None)

source code 

Return list of edges (n1,n2) with n1 in nbunch1 and n2 in nbunch2. If nbunch2 is omitted or nbunch2=None, then nbunch2 is all nodes not in nbunch1.

Nodes in nbunch1 and nbunch2 that are not in the graph are ignored.

nbunch1 and nbunch2 are usually meant to be disjoint, but in the interest of speed and generality, that is not required here.

This routine is faster if nbunch1 is smaller than nbunch2.

node_boundary(self, nbunch1, nbunch2=None)

source code 

Return list of all nodes on external boundary of nbunch1 that are in nbunch2. If nbunch2 is omitted or nbunch2=None, then nbunch2 is all nodes not in nbunch1.

Note that by definition the node_boundary is external to nbunch1.

Nodes in nbunch1 and nbunch2 that are not in the graph are ignored.

nbunch1 and nbunch2 are usually meant to be disjoint, but in the interest of speed and generality, that is not required here.

This routine is faster if nbunch1 is smaller than nbunch2.

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

source code 

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.

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.

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

source code 

Return iterator that return degree(n) or (n,degree(n)) for all n in nbunch. If nbunch is ommitted, then iterate over all nodes.

Can be called in three ways: G.degree_iter(n): return iterator the degree of node n G.degree_iter(nbunch): return a list of values, one for each n in nbunch (nbunch is any iterable container of nodes.) G.degree_iter(): same as nbunch = all nodes in graph.

If with_labels==True, iterator will return an (n,degree(n)) tuple of node and degree.

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

copy(self)

source code 

Return a (shallow) copy of the graph.

Identical to dict.copy() of adjacency dict adj, with name copied as well.

to_undirected(self)

source code 

Return the undirected representation of the graph G.

This graph is undirected, so merely return a copy.

to_directed(self)

source code 

Return a directed representation of the graph G.

A new digraph is returned with the same name, same nodes and with each edge u-v represented by two directed edges u->v and v->u.

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

number_of_edges(self, u=None, v=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)