A cycle is a path that starts and ends at the same vertex with no repeated edges or vertices (except the start/end 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 check if the visited vertex is not the parent?
Because in an undirected graph, the edge to the parent is always visited, so revisiting the parent does not mean a cycle. We look for visited vertices other than the parent to confirm a cycle.
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 keeps track of connected components. When adding an edge, if both vertices belong to the same set, a cycle is detected. Otherwise, the sets are united.
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
CVisiting the parent vertex again
DNo edges left to explore
✗ 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 the following is NOT true about cycle detection in undirected graphs?
ADFS can be used to detect cycles
BA cycle can be detected by checking only the degree of vertices
CUnion-Find can detect cycles efficiently
DA cycle must include at least one vertex repeated twice
✗ Incorrect
Checking only the degree of vertices is not sufficient to detect cycles in undirected graphs.
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 count the number of edges
DTo store the depth of the vertex
✗ Incorrect
The parent parameter helps ignore the edge leading back to the parent vertex, which is not a cycle.
What is the worst-case time complexity of cycle detection using Union-Find?
AO(V + E)
BO(E log V)
CO(V^2)
DO(E α(V)) where α is the inverse Ackermann function
✗ Incorrect
Union-Find operations run in almost constant time, O(α(V)), making total complexity O(E α(V)) for E edges.
If a graph has no cycles, what kind of graph is it called?
ADisconnected graph
BComplete graph
CTree
DDirected graph
✗ Incorrect
A connected graph with no cycles is called a tree.
Explain how DFS can be used to detect a cycle in an undirected graph.
Think about revisiting vertices during traversal.
You got /4 concepts.
Describe how the Union-Find data structure detects cycles when edges are added.
Focus on connected components and set membership.
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 the graph is disconnected, it contains a cycle.
B. If all nodes are visited exactly once, the graph has a cycle.
C. If we visit a node that is already visited and is not the parent, a cycle exists.
D. If the graph has more edges than nodes, it always has 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 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 C
Quick Check:
Cycle detection in undirected graph = check visited node not parent [OK]
Hint: Check visited neighbors excluding parent to find cycles [OK]
Common Mistakes:
Ignoring the parent node in cycle check
Assuming disconnected graph always has cycles
Confusing edge count with cycle presence
2. Which of the following is the correct function signature in C for DFS used in cycle detection in an undirected graph?
easy
A. bool dfs(int node, int parent, int visited[], int graph[][100], int n)
B. int dfs(int node, int visited[], int graph[][100])
C. void dfs(int node, int graph[][100], int visited[])
D. int dfs(int node, int parent, int graph[][100])
Solution
Step 1: Identify parameters needed for DFS cycle detection
We need current node, parent node, visited array, adjacency matrix, and number of nodes.
Step 2: Match function signature with parameters
bool dfs(int node, int parent, int visited[], int graph[][100], int n) includes all required parameters with correct types and order.
Final Answer:
bool dfs(int node, int parent, int visited[], int graph[][100], int n) -> Option A
Quick Check:
DFS signature includes node, parent, visited, graph, size [OK]
Hint: Include parent parameter to avoid false cycle detection [OK]
Common Mistakes:
Omitting parent parameter
Wrong return type for DFS
Missing graph size parameter
3. Consider the following adjacency matrix for an undirected graph with 4 nodes:
Starting at 0, DFS visits 1, then 2 and 3. Node 1 connects back to 0 (parent), no cycle there. No back edge to visited non-parent node is found, so no cycle.
Final Answer:
No cycle detected -> Option B
Quick Check:
Graph edges form a tree structure = No cycle [OK]
Hint: Trace DFS and check visited nodes excluding parent [OK]
Common Mistakes:
Assuming any connected graph has cycle
Ignoring parent node in cycle check
Confusing adjacency matrix indexing
4. The following C code snippet is intended to detect a cycle in an undirected graph using DFS. What is the bug?
bool dfs(int node, int parent, int visited[], int graph[][100], int n) {
visited[node] = 1;
for (int i = 0; i < n; i++) {
if (graph[node][i] == 1) {
if (!visited[i]) {
if (dfs(i, node, visited, graph, n))
return true;
} else if (i != parent) {
return true;
}
}
}
return false;
}
medium
A. The visited array is not initialized before calling dfs.
B. The parent parameter is not passed correctly in recursive calls.
C. The graph adjacency matrix is accessed out of bounds.
D. The function should return void, not bool.
Solution
Step 1: Check initialization of visited array
The code snippet does not show visited array initialization, which is required to mark nodes unvisited before DFS.
Step 2: Verify other parameters and logic
Parent is passed correctly, adjacency matrix access is within bounds, and return type bool is appropriate for cycle detection.
Final Answer:
The visited array is not initialized before calling dfs. -> Option A
Quick Check:
Initialize visited[] before DFS to avoid incorrect results [OK]
Hint: Always initialize visited array to zero before DFS [OK]
Common Mistakes:
Forgetting to initialize visited array
Passing wrong parent in recursion
Misusing return type for DFS
5. You have an undirected graph represented by adjacency lists:
0: 1, 2
1: 0, 3
2: 0, 3
3: 1, 2
Using cycle detection with DFS, which nodes form the cycle and why?
hard
A. Nodes 0, 1, 3 form a cycle because 0 connects to 1 and 1 connects to 3.
B. Nodes 0, 2, 3 form a cycle because 0 connects to 2 and 2 connects to 3 which connects back to 0.
C. Nodes 1, 2, 3 form a cycle because 1 connects to 3 and 2 connects to 3.
D. Nodes 0, 1, 2, 3 form a cycle because they form a closed loop through all edges.
Solution
Step 1: Analyze adjacency lists for cycles
Edges: 0-1, 0-2, 1-3, 2-3. These edges connect nodes forming a square shape.
Step 2: Identify cycle involving all nodes
Starting at 0, path 0->1->3->2->0 forms a closed loop including all four nodes, indicating a cycle involving nodes 0,1,2,3.
Final Answer:
Nodes 0, 1, 2, 3 form a cycle because they form a closed loop through all edges. -> Option D
Quick Check:
Cycle includes all nodes forming closed loop [OK]
Hint: Look for closed loops connecting all nodes [OK]
Common Mistakes:
Identifying partial cycles ignoring full loop
Confusing connected nodes with cycle nodes
Missing edges that complete the cycle
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.