发现使用Python-networkx中的两个图中的共同路径

发布于 2025-02-10 08:20:27 字数 263 浏览 2 评论 0原文

我有两个digraphs,例如g和h,我想计算g的一部分

。发电机: g_gen = nx.all_simple_paths(g,src,dst) h_gen = nx.all_simple_paths(h,src,dst),

因为路径的数量相当高(图通常具有100个节点),我无法求助于构建列表等。是处理它的更聪明的方法。另外,我还想根据路径长度来区分。

..或者可以使用不同的模块找到更好的解决方案?

事先感谢您对此的任何帮助。

蒂埃里

I have two DiGraphs, say G and H, and I would like to count how many paths of G are part of H.

For any node pairs (src, dst) I can generate the paths between them using the 'all_simple_paths' function to get the generators:
G_gen = nx.all_simple_paths(G, src, dst)
H_gen = nx.all_simple_paths(H, src, dst)

Since the amount of paths is considerably high (the graphs have typically 100 nodes) I cannot resort to building lists etc.. (e.g. list(G_gen)) so I am wondering if there are smarter ways to deal with it. In addition, I would also like to distinguish based on the path lengths.

.. or maybe a better solution can be found with a different module ?

Thanks in advance for any help on this.

Thierry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冧九 2025-02-17 08:20:27

我想知道nx.intersection是否有某种原因(请参阅此处)在这里无法使用吗?我不确定它是否检查了引擎盖下的方向,但似乎也不会将输出迫使标准Graph输出。以下可能有效:

# Create a couple of random preferential attachment graphs
G = nx.barabasi_albert_graph(100, 5)
H = nx.barabasi_albert_graph(100, 5)

# Convert to directed
G = G.to_directed()
H = H.to_directed()

# Get intersection
intersection = nx.intersection(G, H)

# Print info for each
print(nx.info(G))
print(nx.info(H))
print(nx.info(intersection))

哪些输出:

>>> DiGraph with 100 nodes and 950 edges
>>> DiGraph with 100 nodes and 950 edges
>>> DiGraph with 100 nodes and 176 edges

节点全部共享在示例中,因为节点ID只是简单的整数,因此它们遵循相同的一代索引。有了真实的数据,我想您的节点集可能不像这里那样等效,您可能也会看到那里的差异。

在路径长度上,我不太确定您将如何解决。交点只检查了两个图之间共享哪些节点和边缘,并返回那些都不知道的任何其他条件。通过将源代码与相交函数调整一些条件检查,可能有一种方法可以施加一些其他约束。

我想这不会检查路径的数量,而是 edges的数量,所以我想您正在寻找比这更具体的东西。 /strong>,但至少在交叉路口之外没有任何路径,因为所有共享路径都必须在两者中都包含相同的边缘(因为这两个路径丢失了边缘,因此它不能作为共享的路径存在解决方案)。

希望这以某种方式或形式有所帮助,尽管我觉得我已经过度简化了您的问题。

编辑:直观地,您问题的完整解决方案可能是简单地列举交叉路口中的所有可能路径。

I wonder if there is some reason why nx.intersection (see here) wouldn't work here? I'm not sure if it checks for direction under the hood but it doesn't seem to force outputs to standard Graph output either. Below might work:

# Create a couple of random preferential attachment graphs
G = nx.barabasi_albert_graph(100, 5)
H = nx.barabasi_albert_graph(100, 5)

# Convert to directed
G = G.to_directed()
H = H.to_directed()

# Get intersection
intersection = nx.intersection(G, H)

# Print info for each
print(nx.info(G))
print(nx.info(H))
print(nx.info(intersection))

which outputs:

>>> DiGraph with 100 nodes and 950 edges
>>> DiGraph with 100 nodes and 950 edges
>>> DiGraph with 100 nodes and 176 edges

The nodes are all shared in the example since the node ids are just simple integers and so they follow the same generation index. With real data I suppose your node sets might not be equivalent like here and you probably will see differences there too.

On the path lengths I'm not quite sure how you would go about that. The intersection just checks which nodes and edges are shared between two graphs and returns those that are in both, unaware of any other conditions I suspect. There might be a way to impose some additional constraints by adapting the source code with of the intersection function with some conditional checks.

I guess this doesn't check the number of paths but rather the number of edges, so I suppose you're looking for something more specific than this. But at the very least no path can exist outside of the intersection, since all shared paths must contain the same edges in both (since if an edge is missing from a path in either, it cannot exist as a path in the shared solution).

Hope this helps in some way shape or form, though I feel I've oversimplified your question quite a bit.

EDIT: Intuitively, the full solution to your question might be to simply enumerate all possible paths in the intersection.

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