在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]       │ []                                                                 │
│ []        │ []                                                                 │
└───────────┴────────────────────────────────────────────────────────────────────┘
相关推荐
geovindu7 小时前
sql:SQL Pagination and Limiting Results using PorstgreSQL 18.0
大数据·数据仓库·sql·postgresql·数据库开发·数据库架构·1024程序员节
企业数字化笔记9 小时前
视频人体姿态怎么做三维可视化?2D关键点、深度局限与技术选型
人工智能·算法·音视频
?abc!9 小时前
(数组)581. 最短无序连续子数组
数据结构·算法·leetcode
小郎君65812 小时前
MPTT树复选框的半选状态:一棵百万节点树从递归到计数器,再到区间覆盖
算法
酌沧12 小时前
MemT让记忆成为模型内生能力
jvm·人工智能·算法
Shimeri13 小时前
越卷越像:我用遗传算法做了个"内卷"实验
算法·three.js·数据可视化·遗传算法·进化算法
Evand J13 小时前
【PMSM例程1】永磁同步电机(PMSM)转速与转子电角度观测算法|基于扩展卡尔曼滤波(EKF)的传感器状态估计与误差。附下载链接
算法·电机·1024程序员节·卡尔曼滤波
guo_wen_qiang13 小时前
二叉树的层序遍历-使用双端队列Deque
算法·leetcode·1024程序员节
爱读书的小胖13 小时前
LeetCode 55 跳跃游戏 C 语言解题分析
c语言·数据结构·算法·leetcode·1024程序员节
CCblandy13 小时前
前缀缓存命中率不靠引擎靠排列:本机仿真 3 轮,易变字段放开头 98.0%→0.0%
人工智能·python·算法·自然语言处理·性能优化