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 data structures are commonly used to detect cycles in a directed graph?
Depth-First Search (DFS) with recursion stack or colors (white, gray, black) is commonly used to detect cycles in directed graphs.
Click to reveal answer
intermediate
Explain the role of the recursion stack in DFS for cycle detection.
The recursion stack keeps track of vertices currently in the path. If during DFS, we visit a vertex already in the recursion stack, it means a cycle exists.
Click to reveal answer
intermediate
What is the difference between visited and recursion stack arrays in cycle detection?
The visited array marks all nodes that have been explored, while the recursion stack marks nodes currently in the active DFS path to detect back edges indicating cycles.
Click to reveal answer
intermediate
Why can't cycle detection in directed graphs be done by just checking visited nodes?
Because revisiting a visited node does not always mean a cycle; it must be in the current DFS path (recursion stack) to confirm a cycle in directed graphs.
Click to reveal answer
What does it mean if a node is found in the recursion stack during DFS?
AA cycle is detected
BThe node is fully processed
CThe node is unvisited
DThe graph is disconnected
✗ Incorrect
Finding a node in the recursion stack means we have a back edge, indicating a cycle.
Which traversal method is typically used for cycle detection in directed graphs?
ABreadth-First Search (BFS)
BDijkstra's Algorithm
CDepth-First Search (DFS)
DPrim's Algorithm
✗ Incorrect
DFS is used because it can track the recursion stack to detect cycles.
What does the 'visited' array represent in cycle detection?
ANodes currently in the recursion stack
BNodes that have been explored at least once
CNodes that are unreachable
DNodes with no outgoing edges
✗ Incorrect
The visited array marks nodes that have been explored to avoid repeated work.
If no node is found in the recursion stack during DFS, what can we conclude?
AThe graph has no cycles
BThe graph is disconnected
CThe graph contains a cycle
DThe graph is undirected
✗ Incorrect
No node in recursion stack means no back edge, so no cycle.
Which of these is a sign of a cycle in a directed graph during DFS?
AVisiting a node that is not visited
BVisiting a node with no incoming edges
CVisiting a node with no outgoing edges
DVisiting a node that is in recursion stack
✗ Incorrect
Visiting a node in recursion stack means a cycle is detected.
Describe how Depth-First Search (DFS) can be used to detect a cycle in a directed graph.
Think about tracking nodes in the current path.
You got /4 concepts.
Explain why simply checking if a node is visited is not enough to detect cycles in directed graphs.
Consider the difference between nodes visited before and nodes in the current path.
You got /4 concepts.
Practice
(1/5)
1. What does cycle detection in a directed graph help us find?
easy
A. Whether there is a path that starts and ends at the same node following edge directions
B. The shortest path between two nodes
C. The total number of nodes in the graph
D. The maximum degree of any node
Solution
Step 1: Understand cycle in directed graph
A cycle means a path where you can start from a node and follow 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 anywhere in the graph.
Final Answer:
Whether there is a path that starts and ends at the same node following edge directions -> Option A
Quick Check:
Cycle detection = find directed loops [OK]
Hint: Cycle means path back to start following edge directions [OK]
Common Mistakes:
Confusing cycle with shortest path
Thinking cycle ignores edge directions
Mixing cycle with node count
2. Which of the following is the correct way to declare a graph adjacency list in C++ for cycle detection?
easy
A. vector graph(100);
B. int graph[100][100];
C. map graph;
D. vector<int> graph[100];
Solution
Step 1: Understand adjacency list declaration
For cycle detection, adjacency list is often a vector of vectors to store edges from each node.
Step 2: Check each option
A: vector graph(100); - standard and flexible vector of vectors for adjacency list. B: adjacency matrix, not list. C: map, valid but less common and heavier for sequential nodes. D: array of vectors, works but fixed size and less modern than vector of vectors.
Final Answer:
vector graph(100); -> Option A
Quick Check:
Use vector of vectors for adjacency list [OK]
Hint: Use vector for flexible adjacency list [OK]
Common Mistakes:
Using adjacency matrix instead of list
Confusing map with vector
Using raw arrays limiting flexibility
3. What will be the output of the following code snippet detecting cycle in a directed graph?