Python igraph:在有向图中获取所有可能的路径

编程入门 行业动态 更新时间:2024-10-11 21:27:11
本文介绍了Python igraph:在有向图中获取所有可能的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用igraph(Python)并希望在有向图中的两个节点之间获得所有可能的路径。我知道函数 get_all_shortest_paths ,它用于最短路径,但找不到一个通用路径。

更新:

我的主要目标是获取这些路径中的所有节点,以便我可以获得这些节点的子图。

<因为你在你的问题中提到你的最终目标是只得到这些路径中的节点而不是路径本身,我认为你甚至不需要必须计算路径。

在igraph中的 Graph 对象有一个名为子组件的方法。默认情况下,它为您提供与给定输入节点处于相同(弱连接)组件的所有节点。但是,它也有一个模式参数。当您将 mode 设置为out时,它会为您提供从某个节点到达的所有节点。当您在中将 mode 设置为时,它会为您提供到达某个节点的所有节点。因此,您可能需要从源顶点到可以到达目标顶点的一组可达节点的交集:

s = set(graph.subcomponent(source,mode =out))t = set(graph.subcomponent(target,mode =in)) s.intersection t)

无论如何,这可能比计算所有路径要快。

I am using igraph (Python) and would like to get all possible paths between two nodes in a directed graph. I am aware of the function get_all_shortest_paths, which is for shortest paths, but could not find a general one.

Update:

My main goal is to get all nodes in these paths, so that I can then get a subgraph of these nodes.

解决方案

Since you mentioned in your question that your ultimate goal is to get only the nodes that are in these paths and not the paths themselves, I think you don't even have to calculate the paths.

The Graph object in igraph has a method called subcomponent. By default, it gives you all the nodes that are in the same (weakly connected) component as a given input node. However, it also has a mode argument. When you set mode to "out", it will give you all the nodes that are reachable from a certain node. When you set mode to "in", it will give you all the nodes from where you can reach a certain node. So, you'll probably need the intersection of the set of reachable nodes from your source vertex and the set of nodes that can reach your target vertex:

s=set(graph.subcomponent(source, mode="out")) t=set(graph.subcomponent(target, mode="in")) s.intersection(t)

This is probably way faster than calculating all the paths anyway.

更多推荐

Python igraph:在有向图中获取所有可能的路径

本文发布于:2023-11-28 21:27:27,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图中   路径   Python   igraph

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!