簡體   English   中英

用於 python 的 igraph package 是否包含計算圖距離矩陣的方法?

[英]Does the igraph package for python contain a method to compute the distance matrix of a graph?

igraph package 的 R 版本包含distances function ,根據文檔,它返回傳遞給它的圖形的距離矩陣。

但是,我在 package 的 python 版本中找不到這個 function 或類似的。 我能找到的唯一提供的 function 是Point.distance ,在我對文檔的理解中,它只計算二維平面中兩個點的距離,而不是網絡圖。

在這個 package 的 python 版本中是否有一個 function 返回圖形的距離矩陣?

本質上,我正在尋找的是以下內容:

# create the graph
graph = Graph(n=5, edges=[(0,1), (1,4), (2,3), (2,4), (2,5) , (3,5), (4,0)], directed=False)

# pseudo code to calculate distance
dist_matrix = distances(graph)

任何幫助深表感謝。

其中一種方法是使用g.get_all_shortest_paths(m)

import igraph as ig
g = ig.Graph(n=5, edges=[(0,1), (1,4), (2,3), (2,4), (2,5) , (3,5), (4,0)], directed=False)
>>> [[len(n) for n in g.get_all_shortest_paths(m)] for m in g.vs]
[[1, 2, 3, 4, 2, 4], [2, 1, 3, 4, 2, 4], [3, 3, 1, 2, 2, 2], [4, 4, 2, 1, 3, 2], [2, 2, 2, 3, 1, 3], [4, 4, 2, 2, 3, 1]]

另一種選擇更容易和更快:

>>> g.shortest_paths()
[[0, 1, 2, 3, 1, 3], [1, 0, 2, 3, 1, 3], [2, 2, 0, 1, 1, 1], [3, 3, 1, 0, 2, 1], [1, 1, 1, 2, 0, 2], [3, 3, 1, 1, 2, 0]]

暫無
暫無

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

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