我怎样才能在图中找到所有'长'简单的非循环路径?

编程入门 行业动态 更新时间:2024-10-11 05:21:07
本文介绍了我怎样才能在图中找到所有'长'简单的非循环路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 假设我们有一个完全连接的有向图 G 。顶点是 [a,b,c] 。每个顶点之间在两个方向上都有边。

给定起始顶点 a ,我想遍历只有当我点击一个已经在路径中的顶点时才保存路径。 $ b

因此,函数 full_paths(a, G)应返回:

- [{a,b},{b,c}, {c,d}] - [{a,b},{b,d},{d,c}] - [{a,c},{c,b},{b ,d}] - [{a,c},{c,d},{d,b}] - [{a,d},{d,c},{c,b }] - [{a,d},{d,b},{b,c}]

我不需要像'code> [{a,b}] 或 [{a,b},{b ,c}] ,因为它已包含在第一个结果中。

除了生成powerset之外,还有其他方法可以完成它的G和过滤出一定大小的结果?

我该如何计算这个值?

编辑:正如Ethan指出的那样,这可以通过深度优先搜索方法解决,但不幸最近我不明白如何修改它,使它在回溯之前存储路径(我使用Ruby Gratr 实现我的算法)

解决方案

href =en.wikipedia/wiki/Depth-first_search =nofollow>深度优先搜索或某些变体?深度优先搜索尽可能遍历,然后回溯。每次需要回溯时,您都可以记录路径。

Let's say we have a fully connected directed graph G. The vertices are [a,b,c]. There are edges in both directions between each vertex.

Given a starting vertex a, I would like to traverse the graph in all directions and save the path only when I hit a vertex which is already in the path.

So, the function full_paths(a,G) should return:

- [{a,b}, {b,c}, {c,d}] - [{a,b}, {b,d}, {d,c}] - [{a,c}, {c,b}, {b,d}] - [{a,c}, {c,d}, {d,b}] - [{a,d}, {d,c}, {c,b}] - [{a,d}, {d,b}, {b,c}]

I do not need 'incomplete' results like [{a,b}] or [{a,b}, {b,c}], because it is contained in the first result already.

Is there any other way to do it except of generating a powerset of G and filtering out results of certain size?

How can I calculate this?

Edit: As Ethan pointed out, this could be solved with depth-first search method, but unfortunately I do not understand how to modify it, making it store a path before it backtracks (I use Ruby Gratr to implement my algorithm)

解决方案

Have you looked into depth first search or some variation? A depth first search traverses as far as possible and then backtracks. You can record the path each time you need to backtrack.

更多推荐

我怎样才能在图中找到所有'长'简单的非循环路径?

本文发布于:2023-11-28 21:29:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图中   路径   简单

发布评论

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

>www.elefans.com

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