有向无环图中从源到汇的所有路径的列表
Possible Duplicate:
[python]: path between two nodes
Can anyone point me to some resources on how to do this? I'm using networkx
as my python library.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是基于 Alex Martelli 的答案,但它应该有效。它取决于表达式
source_node.children
生成一个迭代器,该迭代器将迭代source_node
的所有子节点。它还依赖于==
运算符有一种工作方式来比较两个节点以查看它们是否相同。使用is
可能是更好的选择。显然,在您使用的库中,获取所有子级的可迭代的语法是 graph[source_node] ,因此您需要相应地调整代码。我主要担心的是,这会进行深度优先搜索,当从源到一个节点有多个路径时,该节点是所有源的孙子、曾孙等,但不一定是接收器的父节点,这会浪费精力。如果它记住给定源节点和接收器节点的答案,则可以避免额外的工作。
这是一个如何工作的示例:
这还允许您在调用之间保存记忆字典,因此如果您需要计算多个源节点和接收器节点的答案,您可以避免很多额外的工作。
This is based on Alex Martelli's answer, but it should work. It depends on the expression
source_node.children
yielding an iterable that will iterate over all the children ofsource_node
. It also relies on there being a working way for the==
operator to compare two nodes to see if they are the same. Usingis
may be a better choice. Apparently, in the library you're using, the syntax for getting an iterable over all the children isgraph[source_node]
, so you will need to adjust the code accordingly.My main concern is that this does a depth first search, it will waste effort when there are several paths from the source to a node that's a grandchild, great grandchild, etc all of source, but not necessarily a parent of sink. If it memoized the answer for a given source and sink node it would be possible to avoid the extra effort.
Here is an example of how that would work:
This also allows you to save the memoization dictionary between invocations so if you need to compute the answer for multiple source and sink nodes you can avoid a lot of extra effort.
这个实际上可以与 networkx 一起使用,并且它是非递归的,这对于大型图来说可能很好。
This one actually works with networkx, and it's non-recursive, which may be nice for large graphs.
我不确定是否有特殊的优化可用——在寻找其中任何一个之前,我会做一个简单的递归解决方案,类似于(仅使用networkx通过节点索引图的功能给出可迭代的结果)邻居节点[[一个字典,在networkx的情况下,但我没有特别使用它]])...:
这应该被证明是正确的(但我不会做证明,因为它已经很晚了并且我很累,头脑模糊;-),可用于验证任何进一步的优化;-)。
我尝试的第一个优化是某种简单的记忆:如果我已经计算了从某个节点 N 到任何目标节点的路径集(无论我进行计算时通向 N 的前缀是什么),我可以隐藏将其保存在键 N 下的字典中,并且当我通过不同的路线再次到达 N 时避免进一步的重新计算;-)。
I'm not sure if there are special optimizations available -- before looking for any of them, I'd do a simple recursive solution, something like (using of networkx only the feature that indexing a graph by a node gives an iterable yielding its neighbor nodes [[a dict, in networkx's case, but I'm not making use of that in particular]])...:
This should be provably correct (but I'm not going to do the proof because it's very late and I'm tired and fuzzy-headed;-) and usable to verify any further optimizations;-).
First optimization I'd try would be some kind of simple memoizing: if I've already computed the set of paths from some node N to any goal node (whatever the prefix leading to N was when I did that computation), I can stash that away in a dict under key N and avoid further recomputations if and when I get to N again by a different route;-).