跳到主要内容

资源 · 指南或文章

Detect Cycle in Graphs

阐明环检测背后的术语,即闭合通路、简单环和回路,以及这些定义在有向图和无向图之间有何不同。做“检测图中是否有环”类题目之前,可以先读它把概念理清。

审核日期

打开原站: Detect Cycle in Graphs (在新标签页中打开原网站)

练习这个主题打开练习面试官,并带上一道关于 图 的题目。

添加到路线图

把这个资源放进你正在跟进的路线图。它会作为你自己的补充资源显示;路线图的步骤不会改变,也不计入你已记录的步骤。

来源内容语言:英语

In graph theory, a cycle is a path of edges and vertices wherein a vertex is reachable from itself. There are several different types of cycles, principally a closed walk and a simple cycle. A closed walk consists of a sequence of vertices starting and ending at the same vertex, with each two consecutive vertices in the sequence adjacent to each other in the graph. In a directed graph, each edge must be traversed by the walk consistently with its direction: the edge must be oriented from the earlier of two consecutive vertices to the later of the two vertices in the sequence. The choice of starting vertex is not important: traversing the same cyclic sequence of edges from different starting vertices produces the same closed walk. A simple cycle may be defined either as a closed walk with no repetitions of vertices and edges allowed, other than the repetition of the starting and ending vertex, or as the set of edges in such a walk. The two definitions are equivalent in directed graphs, where simple cycles are also called directed cycles: the cyclic sequence of vertices and edges in a walk is completely determined by the set of edges that it uses. In undirected graphs the set of…

PrepKit 概览

本概览由 PrepKit 编辑根据源代码仓库撰写并审核,并非资源本身的文字。

适合人群
来源未说明适用级别
形式
参考资料

涵盖内容

  • 闭合通路的定义,包括有向通路必须沿边的方向行进这一规则
  • 简单环与回路的区别,以及为什么每个无向环对应两个有向环
  • 一张着色的示例图,区分路径 H-A-B、含重复顶点的通路以及简单环 H-D-G-H
  • 分别针对无向图和有向图环检测的参考资料列表

使用建议

这是 PrepKit 的学习建议,并非来源的说法。

两种版本都要解:无向图用记录父节点的 DFS 或并查集,有向图用三色 DFS。解释为什么无向图的方法在有向图上会失效。

范围与局限

只有定义。README 没有描述检测算法、复杂度和代码,这些只能通过 GeeksForGeeks 和 YouTube 链接查看。

获取方式与形式

GitHub 上采用 MIT 许可的 trekhleb/javascript-algorithms 仓库中的 README;英文,免费阅读。

PrepKit 于 2026年10月2日 审核。 资源来源提交: 85293e3e2b88

本概览的依据
  • src/algorithms/graph/detect-cycle/README.md

收录此条目的页面

订阅 PrepKit 通讯

可选。使用 PrepKit 并不需要订阅。

我们如何处理你的邮箱