def edges_(): ''' edges(<Graph>, [<nbunch>]) returns Python list of edges incident to nodes in nbunch - If <nbunch> is not provided or None, return all edges in <Graph> ''' g = Graph([(1, 5), (5, 5)]) g.add_edge(2, 4) g.add_edge(1, 2) # 2 is connected to 4 and 1, so return those edges print(function.edges(g, 2)) # [(2, 4), (2, 1)] # With no <nbunch>, return all edges print(function.edges(g)) # [(1, 5), (1, 2), (5, 5), (2, 4)]
def add_edge(self, u, v=None): if v is None: (u,v)=u # no v given, assume u is an edge tuple if self.has_edge(u,v): return # no parallel edges allowed elif u in self and v in self: raise NetworkXError("adding edge %s-%s not allowed in tree"%(u,v)) elif u in self or v in self: Graph.add_edge(self,u,v) return elif len(self.adj)==0: # first leaf Graph.add_edge(self,u,v) return else: raise NetworkXError("adding edge %s-%s not allowed in tree"%(u,v))
def add_edge(self, u, v=None): if v is None: (u, v) = u # no v given, assume u is an edge tuple if self.has_edge(u, v): return # no parallel edges allowed elif u in self and v in self: raise NetworkXError("adding edge %s-%s not allowed in tree" % (u, v)) elif u in self or v in self: Graph.add_edge(self, u, v) return elif len(self.adj) == 0: # first leaf Graph.add_edge(self, u, v) return else: raise NetworkXError("adding edge %s-%s not allowed in tree" % (u, v))
def add_edge_(): ''' - <Graph>.edge[<u>][<v>] no longer exists, so use <Graph>.edges[<u>, <v>] - Unlike v1.11 which accepted a dict as a third argument, only additionals kwargs are accepted after <u> and <v> - The rules for arbitrary attributes are the same as v1.11 (see notes below) ''' g = Graph([(1, 5), (5, 5)]) g.edges[1, 5]['foo'] = 'bar' #g.add_edge(1, 5, {}) # TypeError # This is wrong. Don't do this! #g.add_edge(1, 5, attr_dict={}) #print(g.edges[1, 5]) # {'foo': 'bar', 'attr_dict': {}} # A duplciate edge cannot remove existing arbitrary edge attributes g.add_edge(1, 5) print(g.edges[1, 5]) # {'foo': 'bar'}
def add_edge(): ''' - Adding an edge with nonexistent nodes creates those nodes - Adding a duplicate edge can update the arbitrary attribute data for that edge (see below) - The rules for updating arbitrary attributes are the same as those for adding a duplicate node ''' g = Graph([(1, 5), (5, 5)]) g.edge[1][5]['foo'] = 'bar' # A duplciate edge cannot remove existing arbitrary edge attributes. # - Also, {} and attr_dict={} are equivalent #g.add_edge(1, 5, attr_dict={}) #g.add_edge(1, 5, {}) #print(g.edge[1][5]) # {'foo': 'bar'} # A duplicate edge can overwrite existing arbitrary edge attributes #g.add_edge(1, 5, attr_dict={'foo': 'fru'}) g.add_edge(1, 5, foo='fru') print(g.edge[1][5]) # {'foo': 'fru'}
def make_nonmultigraph(multigraph): """ Removes duplicate edges. Instead of having multiple edges going from the same source to the same target, this function adds one edge with a weight attribute, Parameters: multigraph: The multi-graph with multi-edges Return: G: A new graph which is equivalent to the multi-graph. """ G = Graph() for node in multigraph.nodes_iter(): G.add_node(node) for edge in multigraph.edges_iter(): for existing_edge in G.edges_iter(): if existing_edge[0] == edge[0] and existing_edge[1] == edge[1]: #If the edge is already in the existing edge list... G.edge[edge[0]][edge[1]]['weight'] += 1 # the existing edge's weight is incremented G.add_edge(edge[0], edge[1], weight=1) return G
def make_nonmultigraph(multigraph): """ Removes duplicate edges. Instead of having multiple edges going from the same source to the same target, this function adds one edge with a weight attribute, Parameters: multigraph: The multi-graph with multi-edges Return: G: A new graph which is equivalent to the multi-graph. """ G = Graph() for node in multigraph.nodes_iter(): G.add_node(node) for edge in multigraph.edges_iter(): for existing_edge in G.edges_iter(): if existing_edge[0] == edge[0] and existing_edge[1] == edge[ 1]: #If the edge is already in the existing edge list... G.edge[edge[0]][edge[1]][ 'weight'] += 1 # the existing edge's weight is incremented G.add_edge(edge[0], edge[1], weight=1) return G
def _check_cycle(mode_declarations): g = Graph().to_directed() for decl in mode_declarations: input_args = decl.input_arguments() output_args = decl.output_arguments() for o in output_args: g.add_node(o.name) for i in input_args: g.add_node(i.name) for o in output_args: g.add_edge(i.name, o.name) try: res = find_cycle(g) except NetworkXNoCycle: return raise CheckFailed('Cycle found: %s' % str(res))
def add_edge(self, u, v=None): if v is None: (u,v)=u # no v given, assume u is an edge tuple if self.has_edge(u,v): # no parallel edges return elif u in self and v in self: raise NetworkXError, "adding edge %s-%s not allowed in tree"%(u,v) elif u in self: Graph.add_edge(self,u,v) self.par[v]=u return elif v in self: Graph.add_edge(self,u,v) self.par[u]=v return elif len(self.adj)==0: # first leaf Graph.add_edge(self,u,v) self.par[v]=u # u is v's parent return else: raise NetworkXError, "adding edge %s-%s not allowed in tree"%(u,v)
def add_edge(self, u, v=None): if v is None: (u,v)=u # no v given, assume u is an edge tuple if self.has_edge(u,v): return # no parallel edges if u in self: # u is in forest if v in self: # v is in forest if self.comp[u]==self.comp[v]: # same tree? raise NetworkXError, \ "adding edge %s-%s not allowed in forest"%(u,v) else: # join two trees Graph.add_edge(self,u,v) ucomp=self.comp[u] # set component number of v tree to u tree for n in component.node_connected_component(self,v): self.comp[n]=ucomp else: # add u-v to tree in component with u Graph.add_edge(self,u,v) self.comp[v]=self.comp[u] else: # make new tree with only u-v Graph.add_edge(self,u,v) self.comp[u]=self.nc self.comp[v]=self.nc self.nc+=1
def add_edge(self, u, v=None): if v is None: (u, v) = u # no v given, assume u is an edge tuple if self.has_edge(u, v): return # no parallel edges if u in self: # u is in forest if v in self: # v is in forest if self.comp[u] == self.comp[v]: # same tree? raise NetworkXError, \ "adding edge %s-%s not allowed in forest"%(u,v) else: # join two trees Graph.add_edge(self, u, v) ucomp = self.comp[u] # set component number of v tree to u tree for n in component.node_connected_component(self, v): self.comp[n] = ucomp else: # add u-v to tree in component with u Graph.add_edge(self, u, v) self.comp[v] = self.comp[u] else: # make new tree with only u-v Graph.add_edge(self, u, v) self.comp[u] = self.nc self.comp[v] = self.nc self.nc += 1
def add_edge(self, u, v=None): if v is None: (u, v) = u # no v given, assume u is an edge tuple if self.has_edge(u, v): # no parallel edges return elif u in self and v in self: raise NetworkXError, "adding edge %s-%s not allowed in tree" % (u, v) elif u in self: Graph.add_edge(self, u, v) self.par[v] = u return elif v in self: Graph.add_edge(self, u, v) self.par[u] = v return elif len(self.adj) == 0: # first leaf Graph.add_edge(self, u, v) self.par[v] = u # u is v's parent return else: raise NetworkXError, "adding edge %s-%s not allowed in tree" % (u, v)