[英]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
庫提供。
當您的代碼崩潰並且您想了解原因時,它非常有用 - 您可以僅在問題節點附近渲染圖形的“位置/區域”。
使用single_source_shortest_path
或single_source_shortest_path_length
與p
的截止值
就像是:
nx.single_source_shortest_path_length(G ,source=i, cutoff=p)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.