0
0
DSA Typescriptprogramming~10 mins

Cycle Detection in Undirected Graph in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Cycle Detection in Undirected Graph
Start at any node
Mark node as visited
For each neighbor
If neighbor not visited
Recurse DFS
Cycle found
Return result
Start DFS from any node, mark nodes visited, check neighbors. If a visited neighbor is not the parent, cycle exists.
Execution Sample
DSA Typescript
const graph = {
  0: [1, 2],
  1: [0, 2],
  2: [0, 1, 3],
  3: [2]
};
// Detect cycle using DFS
Detects cycle in an undirected graph using DFS and parent tracking.
Execution Table
StepOperationCurrent NodeVisited SetParentNeighbor CheckedCycle DetectedVisual State
1Start DFS0{}nullNoneNoNodes: 0 Edges: 0
2Visit node 00{0}null1No0 -> visited
3Check neighbor 1 of 00{0}null1No0 connected to 1
4Neighbor 1 not visited, DFS to 11{0}0NoneNoTraverse 0->1
5Visit node 11{0,1}00No1 -> visited
6Check neighbor 0 of 11{0,1}00NoNeighbor is parent, ignore
7Check neighbor 2 of 11{0,1}02NoCheck 2 from 1
8Neighbor 2 not visited, DFS to 22{0,1}1NoneNoTraverse 1->2
9Visit node 22{0,1,2}10No2 -> visited
10Check neighbor 0 of 22{0,1,2}10Yes0 visited and not parent, cycle found
11Cycle detected, stop DFS2{0,1,2}10YesCycle confirmed
12Return TrueN/A{0,1,2}N/AN/AYesCycle exists in graph
💡 Cycle detected at step 10 when neighbor 0 of node 2 is visited and not parent
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 9Final
Current Nodenull012N/A
Visited Set{}{0}{0,1}{0,1,2}{0,1,2}
Parentnullnull01N/A
Cycle DetectedNoNoNoNoYes
Key Moments - 3 Insights
Why do we ignore the neighbor if it is the parent node during DFS?
Because the parent node is where we came from, so visiting it again does not mean a cycle. See step 6 where neighbor 0 is parent of 1 and no cycle is detected.
How do we know a cycle exists when we find a visited neighbor that is not the parent?
If a neighbor is visited and not the parent, it means we found a back edge forming a loop. This happens at step 10 where neighbor 0 of node 2 is visited but not parent, so cycle is detected.
What happens if the graph has no edges or is empty?
DFS will visit nodes but never find a visited neighbor that is not parent, so no cycle is detected and DFS ends normally.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the visited set after visiting node 1?
A{0}
B{0,1}
C{1}
D{}
💡 Hint
Check step 5 in the execution table under Visited Set column
At which step does the cycle get detected in the graph?
AStep 10
BStep 8
CStep 6
DStep 12
💡 Hint
Look at Cycle Detected column in execution table rows
If the graph had no edges, how would the cycle detection result change?
AAlgorithm would crash
BCycle detected immediately
CNo cycle detected
DCycle detected at last step
💡 Hint
Refer to key moment about empty graph and no edges
Concept Snapshot
Cycle Detection in Undirected Graph using DFS:
- Start DFS from any node
- Mark node visited
- For each neighbor:
  - If not visited, DFS recursively
  - If visited and not parent, cycle found
- Return true if cycle detected, else false
Full Transcript
Cycle detection in an undirected graph uses depth-first search (DFS). We start from any node and mark it visited. For each neighbor, if it is not visited, we recursively DFS into it. If a neighbor is visited and is not the parent node, it means a cycle exists. We track the parent to avoid false positives from the edge we came from. The process stops when a cycle is found or all nodes are visited without finding a cycle. This method works because a back edge to a visited node other than the parent indicates a loop in the graph.