What is the main idea behind Topological Sort using DFS?
It visits nodes deeply first, then adds them to the order after all their dependencies are visited, ensuring that each node appears before nodes that depend on it.
Click to reveal answer
beginner
In Topological Sort using DFS, when do we add a node to the result list?
We add a node to the result list only after visiting all its neighbors (dependencies) recursively.
Click to reveal answer
intermediate
Why is Topological Sort only possible on Directed Acyclic Graphs (DAGs)?
Because cycles create circular dependencies, making it impossible to order nodes linearly without breaking dependency rules.
Click to reveal answer
beginner
What data structure is commonly used to keep track of visited nodes in DFS for Topological Sort?
A boolean array or set is used to mark nodes as visited to avoid revisiting and infinite loops.
Click to reveal answer
intermediate
How does the order of nodes in the final topological sort relate to the order they are added during DFS?
Nodes are added after visiting all dependencies, so the final order is the reverse of the order nodes are added during DFS.
Click to reveal answer
What type of graph is required for Topological Sort using DFS?
ADirected Acyclic Graph (DAG)
BUndirected Graph
CCyclic Graph
DWeighted Graph
✗ Incorrect
Topological Sort works only on Directed Acyclic Graphs because cycles prevent a linear ordering.
When performing DFS for Topological Sort, when do you add a node to the result list?
ABefore visiting its neighbors
BRandomly
CWhen first discovered
DAfter visiting all its neighbors
✗ Incorrect
Nodes are added after all their neighbors are visited to ensure dependencies come first.
What happens if the graph contains a cycle during Topological Sort using DFS?
AThe algorithm detects the cycle and fails
BThe graph is converted to a tree
CThe cycle is ignored
DThe algorithm completes successfully
✗ Incorrect
Cycles cause circular dependencies, so the algorithm must detect and handle them to avoid infinite loops.
Which data structure is typically used to store the final topological order in DFS?
AQueue
BStack or list reversed at the end
CHash Map
DPriority Queue
✗ Incorrect
Nodes are pushed onto a stack or list after visiting neighbors, then reversed to get the correct order.
What is the time complexity of Topological Sort using DFS for a graph with V vertices and E edges?
AO(E^2)
BO(V^2)
CO(V + E)
DO(log V)
✗ Incorrect
DFS visits each vertex and edge once, so the time complexity is linear in vertices plus edges.
Explain step-by-step how Topological Sort using DFS works on a Directed Acyclic Graph.
Think about visiting dependencies before the node itself.
You got /5 concepts.
Describe why cycles prevent Topological Sort and how DFS can detect them.
Consider how revisiting nodes during DFS signals a cycle.
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 count the number of connected components
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 ensures dependencies come before dependents
Solution
Step 1: Understand DFS role in Topological Sort
DFS visits nodes deeply, ensuring all nodes reachable from a node are visited before it is processed.
Step 2: Relate DFS order to dependencies
In Topological Sort, nodes are added after visiting all their dependencies, so dependencies come before dependents.
Final Answer:
To visit nodes in a way that ensures dependencies come before dependents -> Option D
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 sorting
Thinking DFS finds shortest paths
Mixing cycle detection with topological order
2. Which of the following is the correct way to mark a node as visited in a DFS-based topological sort in JavaScript?
easy
A. visited = node;
B. visited.push(node);
C. visited[node] = true;
D. visited[node] == true;
Solution
Step 1: Understand visited marking in DFS
Visited is usually an array or object where each node's visited status is stored as a boolean.
Step 2: Correct syntax for marking visited
Assigning true to visited[node] marks that node visited; using '==' is comparison, not assignment.
Final Answer:
visited[node] = true; -> Option C
Quick Check:
Assign true to visited[node] = A [OK]
Hint: Use assignment (=), not comparison (==), to mark visited [OK]
Common Mistakes:
Using '==' instead of '=' for assignment
Using push on visited array instead of index assignment
Overwriting visited variable instead of marking node
3. What will be the output of the following code snippet performing topological sort using DFS on the graph with edges: 5->2, 5->0, 4->0, 4->1, 2->3, 3->1?
const graph = {
5: [2, 0],
4: [0, 1],
2: [3],
3: [1],
0: [],
1: []
};
const visited = {};
const stack = [];
function dfs(node) {
visited[node] = true;
for (const neighbor of graph[node]) {
if (!visited[neighbor]) dfs(neighbor);
}
stack.push(node);
}
for (const node in graph) {
if (!visited[node]) dfs(node);
}
console.log(stack.reverse());
medium
A. [5, 4, 2, 3, 1, 0]
B. [4, 5, 0, 2, 3, 1]
C. [5, 4, 0, 2, 3, 1]
D. [4, 5, 2, 3, 1, 0]
Solution
Step 1: Perform DFS and track stack push order
DFS visits nodes deeply, pushing nodes after visiting neighbors. For node 5: visits 2, then 3, then 1, then 0; for node 4: visits 0 and 1. Stack push order is [1,3,2,0,5,4] reversed to [4,5,0,2,3,1].