Practice Discrete Math

Algorithms / Graph Cycle Detection

Least You Need to Know: Cycle Detection in Directed and Undirected Graphs

Cycle detection depends on graph type. In undirected graphs, you must ignore the edge back to the parent. In directed graphs, the key signal is whether DFS re-enters a node that is still on the current recursion path.

جو کم از کم جاننا ضروری ہے

اہم علامتیں

parent the previous node in undirected DFS
recursion stack nodes currently active on the DFS path
back edge edge to an ancestor on the current DFS path

مختصر حل شدہ مثال

  • In a directed graph, DFS marks nodes as unvisited, active, or finished.
  • When DFS follows an edge to an active node, it has found a cycle on the current path.
  • In an undirected graph, that same check must ignore the edge back to the parent.

عام غلطیاں

اس قسم کے سوال کو کیسے پہچانیں

Next recommended lesson

Continue through this topic with Least You Need to Know: Modeling Interview Problems as Graph Search.

Least You Need to Know: Modeling Interview Problems as Graph Search

Related lessons

Keep going with nearby lessons in the same topic.

More ways to explore

مشق شروع کریں