在duckdb 递归CTE中实现深度优先搜索DFS

原帖地址 https://github.com/duckdb/duckdb/discussions/15386

通常的递归CTE都是广度优先搜索(BFS)

sql 复制代码
WITH RECURSIVE 
edges(a, b) as(
VALUES
  (1, 2),
  (1, 3),
  (2, 4),
  (4, 5),
  (4, 6)
), 
bfs(node, path) AS (
  SELECT 1 AS node, [] :: STRUCT("from" INT, "to" INT)[] AS path -- Start with node 1 (root)
  UNION ALL
  SELECT e.b, bft.path|| [{'from': bft.node, 'to': e.b}]  
  FROM   bfs AS bft,
         edges AS e
  WHERE  bft.node = e.a
)
SELECT * FROM bfs;

┌───────┬────────────────────────────────────────────────────────────────────┐
│ node  │                                path                                │
│ int32 │               struct("from" integer, "to" integer)[]               │
├───────┼────────────────────────────────────────────────────────────────────┤
│     1 │ []                                                                 │
│     2 │ [{'from': 1, 'to': 2}]                                             │
│     3 │ [{'from': 1, 'to': 3}]                                             │
│     4 │ [{'from': 1, 'to': 2}, {'from': 2, 'to': 4}]                       │
│     5 │ [{'from': 1, 'to': 2}, {'from': 2, 'to': 4}, {'from': 4, 'to': 5}] │
│     6 │ [{'from': 1, 'to': 2}, {'from': 2, 'to': 4}, {'from': 4, 'to': 6}] │
└───────┴────────────────────────────────────────────────────────────────────┘

DuckDB CTE模块的设计者kryonix提供了如下技巧提供DFS,但是还有问题,没有求出全部路径。

sql 复制代码
WITH RECURSIVE 
edges(a, b) as(
VALUES
  (1, 2),
  (1, 3),
  (2, 4),
  (4, 5),
  (4, 6)
), 
dfs(stack, path) AS (
  SELECT [1] AS stack, [] :: STRUCT("from" INT, "to" INT)[] AS path -- Start with node 1 (root)
    UNION ALL
  (WITH siblings AS (
    SELECT ARRAY_AGG(e.b ORDER BY e.b ASC) AS siblings
    --                                ^^^
    -- This determines the order of traversal of the siblings
    FROM   dfs AS dft,
           edges AS e
    WHERE  dft.stack[1] = e.a
  )
  SELECT x.*
  FROM   siblings AS _(s), LATERAL
         (SELECT s || dft.stack[2:] AS stack, -- Push the stack
                 dft.path || [{'from': dft.stack[1], 'to': s[1]}] AS path -- Add the edge to the path
          FROM   dfs AS dft
          WHERE  array_length(s) > 0 -- There are more siblings to traverse
            UNION ALL
          SELECT dft.stack[2:], -- Pop the stack
                 [] :: STRUCT("from" INT, "to" INT)[] AS path -- Reset the path
          FROM   dfs AS dft
          WHERE  array_length(s) IS NULL AND dft.stack <> [] -- No more siblings to traverse
          ) AS x
  )
)
SELECT * FROM dfs;

┌───────────┬────────────────────────────────────────────────────────────────────┐
│   stack   │                                path                                │
│  int32[]  │               struct("from" integer, "to" integer)[]               │
├───────────┼────────────────────────────────────────────────────────────────────┤
│ [1]       │ []                                                                 │
│ [2, 3]    │ [{'from': 1, 'to': 2}]                                             │
│ [4, 3]    │ [{'from': 1, 'to': 2}, {'from': 2, 'to': 4}]                       │
│ [5, 6, 3] │ [{'from': 1, 'to': 2}, {'from': 2, 'to': 4}, {'from': 4, 'to': 5}] │
│ [6, 3]    │ []                                                                 │
│ [3]       │ []                                                                 │
│ []        │ []                                                                 │
└───────────┴────────────────────────────────────────────────────────────────────┘
相关推荐
551只玄猫1 分钟前
【数据库原理 实验报告3】索引的创建以及数据更新
数据库·sql·课程设计·实验报告·操作系统原理
ic爱吃蓝莓1 小时前
数据结构 | HashMap原理
数据结构·学习·算法·链表·哈希算法
add45a1 小时前
C++编译期数据结构
开发语言·c++·算法
2401_891482171 小时前
C++模块化编程指南
开发语言·c++·算法
沉鱼.441 小时前
树、堆的题目集
算法
暮冬-  Gentle°1 小时前
自定义类型转换机制
开发语言·c++·算法
2301_816651222 小时前
嵌入式C++低功耗设计
开发语言·c++·算法
机器学习之心2 小时前
PSO-LightGBM-ABKDE粒子群算法优化轻量级梯度提升机自适应带宽核密度估计多变量回归区间预测Matlab实现
算法·matlab·回归·abkde·自适应带宽核密度估计·pso-lightgbm·粒子群算法优化轻量级梯度提升机
qq_416018722 小时前
分布式缓存一致性
开发语言·c++·算法
CoovallyAIHub2 小时前
多 Agent 手术推理框架:Agent 辩论+RAG 补上手术知识,零样本超越监督基线 14.6 个百分点
算法·架构·机器人