本文整理汇总了Python中networkx.DiGraph.successors_iter方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.successors_iter方法的具体用法?Python DiGraph.successors_iter怎么用?Python DiGraph.successors_iter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.DiGraph
的用法示例。
在下文中一共展示了DiGraph.successors_iter方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: contract_chains
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import successors_iter [as 别名]
def contract_chains(g:nx.DiGraph, blockname='block'):
'''Contract chains with indegree=outdegree=1:
i.e. turns > - - - <
into [>---<]
The label of the chain is the label of its first element.
g.node[n][blockname] will hold list of dictionaries.
'''
from itertools import chain
starts = set(chain((n for n in g if g.in_degree(n) != 1),
chain.from_iterable(g.successors_iter(n) for n in g
if g.out_degree(n) > 1)))
blocks = nx.DiGraph()
for label in starts:
n = label
block = []
while True:
block.append(g.node[n])
if g.out_degree(n) != 1:
break
next_n = next(g.successors_iter(n))
if g.in_degree(next_n) != 1:
break
n = next_n
if label not in blocks:
blocks.add_node(label)
blocks.add_edges_from((label, suc) for suc in g.successors_iter(n))
blocks.node[label][blockname] = block
return blocks