![](/img/trans.png)
[英]How can I find the number of unique undirected edges of a graph using networkx?
[英]How can I find all possibly paths in undirected graph using python?
讓我們舉一個例子,我們有這樣的numpy矩陣:
[[ 0 3 5 0]
[ 3 0 2 9]
[ 5 2 0 7]
[ 0 9 7 0]]
我想找到從0到3的所有可能路徑。我希望解決方案是中立的。 我將始終從0轉到矩陣的最新索引。 在這種情況下3.也許已經有一個現成的算法,或者有人可以展示自己的解決方案? 提前致謝。
您可以使用networkx
。 它是python圖形理論的軟件包。
import networkx as nx
import numpy as np
A=np.matrix([[ 0, 3, 5, 0],
[ 3, 0, 2, 9],
[ 5, 2, 0, 7],
[ 0, 9, 7, 0]])
G=nx.from_numpy_matrix(A)
print(nx.shortest_path(G,source=0,target=3)) # shortest path
# Result
# [0, 1, 3]
print(list(nx.all_simple_paths(G,source=0,target=3))) # all simple paths
# Result
# [[0, 1, 2, 3], [0, 1, 3], [0, 2, 1, 3], [0, 2, 3]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.