簡體   English   中英

如何使用networkx計算“附近”節點

[英]how to compute 'nearby' nodes with networkx

我在這里尋找的很可能是networkx一個內置函數,並且有一個數學名稱——如果是這樣,我想知道它是什么! 谷歌似乎很難找到。

給定圖G和一個起始節點i ,我想找到的所有節點的子圖“內P邊緣”從i -即,那些被連接到i由小於一路徑P的邊緣。

我對此的實施草案是:

import networkx as nx

N = 30
G = nx.Graph()

# populate the graph...
G.add_cycle(range(N))

# the starting node:
i = 15

# the 'distance' limit:
P = 4

neighborhood = [i]
new_neighbors = [i]
depth = 0

while depth < P:
    new_neighbors = list(set(sum([
        [k for k in G[j].keys() if k not in neighborhood]
    for j in new_neighbors], [])))

    neighborhood.extend(new_neighbors)

    depth += 1

Gneighbors = G.subgraph(neighborhood)

順便說一下,這段代碼有效,所以我不需要幫助來實現。 我只想知道這是否有名稱,以及它是否由networkx庫提供。

當您的代碼崩潰並且您想了解原因時,它非常有用 - 您可以僅在問題節點附近渲染圖形的“位置/區域”。

晚了兩年,但我一直在尋找同樣的東西,並找到了一個我認為可以得到你想要的子圖的內置ego_graphego_graph 函數簽名和文檔:

ego_graph(G, n, radius=1, center=True, undirected=False, distance=None)

返回以給定半徑內的節點 n 為中心的鄰居的誘導子圖。

使用single_source_shortest_pathsingle_source_shortest_path_lengthp的截止值

就像是:

nx.single_source_shortest_path_length(G ,source=i, cutoff=p)

暫無
暫無

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

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