Package networkx :: Module base :: Class Graph

[frames | no frames]

## Type Graph

    object --+
             |
            Graph

Known Subclasses:
  `DiGraph`, `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.

Method Summary
| ``__init__`(self, **kwds)` Initialize Graph.
| ``__contains__`(self, n)` Return True if n is a node in graph.
| ``__getitem__`(self, n)` Return the neighbors of node n as a list.
| ``__iter__`(self)` Return an iterator over the nodes in G.
| ``__len__`(self)` Return the number of nodes in graph.
| `__str__(self)`
| ``add_cycle`(self, nlist)` Add the cycle of nodes in nlist to graph
| ``add_edge`(self, u, v)` Add a single edge (u,v) to the graph.
| ``add_edges_from`(self, ebunch)` Add all the edges in ebunch to the graph.
| ``add_node`(self, n)` Add a single node n to the graph.
| ``add_nodes_from`(self, nbunch)` Add multiple nodes to the graph.
| ``add_path`(self, nlist)` Add the path through the nodes in nlist to graph
| ``clear`(self)` Remove name and delete all nodes and edges from graph.
| ``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)` Return iterator that return degree(n) or (n,degree(n)) for all n in nbunch.
| ``delete_edge`(self, u, v)` Delete the single edge (u,v).
| ``delete_edges_from`(self, ebunch)` Delete the edges in ebunch from the graph.
| ``delete_node`(self, n)` Delete node n from graph.
| ``delete_nodes_from`(self, nbunch)` Remove nodes in nbunch from graph.
| ``edge_boundary`(self, nbunch1, nbunch2)` Return list of edges (n1,n2) with n1 in nbunch1 and n2 in nbunch2.
| ``edges`(self, nbunch, with_labels)` 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.
| ``edges_iter`(self, nbunch, with_labels)` Return iterator that iterates once over each edge adjacent to nodes in nbunch, or over all edges in graph if no nodes are specified.
| ``has_edge`(self, u, v)` Return True if graph contains edge u-v.
| ``has_neighbor`(self, u, v)` Return True if node u has neighbor v.
| ``has_node`(self, n)` Return True if graph has node n.
| ``is_directed`(self)` Return True if graph is directed.
| ``neighbors`(self, n, with_labels)` Return a list of nodes connected to node n.
| ``neighbors_iter`(self, n, with_labels)` Return an iterator over all neighbors of node n.
| ``node_boundary`(self, nbunch1, nbunch2)` Return list of all nodes on external boundary of nbunch1 that are in nbunch2.
| ``nodes`(self)` Return a copy of the graph nodes in a list.
| ``nodes_iter`(self)` Return an iterator over the graph nodes.
| ``number_of_edges`(self)` Return the size of a graph = number of edges.
| ``number_of_nodes`(self)` Return number of nodes.
| ``order`(self)` Return the order of a graph = number of nodes.
| ``print_dna`(self)` Print graph "DNA": a dictionary of graph names and properties.
| ``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 graph G.
| ``to_undirected`(self)` Return the undirected representation of the graph G.
Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`

Method Details

### __init__(self, **kwds)
(Constructor)

Initialize Graph.

    >>> G=Graph(name="empty") creates empty graph G with G.name="empty"

  Overrides:
    `__builtin__.object.__init__`

### __contains__(self, n)
(In operator)

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)

Return the neighbors of node n as a list.

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

### __iter__(self)

Return an iterator over the nodes in G.

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

### __len__(self)
(Length operator)

Return the number of nodes in graph.

### add_cycle(self, nlist)

Add the cycle of nodes in nlist to graph

### add_edge(self, u, v=None)

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

Can be used in two basic forms: G.add_edge(u,v) or G.add_edge( (u,v) ) are equivalent forms of adding a single edge between nodes u and v. Nodes are nor required to exist before adding an edge; they will be added in silence.

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)

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.

### add_node(self, n)

Add a single node n to the graph.

The node n can be any hashable object (it is used as a key in a dictionary). On many platforms this includes mutables such as Graphs e.g., though one should be careful the hash doesn't change on mutables.

Examples:

    >>> G=Graph()
    >>> K3=complete_graph(3)
    >>> G.add_node(1)
    >>> G.add_node('Hello')
    >>> G.add_node(K3)
    >>> G.nodes()
    [1, 'Hello', <networkx.base.Graph object at 0x5f7430>]

### add_nodes_from(self, nbunch)

Add multiple nodes to the graph.

nbunch: A container of nodes that will be iterated through once (thus it can be an iterator or an iterable) Each element of the container should be hashable.

Examples:

    >>> G=Graph()
    >>> K3=complete_graph(3)
    >>> G.add_nodes_from(1)
    NetworkXError, Container 1 is not an iterator or iterable. Use add_node?
    >>> G.add_nodes_from('Hello')
    >>> G.add_nodes_from(K3)
    >>> G.nodes()
    ['H', 'e', 'l', 'l', 'o', 1, 2, 3]

### add_path(self, nlist)

Add the path through the nodes in nlist to graph

### clear(self)

Remove name and delete all nodes and edges from graph.

### copy(self)

Return a (shallow) copy of the graph.

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

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

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)

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.

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

### delete_edge(self, u, v=None)

Delete the single edge (u,v).

Can be used in two basic forms: Both G.delete_edge(u,v) and G.delete_edge( (u,v) ) are equivalent forms of deleting a single edge between nodes u and v. Return quietly witoutt complaining if the nodes or the edge do not exist.

### delete_edges_from(self, ebunch)

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.

### delete_node(self, n)

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

### 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 mean some nodes got deleted and other valid nodes did not.

### edge_boundary(self, nbunch1, nbunch2=None)

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 must be disjoint, else raise an exception.

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

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.

See add_node for definition of nbunch.

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

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

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

See add_node for definition of nbunch.

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

### has_edge(self, u, v=None)

Return True if graph contains edge u-v.

### has_neighbor(self, u, v=None)

Return True if node u has neighbor v.

### has_node(self, n)

Return True if graph has node n.

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

### is_directed(self)

Return True if graph is directed.

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

### neighbors_iter(self, n, with_labels=False)

Return an iterator over all neighbors of node n.

If with_labels=True, return an iterator of (neighbor, 1) tuples.

### node_boundary(self, nbunch1, nbunch2=None)

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 must be disjoint (when restricted to the graph), else a NetworkXError is raised.

### nodes(self)

Return a copy of the graph nodes in a list.

### nodes_iter(self)

Return an iterator over the graph nodes.

### number_of_edges(self)

Return the size of a graph = number of edges.

### number_of_nodes(self)

Return number of nodes.

### order(self)

Return the order of a graph = number of nodes.

### print_dna(self)

Print graph "DNA": a dictionary of graph names and properties.

In this version the dna is provided as a user-defined variable and should not be relied on.

### size(self)

Return the size of a graph = number of edges.

### 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 iterable (non-string) container of nodes for which len(nbunch) is defined. For example, a list, dict, set, Graph, numeric array, or user-defined iterable object.

Setting inplace=True will return the induced subgraph in 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.

### to_directed(self)

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.

### to_undirected(self)

Return the undirected representation of the graph G.

This graph is undirected, so merely return a copy.

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