在 SQL 中实现不相交集近似(联合查找)

问题描述 投票:0回答:3

使用 SQL 实现近似不相交集的最佳方法是什么?

详情

我有一张边表,存储为

[vertex_a, vertex_b]
的两列表。

我需要一个不同集合的表,存储为

[vertex, set_id]
每个顶点一行,用不相交的 set_id 标记每个顶点。

约束

  • 必须是纯SQL实现。它可以利用特定于 Postgres 的函数,但高度首选纯 ANSI SQL。
  • 结果可以是近似的——当一些集合实际连接时,将它们标记为不相交是可以接受的。如果可以调整近似边界,那就更好了——例如,通过增加迭代次数。
  • 图书馆已经出局(没有 Boost、Numpy、Scipy)。必须是SQL。
  • 大多数集合将包含 1 到 3 个顶点。很少有大集合,预计最多为 10 个顶点。

相关

sql postgresql graph-theory
3个回答
1
投票

我实际上正在处理同样的问题。不幸的是,我不认为可以找到一个非常有效的解决方案——至少不能轻易地使用 SQL。只需删除“distinct”和自我消除查询即可观察工作集的大小。也就是说,以下解决方案将起作用。

drop table if exists foobar;
drop function if exists addset(v int[], a int);
/* our vertices table */
create table foobar (
   src int,
   dst int
);

/* Create a small function to treat an array like a set, 
   not strictly necessary but convenient */
create function addset(v int[], a int) returns int[]
as $$
begin
    return (select array_agg(e order by e) 
                   from (select unnest(v || a) as e) f);
end
$$ language plpgsql;

/* fill our table with vertices, note the ordering of each value */
insert into foobar (src, dst) 
     values (1,2), (1,3), (2,3),  (3,4), (4,5), (6,7), (6,8);
/* use a recursive query to extend the sets */
with recursive foo_union (v) as (
    select array[src, dst] as v from foobar /* starter sets */
    union all
    /* join self to original array; i can use a CTE as a 'starter',
       but that is not necessary here */
    select addset(v, dst) as v from foo_union u, foobar f
        where src = any(v) and not dst = any(v)
 ) select distinct v from foo_union a where not exists (
     /* eliminate the many overlapping results */
     select * from foo_union b where b.v @> a.v and b.v != a.v
 );

但是,这在更大的数据集上是完全不切实际的;任何其他解决方案都需要迭代。


0
投票

这段纯 sql 代码在 5 分钟内对大约 35000 条记录进行了分组(8 核/32 GB 内存)。享受吧。

--table with RELATIONS, idea is to place every related item in a bucket
create table RELATIONS
(
    bucket int,        -- initially 0
    bucketsub int,    -- initially 0
    relnr1 float,    
    relnr2 float    -- relnr1 = a, relnr2 = b means a and b are related
)

create table ##BucketRelnrs ( relnr float ); --table functions as temp list
declare @bucket int; 
declare @bucketsub int;
declare @nrOfUpdates int;
declare @relnr float;

set @bucket=0;
set @relnr=0;
WHILE @relnr>=0 and @bucket<50000 --to prevent the while loop from hanging.
BEGIN
    set @bucket = @bucket+1
    set @bucketsub=1;

    set @relnr = (select isnull(min(relnr1),-1) from RELATIONS where bucket=0); --pick the smallest relnr that has not been assigned a bucket yet
    set @nrOfUpdates = (select count(*) from RELATIONS where bucket=0 and (relnr1=@relnr or relnr2=@relnr));
    update RELATIONS set bucket=@bucket, bucketsub=@bucketsub where bucket=0 and (relnr1=@relnr or relnr2=@relnr);
    set @bucketsub = @bucketsub+1;

    WHILE @nrOfUpdates>0 and @bucketsub<=10    --to prevent the inner while loop from hanging, actually determines the number of iterations
    BEGIN
        --refill temp list with newly found related relnrs
        truncate table ##BucketRelnrs;
        insert into ##BucketRelnrs
        select distinct relnr1 from RELATIONS where bucket=@bucket
        union select distinct relnr2 from RELATIONS where bucket=@bucket;

        --calculate the number of relations that will be updated next, if zero then stop iteration
        set @nrOfUpdates =
        (
            select count(*) from RELATIONS where bucket=0
            and (relnr1 in (select relnr from ##BucketRelnrs) or relnr2 in (select relnr from ##BucketRelnrs))
        );

        --update the RELATIONS table
        update RELATIONS set bucket=@bucket, bucketsub=@bucketsub where bucket=0
        and (relnr1 in (select relnr from ##BucketRelnrs) or relnr2 in (select relnr from ##BucketRelnrs));

        set @bucketsub = @bucketsub+1;
    END
END

drop table ##BucketRelnrs; --clean temp table

0
投票

如果你需要逐步维护它,我会在你的顶点表中添加第三个名为 height 的 int 列,将 set_id 视为父指针而不是连接的组件,并在你的关系表中添加外键约束和触发器来做父指针插入。

然后,要查看连接的组件,您可以使用递归视图。

如果你已经有一个大表并且你需要一个高效的批处理,那么主要问题是标准方法不是缓存无视并且缓存行为非常差,而 SQL 基本上是为了阻止这种情况而设计的。

© www.soinside.com 2019 - 2024. All rights reserved.