簡體   English   中英

Networkx:獲取節點之間的距離

[英]Networkx: Get the distance between nodes

我是使用 NetworkX 的初學者,我正在嘗試找到一種方法來檢測哪些節點彼此之間的距離為 x。 我已經開始使用這個算法來獲取所有對

path=nx.all_pairs_dijkstra_path(G)

NetworkX具有自動計算加權和未加權圖形的最短路徑(或僅路徑長度)的方法。 確保為您的用例使用正確的方法。

networkx.all_pairs_shortest_path - 計算未加權圖中所有節點之間的最短路徑

networkx.all_pairs_shortest_path_length - 計算未加權圖中所有節點之間最短路徑的長度

networkx.all_pairs_dijkstra_path - 計算加權圖中所有節點之間的最短路徑

networkx.all_pairs_dijkstra_path_length - 計算加權圖中所有節點之間最短路徑的長度

當在圖上執行時,這些方法中的每一個將計算具有相應的最短路徑或最短路徑的長度作為值的節點的字典矩陣(“詞典詞典”)。 我將通過一個例子證明這一點:

>>> import networkx as nx
>>> G = nx.Graph()
>>> G.add_nodes_from(["A", "B", "C", "D", "E"])
>>> G.add_edges_from([("A", "B"), ("B", "C"), ("C", "D"), ("D", "E")])
>>> sp = nx.all_pairs_shortest_path(G)
>>> sp["A"]["E"]
['A', 'B', 'C', 'D', 'E']
>>> spl = nx.all_pairs_shortest_path_length(G)
>>> spl["A"]["E"]
4

如您所見,我生成了一個包含五個節點的圖形,並將每個節點鏈接到具有邊緣的下一個節點。 我在sp存儲了最短路徑的矩陣,在spl存儲了最短路徑長度的矩陣。 當我需要知道兩個節點之間的最短路徑時,例如節點"A"和節點"E" ,我只是像一個矩陣或字典字典那樣訪問spsp["A"]["E"] 然后它將返回兩個節點之間的整個最短路徑。 用於最短路徑長度的方法以類似的方式工作,但是它將僅返回任何兩個給定節點之間的邊數。

下一個代碼片段可能會讓我更清楚我對字典矩陣的意思。 如果我在sp請求節點"A"所有條目,它將返回另一個字典,其中包含每個其他節點的條目:

>>> sp["A"]
{'B': ['A', 'B'], 'A': ['A'], 'E': ['A', 'B', 'C', 'D', 'E'], 'C': ['A', 'B', 'C
'], 'D': ['A', 'B', 'C', 'D']}

如果你想通過使用for循環檢查所有節點之間的距離,你可以迭代矩陣的第一個字典的鍵,然后遍歷該字典內的字典。 它聽起來比聽起來容易:

>>> for node1 in spl:
...   for node2 in spl[node1]:
...     print("Length between", node1, "and", node2, "is", spl[node1][node2])
...
Length between B and B is 0
Length between B and A is 1
Length between B and E is 3
Length between B and C is 1
Length between B and D is 2
Length between A and B is 1
... (and so on!)

請讓我知道,如果你有任何問題。

這是查找節點之間距離的一種解決方案:

G = nx.Graph()
G.add_edges_from([(1,2), (1,3), (2,3), (2,4), (3,4)])
path = nx.all_pairs_shortest_path_length(G) # This is a generator
dpath = {x[0]:x[1] for x in path}           # Create a dictionary from it 
# To find e.g. the distance between node 1 and node 4: 
print(dpath[1][4]) # = 2

我一直在使用它來計算最長路徑 - 眾所周知,有向圖很難,但對於樹狀圖來說很容易。 雖然您不是特別要求這樣做,但它可能會幫助其他人。 僅供參考,使用 all_pairs_shortest_path_length 對於大型網絡來說非常昂貴。

        # Find the longest path for a tree
        # Choose a random start. (or first).
        rnd_node = choice(nodes)
        # Find distances from random node by calculating all distances
        r_dists = dict(nx.single_source_shortest_path_length(g, rnd_node))
        begins = [node for node, dist in r_dists.items() if dist == max(r_dists.values())]
        # being the longest from a random start, makes it suitable as a start to the longest path.
        begin = choice(begins)
        # Find distances from begin by calculating all distances.
        # If we want to know the actual journey, use 'single_source_shortest_path()' instead.
        s_dists = dict(nx.single_source_shortest_path_length(g, begin))
        stops = [node for node, dist in s_dists.items() if dist == max(s_dists.values())]
        dist, stop = choice(stops)

根據您可以使用的圖表的大小

distances = nx.floyd_warshall_numpy(graph)
nodes = np.where(distances==4)

得到距離矩陣。 從中您可以過濾您的距離。 行和列是list(g)中的節點

暫無
暫無

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

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