图与递归查询:从祖先路径到循环检测
1. 祖先路径查询
在处理图数据时,查询给定节点的祖先(也称为路径)是常见需求,例如返回给定员工的管理链。与返回子图的算法类似,返回祖先的算法使用迭代逻辑,但方向相反,从给定节点“向上”遍历到父节点。
1.1 创建 Managers 函数
可以运行以下代码创建 Managers 函数:
-- Function: Managers, Ancestors with optional level limit
--
-- Input : @empid INT : Employee id
-- @maxlevels : Max number of levels to return
--
-- Output : @Mgrs Table: id and level of managers of
-- input employee in all levels <= @maxlevels
--
-- Process : * In a loop, while current manager is not null
-- and previous level is smaller than @maxlevels
-- insert into @Mgrs current manager,
-- and get next level manager
--------------------
超级会员免费看
订阅专栏 解锁全文
10

被折叠的 条评论
为什么被折叠?



