使用 pydot 查找开始、结束和循环

发布于 2024-10-31 14:33:40 字数 558 浏览 3 评论 0原文

有没有办法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揪着可爱 2024-11-07 14:33:40

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.

夜光 2024-11-07 14:33:40

好吧,您拥有整个图结构,通过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文