簡體   English   中英

計算NetworkX圖的斷開連接的組件數

[英]Calculating the number of disconnected components of a NetworkX graph

從隨機生成的樹開始,我想考慮樹的每個節點,並可能以p概率將其刪除。 由於樹沒有周期,並且任何一對節點之間都有唯一的路徑,因此刪除一個節點應該使d斷開連接的樹處於喚醒狀態,其中d是該節點的度數。

我的問題是,一旦為整個圖表完成此操作,如何檢查這些未連接的段中有多少?

import networkx as nx
import random as rand

n = 20
p = 0.1

G = nx.random_tree(n)
for i in range(0, n):
    if rand.random() < p:
        G.remove_node(i)

x = G.count_disconnected_components() # is there anything that accomplishes this?

例如,對於此圖, G.count_disconnected_components()應該返回3。 具有三個未連接組件的圖形

在我看來,您實際上實際上是想計算連接零件的數量。 試試number_connected_components

print(list(nx.connected_components(G)))
print(nx.number_connected_components(G))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM