Source code for networkx.algorithms.components.attracting
"""吸引组件。"""
import networkx as nx
from networkx.utils.decorators import not_implemented_for
__all__ = [
"number_attracting_components",
"attracting_components",
"is_attracting_component",
]
[docs]
@not_implemented_for("undirected")
@nx._dispatchable
def attracting_components(G):
"""生成图 `G` 中的吸引组件。
有向图 `G` 中的吸引组件是一个强连通组件,具有以下性质:一旦随机游走者进入该组件,它将永远不会离开该组件。
吸引组件中的节点也可以被视为循环节点。如果随机游走者进入包含该节点的吸引器,那么该节点将被无限次访问。
要获取每个组件上的诱导子图,请使用:
``(G.subgraph(c).copy() for c in attracting_components(G))``
Parameters
----------
G : DiGraph, MultiDiGraph
要分析的图。
Returns
-------
attractors : 集合生成器
一个生成器,生成 `G` 的每个吸引组件的节点集合。
Raises
------
NetworkXNotImplemented
如果输入图是无向图。
See Also
--------
number_attracting_components
is_attracting_component
"""
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
for n in cG:
if cG.out_degree(n) == 0:
yield scc[n]
[docs]
@not_implemented_for("undirected")
@nx._dispatchable
def number_attracting_components(G):
"""返回图 `G` 中的吸引组件数量。
Parameters
----------
G : DiGraph, MultiDiGraph
要分析的图。
Returns
-------
n : int
G 中的吸引组件数量。
Raises
------
NetworkXNotImplemented
如果输入图是无向图。
See Also
--------
attracting_components
is_attracting_component
"""
return sum(1 for ac in attracting_components(G))
[docs]
@not_implemented_for("undirected")
@nx._dispatchable
def is_attracting_component(G):
"""返回 True 如果 `G` 由一个单一的吸引组件组成。
Parameters
----------
G : DiGraph, MultiDiGraph
要分析的图。
Returns
-------
attracting : bool
True 如果 `G` 有一个单一的吸引组件。否则,False。
Raises
------
NetworkXNotImplemented
如果输入图是无向的。
See Also
--------
attracting_components
number_attracting_components
"""
ac = list(attracting_components(G))
if len(ac) == 1:
return len(ac[0]) == len(G)
return False