在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]       │ []                                                                 │
│ []        │ []                                                                 │
└───────────┴────────────────────────────────────────────────────────────────────┘
相关推荐
Navigator_Z11 小时前
LeetCode //C - 1240. Tiling a Rectangle with the Fewest Squares
c语言·算法·leetcode
稻米哟11 小时前
力扣100——双指针
算法·leetcode
明志数科12 小时前
具身智能真机采集工程实践:5类高频失效模式与规避清单
人工智能·算法·机器学习
CSDN_RTKLIB12 小时前
空间哈希与拓扑缓存
算法
子非鱼a12 小时前
【WEB】[GXYCTF2019]BabySQli
sql
大熊背13 小时前
IspPipeline色相旋转模块实现
人工智能·算法·计算机视觉
爱学习的小白柏13 小时前
同城双活的核心不是双活,是逼着数据别出机房
大数据·人工智能·算法·langchain·ai编程
鹿角片ljp13 小时前
LeetCode 31:下一个排列|右找小,右找大,交换,右反转
java·数据结构·算法
橘子汽水16814 小时前
Leetcode 198,118打家劫舍,杨辉三角
算法·leetcode
LuminousCPP15 小时前
数据结构-排序(三):快速排序进阶|挖坑法、双指针交换与手动栈非递归实现
c语言·数据结构·笔记·算法·排序算法