簡體   English   中英

在網絡圖中獲取斷開的節點對?

[英]Get disconnected pairs of nodes in the network graph?

這是我的數據集:

4095    546
3213    2059 
4897    2661 
...
3586    2583
3437    3317
3364    1216

每條線是一對節點,它們之間有一條邊。 整個數據集構建一個圖形。 但是我想獲得許多彼此斷開的節點對。 如何從數據集中獲得1000個(或更多)這樣的節點對? 如:

2761    2788
4777    3365
3631    3553
...
3717    4074
3013    2225

每條線都是一對沒有邊的節點。

只需執行BFS或DFS即可在O(|E|)時間內獲得每個連接組件的大小。 然后,一旦有了組件的大小,就可以輕松獲得斷開連接的節點數:這是每對大小的乘積之和。

例如。 如果您的圖具有3個大小為50、20、100的已連接組件。那么,斷開連接的節點對的數量為: 50*20 + 50*100 + 20*100 = 8000

如果要實際輸出斷開連接的對而不是僅對它們進行計數,則可能應該使用並發查找,然后遍歷所有對節點,如果它們不在同一組件中,則將其輸出。

請參見編輯部分!

我認為其他選項比較籠統,從編程角度來看可能更好。 我只是有個簡單的主意,您如何使用numpy以一種非常簡單的方式獲取列表。

首先創建鄰接矩陣,您的節點列表是一個數組:

    import numpy as np
    node_list= np.random.randint(10 , size=(10, 2))
    A = np.zeros((np.max(node_list) + 1, np.max(node_list) + 1)) # + 1 to account for zero indexing
    A[node_list[:, 0],  node_list[:, 1]] = 1 # set connected nodes to 1
    x, y = np.where(A == 0) # Find disconnected nodes
    disconnected_list = np.vstack([x, y]).T # The final list of disconnected nodes

不過,我不知道如何在大型網絡中使用。

編輯:上面的解決方案是我想得太快了。 到目前為止,以上解決方案提供了節點之間的缺失邊緣,而不是未連接的節點(在有向圖的情況下)。 此外,disconnected_list包括每個節點兩次。 這是解決方案的第二個想法:

    import numpy as np
    node_list= np.random.randint(10 , size=(10, 2))
    A = np.zeros((np.max(node_list) + 1, np.max(node_list) + 1)) # + 1 to account for zero indexing
    A[node_list[:, 0], node_list[:, 1]] = 1 # set connected nodes to 1 
    A[node_list[:, 1], node_list[:, 0]] = 1 # Make the graph symmetric
    A = A + np.triu(np.ones(A.shape)) # Add ones to the upper triangular
    # matrix, so they are not considered in np.where (set k if you want to consider the diagonal)
    x, y = np.where(A == 0) # Find disconnected nodes
    disconnected_list = np.vstack([x, y]).T # The final list of disconnected nodes

暫無
暫無

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

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