本文整理汇总了Python中networkx.classes.graph.Graph.add_edge方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.add_edge方法的具体用法?Python Graph.add_edge怎么用?Python Graph.add_edge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.classes.graph.Graph
的用法示例。
在下文中一共展示了Graph.add_edge方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_edge
# 需要导入模块: from networkx.classes.graph import Graph [as 别名]
# 或者: from networkx.classes.graph.Graph import add_edge [as 别名]
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))
示例2: make_nonmultigraph
# 需要导入模块: from networkx.classes.graph import Graph [as 别名]
# 或者: from networkx.classes.graph.Graph import add_edge [as 别名]
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