2011-04-08 65 views
2

有沒有辦法在pydot中實現這一點?尋找開始,結束並與pydot循環

採取下面的例子:

[輸出點文件]

strict graph g{ 
"A" -> "B"; 
"B" -> "C"; 
"C" -> "D"; 
"D" -> "E"; 
} 

[Python的]

print(num.start) 
>>> A 
print(num.steps) 
>>> ["a,b","b,c","c,d","d,e"] 
print(num.end) 
>>> E 

或與下列情況下:

[輸出點文件]

strict graph g{ 
"A" -> "B"; 
"B" -> "C"; 
"C" -> "A"; 
} 

[python]的

if num["A"] == num.loop: 
print("[%s] loop detected")%(num["A"]) 

回答

1

Pydot可以寫點文件,但它不適用於分析圖形。

您需要改爲NetworkX。它可以read and write dot files,find circles,找到可達節點並進行拓撲排序。

查看維基百科圖表的術語和NetworkX可以完成剩下的工作。

+0

爲了將來的參考,我有一個使用networkx在點圖中標識週期的python腳本示例:http://blog.jasonantman.com/2012/03/python-script-to-find-dependency-cycles -in-graphviz-dot-files /感謝Jochen爲我提供鏈接以提供腳本。 – 2012-03-29 03:06:11

0

好你的整個圖形結構,通過graph.get_edge_list()可以實現標準的深度優先搜索來查找節點之間的最短路徑。查找循環與standard graph algorithms同樣完成。有關如何在兩個節點之間執行最短路徑的源代碼,請參見Graph implementations in Python上的這篇文章。

如果你正在尋找pydot庫,請爲你做這件事,你可能會運氣不好。