有向树(igraph)中从一个节点到另一个节点的所有可能路径

发布于 2024-09-28 01:18:00 字数 479 浏览 3 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(1

揽清风入怀 2024-10-05 01:18:00

您正在有向无环图 (DAG) 中查找一个节点与另一个节点之间的所有路径。

树始终是 DAG,但 DAG 并不总是树。不同之处在于,树的分支不允许连接,只能分开,而 DAG 的分支可以流在一起,只要不引入循环。

您的解决方案可以在 “Python 模式 - 实现”中找到 find_all_paths()图表。” 这需要进行一些调整才能与 igraph 一起使用。我没有安装 igraph,但使用模拟,这似乎有效:

def adjlist_find_paths(a, n, m, path=[]):
  "Find paths from node index n to m using adjacency list a."
  path = path + [n]
  if n == m:
    return [path]
  paths = []
  for child in a[n]:
    if child not in path:
      child_paths = adjlist_find_paths(a, child, m, path)
      for child_path in child_paths:
        paths.append(child_path)
  return paths

def paths_from_to(graph, source, dest):
  "Find paths in graph from vertex source to vertex dest."
  a = graph.get_adjlist()
  n = source.index
  m = dest.index
  return adjlist_find_paths(a, n, m)

从文档中不清楚 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:

def adjlist_find_paths(a, n, m, path=[]):
  "Find paths from node index n to m using adjacency list a."
  path = path + [n]
  if n == m:
    return [path]
  paths = []
  for child in a[n]:
    if child not in path:
      child_paths = adjlist_find_paths(a, child, m, path)
      for child_path in child_paths:
        paths.append(child_path)
  return paths

def paths_from_to(graph, source, dest):
  "Find paths in graph from vertex source to vertex dest."
  a = graph.get_adjlist()
  n = source.index
  m = dest.index
  return adjlist_find_paths(a, n, m)

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.

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