Challenge - 5 Problems
Kahn's Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kahn's Algorithm on a Simple DAG
What is the printed topological order of the graph after running Kahn's Algorithm BFS?
DSA Java
import java.util.*; public class Main { public static void kahnTopologicalSort(int vertices, List<List<Integer>> graph) { int[] indegree = new int[vertices]; for (int i = 0; i < vertices; i++) { for (int node : graph.get(i)) { indegree[node]++; } } Queue<Integer> queue = new LinkedList<>(); for (int i = 0; i < vertices; i++) { if (indegree[i] == 0) { queue.add(i); } } List<Integer> topoOrder = new ArrayList<>(); while (!queue.isEmpty()) { int current = queue.poll(); topoOrder.add(current); for (int neighbor : graph.get(current)) { indegree[neighbor]--; if (indegree[neighbor] == 0) { queue.add(neighbor); } } } if (topoOrder.size() != vertices) { System.out.println("Cycle detected, no topological order exists."); } else { for (int node : topoOrder) { System.out.print(node + " -> "); } System.out.println("null"); } } public static void main(String[] args) { int vertices = 4; List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < vertices; i++) { graph.add(new ArrayList<>()); } graph.get(0).add(1); graph.get(0).add(2); graph.get(1).add(2); graph.get(2).add(3); kahnTopologicalSort(vertices, graph); } }
Attempts:
2 left
💡 Hint
Start with nodes having zero incoming edges and remove edges as you process nodes.
✗ Incorrect
Node 0 has no incoming edges, so it starts. Then 1 and 2 become zero indegree in order, followed by 3. The order is 0 -> 1 -> 2 -> 3 -> null.
❓ Predict Output
intermediateDetecting Cycle with Kahn's Algorithm
What will be the output when Kahn's Algorithm BFS is run on this graph with a cycle?
DSA Java
import java.util.*; public class Main { public static void kahnTopologicalSort(int vertices, List<List<Integer>> graph) { int[] indegree = new int[vertices]; for (int i = 0; i < vertices; i++) { for (int node : graph.get(i)) { indegree[node]++; } } Queue<Integer> queue = new LinkedList<>(); for (int i = 0; i < vertices; i++) { if (indegree[i] == 0) { queue.add(i); } } List<Integer> topoOrder = new ArrayList<>(); while (!queue.isEmpty()) { int current = queue.poll(); topoOrder.add(current); for (int neighbor : graph.get(current)) { indegree[neighbor]--; if (indegree[neighbor] == 0) { queue.add(neighbor); } } } if (topoOrder.size() != vertices) { System.out.println("Cycle detected, no topological order exists."); } else { for (int node : topoOrder) { System.out.print(node + " -> "); } System.out.println("null"); } } public static void main(String[] args) { int vertices = 3; List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < vertices; i++) { graph.add(new ArrayList<>()); } graph.get(0).add(1); graph.get(1).add(2); graph.get(2).add(0); // cycle edge kahnTopologicalSort(vertices, graph); } }
Attempts:
2 left
💡 Hint
If the number of nodes processed is less than total nodes, a cycle exists.
✗ Incorrect
Because of the cycle 0->1->2->0, no node has zero indegree initially, so the queue is empty and no nodes are processed. The algorithm detects a cycle.
🧠 Conceptual
advancedUnderstanding Indegree in Kahn's Algorithm
In Kahn's Algorithm BFS for topological sorting, what does the 'indegree' array represent?
Attempts:
2 left
💡 Hint
Think about which nodes can be processed first in topological order.
✗ Incorrect
Indegree counts how many edges come into a node. Nodes with zero indegree have no dependencies and can be processed first.
❓ Predict Output
advancedOutput of Kahn's Algorithm on a Graph with Multiple Valid Orders
What is the output of the following code running Kahn's Algorithm BFS on this graph?
DSA Java
import java.util.*; public class Main { public static void kahnTopologicalSort(int vertices, List<List<Integer>> graph) { int[] indegree = new int[vertices]; for (int i = 0; i < vertices; i++) { for (int node : graph.get(i)) { indegree[node]++; } } Queue<Integer> queue = new LinkedList<>(); for (int i = 0; i < vertices; i++) { if (indegree[i] == 0) { queue.add(i); } } List<Integer> topoOrder = new ArrayList<>(); while (!queue.isEmpty()) { int current = queue.poll(); topoOrder.add(current); for (int neighbor : graph.get(current)) { indegree[neighbor]--; if (indegree[neighbor] == 0) { queue.add(neighbor); } } } if (topoOrder.size() != vertices) { System.out.println("Cycle detected, no topological order exists."); } else { for (int node : topoOrder) { System.out.print(node + " -> "); } System.out.println("null"); } } public static void main(String[] args) { int vertices = 5; List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < vertices; i++) { graph.add(new ArrayList<>()); } graph.get(0).add(2); graph.get(1).add(2); graph.get(1).add(3); graph.get(3).add(4); kahnTopologicalSort(vertices, graph); } }
Attempts:
2 left
💡 Hint
Nodes 0 and 1 have zero indegree initially and can be processed in any order, but queue order affects output.
✗ Incorrect
Queue adds nodes with zero indegree in order 0 then 1, so output starts with 0 then 1. Then 2, 3, 4 follow.
🧠 Conceptual
expertWhy Kahn's Algorithm Uses a Queue
Why does Kahn's Algorithm for topological sorting use a queue data structure to process nodes?
Attempts:
2 left
💡 Hint
Think about how dependencies are resolved layer by layer.
✗ Incorrect
The queue helps process nodes with zero indegree first, then their neighbors, ensuring dependencies are respected in BFS order.
