Home | Trees | Indices | Help |
---|
|
Graph
A simple graph that has no self-loops or multiple (parallel) edges.
An empty graph is created with
>>> G=Graph()
DiGraph
A directed graph that has no self-loops or multiple (parallel) edges. Subclass of Graph.
An empty digraph is created with
>>> G=DiGraph()
XGraph
A graph that has (optional) self-loops or multiple (parallel) edges and arbitrary data on the edges. Subclass of Graph.
An empty graph is created with
>>> G=XGraph()
XDiGraph
A directed graph that has (optional) self-loops or multiple (parallel) edges and arbitrary data on the edges.
A simple digraph that has no self-loops or multiple (parallel) edges. Subclass of DiGraph which is a subclass of Graph.
An empty digraph is created with
>>> G=DiGraph()
The XGraph and XDiGraph classes extend the Graph and DiGraph classes by allowing (optional) self loops, multiedges and by decorating each edge with an object x.
Each XDiGraph or XGraph edge is a 3-tuple e=(n1,n2,x), representing an edge between nodes n1 and n2 that is decorated with the object x. Here n1 and n2 are (hashable) node objects and x is a (not necessarily hashable) edge object. If multiedges are allowed, G.get_edge(n1,n2) returns a list of edge objects.
Whether an XGraph or XDiGraph allow self-loops or multiple edges is determined initially via parameters selfloops=True/False and multiedges=True/False. For example, the example empty XGraph created above is equivalent to
>>> G=XGraph(selfloops=False, multiedges=False)
Similar defaults hold for XDiGraph. The command
>>> G=XDiGraph(multiedges=True)
creates an empty digraph G that does not allow selfloops but does allow for multiple (parallel) edges. Methods exist for allowing or disallowing each feature after instatiation as well.
Note that if G is an XGraph then G.add_edge(n1,n2) will add the edge (n1,n2,None), and G.delete_edge(n1,n2) will attempt to delete the edge (n1,n2,None). In the case of multiple edges between nodes n1 and n2, one can use G.delete_multiedge(n1,n2) to delete all edges between n1 and n2.
The following shorthand is used throughout NetworkX documentation and code: (we use mathematical notation n,v,w,... to indicate a node, v=vertex=node).
Each class provides basic graph methods.
- G.add_node(n), G.add_nodes_from(nlist)
- G.delete_node(n), G.delete_nodes_from(nlist)
- 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)
- G.has_node(n)
- n in G (equivalent to G.has_node(n))
- for n in G: (iterate through the nodes of G)
- G.nodes()
- G.nodes_iter()
- G.has_edge(n1,n2), G.has_neighbor(n1,n2), G.get_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.order()
- G.number_of_edges(), G.size()
- G.edge_boundary(nbunch1), G.node_boundary(nbunch1)
- G.degree(n), G.degree(nbunch)
- G.degree_iter(n), G.degree_iter(nbunch)
- G.is_directed()
- G.info() # print variaous info about a graph
- G.prepare_nbunch(nbunch) # return list of nodes in G and nbunch
- G.subgraph(nbunch)
- G.subgraph(nbunch,create_using=H)
- G.copy()
- G.to_undirected()
- G.to_directed()
The graph classes implement 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 None (the Python None type) for Graph and DiGraph or user specified (default is None) for XGraph and XDiGraph. The dictionary of dictionary structure allows fast addition, deletion and lookup of nodes and neighbors in large graphs.
XGraph and Graph differ in the way edge data is handled. XGraph edges are 3-tuples (n1,n2,x) and Graph edges are 2-tuples (n1,n2). XGraph inherits from the Graph class, and XDiGraph from the DiGraph class.
Graph and XGraph are similar in the following ways:
Edgeless graphs are the same in XGraph and Graph. For an edgeless graph, represented by G (member of the Graph class) and XG (member of XGraph class), there is no difference between the datastructures G.adj and XG.adj, other than possibly in the ordering of the keys in the adj dict.
Basic graph construction code for G=Graph() will also work for G=XGraph(). In the Graph class, the simplest graph construction consists of a graph creation command G=Graph() followed by a list of graph construction commands, consisting of successive calls to the methods:
G.add_node, G.add_nodes_from, G.add_edge, G.add_edges, G.add_path, G.add_cycle G.delete_node, G.delete_nodes_from, G.delete_edge, G.delete_edges_from
with all edges specified as 2-tuples,
If one replaces the graph creation command with G=XGraph(), and then apply the identical list of construction commands, the resulting XGraph object will be a simple graph G with identical datastructure G.adj. This property ensures reuse of code developed for graph generation in the Graph class.
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0beta1 on Sun Aug 17 12:04:44 2008 | http://epydoc.sourceforge.net |