Challenge - 5 Problems
Topological Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Topological Sort on a Simple DAG
What is the output of the following C# code performing a topological sort using DFS on the given graph?
DSA C#
using System; using System.Collections.Generic; class Graph { private int V; private List<int>[] adj; public Graph(int v) { V = v; adj = new List<int>[v]; for (int i = 0; i < v; i++) { adj[i] = new List<int>(); } } public void AddEdge(int v, int w) { adj[v].Add(w); } private void DFS(int v, bool[] visited, Stack<int> stack) { visited[v] = true; foreach (int i in adj[v]) { if (!visited[i]) { DFS(i, visited, stack); } } stack.Push(v); } public void TopologicalSort() { Stack<int> stack = new Stack<int>(); bool[] visited = new bool[V]; for (int i = 0; i < V; i++) { if (!visited[i]) { DFS(i, visited, stack); } } while (stack.Count > 0) { Console.Write(stack.Pop() + " "); } } } class Program { static void Main() { Graph g = new Graph(6); g.AddEdge(5, 2); g.AddEdge(5, 0); g.AddEdge(4, 0); g.AddEdge(4, 1); g.AddEdge(2, 3); g.AddEdge(3, 1); g.TopologicalSort(); } }
Attempts:
2 left
💡 Hint
Remember that in topological sort using DFS, nodes are pushed to stack after visiting all their descendants.
✗ Incorrect
The DFS visits nodes starting from 5 and 4, pushing nodes to stack after visiting all reachable nodes. The correct topological order is 5 4 2 3 1 0.
🧠 Conceptual
intermediateUnderstanding the Role of Stack in DFS Topological Sort
In the DFS-based topological sort algorithm, why do we push the node onto the stack only after visiting all its neighbors?
Attempts:
2 left
💡 Hint
Think about the dependency relationship in a directed acyclic graph.
✗ Incorrect
Pushing nodes after visiting all neighbors ensures that all nodes that depend on the current node are placed before it in the final order, respecting dependencies.
❓ Predict Output
advancedDetecting Cycle Impact on Topological Sort Output
What will be the output or error when running this C# code that attempts a topological sort on a graph with a cycle?
DSA C#
using System; using System.Collections.Generic; class Graph { private int V; private List<int>[] adj; public Graph(int v) { V = v; adj = new List<int>[v]; for (int i = 0; i < v; i++) { adj[i] = new List<int>(); } } public void AddEdge(int v, int w) { adj[v].Add(w); } private bool DFS(int v, bool[] visited, bool[] recStack, Stack<int> stack) { visited[v] = true; recStack[v] = true; foreach (int i in adj[v]) { if (!visited[i]) { if (DFS(i, visited, recStack, stack)) return true; } else if (recStack[i]) { return true; // cycle detected } } recStack[v] = false; stack.Push(v); return false; } public void TopologicalSort() { Stack<int> stack = new Stack<int>(); bool[] visited = new bool[V]; bool[] recStack = new bool[V]; for (int i = 0; i < V; i++) { if (!visited[i]) { if (DFS(i, visited, recStack, stack)) { Console.WriteLine("Cycle detected, topological sort not possible"); return; } } } while (stack.Count > 0) { Console.Write(stack.Pop() + " "); } } } class Program { static void Main() { Graph g = new Graph(3); g.AddEdge(0, 1); g.AddEdge(1, 2); g.AddEdge(2, 0); // cycle here g.TopologicalSort(); } }
Attempts:
2 left
💡 Hint
Check how the code detects cycles using recursion stack.
✗ Incorrect
The code detects a cycle using the recursion stack and prints the message instead of producing a topological order.
🔧 Debug
advancedIdentify the Bug in DFS Topological Sort Implementation
What is the bug in this DFS-based topological sort code snippet that causes incorrect output?
DSA C#
private void DFS(int v, bool[] visited, Stack<int> stack) { visited[v] = true; foreach (int i in adj[v]) { if (!visited[i]) { DFS(i, visited, stack); } stack.Push(v); } }
Attempts:
2 left
💡 Hint
Consider when nodes should be pushed to the stack in DFS topological sort.
✗ Incorrect
Pushing the node inside the loop causes it to be pushed multiple times, leading to incorrect topological order.
🚀 Application
expertTopological Sort Output for Complex Graph
Given the following directed acyclic graph edges, what is the topological sort output using DFS?
Edges:
0 -> 1
0 -> 2
1 -> 3
2 -> 3
3 -> 4
4 -> 5
2 -> 5
DSA C#
using System; using System.Collections.Generic; class Graph { private int V; private List<int>[] adj; public Graph(int v) { V = v; adj = new List<int>[v]; for (int i = 0; i < v; i++) { adj[i] = new List<int>(); } } public void AddEdge(int v, int w) { adj[v].Add(w); } private void DFS(int v, bool[] visited, Stack<int> stack) { visited[v] = true; foreach (int i in adj[v]) { if (!visited[i]) { DFS(i, visited, stack); } } stack.Push(v); } public void TopologicalSort() { Stack<int> stack = new Stack<int>(); bool[] visited = new bool[V]; for (int i = 0; i < V; i++) { if (!visited[i]) { DFS(i, visited, stack); } } while (stack.Count > 0) { Console.Write(stack.Pop() + " "); } } } class Program { static void Main() { Graph g = new Graph(6); g.AddEdge(0, 1); g.AddEdge(0, 2); g.AddEdge(1, 3); g.AddEdge(2, 3); g.AddEdge(3, 4); g.AddEdge(4, 5); g.AddEdge(2, 5); g.TopologicalSort(); } }
Attempts:
2 left
💡 Hint
Trace DFS calls starting from node 0 and remember nodes are pushed after visiting all descendants.
✗ Incorrect
The DFS visits nodes in order 0, then 1 and 2, exploring their descendants. The correct topological order is 0 2 1 3 4 5.
