凝聚#

condensation(G, scc=None)[source]#

返回 G 的凝聚图。

G 的凝聚图是将每个强连通分量收缩为一个节点后形成的图。

参数:
GNetworkX DiGraph

一个有向图。

scc: list 或 generator (可选,默认为 None)

强连通分量。如果提供,scc 中的元素必须对 G 的节点进行划分。如果未提供,则会计算为 scc=nx.strongly_connected_components(G)。

返回值:
CNetworkX DiGraph

G 的凝聚图 C。节点标签是整数,对应于 G 的强连通分量列表中的分量索引。C 有一个名为 'mapping' 的图属性,其中包含一个字典,将原始节点映射到它们在 C 中所属的节点。C 中的每个节点也有一个名为 'members' 的节点属性,其中包含构成 C 中该节点所代表的 SCC 的 G 中的原始节点集合。

抛出异常:
NetworkXNotImplemented

如果 G 是无向图。

注意

将所有强连通分量收缩为单个节点后,结果图是一个有向无环图。

示例

使用杠铃图将两组强连通节点收缩为两个不同的 SCC。

>>> G = nx.barbell_graph(4, 0)
>>> G.remove_edge(3, 4)
>>> G = nx.DiGraph(G)
>>> H = nx.condensation(G)
>>> H.nodes.data()
NodeDataView({0: {'members': {0, 1, 2, 3}}, 1: {'members': {4, 5, 6, 7}}})
>>> H.graph["mapping"]
{0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1}

将一个完全图收缩为一个单独的 SCC。

>>> G = nx.complete_graph(7, create_using=nx.DiGraph)
>>> H = nx.condensation(G)
>>> H.nodes
NodeView((0,))
>>> H.nodes.data()
NodeDataView({0: {'members': {0, 1, 2, 3, 4, 5, 6}}})