Logo Search packages:      
Sourcecode: python-networkx version File versions  Download package

def networkx::base::Graph::add_edge (   self,
  u,
  v = None 
) [inherited]

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

Reimplemented in networkx::base::DiGraph, networkx::tree::Tree, networkx::tree::RootedTree, networkx::tree::DirectedTree, and networkx::tree::Forest.

Definition at line 557 of file base.py.

00557                                  :  
        """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

        """
        if v is None:
            (u,v)=u  # no v given, assume u is an edge tuple
        # add nodes            
        if u not in self.adj:
            self.adj[u]={}
        if v not in self.adj:
            self.adj[v]={}
        # don't create self loops, fail silently, nodes are still added
        if u==v: 
            return  
        self.adj[u][v]=None
        self.adj[v][u]=None


Generated by  Doxygen 1.6.0   Back to index