使用SQL查找有向图中的周期

编程入门 行业动态 更新时间:2024-10-21 09:48:40
本文介绍了使用SQL查找有向图中的周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这些表将是Node(NodeID) INT)和Edge(EdgeID INT,NodeID1 INT,NodeID2 INT)

在有向图中寻找循环的性能良好的解决方案是什么? b $ b

解决方案

解决方案结果非常简单明了,但稍微长一点: 生成所有通过图的路径的列表,使得没有路径多于一次包含相同的边。

从这些信息中,我们获取开始的路径列表并在同一个节点结束。

从这个最终边的列表中,我们根据前两步的计算重新构建所有循环的路径。

我发布了 TSQL 在我的博客上。

There are already a couple of questions on finding cycles, but I did not find a solution in SQL (MSSQL preferred).

The tables would be Node (NodeID INT) and Edge (EdgeID INT, NodeID1 INT, NodeID2 INT)

What would be a well-performing solution to find cycles in a directed graph?

解决方案

The solution turned out quite simple and straightforward, but a bit longer:

First the list of all paths through the graph is generated, such that no path contains the same edge more than once.

From this information, we take the list of paths that start and end in the same node.

From this list of "final" edges we reconstruct all paths with cycles based on the calculation of the previous two steps.

I posted the complete solution in TSQL on my blog.

更多推荐

使用SQL查找有向图中的周期

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

发布评论

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

>www.elefans.com

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