Add a single edge to the graph. Can be called as G.add_edge(n1,n2,x) or as G.add_edge(e), where e=(n1,n2,x). 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=XGraph() then G.add_edge(1,2,"blue") will add the 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"). 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. G.add_edge(1,2,"green") will add both edges (1,2,X) and (2,1,X). If self.selfloops=False, then calling add_edge(n1,n1,x) will have no effect on the Graph. Objects associated to an edge can be retrieved using edges(), edge_iter(), or get_edge(). Definition at line 280 of file xbase.py. 00280 : """Add a single edge to the graph. Can be called as G.add_edge(n1,n2,x) or as G.add_edge(e), where e=(n1,n2,x). 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=XGraph() then G.add_edge(1,2,"blue") will add the 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"). 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. G.add_edge(1,2,"green") will add both edges (1,2,X) and (2,1,X). If self.selfloops=False, then calling add_edge(n1,n1,x) will have no effect on the Graph. Objects associated to an edge can be retrieved using edges(), edge_iter(), or get_edge(). """ if n2 is None: # add_edge was called as add_edge(e), with e=(n1,n2,x) 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.adj: self.adj[n1]={} if n2 not in self.adj: self.adj[n2]={} # self loop? quietly return if not allowed if not self.selfloops and n1==n2: return if self.multiedges: # add x to the end of the list of objects # that defines the edges between n1 and n2 self.adj[n1][n2]=self.adj[n1].get(n2,[])+ [x] if n1!=n2: self.adj[n2][n1]=self.adj[n2].get(n1,[])+ [x] else: # x is the new object assigned to single edge between n1 and n2 self.adj[n1][n2]=x if n1!=n2: self.adj[n2][n1]=x # a copy would be required to avoid # modifying both at the same time # when doing a delete_edge
