问题描述
当我尝试从有向图中的顶点搜索可能的路径时。我有一个 SQL 递归来做到这一点。但是,我不想使用数据库,因此想将 SQL 递归转换为没有 SQL 查询的节点 js 递归函数,但我无法弄清楚。 我该怎么做?这是我的 SQL 递归
drop table edges;
create table edges (route_id integer,stuff_id integer,option_id integer);
insert into edges (route_id,stuff_id,option_id) values (36,73,75);
insert into edges (route_id,option_id) values (37,74);
insert into edges (route_id,option_id) values (38,88);
insert into edges (route_id,option_id) values (39,82,73);
WITH RECURSIVE search_path (depth,option_tree) AS (
SELECT 1,ARRAY[e.stuff_id]
FROM edges e
UNION ALL
SELECT f.depth + 1,option_tree || d.option_id
FROM edges d,search_path f
WHERE f.option_tree[array_length(option_tree,1)] = d.stuff_id
)
SELECT * FROM search_path
WHERE option_tree[1] = 82
ORDER BY depth DESC;
我们可以通过 https://extendsclass.com/postgresql-online.html
测试 SQL解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)