What is the main idea behind Topological Sort using DFS?
We visit each node and explore all its neighbors first. After visiting all neighbors, we add the node to the result. This way, nodes appear after all nodes they depend on.
Click to reveal answer
beginner
In Topological Sort using DFS, when do we add a node to the sorted list?
We add the node after visiting all its neighbors (i.e., when the DFS call for that node finishes). This ensures dependencies come first.
Click to reveal answer
intermediate
Why can't Topological Sort be applied to graphs with cycles?
Because cycles mean some nodes depend on each other in a loop, making it impossible to order them linearly without breaking dependencies.
Click to reveal answer
beginner
What data structure is commonly used to store the result of Topological Sort using DFS?
A stack or a list where nodes are added after visiting all neighbors. The final order is the reverse of the order nodes were added.
Click to reveal answer
intermediate
How does DFS help in detecting cycles during Topological Sort?
By tracking nodes currently in the recursion stack (visiting), if we visit a node already in this stack, it means a cycle exists.
Click to reveal answer
What is the first step in Topological Sort using DFS?
AReverse the graph edges
BAdd all nodes to the result list
CCheck if the graph is empty
DVisit an unvisited node and start DFS
✗ Incorrect
We start DFS from an unvisited node to explore its dependencies.
When do we add a node to the topological order in DFS?
AAfter visiting all neighbors
BBefore visiting neighbors
CWhen the node is first discovered
DWhen a cycle is detected
✗ Incorrect
We add the node after all its neighbors are visited to ensure dependencies come first.
What does a cycle in the graph mean for Topological Sort?
ASort is still possible
BSort is impossible
CSort order is reversed
DSort is faster
✗ Incorrect
Cycles prevent a linear ordering of nodes respecting dependencies.
Which data structure helps reverse the order of nodes after DFS?
AStack
BLinked list
CQueue
DHash map
✗ Incorrect
A stack stores nodes in reverse finishing order, which we reverse to get topological order.
How can DFS detect cycles during Topological Sort?
ABy counting edges
BBy checking if a node is visited twice
CBy checking if a node is in the current recursion stack
DBy sorting nodes alphabetically
✗ Incorrect
If a node is found in the current recursion stack, it means a cycle exists.
Explain step-by-step how Topological Sort using DFS works on a directed graph.
Think about visiting nodes and when to add them to the order.
You got /5 concepts.
Describe how cycle detection is integrated into Topological Sort using DFS.
Focus on how recursion stack helps find cycles.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using DFS in Topological Sort on a directed graph?
easy
A. To visit nodes in a way that ensures dependencies come before dependents
B. To find the shortest path between two nodes
C. To detect cycles in an undirected graph
D. To count the number of connected components
Solution
Step 1: Understand DFS role in Topological Sort
DFS visits nodes deeply, ensuring all dependencies of a node are visited before the node itself.
Step 2: Relate DFS order to dependencies
By pushing nodes to a stack after visiting all neighbors, we get an order where dependencies come before dependents.
Final Answer:
To visit nodes in a way that ensures dependencies come before dependents -> Option A
Quick Check:
DFS ensures dependency order = A [OK]
Hint: DFS post-order gives correct dependency order [OK]
Common Mistakes:
Confusing DFS with BFS for topological sort
Thinking topological sort finds shortest paths
Assuming DFS detects cycles in undirected graphs here
2. Which of the following Go code snippets correctly declares a recursive DFS function for topological sort?
easy
A. func dfs(node int, graph map[int][]int, visited map[int]bool, stack *[]int) { if visited[node] { return } visited[node] = true *stack = append(*stack, node) for _, neighbor := range graph[node] { dfs(neighbor, graph, visited, stack) } }
B. func dfs(node int, graph map[int][]int, visited map[int]bool, stack []int) { if visited[node] { return } visited[node] = true for _, neighbor := range graph[node] { dfs(neighbor, graph, visited, stack) } stack = append(stack, node) }
C. func dfs(node int, graph map[int][]int, visited map[int]bool, stack *[]int) { if visited[node] { return } for _, neighbor := range graph[node] { dfs(neighbor, graph, visited, stack) } visited[node] = true *stack = append(*stack, node) }
D. func dfs(node int, graph map[int][]int, visited map[int]bool, stack *[]int) { if !visited[node] { visited[node] = true for _, neighbor := range graph[node] { dfs(neighbor, graph, visited, stack) } *stack = append(*stack, node) } }
Solution
Step 1: Check visited condition and marking
func dfs(node int, graph map[int][]int, visited map[int]bool, stack *[]int) { if !visited[node] { visited[node] = true for _, neighbor := range graph[node] { dfs(neighbor, graph, visited, stack) } *stack = append(*stack, node) } } correctly checks if node is not visited before processing and marks it visited inside the condition.
Step 2: Confirm stack append and recursion
It recursively calls dfs on neighbors and appends node to stack after visiting neighbors, using pointer to modify stack.
Final Answer:
Option D code snippet -> Option D
Quick Check:
Correct DFS recursion and stack append = D [OK]
Hint: Use pointer for stack to append in recursive DFS [OK]
Common Mistakes:
Passing stack by value causing no changes
Marking visited after recursion causing repeated visits
Appending node before visiting neighbors
3. What is the output order of nodes after performing topological sort using DFS on this graph?
Step 3: Consider DFS starting order and visited nodes
Since DFS visits 5 first, then 4, the final topological order is the reverse of the post-order stack: [4, 5, 2, 3, 1, 0]. However, the question states DFS visits nodes in ascending order starting from 5, then 4. The actual post-order stack is [0,1,3,2,5,4], so reversed order is [4,5,2,3,1,0].
Final Answer:
[4, 5, 2, 3, 1, 0] -> Option B
Quick Check:
Post-order reverse = [4,5,2,3,1,0] [OK]
Hint: Reverse DFS post-order for topological sort output [OK]
Common Mistakes:
Not reversing the stack after DFS
Visiting nodes in wrong order
Ignoring already visited nodes
4. Identify the bug in this DFS-based topological sort code snippet: