What is the main idea behind Topological Sort using DFS?
We visit each node and explore all its descendants first. After visiting all descendants, we add the node to the result. This way, nodes appear after all nodes they depend on.
Click to reveal answer
beginner
Why can Topological Sort only be applied to Directed Acyclic Graphs (DAGs)?
Because if there is a cycle, there is no way to order nodes so that each node comes before nodes it points to. Cycles create contradictions in ordering.
Click to reveal answer
beginner
In DFS-based Topological Sort, when do we add a node to the sorted list?
We add the node after visiting all its neighbors (children). This ensures all dependencies come before the node.
Click to reveal answer
intermediate
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 descendants. The final order is the reverse of the finishing times.
Click to reveal answer
intermediate
How does DFS help detect cycles during Topological Sort?
By tracking nodes in the current recursion stack, if we visit a node already in this stack, it means a cycle exists.
Click to reveal answer
What type of graph is required for Topological Sort to work?
AUndirected Graph
BDirected Acyclic Graph (DAG)
CGraph with cycles
DAny graph
✗ Incorrect
Topological Sort requires a Directed Acyclic Graph (DAG) because cycles prevent a valid ordering.
In DFS-based Topological Sort, when is a node added to the output list?
AAfter visiting all its neighbors
BBefore visiting its neighbors
CWhen first discovered
DRandomly
✗ Incorrect
Nodes are added after visiting all neighbors to ensure dependencies come first.
What does a cycle in the graph indicate during Topological Sort?
AOrdering is possible
BGraph is disconnected
CGraph is undirected
DOrdering is impossible
✗ Incorrect
A cycle means no valid topological ordering exists.
Which data structure is often used to store nodes in DFS Topological Sort?
AQueue
BHash Map
CStack
DPriority Queue
✗ Incorrect
A stack stores nodes in reverse finishing order for topological sort.
How can DFS detect a cycle during Topological Sort?
ABy checking if a node is in the current recursion stack
BBy checking if a node is visited twice in total
CBy counting edges
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 DFS is used to perform Topological Sort on a graph.
Think about visiting nodes and when to add them to the output.
You got /5 concepts.
Describe how cycle detection works during DFS-based Topological Sort and why it is important.
Focus on the recursion stack and revisiting nodes.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using DFS in Topological Sort on a Directed Acyclic Graph (DAG)?
easy
A. To count the total number of nodes in the graph
B. To find the shortest path between two nodes
C. To detect cycles in an undirected graph
D. To visit nodes in a way that all dependencies come before the node
Solution
Step 1: Understand DFS role in Topological Sort
DFS visits nodes deeply, ensuring all nodes reachable from a node are processed first.
Step 2: Relation to dependencies in DAG
In Topological Sort, DFS ensures nodes are ordered so that all dependencies (edges) come before the node itself.
Final Answer:
To visit nodes in a way that all dependencies come before the node -> Option D
Quick Check:
DFS orders nodes respecting dependencies [OK]
Hint: DFS orders nodes so dependencies appear before dependents [OK]
Common Mistakes:
Confusing DFS with BFS for topological order
Thinking DFS finds shortest paths
Assuming DFS detects cycles in undirected graphs here
2. Which of the following is the correct way to mark a node as visited in a DFS-based topological sort in C++?
easy
A. visited(node) = true;
B. visited[node] = true;
C. visited[node] == true;
D. visited[node] != false;
Solution
Step 1: Understand C++ syntax for arrays or vectors
To mark a node visited, we assign true to visited[node] using = operator.
Step 2: Analyze options for correctness
visited[node] = true; uses correct syntax: visited[node] = true; Options A, C, D use wrong syntax or comparison operators.
Final Answer:
visited[node] = true; -> Option B
Quick Check:
Assignment uses =, not == or function call [OK]
Hint: Use = to assign true to visited[node] in C++ [OK]
Common Mistakes:
Using == instead of = for assignment
Using parentheses instead of brackets for indexing
Using != false which is confusing and incorrect here
3. What will be the output topological order of the following graph using DFS?
Edges:
0 -> 1
0 -> 2
1 -> 3
2 -> 3
medium
A. [0, 1, 2, 3]
B. [3, 1, 2, 0]
C. [0, 2, 1, 3]
D. [1, 0, 2, 3]
Solution
Step 1: Perform DFS starting from node 0
Visit 0, then 1 and 3, then backtrack and visit 2 and 3 (already visited). The order of finishing nodes is 3,1,2,0 reversed gives topological order.
Step 2: Determine topological order
Reversing finishing times gives [0, 2, 1, 3].
Final Answer:
[0, 2, 1, 3] -> Option C
Quick Check:
DFS finishing order reversed = [0,2,1,3] [OK]
Hint: Reverse DFS finishing order for topological sort output [OK]
Common Mistakes:
Not reversing the DFS finishing order
Visiting nodes in wrong order
Confusing edges direction
4. Identify the error in this DFS-based topological sort code snippet:
void dfs(int node, vector<bool> &visited, stack<int> &st, vector<int> adj[]) {
visited[node] = true;
for (auto x : adj[node]) {
if (!visited[x])
dfs(x, visited, st, adj);
}
st.push(node);
}
// Called for all nodes
for (int i = 0; i < n; i++) {
if (!visited[i])
dfs(i, visited, st, adj);
}
medium
A. No error; code correctly implements DFS topological sort
B. Visited array should be reset inside dfs
C. Stack should be replaced with queue
D. Nodes should be pushed to stack before recursive calls
Solution
Step 1: Analyze DFS function logic
DFS marks node visited, recursively visits neighbors, then pushes node to stack after all neighbors processed.
Step 2: Check correctness of stack usage and visited array
Stack is used to store nodes in finishing order, visited array is correctly used and not reset inside DFS.
Final Answer:
No error; code correctly implements DFS topological sort -> Option A
Quick Check:
DFS post-order push to stack is correct [OK]
Hint: Push node after recursion to stack for correct order [OK]
Using queue instead of stack for topological order
5. Given a graph with edges:
5 -> 2
5 -> 0
4 -> 0
4 -> 1
2 -> 3
3 -> 1
Which of the following is a valid topological sort using DFS?
hard
A. [5, 4, 2, 3, 1, 0]
B. [4, 5, 0, 2, 1, 3]
C. [0, 1, 2, 3, 4, 5]
D. [1, 3, 2, 0, 4, 5]
Solution
Step 1: Analyze dependencies from edges
5 -> 2, 5 -> 0 mean 5 before 2 and 0; 4 -> 0, 4 -> 1 mean 4 before 0 and 1; 2 -> 3 mean 2 before 3; 3 -> 1 mean 3 before 1. So 5 and 4 come before their successors.
Step 2: Validate options respecting dependencies
[5, 4, 2, 3, 1, 0] respects all edges: 5 before 2 and 0, 4 before 0 and 1, 2 before 3, 3 before 1. Others violate these orders.
Final Answer:
[5, 4, 2, 3, 1, 0] -> Option A
Quick Check:
All edges respected in order [OK]
Hint: Check all edges point forward in topological order [OK]
Common Mistakes:
Ignoring edge directions
Placing dependent nodes before dependencies
Assuming any order is valid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.