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 graph traversal methods can be used to detect cycles in an undirected graph?
Depth-First Search (DFS) and Breadth-First Search (BFS) can be used to detect cycles by checking if a visited vertex is encountered again through a different path.
Click to reveal answer
intermediate
In cycle detection using DFS, why do we keep track of the parent vertex?
To avoid falsely detecting a cycle when revisiting the immediate parent vertex, since the edge back to the parent is not 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
beginner
Explain the role of the 'visited' array in cycle detection for undirected graphs.
The 'visited' array keeps track of which vertices have been explored to avoid repeated visits and to detect if a vertex is revisited through a different path, indicating a cycle.
Click to reveal answer
What does detecting a cycle in an undirected graph mean?
ACounting the number of edges
BFinding the shortest path between two vertices
CChecking if the graph is connected
DFinding a path that starts and ends at the same vertex without repeating edges
✗ Incorrect
A cycle is a path that starts and ends at the same vertex without repeating edges.
Which data structure is commonly used to keep track of visited vertices during DFS?
ABoolean array
BStack
CPriority queue
DQueue
✗ Incorrect
A boolean array is used to mark vertices as visited or not during DFS.
Why do we pass the parent vertex in DFS cycle detection?
ATo avoid revisiting the parent and falsely detecting a cycle
BTo find the shortest path
CTo count the number of edges
DTo sort the vertices
✗ Incorrect
Passing the parent vertex helps avoid false cycle detection when revisiting the immediate parent.
What is the time complexity of cycle detection using DFS in an undirected graph?
AO(V^2)
BO(E^2)
CO(V + E)
DO(log V)
✗ Incorrect
DFS visits each vertex and edge once, so the time complexity is O(V + E).
If a visited vertex is found that is not the parent during DFS, what does it indicate?
AGraph is disconnected
BA cycle exists
CNo cycle
DGraph is directed
✗ Incorrect
Finding a visited vertex other than the parent means a cycle is detected.
Describe the steps to detect a cycle in an undirected graph using DFS.
Think about how DFS explores neighbors and tracks parents.
You got /5 concepts.
Explain why cycle detection in undirected graphs requires tracking the parent vertex during traversal.
Consider what happens when you revisit the vertex you came from.
You got /3 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 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 keeps track of 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 of the current node, 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 = check visited node not parent [OK]
Hint: Check visited nodes excluding parent during DFS [OK]
Common Mistakes:
Ignoring parent node in cycle check
Assuming disconnected graph always has cycle
Confusing cycle detection with edge count
2. Which of the following C# code snippets correctly initializes an adjacency list for an undirected graph with 3 nodes?
easy
A. var graph = new List<int>[3]; for(int i=0; i<3; i++) graph[i] = new List<int>();
B. var graph = new List<int>[3]; graph = new List<int>();
C. List<int> graph = new List<int>[3];
D. var graph = new List(); graph.Add(new List<int>());
Solution
Step 1: Understand adjacency list initialization
We need an array of lists, each list initialized separately to hold neighbors.
Step 2: Check code correctness
var graph = new List<int>[3]; for(int i=0; i<3; i++) graph[i] = new List<int>(); creates an array of List<int> and initializes each element with a new List<int>, which is correct.
Final Answer:
var graph = new List<int>[3]; for(int i=0; i<3; i++) graph[i] = new List<int>(); -> Option A
Quick Check:
Initialize array of lists with loop [OK]
Hint: Initialize each list in array separately [OK]
Common Mistakes:
Not initializing each list element
Using wrong data types for adjacency list
Assigning list directly to array variable
3. Consider the following C# code snippet for cycle detection in an undirected graph: