Home | Trees | Index | Help |
|
---|
Package NX :: Module base |
|
Base classes for graphs and digraphs.
Unless otherwise specified, by graph we mean a simple graph that has no self-loops or multiple (parallel) edges. (See the module xbase.py for graph classes XGraph and XDiGraph that allow for self-loops, mutiple edges and arbitrary objects associated with edges.)
The following classes are provided:
This module implements graphs using data structures based on an adjacency list implemented as a node-centric dictionary of dictionaries. The dictionary contains keys corresponding to the nodes and the values are dictionaries of neighboring node keys with the value 1. This allows fast addition, deletion and lookup of nodes and neighbors in large graphs. The underlying datastructure should only be visible in this module. In all other modules, instances of graph-like objects are manipulated solely via the methods defined here and not by acting directly on the datastructure.
The following notation is used throughout NetworkX documentation and code: (we use mathematical notation n,v,w,... to indicate a node, v=vertex=node).
The Graph class provides rudimentary graph operations:
- G.add_node(n), G.add_nodes_from(nbunch)
- G.delete_node(n), G.delete_nodes_from(nbunch)
- G.add_edge(n1,n2), G.add_edge(e), where e=(u,v)
- G.add_edges_from(ebunch)
- G.delete_edge(n1,n2), G.delete_edge(e), where e=(u,v)
- G.delete_edges_from(ebunch)
- G.add_path(nlist)
- G.add_cycle(nlist)
- G.clear()
- G.subgraph(nbunch,inplace=True)
- len(G)
- n in G (equivalent to G.has_node(n))
- G.has_node(n)
- G.nodes()
- G.nodes_iter()
- G.has_edge(n1,n2)
- G.edges(), G.edges(n), G.edges(nbunch)
- G.edges_iter(), G.edges_iter(n), G.edges_iter(nbunch)
- G.neighbors(n)
- G[n] (equivalent to G.neighbors(n))
- G.neighbors_iter(n) # iterator over neighbors
- G.number_of_nodes()
- G.number_of_edges()
- G.degree(n), G.degree(nbunch)
- G.degree_iter(n), G.degree_iter(nbunch)
- G.is_directed()
- G.subgraph(nbunch)
- G.subgraph(nbunch,create_using=H)
- G.copy()
- G.to_undirected()
- G.to_directed()
Create an empty graph structure (a "null graph") with zero nodes and zero edges.
>>> from NX import * >>> G=Graph()
G can be grown in several ways. By adding one node at a time:
>>> G.add_node(1)
by adding a list of nodes:
>>> G.add_nodes_from([2,3])
by using an iterator:
>>> G.add_nodes_from(xrange(100,110))
or by adding any nbunch of nodes (see above definition of an nbunch):
>>> H=path_graph(10) >>> G.add_nodes_from( H )
(H can be another graph, or dict, or set, or even a file.)
>>> G.add_node( H )
(Any hashable object can represent a node, e.g. a Graph, a customized node object, etc.)
G can also be grown by adding one edge at a time:
>>> G.add_edge( (1,2) )
by adding a list of edges:
>>> G.add_edges_from([(1,2),(1,3)])
or by adding any ebunch of edges (see above definition of an ebunch):
>>> G.add_edges_from(H.edges())
There are no complaints when adding existing nodes or edges:
>>> G=Graph() >>> G.add_edge([(1,2),(1,3)])
will add new nodes as required.
Classes | |
---|---|
DiGraph |
A graph with directed edges. |
Graph |
Graph is a simple graph without any multiple (parallel) edges or self-loops. |
Exceptions | |
---|---|
NetworkXError |
Exception for a serious error in NetworkX |
NetworkXException |
Base class for exceptions in NetworkX. |
Function Summary | |
---|---|
Return degree of single node or of nbunch of nodes. | |
Return a list of the frequency of each degree value. | |
Return the density of a graph. | |
Return list of edges adjacent to nodes in nbunch. | |
Return iterator over edges adjacent to nodes in nbunch. | |
Return a list of nodes connected to node n. | |
Return a copy of the graph nodes in a list. | |
Return an iterator over the graph nodes. | |
Return the size of a graph = number of edges. | |
Return the order of a graph = number of nodes. |
Variable Summary | |
---|---|
str |
__author__ = 'Aric Hagberg (hagberg@lanl.gov)\nPieter Sw...
|
str |
__credits__ = ''
|
str |
__date__ = '$Date: 2005/04/01 19:35:22 $'
|
str |
__revision__ = '$Revision: 1.104 $'
|
Function Details |
---|
degree(G, nbunch=None, with_labels=False)Return degree of single node or of nbunch of nodes. If nbunch is ommitted, then return degrees of all nodes. |
degree_histogram(G)Return a list of the frequency of each degree value. The degree values are the index in the list. Note: the bins are width one, hence len(list) can be large (Order(number_of_edges)) |
density(G)Return the density of a graph. density = size/(order*(order-1)/2) density()=0.0 for an edge-less graph and 1.0 for a complete graph. |
edges(G, nbunch=None, with_labels=False)Return list of edges adjacent to nodes in nbunch. Return all edges if nbunch is unspecified or nbunch=None. |
edges_iter(G, nbunch=None, with_labels=False)Return iterator over edges adjacent to nodes in nbunch. Return all edges if nbunch is unspecified or nbunch=None. |
neighbors(G, n, with_labels=False)Return a list of nodes connected to node n. |
nodes(G)Return a copy of the graph nodes in a list. |
nodes_iter(G)Return an iterator over the graph nodes. |
number_of_edges(G)Return the size of a graph = number of edges. |
number_of_nodes(G)Return the order of a graph = number of nodes. |
Variable Details |
---|
__author__
|
__credits__
|
__date__
|
__revision__
|
Home | Trees | Index | Help |
|
---|
Generated by Epydoc 2.1 on Mon Apr 11 10:59:22 2005 | http://epydoc.sf.net |