【问题标题】:Implementing Disjoint Set Approximation (Union Find) in SQL在 SQL 中实现不相交集逼近(Union Find)
【发布时间】:2015-10-28 14:23:01
【问题描述】:

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

详情

我有一个边表,存储为[vertex_a, vertex_b] 的两列表。

我需要一个不同集合的表,存储为[vertex, set_id],每个顶点一行,用不相交的 set_id 标记每个顶点。

约束

  • 必须是纯 SQL 实现。它可以利用 Postgres 特定的函数,但更倾向于使用纯 ANSI SQL。
  • 结果可能是近似的 - 当它们实际连接时,将几个集合标记为不相交是可以接受的。如果可以调整近似边界(例如通过增加迭代次数),那就更好了。
  • 库已出(无 Boost、Numpy、Scipy)。必须是 SQL。
  • 大多数集合将包含 1 到 3 个顶点。很少有大型集合,预计最多为 10 个顶点。

相关

【问题讨论】:

  • 样本数据和预期输出?
  • 你能告诉我们你已经尝试解决这个问题吗? (Stack Overflow 不是代码编写服务)

标签: sql postgresql graph-theory


【解决方案1】:

这个纯 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

【讨论】:

  • 这在 Postgres 上不起作用(而且绝对不是“ANSI SQL”)
【解决方案2】:

我实际上正在解决同样的问题。不幸的是,我认为找不到非常有效的解决方案——至少使用 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
 );

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 2019-05-31
    • 2018-03-05
    • 1970-01-01
    相关资源
    最近更新 更多