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 by the edge we came from.
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 checking if two vertices belong to the same set before union; if yes, adding an edge creates a cycle.
Click to reveal answer
What indicates a cycle during DFS traversal in an undirected graph?
AVisiting a vertex that is already visited and is not the parent
BVisiting a vertex for the first time
CReaching a vertex with no neighbors
DVisiting the parent vertex again
✗ Incorrect
A cycle is detected if a visited vertex is found again during DFS and it is not the parent of the current vertex.
Which of these is NOT a valid method to detect cycles in an undirected graph?
ABreadth-First Search with parent tracking
BSorting vertices by degree
CUnion-Find (Disjoint Set) data structure
DDepth-First Search with parent tracking
✗ Incorrect
Sorting vertices by degree does not help detect cycles in an undirected graph.
What is the role of the 'parent' parameter in DFS cycle detection?
ATo avoid counting the edge back to the immediate predecessor as a cycle
BTo mark the starting vertex
CTo store the depth of the vertex
DTo count the number of cycles
✗ Incorrect
The parent parameter helps avoid false positives by ignoring the edge back to the immediate predecessor.
What is the initial step in Union-Find cycle detection when adding an edge?
ASort the vertices
BAdd the edge without checks
CCheck if both vertices belong to the same set
DRemove one vertex
✗ Incorrect
Before adding an edge, Union-Find checks if both vertices are in the same set to detect a cycle.
What is the output after detecting a cycle in an undirected graph?
ANumber of edges
BList of all vertices
CGraph with no edges
DTrue or indication that a cycle exists
✗ Incorrect
Cycle detection returns a boolean or indication that a cycle exists in the graph.
Explain how Depth-First Search can be used to detect a cycle in an undirected graph.
Think about revisiting nodes and how parent helps.
You got /4 concepts.
Describe how the Union-Find data structure detects cycles when edges are added.
Focus on sets and merging.
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 all nodes are visited exactly once, there is a cycle.
C. If the graph has more edges than nodes, it always has 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 keeps track of the parent node to avoid false cycle detection.
Step 2: Detect cycle condition
If a visited node is found again and it is not the parent, it means a cycle exists.
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 in undirected graph = visit visited node not parent [OK]
Hint: Check visited nodes excluding parent during DFS [OK]
Common Mistakes:
Ignoring parent node when checking visited nodes
Assuming disconnected graph always has cycles
Confusing cycle detection with number of edges
2. Which of the following Go code snippets correctly initializes an adjacency list for an undirected graph with 3 nodes?