A cycle in a directed graph is a path that starts and ends at the same vertex, following the direction of edges.
Click to reveal answer
beginner
Which data structure is commonly used to detect cycles in a directed graph using DFS?
A recursion stack (or call stack) is used along with a visited set to detect cycles during DFS traversal.
Click to reveal answer
intermediate
What does it mean if a node is found in the recursion stack during DFS?
It means a cycle is detected because the node is being visited again before the previous visit finished.
Click to reveal answer
intermediate
Why can't we use a simple visited set alone to detect cycles in directed graphs?
Because a node can be visited multiple times in different paths without forming a cycle; the recursion stack helps track the current path.
Click to reveal answer
advanced
Explain the role of three states: unvisited, visiting, and visited in cycle detection.
Unvisited means the node is not explored yet; visiting means the node is in the current DFS path (recursion stack); visited means the node and its descendants are fully explored. A cycle is detected if we reach a visiting node again.
Click to reveal answer
What indicates a cycle during DFS in a directed graph?
AFinding a node already in the recursion stack
BFinding a node not visited before
CFinding a node with no outgoing edges
DFinding a node with only one incoming edge
✗ Incorrect
A cycle is detected if a node is found in the recursion stack during DFS, meaning the path loops back.
Which of these is NOT a state used in cycle detection DFS?
AProcessed
BVisiting
CVisited
DUnvisited
✗ Incorrect
The common states are unvisited, visiting, and visited. 'Processed' is not a standard term here.
Why is a recursion stack needed in cycle detection for directed graphs?
ATo store all visited nodes
BTo count the number of edges
CTo track nodes in the current path
DTo sort nodes
✗ Incorrect
The recursion stack tracks nodes in the current DFS path to detect cycles.
If a node is marked visited but not in recursion stack, what does it mean?
ANode is part of a cycle
BNode is fully explored with no cycle found here
CNode is unvisited
DNode is currently being explored
✗ Incorrect
Visited nodes outside recursion stack are fully explored and safe from cycles in current path.
Which algorithm is commonly used for cycle detection in directed graphs?
ADijkstra's Algorithm
BBreadth-First Search (BFS)
CPrim's Algorithm
DDepth-First Search (DFS)
✗ Incorrect
DFS is commonly used for cycle detection in directed graphs.
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 and revisiting them.
You got /4 concepts.
Explain why a simple visited set is not enough to detect cycles in directed graphs and how recursion stack helps.
Consider difference between nodes visited before and nodes in current exploration.
You got /3 concepts.
Practice
(1/5)
1. What does cycle detection in a directed graph help us find?
easy
A. The total number of nodes in the graph
B. The shortest path between two nodes
C. The maximum degree of any node
D. Whether 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 from a node and following directed edges, you can return to the same node.
Step 2: Identify what cycle detection finds
Cycle detection checks if such a path exists in the graph.
Final Answer:
Whether there is a path that starts and ends at the same node following the direction of edges -> Option D
Quick Check:
Cycle detection = path returning to start node [OK]
Hint: Cycle means path loops back to start node following edges [OK]
Common Mistakes:
Confusing cycle with shortest path
Ignoring edge directions
Thinking cycle means any repeated node without direction
2. Which Python data structure is commonly used to keep track of nodes currently in the recursion stack during cycle detection in a directed graph?
easy
A. Set
B. List
C. Dictionary
D. Queue
Solution
Step 1: Identify recursion stack tracking
During DFS, nodes currently being explored are tracked to detect cycles.
Step 2: Choose data structure for fast membership check
Set allows O(1) check if a node is in recursion stack.
Final Answer:
Set -> Option A
Quick Check:
Recursion stack uses set for fast lookup [OK]
Hint: Use set for fast check if node is in recursion stack [OK]
Common Mistakes:
Using list which is slower for membership check
Using queue which is FIFO, not suitable here
Using dictionary without keys for membership
3. What will be the output of the following code snippet?
graph = {0: [1], 1: [2], 2: [0]}
visited = set()
rec_stack = set()
def dfs(node):
if node in rec_stack:
return True
if node in visited:
return False
visited.add(node)
rec_stack.add(node)
for neighbor in graph.get(node, []):
if dfs(neighbor):
return True
rec_stack.remove(node)
return False
print(dfs(0))
medium
A. True
B. False
C. None
D. Error
Solution
Step 1: Trace DFS calls starting from node 0
Node 0 calls dfs(1), which calls dfs(2), which calls dfs(0) again.
Step 2: Detect cycle when node 0 is in recursion stack
When dfs(0) is called second time, node 0 is in rec_stack, so returns True.
Final Answer:
True -> Option A
Quick Check:
Cycle detected = True [OK]
Hint: Cycle if node revisited in recursion stack returns True [OK]
Common Mistakes:
Ignoring recursion stack check
Confusing visited with recursion stack
Assuming no cycle returns True
4. Find the bug in this cycle detection code snippet for a directed graph:
def dfs(node, graph, visited, rec_stack):
visited.add(node)
rec_stack.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs(neighbor, graph, visited, rec_stack)
elif neighbor in rec_stack:
return True
rec_stack.remove(node)
return False
medium
A. Should use list instead of set for rec_stack
B. Missing return value when dfs(neighbor) is called recursively
C. Visited set should be cleared after recursion
D. Graph keys should be integers only
Solution
Step 1: Check recursive call return usage
The recursive call dfs(neighbor, ...) is made but its return value is ignored.
Step 2: Fix by returning True if recursive call finds cycle
Should return True immediately if dfs(neighbor, ...) returns True.
Final Answer:
Missing return value when dfs(neighbor) is called recursively -> Option B
Quick Check:
Return True on recursive cycle detection [OK]
Hint: Return True immediately when recursive call detects cycle [OK]
Common Mistakes:
Ignoring recursive call return value
Using wrong data structure for rec_stack
Clearing visited incorrectly
5. Given a directed graph represented as adjacency list: