A cycle in a directed graph is a path that starts and ends at the same vertex, following the direction of edges, without repeating any vertex.
Click to reveal answer
beginner
Which algorithm is commonly used to detect cycles in a directed graph?
Depth-First Search (DFS) with recursion stack tracking is commonly used to detect cycles in a directed graph.
Click to reveal answer
intermediate
In cycle detection using DFS, what does it mean if a node is found in the recursion stack?
If a node is found in the recursion stack during DFS, it means a cycle exists because the node is being visited again before the previous visit finished.
Click to reveal answer
intermediate
What are the two main data structures used in DFS-based cycle detection in directed graphs?
The two main data structures are: 1) a visited set to track visited nodes, and 2) a recursion stack to track nodes in the current DFS path.
Click to reveal answer
advanced
Why can't we use simple visited flags alone to detect cycles in directed graphs?
Because a node might be visited multiple times in different paths, simple visited flags can't distinguish between nodes in the current path and nodes visited earlier, so recursion stack is needed.
Click to reveal answer
What does a cycle in a directed graph imply?
AThere is a path from a node back to itself following edge directions
BThe graph is disconnected
CThe graph has no edges
DAll nodes have the same number of edges
✗ Incorrect
A cycle means a path exists from a node back to itself following the direction of edges.
Which data structure helps track nodes currently in the DFS path for cycle detection?
AQueue
BRecursion stack
CPriority queue
DHash map
✗ Incorrect
The recursion stack tracks nodes in the current DFS path to detect cycles.
If a node is visited and not in the recursion stack, what does it mean for cycle detection?
ACycle detected
BNode is isolated
CGraph is disconnected
DNo cycle through this node in current path
✗ Incorrect
If a node is visited but not in recursion stack, it means no cycle through this node in the current DFS path.
What is the time complexity of DFS-based cycle detection in a directed graph with V vertices and E edges?
AO(V + E)
BO(V^2)
CO(E^2)
DO(1)
✗ Incorrect
DFS visits each vertex and edge once, so time complexity is O(V + E).
Which Rust feature helps avoid stack overflow in deep DFS recursion?
AUnsafe code
BUsing global variables
CIterative DFS using explicit stack
DMacros
✗ Incorrect
Iterative DFS with an explicit stack avoids deep recursion and stack overflow.
Explain how DFS with recursion stack detects cycles in a directed graph.
Think about nodes currently being explored versus already explored.
You got /4 concepts.
Describe the difference between visited set and recursion stack in cycle detection.
One tracks all visits, the other tracks current path.
You got /3 concepts.
Practice
(1/5)
1. What does cycle detection in a directed graph help us find?
easy
A. If the graph has any isolated nodes
B. If the graph has equal number of nodes and edges
C. If the graph is fully connected
D. If there is a path that starts and ends at the same node following the direction of edges
Solution
Step 1: Understand cycle in directed graph
A cycle means starting at a node and following edges in their direction to come back to the same node.
Step 2: Identify what cycle detection finds
Cycle detection checks if such a path exists in the graph.
Final Answer:
If there is a path that starts and ends at the same node following the direction of edges -> Option D
Quick Check:
Cycle detection = path starting and ending at same node [OK]
Hint: Cycle means a path back to start following edge directions [OK]
Common Mistakes:
Confusing cycle with connectivity
Thinking cycle ignores edge directions
Assuming cycle means isolated nodes
2. Which Rust data structure is best to keep track of nodes currently in the recursion stack during DFS for cycle detection?
easy
A. HashSet<i32>
B. Vec<bool>
C. Vec<i32>
D. HashMap
Solution
Step 1: Identify purpose of recursion stack tracking
We need to mark nodes currently being explored to detect back edges indicating cycles.
Step 2: Choose data structure for boolean tracking
Vec<bool> is efficient to mark true/false for each node index during DFS recursion.
Final Answer:
Vec<bool> -> Option B
Quick Check:
Use Vec<bool> to track recursion stack nodes [OK]
Hint: Use boolean vector to mark recursion stack nodes [OK]
Common Mistakes:
Using HashMap when simple Vec<bool> suffices
Using Vec<i32> which stores values, not boolean flags
Using HashSet which is less efficient here
3. What will be the output of the following Rust code snippet detecting cycle in a directed graph?
fn main() {
let graph = vec![
vec![1], // 0 -> 1
vec![2], // 1 -> 2
vec![0], // 2 -> 0 forms a cycle
vec![4], // 3 -> 4
vec![] // 4 no outgoing edges
];
println!("{}", has_cycle(&graph));
}
fn has_cycle(graph: &Vec) -> bool {
let n = graph.len();
let mut visited = vec![false; n];
let mut rec_stack = vec![false; n];
for node in 0..n {
if !visited[node] && dfs(node, graph, &mut visited, &mut rec_stack) {
return true;
}
}
false
}
fn dfs(node: usize, graph: &Vec, visited: &mut Vec<bool>, rec_stack: &mut Vec<bool>) -> bool {
visited[node] = true;
rec_stack[node] = true;
for &neighbor in &graph[node] {
if !visited[neighbor] && dfs(neighbor, graph, visited, rec_stack) {
return true;
} else if rec_stack[neighbor] {
return true;
}
}
rec_stack[node] = false;
false
}
medium
A. true
B. false
C. Compilation error
D. Runtime panic
Solution
Step 1: Analyze graph edges for cycles
Nodes 0->1, 1->2, 2->0 form a cycle because 2 points back to 0.
Step 2: Understand DFS cycle detection logic
DFS marks nodes visited and tracks recursion stack. When it finds a neighbor in recursion stack, it returns true indicating cycle.
Final Answer:
true -> Option A
Quick Check:
Cycle exists in 0->1->2->0 path = true [OK]
Hint: Cycle if DFS finds neighbor in recursion stack [OK]
Common Mistakes:
Ignoring recursion stack check
Assuming no cycle because graph is small
Confusing visited with recursion stack
4. Identify the bug in this Rust DFS function for cycle detection in a directed graph: