有向树(igraph)中从一个节点到另一个节点的所有可能路径
我使用 python 绑定 到 igraph 来表示有向树。我想找到从该图中的一个节点到另一个节点的所有可能路径。不幸的是,我在 igraph 中找不到执行此任务的现成函数?
编辑
对无限数量路径的关注
我正在谈论的图实际上是具有单根的有向无环图(DAG)。它代表事件的单向级联,在级联的各个级别上,这些事件可以拆分或连接在一起。正如我所说,这是一个单向图。还假设该图不包含任何循环。由于这两个原因,无限的路径列表是不可能的。
我想做什么?
我的目标是找到从图的顶部(根)到给定节点的所有可能路径。
I use python binding to igraph to represent a directed tree. I would like to find all possible paths from one node in that graph to another one. Unfortunately, I couldn't find a ready to use function in igraph that performs this task?
EDIT
The concerns on infinite number of paths
the graph I'm talking about is actually a directed acyclic graph (DAG) with a single root. It represents a unidirectional cascade of events that, on various levels of the cascade, can either split or join together. As I said, this is a unidirectional graph. It is also provided that the graph does not contain any cycles. Due to these two reasons, infinite list of paths, is impossible.
What am I trying to do?
My goal is to find all the possible paths that lead from the top of the graph (the root) to the given node.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在有向无环图 (DAG) 中查找一个节点与另一个节点之间的所有路径。
树始终是 DAG,但 DAG 并不总是树。不同之处在于,树的分支不允许连接,只能分开,而 DAG 的分支可以流在一起,只要不引入循环。
您的解决方案可以在 “Python 模式 - 实现”中找到
find_all_paths()
图表。” 这需要进行一些调整才能与 igraph 一起使用。我没有安装 igraph,但使用模拟,这似乎有效:从文档中不清楚 adjlist 是顶点索引列表的列表还是顶点对象本身列表的列表。我假设列表包含索引以简化 adjlist 的使用。结果,返回的路径是按照顶点索引表示的。如果您需要这些,则必须将它们映射回顶点对象,或者调整代码以附加顶点而不是其索引。
You are looking for all paths between one node and another in a directed acyclic graph (DAG).
A tree is always a DAG, but a DAG isn't always a tree. The difference is that a tree's branches are not allowed to join, only divide, while a DAG's branches can flow together, so long as no cycles are introduced.
Your solution can be found as
find_all_paths()
in "Python Patterns - Implementing Graphs." This requires a little adaptation to use with igraph. I don't have igraph installed, but using mocks, this seems to work:It was unclear from the documentation whether the adjlist is a list of lists of vertex indices or a list of list of vertex objects themselves. I assumed that the lists contained indices to simplify using the adjlist. As a result, the returned paths are in terms of vertex indices. You will have to map these back to vertex objects if you need those instead, or adapt the code to append the vertex rather than its index.