简体   繁体   English

使用SQL的图形中的最长路径

[英]Longest path in graph using SQL

I've a graph which is saved as two tables with names Edge and Node as follows in SQL: 我有一个图形,该图形另存为两个表,名称分别为Edge和Node,如下所示:

Node table:
Id Name
10 A
11 B
12 C

Edge Table:
From To Weight
10 11 0.3
10 14 0.2
10 12 0.5
11 12 0.6
12 10 0.8

For example one possible solution for path from node 10 to node 12 is as follows: 例如,从节点10到节点12的路径的一种可能解决方案如下:

From    To  Weight  Path
10  12  0.9 10,11,12

Now, I want to find out the longest path between nodes of the graph (for any possible path). 现在,我想找出图的节点之间的最长路径(对于任何可能的路径)。

I've changed dijkstra with negative edges and it looped and doesn't return any result. 我更改了带有负边缘的dijkstra,它循环播放并且不返回任何结果。

Is there any procedure or algorithm to find out the desired result? 是否有任何程序或算法可以找出所需的结果?

Recently I had a similar issue and tried to find the longest path using SQL recursive CTE query . 最近,我遇到了类似的问题,并尝试使用SQL递归CTE查询找到最长的路径 Please refer to given article. 请参考给定的文章。

I tried to modify the above solution for your problem, and get a list of all possible paths as seen in following output screen 我尝试为您的问题修改上述解决方案,并获得所有可能路径的列表,如以下输出屏幕所示

在此处输入图片说明

If this is the result you want to achive, I can continue describing my solution on SQL Server 如果这是您要达到的结果,我可以继续在SQL Server上描述我的解决方案

The referred SQL query calculates the longest path for given two nodes. 引用的SQL查询计算给定两个节点的最长路径。 So first of all we need to define all possible starting and destination nodes 因此,首先我们需要定义所有可能的起始节点和目标节点

Below query lists the combination of all possible sets 以下查询列出了所有可能的组合

select Nodes_From.Id as [From], Nodes_To.Id as [To]
from LongestPath_Nodes as Nodes_From
inner join LongestPath_Nodes as Nodes_To
    on Nodes_From.Id <> Nodes_To.Id

Please note that this excludes "14" for example since it is not in the nodes table (so there is a consistency problem here actually in given test data) 请注意,例如,这不包括“ 14”,因为它不在节点表中(因此实际上在给定的测试数据中存在一致性问题)

So by creating a SQL cursor , I can loop through each node set (from-to node combination) and execute the stored procedure which includes SQL codes calculating longest path for given two nodes from referred article 因此,通过创建一个SQL游标 ,我可以遍历每个节点集(从到节点的组合)并执行存储过程,该存储过程包括SQL代码,该代码计算了引用文章中给定的两个节点的最长路径

I also created a table to store a path's total weight 我还创建了一个表来存储路径的总重量

Create Table LongestPath_Routes ([weight] decimal(10,1), path varchar(100))

As seen in below SQL script, I clear this table at the beginning. 如下面的SQL脚本所示,我一开始就清除了该表。 Then populate it for each from-to node set within the stored procedure Finally after cursor execution is completed I query the table by sorting according to weight column for the longest path (also for the shortest path is possible) 然后针对存储过程中每个从头到尾的节点填充该表。最后,在游标执行完成后,我根据权重列对表进行查询,以查询最长的路径(也可以查询最短的路径)

truncate table LongestPath_Routes

DECLARE @From TinyInt
DECLARE @To TinyInt

DECLARE PathCursor CURSOR FAST_FORWARD FOR
    select Nodes_From.Id as [From], Nodes_To.Id as [To]
    from LongestPath_Nodes as Nodes_From
    inner join LongestPath_Nodes as Nodes_To
        on Nodes_From.Id <> Nodes_To.Id

OPEN PathCursor

FETCH NEXT FROM PathCursor INTO @From, @To

WHILE @@FETCH_STATUS = 0
BEGIN
set nocount on
 EXEC LongestPath_Calculate_for_Nodes @From, @To
set nocount off

 FETCH NEXT FROM PathCursor INTO @From, @To
END

CLOSE PathCursor
DEALLOCATE PathCursor 

select * from LongestPath_Routes order by [weight] desc

I hope this solution helps you 我希望这个解决方案对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM