A cycle is a path that starts and ends at the same vertex with at least one edge, and no edges or vertices are repeated except the starting and ending vertex.
Click to reveal answer
beginner
Which data structure is commonly used to detect cycles in an undirected graph using DFS?
A recursive Depth-First Search (DFS) with a parent tracking variable is used to detect cycles by checking if a visited vertex is encountered again that is not the parent.
Click to reveal answer
intermediate
Why do we need to track the parent node during DFS for cycle detection in undirected graphs?
Because in undirected graphs, edges are bidirectional, so visiting the parent node again does not mean a cycle. Tracking the parent helps ignore this edge and detect real cycles.
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
Explain the difference between cycle detection in directed and undirected graphs.
In undirected graphs, cycles are detected by checking visited nodes excluding the parent. In directed graphs, cycles are detected by tracking recursion stack to find back edges.
Click to reveal answer
What does it mean if during DFS in an undirected graph, you visit a vertex that is already visited and is not the parent?
AGraph is disconnected
BNo cycle, just a back edge to parent
CA cycle is detected
DGraph is a tree
✗ Incorrect
Visiting a previously visited vertex that is not the parent means a cycle exists.
Which of these is NOT required for cycle detection in an undirected graph using DFS?
ARecursion stack tracking
BParent tracking
CVisited array
DGraph adjacency list
✗ Incorrect
Recursion stack tracking is used in directed graphs, not undirected.
What is the initial state of the visited array before DFS starts?
AAll true
BDepends on graph size
CRandom values
DAll false
✗ Incorrect
All vertices are initially unvisited, so visited array is all false.
If an undirected graph has no cycles, what is it called?
AComplete graph
BTree
CDisconnected graph
DDirected graph
✗ Incorrect
A connected undirected graph with no cycles is called a tree.
What is the role of the parent parameter in the DFS function for 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 count the number of cycles
✗ Incorrect
Parent helps ignore the edge leading back to the immediate previous vertex.
Describe the step-by-step process of detecting a cycle in an undirected graph using DFS.
Think about how DFS explores neighbors and how parent helps avoid false positives.
You got /5 concepts.
Explain why cycle detection in undirected graphs requires different handling than in directed graphs.
Focus on edge direction and how it affects cycle detection logic.
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 all nodes are visited once, the graph has a cycle.
B. If we visit a node that is already visited and is not the parent, a cycle exists.
C. If the graph has more edges than nodes, it must have 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 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 B
Quick Check:
Cycle detection in undirected graph = check visited and parent [OK]
Hint: Check visited nodes excluding parent to find cycles [OK]
Common Mistakes:
Ignoring the parent node in cycle check
Assuming disconnected graph always has cycles
Confusing cycle detection with number of edges
2. Which of the following C++ function signatures is correct for a DFS-based cycle detection helper in an undirected graph?
easy
A. bool dfs(int node, int parent, vector& graph, vector<bool>& visited);
B. void dfs(int node, vector<int>& graph, vector<bool>& visited);
C. bool dfs(int node, vector<int>& graph, vector<int>& visited, int parent);
D. int dfs(int node, int parent, vector<int>& graph, vector<bool>& visited);
Solution
Step 1: Analyze function parameters for DFS cycle detection
We need current node, parent node, adjacency list (graph), and visited array.
Step 2: Check return type and parameter types
Return type should be bool to indicate cycle presence; graph is vector of vectors; visited is bool vector.
Final Answer:
bool dfs(int node, int parent, vector& graph, vector<bool>& visited); -> Option A
Quick Check:
Correct DFS signature includes parent and returns bool [OK]
Using wrong graph data type (vector<int> instead of vector)
Missing parent parameter
Wrong return type for cycle detection
3. What will be the output of the following code snippet detecting cycle in an undirected graph?
int main() {
vector graph = {{1,2}, {0,2}, {0,1,3}, {2}};
vector<bool> visited(4, false);
bool hasCycle = false;
function dfs = [&](int node, int parent) {
visited[node] = true;
for (int adj : graph[node]) {
if (!visited[adj]) {
if (dfs(adj, node)) return true;
} else if (adj != parent) {
return true;
}
}
return false;
};
for (int i = 0; i < 4; i++) {
if (!visited[i]) {
if (dfs(i, -1)) {
hasCycle = true;
break;
}
}
}
cout << (hasCycle ? "Cycle Found" : "No Cycle") << endl;
return 0;
}
medium
A. Runtime Error
B. No Cycle
C. Compilation Error
D. Cycle Found
Solution
Step 1: Analyze graph structure
Graph edges: 0-1, 0-2, 1-2, 2-3. Nodes 0,1,2 form a triangle (cycle).
Step 2: Trace DFS cycle detection
DFS visits nodes and finds back edge between 0 and 2 or 1 and 2, detecting cycle.
Final Answer:
Cycle Found -> Option D
Quick Check:
Triangle in graph means cycle detected [OK]
Hint: Triangle or more edges among nodes means cycle [OK]
Common Mistakes:
Ignoring back edge condition
Assuming no cycle if graph is connected
Confusing parent node with visited node
4. Identify the bug in the following cycle detection code for an undirected graph:
bool dfs(int node, int parent, vector& graph, vector<bool>& visited) {
visited[node] = true;
for (int adj : graph[node]) {
if (!visited[adj]) {
if (dfs(adj, node, graph, visited)) return true;
} else {
return true;
}
}
return false;
}
medium
A. It returns true even if adjacent node is the parent, causing false cycle detection.
B. It does not mark nodes as visited.
C. It uses wrong data types for graph and visited.
D. It never returns false.
Solution
Step 1: Review cycle detection condition
The else block returns true if adjacent node is visited, but does not check if it is the parent.
Step 2: Identify impact of missing parent check
Returning true for parent node causes false positive cycle detection.
Final Answer:
It returns true even if adjacent node is the parent, causing false cycle detection. -> Option A
Quick Check:
Check parent node before returning true for visited adjacency [OK]
Hint: Always exclude parent node when checking visited adjacency [OK]
Common Mistakes:
Not checking if visited node is parent
Returning true too early
Incorrect recursion parameters
5. Given a graph with 5 nodes and edges: 0-1, 1-2, 2-3, 3-4, 4-1, which modification to the DFS cycle detection algorithm will correctly identify the cycle and also return the nodes involved in the cycle?
hard
A. Remove the parent check and mark all visited nodes as cycle nodes.
B. Use BFS instead of DFS to detect cycles and print nodes.
C. Modify DFS to store the path stack and return the cycle path when a back edge is found.
D. Count edges and nodes; if edges >= nodes, print all nodes as cycle.
Solution
Step 1: Understand cycle detection with node tracking
DFS can track the current path using a stack or vector to record nodes visited in the current recursion.
Step 2: Modify DFS to return cycle path
When a back edge is found (visited node not parent), extract nodes from the path stack to get cycle nodes.
Final Answer:
Modify DFS to store the path stack and return the cycle path when a back edge is found. -> Option C
Quick Check:
Track path in DFS to identify cycle nodes [OK]
Hint: Use path stack in DFS to retrieve cycle nodes [OK]
Common Mistakes:
Using BFS which doesn't track path easily
Ignoring parent check causing false cycles
Using edge count alone to identify cycle nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.