本文整理汇总了Python中networkx.classes.digraph.DiGraph.add_nodes_from方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.add_nodes_from方法的具体用法?Python DiGraph.add_nodes_from怎么用?Python DiGraph.add_nodes_from使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.classes.digraph.DiGraph
的用法示例。
在下文中一共展示了DiGraph.add_nodes_from方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_example_3
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import add_nodes_from [as 别名]
def get_example_3():
"""get a binarized example, whose original graph is
more complicated than the above example
"""
g = DiGraph()
g.add_nodes_from(range(1, 10))
g.add_edges_from([(1, 2), (1, 3), (1, 7),
(2, 4), (2, 5), (2, 6),
(2, 7), (3, 8), (3, 9)])
rewards = range(1, 10)
for r, n in zip(rewards, g.nodes()):
g.node[n]['r'] = r
# all edges have cost 2 except 1 -> 2 and 1 -> 3(cost 1)
for s, t in g.edges():
g[s][t]['c'] = 2
g[1][2]['c'] = 1
g[1][3]['c'] = 1
g = binarize_dag(g,
vertex_weight_key='r',
edge_weight_key='c',
dummy_node_name_prefix='d_')
# parameters and expected output
U = [0, 2, 3, 4, 100]
expected_edges_set = [
[],
[(1, 7)],
[(1, 'd_1'), ('d_1', 3), (3, 9)],
[(1, 'd_1'), ('d_1', 3), (3, 9), ('d_1', 2)],
# (1, 7) removed to make it a tree
list(set(g.edges()) - set([(1, 7)]))
]
return (g, U, expected_edges_set)