0
0
DSA Cprogramming~10 mins

Cycle Detection in Undirected Graph in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Cycle Detection in Undirected Graph
Start at any unvisited node
Mark node as visited
For each adjacent node
If adjacent not visited
Recurse DFS on adjacent
If adjacent visited and not parent
Cycle detected! Stop
No cycle found in this path
Repeat for all nodes
End: Cycle exists or not
Start DFS from each unvisited node, mark visited, check neighbors. If a visited neighbor is not parent, cycle exists.
Execution Sample
DSA C
void dfs(int node, int parent) {
  visited[node] = 1;
  for (int neighbor : graph[node]) {
    if (!visited[neighbor]) dfs(neighbor, node);
    else if (neighbor != parent) cycle_found = 1;
  }
}
DFS visits nodes, marks visited, detects cycle if visited neighbor is not parent.
Execution Table
StepOperationCurrent NodeNeighborVisited SetParentCycle DetectedGraph State
1Start DFS0-{}-No0: [1,2], 1: [0,3], 2: [0,3], 3: [1,2]
2Visit node 00-{0}-No0: [1,2], 1: [0,3], 2: [0,3], 3: [1,2]
3Check neighbor 101{0}0NoSame
4Visit node 11-{0,1}0NoSame
5Check neighbor 010{0,1}1NoSame
6Neighbor 0 is parent, continue10{0,1}1NoSame
7Check neighbor 313{0,1}1NoSame
8Visit node 33-{0,1,3}1NoSame
9Check neighbor 131{0,1,3}3NoSame
10Neighbor 1 is parent, continue31{0,1,3}3NoSame
11Check neighbor 232{0,1,3}3NoSame
12Visit node 22-{0,1,2,3}3NoSame
13Check neighbor 020{0,1,2,3}3NoSame
14Neighbor 0 visited and not parent (3), cycle detected20{0,1,2,3}3YesSame
15Stop DFS due to cycle--{0,1,2,3}-YesSame
💡 Cycle detected at step 14 when visiting neighbor 0 from node 2 which is visited and not parent
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 8After Step 12After Step 14Final
visited{}{0}{0,1}{0,1,3}{0,1,2,3}{0,1,2,3}{0,1,2,3}
current_node-01322-
parent--0133-
cycle_foundNoNoNoNoNoYesYes
Key Moments - 3 Insights
Why do we check if the visited neighbor is not the parent?
Because the parent is the node we came from, so visiting it again is normal and not a cycle. See step 6 and 10 in execution_table where neighbor is parent and no cycle is detected.
What happens when we find a visited neighbor that is not the parent?
It means we found a back edge, indicating a cycle. This is shown at step 14 where neighbor 0 is visited and not parent, so cycle_found is set to Yes.
Why do we stop DFS immediately after detecting a cycle?
Because once a cycle is found, continuing is unnecessary. Step 15 shows DFS stops after cycle detection at step 14.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the visited set after step 8?
A{0,1,2,3}
B{0,1}
C{0,1,3}
D{}
💡 Hint
Check the Visited Set column at step 8 in execution_table
At which step does the cycle get detected?
AStep 10
BStep 14
CStep 6
DStep 12
💡 Hint
Look for 'cycle detected' in the Cycle Detected column in execution_table
If the graph had no edge between node 2 and node 0, what would happen?
ANo cycle would be detected
BCycle would be detected earlier
CCycle would still be detected at step 14
DDFS would not start
💡 Hint
Refer to the Graph State and cycle detection condition in execution_table step 14
Concept Snapshot
Cycle Detection in Undirected Graph using DFS
- Start DFS from unvisited nodes
- Mark nodes visited
- For each neighbor:
  - If not visited, DFS recursively
  - If visited and not parent, cycle found
- Stop when cycle detected or all nodes visited
Full Transcript
Cycle detection in an undirected graph uses depth-first search (DFS). We start DFS from any unvisited node and mark it visited. For each neighbor, if it is not visited, we recursively DFS on it. If the neighbor is visited and is not the parent node, it means a cycle exists. We track visited nodes and parent to avoid false positives. Once a cycle is detected, we stop the search. This method ensures we find cycles by detecting back edges in the DFS tree.