[英]PostgreSQL SQL query for traversing an entire undirected graph and returning all edges found
I have an edges table in my PostgreSQL database that represents the edges of a directed graph, with two columns: node_from and node_to (value is a node's id). 我的PostgreSQL数据库中有一个edge表,它表示有向图的边缘,有两列: node_from和node_to (value是节点的id)。
Given a single node ( initial_node ) I'd like to be able to traverse the entire graph, but in an undirected way. 给定一个节点( initial_node ),我希望能够遍历整个图,但是以无向的方式。
What I mean is, for instance for the following graph : 我的意思是,例如下图:
(a->b)
(c->b)
(c->d)
If initial_node is a , b , c , or d , in any case, I would get [ a , b , c , d ]. 如果initial_node是a , b , c或d ,在任何情况下,我都会得到[ a , b , c , d ]。
I used the following SQL query (based on http://www.postgresql.org/docs/8.4/static/queries-with.html ): 我使用了以下SQL查询(基于http://www.postgresql.org/docs/8.4/static/queries-with.html ):
WITH RECURSIVE search_graph(uniq, depth, path, cycle) AS (
SELECT
CASE WHEN g.node_from = 'initial_node' THEN g.node_to ELSE g.node_from END,
1,
CASE WHEN g.node_from = 'initial_node' THEN ARRAY[g.node_from] ELSE ARRAY[g.node_to] END,
false
FROM edges g
WHERE 'initial_node' in (node_from, node_to)
UNION ALL
SELECT
CASE WHEN g.node_from = sg.uniq THEN g.node_to ELSE g.node_from END,
sg.depth + 1,
CASE WHEN g.node_from = sg.uniq THEN path || g.node_from ELSE path || g.node_to END,
g.node_to = ANY(path) OR g.node_from = ANY(path)
FROM edges g, search_graph sg
WHERE sg.uniq IN (g.node_from, g.node_to) AND NOT cycle
)
SELECT * FROM search_graph
It worked fine... Until I had a case with 12 nodes that are all connected together, in all directions (for each pair I have both (a->b) and (b->a)), which makes the query loops indefinitely. 它工作得很好......直到我有一个案例有12个节点都连接在一起,在所有方向(对于每一对我有(a-> b)和(b-> a)),这使得查询循环无限期。 (Changing UNION ALL to UNION doesn't eliminate the looping.)
(将UNION ALL更改为UNION不会消除循环。)
Has anyone any piece of advice to handle this issue? 有没有人提出任何建议来处理这个问题?
Cheers, 干杯,
Antoine. 安托万。
I got to this, it should not get into infinite loops with any kind of data: 我得到了这个,它不应该与任何类型的数据进入无限循环:
--create temp table edges ("from" text, "to" text);
--insert into edges values ('initial_node', 'a'), ('a', 'b'), ('a', 'c'), ('c', 'd');
with recursive graph(points) as (
select array(select distinct "to" from edges where "from" = 'initial_node')
union all
select g.points || e1.p || e2.p
from graph g
left join lateral (
select array(
select distinct "to"
from edges
where "from" =any(g.points) and "to" <>all(g.points) and "to" <> 'initial_node') AS p) e1 on (true)
left join lateral (
select array(
select distinct "from"
from edges
where "to" =any(g.points) and "from" <>all(g.points) and "from" <> 'initial_node') AS p) e2 on (true)
where e1.p <> '{}' OR e2.p <> '{}'
)
select distinct unnest(points)
from graph
order by 1
Recursive queries are very limiting in terms of what can be selected, and since they don't allow using the recursive results inside a subselect, one can't use NOT IN (select * from recursive where...). 递归查询在可以选择的内容方面是非常有限的,并且因为它们不允许在子选择内使用递归结果,所以不能使用NOT IN(select * from recursive where ...)。 Storing results in an array, using LEFT JOIN LATERAL and using =ANY() and <>ALL() solved this conundrum.
使用LEFT JOIN LATERAL并使用= ANY()和<> ALL()将结果存储在数组中解决了这个难题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.