A cycle is a path that starts and ends at the same vertex, with all edges and vertices distinct except the first and last vertex.
Click to reveal answer
beginner
Which data structure is commonly used to detect cycles in an undirected graph?
Depth-First Search (DFS) is commonly used to detect cycles by checking if a visited vertex is encountered again and is not the parent of the current vertex.
Click to reveal answer
intermediate
In cycle detection using DFS, why do we keep track of the parent node?
To avoid falsely detecting a cycle when revisiting the immediate parent node, since the parent is connected to the current node by an edge.
Click to reveal answer
intermediate
What is the time complexity of cycle detection in an undirected graph using DFS?
The time complexity is O(V + E), where V is the number of vertices and E is the number of edges, because each vertex and edge is visited once.
Click to reveal answer
advanced
How does the Union-Find (Disjoint Set) data structure help in cycle detection?
Union-Find helps detect cycles by grouping connected vertices. If two vertices of an edge belong to the same group, adding that edge creates a cycle.
Click to reveal answer
What indicates a cycle during DFS in an undirected graph?
AVisiting a vertex for the first time
BNo edges left to explore
CVisiting the parent vertex again
DVisiting a vertex that is already visited and is not the parent
✗ Incorrect
A cycle is detected if DFS visits a vertex that has already been visited and is not the immediate parent.
What is the role of the 'parent' parameter in DFS cycle detection?
ATo avoid counting the edge back to the parent as a cycle
BTo mark the starting vertex
CTo store the depth of the vertex
DTo keep track of visited vertices
✗ Incorrect
The parent parameter helps avoid false cycle detection by ignoring the edge back to the parent.
Which of these is NOT a valid method for cycle detection in undirected graphs?
ATopological Sort
BBreadth-First Search (BFS) with parent tracking
CUnion-Find (Disjoint Set)
DDepth-First Search (DFS)
✗ Incorrect
Topological Sort is used for cycle detection in directed graphs, not undirected graphs.
What is the initial state of the 'visited' array in DFS cycle detection?
AAll vertices marked visited
BRandom vertices visited
CAll vertices marked unvisited
DOnly the root vertex visited
✗ Incorrect
Initially, all vertices are unvisited before DFS starts.
In Union-Find, what does it mean if two vertices have the same root?
AThey belong to different sets
BThey belong to the same set, indicating a cycle if connected by an edge
CThey are disconnected
DThey are isolated vertices
✗ Incorrect
Same root means vertices are in the same set; connecting them again forms a cycle.
Explain how DFS can be used to detect a cycle in an undirected graph.
Think about revisiting nodes and how parent helps avoid false positives.
You got /4 concepts.
Describe how the Union-Find data structure detects cycles in an undirected graph.
Focus on grouping vertices and checking if an edge connects vertices in the same group.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind detecting a cycle in an undirected graph using DFS?
easy
A. If we visit a node that is already visited and is not the parent, a cycle exists.
B. If the graph has more edges than nodes, it always has a cycle.
C. If all nodes are visited exactly once, there is a cycle.
D. If the graph is disconnected, it contains a cycle.
Solution
Step 1: Understand DFS traversal in undirected graph
DFS visits nodes and tracks the parent node to avoid false cycle detection.
Step 2: Detect cycle condition
If during DFS, a visited node is found that is not the parent, it means a back edge exists, indicating a cycle.
Final Answer:
If we visit a node that is already visited and is not the parent, a cycle exists. -> Option A
Quick Check:
Cycle detection = visit visited node not parent [OK]
Hint: Check visited nodes excluding parent during DFS for cycle [OK]
Common Mistakes:
Ignoring the parent node check causes false positives.
Assuming more edges than nodes always means cycle.
Confusing disconnected graph with cycle presence.
2. Which of the following TypeScript function signatures correctly represents a DFS function for cycle detection in an undirected graph?
easy
A. function dfs(node: number, parent: number, visited: boolean[], graph: number[][]): boolean
B. function dfs(node: string, graph: number[][]): void
C. function dfs(visited: boolean[], graph: number[][]): number
D. function dfs(node: number, graph: number[][]): boolean
Solution
Step 1: Identify required parameters for DFS cycle detection
We need current node, parent node, visited array, and graph adjacency list.
Step 2: Match function signature with parameters and return type
function dfs(node: number, parent: number, visited: boolean[], graph: number[][]): boolean has all parameters and returns boolean indicating cycle presence.
Final Answer:
function dfs(node: number, parent: number, visited: boolean[], graph: number[][]): boolean -> Option A
4. Identify the bug in the following TypeScript code for cycle detection in an undirected graph:
function dfs(node: number, parent: number, visited: boolean[], graph: number[][]): boolean {
visited[node] = true;
for (const neighbor of graph[node]) {
if (!visited[neighbor]) {
dfs(neighbor, node, visited, graph);
} else if (neighbor !== parent) {
return true;
}
}
return false;
}
medium
A. Visited array is not updated correctly.
B. Parent parameter is not used in the function.
C. Missing return statement for recursive dfs call result.
D. Graph adjacency list is not passed correctly.
Solution
Step 1: Check recursive DFS call handling
The recursive call dfs(neighbor, node, visited, graph) is made but its boolean result is ignored.
Step 2: Identify missing return for cycle detection
If recursive call returns true (cycle found), it should be returned immediately to propagate detection.
Final Answer:
Missing return statement for recursive dfs call result. -> Option C
Quick Check:
Return recursive DFS result to detect cycle [OK]
Hint: Always return recursive DFS result for cycle detection [OK]
Common Mistakes:
Ignoring return value of recursive DFS calls.
Not updating visited array before recursion.
Misusing parent parameter in cycle check.
5. Given an undirected graph represented as an adjacency list, how can you modify the cycle detection algorithm to also return the nodes forming the cycle?
hard
A. Mark nodes visited twice to identify cycle nodes.
B. Use BFS instead of DFS to find cycles and record nodes.
C. Count edges and nodes; if edges > nodes, return all nodes as cycle.
D. Track the path stack during DFS and return the cycle path when detected.
Solution
Step 1: Understand cycle detection with path tracking
Maintain a stack or list of nodes visited in current DFS path to track traversal.
Step 2: Return cycle nodes when back edge found
When a visited node (not parent) is found, extract nodes from stack forming the cycle path.
Final Answer:
Track the path stack during DFS and return the cycle path when detected. -> Option D
Quick Check:
Use DFS path stack to get cycle nodes [OK]
Hint: Use DFS path stack to capture cycle nodes [OK]
Common Mistakes:
Using BFS which is less natural for cycle path retrieval.
Assuming edge count alone identifies cycle nodes.
Marking nodes visited twice without path context.
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.