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