Add a single directed edge to the digraph. Can be called as G.add_edge(n1,n2,x) or as G.add_edge(e), where e=(n1,n2,x). If called as G.add_edge(n1,n2) or G.add_edge(e), with e=(n1,n2), then this is interpreted as adding the edge (n1,n2,None) to be compatible with the Graph and DiGraph classes. n1,n2 are node objects, and are added to the Graph if not already present. Nodes must be hashable Python objects (except None). x is an arbitrary (not necessarily hashable) object associated with this edge. It can be used to associate one or more, labels, data records, weights or any arbirary objects to edges. The default is the Python None. For example, if the graph G was created with >>> G=XDiGraph() then G.add_edge(1,2,"blue") will add the directed edge (1,2,"blue"). If G.multiedges=False, then a subsequent G.add_edge(1,2,"red") will change the above edge (1,2,"blue") into the edge (1,2,"red"). On the other hand, if G.multiedges=True, then two successive calls to G.add_edge(1,2,"red") will result in 2 edges of the form (1,2,"red") that can not be distinguished from one another. If self.selfloops=False, then any attempt to create a selfloop with add_edge(n1,n1,x) will have no effect on the digraph and will not elicit a warning. Objects imbedded in the edges from n1 to n2 (if any), can be retrieved using get_edge(n1,n2), or calling edges(n1) or edge_iter(n1) to return all edges attached to n1. Definition at line 955 of file xbase.py. 00955 : """Add a single directed edge to the digraph. Can be called as G.add_edge(n1,n2,x) or as G.add_edge(e), where e=(n1,n2,x). If called as G.add_edge(n1,n2) or G.add_edge(e), with e=(n1,n2), then this is interpreted as adding the edge (n1,n2,None) to be compatible with the Graph and DiGraph classes. n1,n2 are node objects, and are added to the Graph if not already present. Nodes must be hashable Python objects (except None). x is an arbitrary (not necessarily hashable) object associated with this edge. It can be used to associate one or more, labels, data records, weights or any arbirary objects to edges. The default is the Python None. For example, if the graph G was created with >>> G=XDiGraph() then G.add_edge(1,2,"blue") will add the directed edge (1,2,"blue"). If G.multiedges=False, then a subsequent G.add_edge(1,2,"red") will change the above edge (1,2,"blue") into the edge (1,2,"red"). On the other hand, if G.multiedges=True, then two successive calls to G.add_edge(1,2,"red") will result in 2 edges of the form (1,2,"red") that can not be distinguished from one another. If self.selfloops=False, then any attempt to create a selfloop with add_edge(n1,n1,x) will have no effect on the digraph and will not elicit a warning. Objects imbedded in the edges from n1 to n2 (if any), can be retrieved using get_edge(n1,n2), or calling edges(n1) or edge_iter(n1) to return all edges attached to n1. """ if n2 is None: # add_edge was called as add_edge(e), with e a tuple if len(n1)==3: #case e=(n1,n2,x) n1,n2,x=n1 else: # assume e=(n1,n2) n1,n2=n1 # x=None # if edge exists, quietly return if multiple edges are not allowed if not self.multiedges and self.has_edge(n1,n2,x): return # add nodes if n1 not in self.succ: self.succ[n1]={} if n1 not in self.pred: self.pred[n1]={} if n2 not in self.succ: self.succ[n2]={} if n2 not in self.pred: self.pred[n2]={} # self loop? quietly return if not allowed if not self.selfloops and n1==n2: return if self.multiedges: # append x to the end of the list of objects # that defines the edges between n1 and n2 self.succ[n1][n2]=self.succ[n1].get(n2,[])+ [x] self.pred[n2][n1]=self.pred[n2].get(n1,[])+ [x] else: # x is the new object assigned to single edge between n1 and n2 self.succ[n1][n2]=x self.pred[n2][n1]=x # note that the same object is referred to # from both succ and pred
