在 SQL 中有向图中计算不同的无向边
给定一个在有向图中保存边的表,如下所示:
CREATE TABLE edges (
from_here int not null,
to_there int not null
)
获取特定节点的不同无向链接数量的最好方法是什么?没有任何重复的有向边,也没有任何节点直接链接到自身,我只是想避免计算重复的无向边(例如 (1,2)
和 (2,1)
) 两次。
这可行,但 NOT IN
对我来说味道很糟糕:
SELECT COUNT(*)
FROM edges
WHERE from_here = 1
OR (to_there = 1 AND from_here NOT IN (
SELECT to_there
FROM edges
WHERE from_here = 1
))
PostgreSQL 特定的解决方案对此很合适。
Given a table holding edges in a directed graph like this:
CREATE TABLE edges (
from_here int not null,
to_there int not null
)
What's the nicest way to get the number of distinct undirected links for a specific node? There aren't any duplicate directed edges nor are any nodes directly linked to themselves, I just want to avoid counting duplicate undirected edges (such as (1,2)
and (2,1)
) twice.
This works but the NOT IN
smells bad to me:
SELECT COUNT(*)
FROM edges
WHERE from_here = 1
OR (to_there = 1 AND from_here NOT IN (
SELECT to_there
FROM edges
WHERE from_here = 1
))
PostgreSQL-specific solutions are fine for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果是这样的情况,对于每条边,都有一个倒数(例如,如果
(1,2)
存在,则(2,1)
必须存在),那么您可以简单地缩小您的列表,如下所示:如果我们不能假设互惠边缘始终存在,那么您可以使用 except 谓词:
If it were the case that for every edge, there was a reciprocal (e.g. if
(1,2)
exists, then(2,1)
must exist), then you could simply narrow your list like so:If we cannot assume that a reciprocal edge always exists, then you could use the Except predicate: