簡體   English   中英

如何僅從特定節點的鄰接矩陣創建網絡圖?

[英]How do I create network graph from an adjacency matrix for specific nodes only?

我有一個鄰接矩陣 5000X5000,我想創建一個網絡圖。 要求是用戶將輸入節點,輸出將是該特定輸入節點的圖形(1 度和 2 度)。

我已經嘗試過使用 Gephi,但由於鄰接矩陣很大,我無法專注於每個節點。 所以我想我是否可以為特定節點創建一個圖(因為我只對每個節點的 1 度和 2 度連接感興趣,而不是超出這個范圍)

Gephi 是基於 UI 的,所以我沒有代碼。

輸入將是 node_id,輸出將是對應於該 node_id 的圖(1 度和 2 度連接)

這是使用networkx的實現:

import networkx as nx
import numpy as np

# make dummy adjacency matrix
a = np.random.rand(100,100)
a = np.tril(a)
a = a>0.95

# make graph from adjaceny matrix
G = nx.from_numpy_matrix(a)


def neigh(G, node, depth):
    """ given starting node, recursively find neighbours
        until desired depth is reached
    """

    node_list = []
    if depth==0:
        node_list.append(node)
    else:
        for neighbor in G.neighbors(node):
            node_list.append(node)
            node_list += neigh(G, neighbor, depth-1)
    return list(set(node_list)) # intermediate conversion to set to lose duplicates. 

# a bit more compressed:
def neigh_short(G, node, depth):
    """ given starting node, recursively find neighbours
        until desired depth is reached
    """

    node_list = [node]
    if depth>0:
        for neighbor in G.neighbors(node)
            node_list += neigh_short(G, neighbor, depth-1)
    return list(set(node_list)) # intermediate conversion to set to lose duplicates. 

# example:
# find all neighbours with distance 2 from node 5:
n = neigh(G, node=5, depth=2)

# extract the respective subgraph from G and store in H
H = G.subgraph(n)

暫無
暫無

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

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