使用 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"])
Is there a way to achieve this in pydot?
Take the following example:
[Outputted Dot File]
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
or with the following case:
[Outputted Dot File]
strict graph g{
"A" -> "B";
"B" -> "C";
"C" -> "A";
}
[Python]
if num["A"] == num.loop:
print("[%s] loop detected")%(num["A"])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Pydot可以编写点文件,但不能用于分析图形。
您需要 NetworkX。它可以读取和写入点文件,查找圆圈,查找可到达的节点并执行拓扑排序。
在维基百科上查找图表术语,NetworkX 可以完成剩下的工作。
Pydot can write dot files, but it is not for analyzing graphs.
You want NetworkX instead. It can read and write dot files, find circles, find reachable nodes and do topological sort.
Look up the terminology of graphs on wikipedia and NetworkX can do the rest.
好吧,您拥有整个图结构,通过 graph.get_edge_list() ,您可以实现标准深度优先搜索来查找节点之间的最短路径。查找循环同样是使用标准图形算法完成的。请参阅这篇关于Python 中的图形实现的文章,了解如何在两个对象之间执行最短路径的源代码节点。
如果您正在寻找 pydot 库,请为您执行此操作,您可能会不走运。
Well you have the whole graph structure, via
graph.get_edge_list()
you can implement standard depth first search to find shortest path between nodes. Finding loops is likewise done with standard graph algorithms. See this article on Graph implementations in Python for sourcecode on how to do shortest path between two nodes.If you're looking for the pydot library do do this for you, you might be out of luck.