匹配节点的图形算法

发布于 2024-10-07 11:16:46 字数 465 浏览 0 评论 0原文

给定一个有向图,我可以使用什么算法来查找其边的随机子集,以便每个节点都有一个传入边和一个传出边?

例如,这可能是我给出的图表:

Starting input graph

这将是一个有效的输出图表:

A valid output graph

这是有效的,因为:

  • 它包含输入图上的所有节点
  • 它的所有边也在输入图上
  • 每个节点恰好有一条离开它的边和一条到达它的边(并且不能是同一条边,不允许循环,每个节点必须连接到至少一个其他节点)。

如果没有可能的解决方案应该被检测到。

有没有有效的算法来解决这个问题?

谢谢!

Given a directed graph, what is an algorithm I can use to find a random subset of its edges so that every node has exactly one incoming and exactly one outgoing edge?

For example, this could be the graph I am given:

Starting input graph

And this would be a valid output graph:

A valid output graph

This is valid because:

  • It contains all the nodes on the input graph
  • All its edges are also on the input graph
  • Every node has exactly one edge leaving it and one edge coming to it (and that can't be the same edge, no loops are allowed, every node has to connect to at least one other node).

If there is no possible solution that should be detected.

Is there an efficient algorithm to solve this?

Thanks!

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

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

发布评论

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

评论(2

酒几许 2024-10-14 11:16:47

这是一个节点周期覆盖问题。它可以被解决为二分图中的最大匹配

简而言之:

  1. 将每个节点一分为二,每个节点位于图的一个分区中,以便所有边都从分区 P1 到分区 P2。在您的示例中,节点 A 和 D 将变成分区 P1 中的节点 A1、D1 和 P2 中的节点 A2、D2。边AD将变为A1-D2,边DA-变为D1-A2。
  2. 然后找到一个最大匹配,有些算法是存在的。
  3. 然后将节点合并回去,就得到了循环覆盖。

It's a node cycles coverage problem. It can be solved as Maximum matchings in bipartite graphs.

In short:

  1. Split every node in two, each in one partition of a graph, so that all edges go from partition P1 to partition P2. In your sample, nodes A and D will turn into nodes A1, D1 in partition P1 and A2, D2 in P2. Edge A-D will turn into A1-D2, and D-A - to D1-A2.
  2. Then find a maximum matching, some algorithms exist.
  3. Then merge the nodes back, and you got a cycle coverage.
君勿笑 2024-10-14 11:16:47

您正在尝试将图表分解为一组循环。

此链接将您引向用于在图表中查找循环的 Tarjan 算法。

之后,您将需要一些搜索策略(鉴于您的限制,某些周期的选择可能不会导致解决方案)。

You're trying to decompose a graph into a set of cycles.

This link points you to Tarjan's algorithm for finding cycles in a graph.

After that, you'll need some search strategy (some choices of cycles may not lead to a solution given your constraints).

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