What is the main idea behind Kahn's Algorithm for topological sorting?
Kahn's Algorithm repeatedly removes nodes with zero incoming edges (in-degree zero) and adds them to the sorted list, updating in-degrees of connected nodes until all nodes are processed or a cycle is detected.
Click to reveal answer
beginner
In Kahn's Algorithm, what does a node with in-degree zero represent?
A node with in-degree zero has no dependencies and can be safely placed next in the topological order.
Click to reveal answer
intermediate
Why does Kahn's Algorithm use a queue data structure?
The queue stores nodes with in-degree zero to process them in order, ensuring nodes are added to the topological order only after all their dependencies are resolved.
Click to reveal answer
intermediate
What does it mean if after running Kahn's Algorithm, not all nodes are included in the topological order?
It means the graph contains a cycle, so a valid topological order does not exist.
Click to reveal answer
intermediate
How does Kahn's Algorithm update the in-degree of nodes during execution?
When a node is removed from the queue, the algorithm decreases the in-degree of all its neighbors by one, reflecting that one dependency has been resolved.
Click to reveal answer
What data structure is primarily used in Kahn's Algorithm to store nodes with zero in-degree?
AQueue
BStack
CSet
DPriority Queue
✗ Incorrect
Kahn's Algorithm uses a queue to process nodes with zero in-degree in a first-in-first-out manner.
What does the presence of a cycle in the graph imply for topological sorting using Kahn's Algorithm?
ASort will be reversed
BTopological sort is not possible
CSort will include duplicates
DSort will be partial
✗ Incorrect
If a cycle exists, Kahn's Algorithm cannot process all nodes, so a valid topological order does not exist.
What is the initial step in Kahn's Algorithm?
ASort nodes alphabetically
BAdd all nodes to the queue
CRemove all edges
DCalculate in-degree of all nodes
✗ Incorrect
The algorithm starts by calculating the in-degree of each node to identify nodes with zero in-degree.
When a node is removed from the queue in Kahn's Algorithm, what happens next?
ADecrease in-degree of its neighbors by one
BIncrease in-degree of its neighbors by one
CAdd all neighbors to the queue immediately
DRemove neighbors from the graph
✗ Incorrect
Removing a node means one dependency is resolved, so neighbors' in-degree is decreased by one.
Which of the following best describes the order produced by Kahn's Algorithm?
AA reversed order of input nodes
BA random ordering of nodes
CA linear ordering of nodes respecting dependencies
DAn ordering based on node values
✗ Incorrect
Kahn's Algorithm produces a linear order where each node appears after all its dependencies.
Explain step-by-step how Kahn's Algorithm performs topological sorting on a directed graph.
Think about how dependencies are resolved one by one.
You got /6 concepts.
Describe how Kahn's Algorithm detects cycles in a graph during topological sorting.
Consider what happens if some nodes never get in-degree zero.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of Kahn's Algorithm in graph theory?
easy
A. To find a topological ordering of a Directed Acyclic Graph (DAG)
B. To detect cycles in an undirected graph
C. To find the shortest path between two nodes
D. To calculate the minimum spanning tree
Solution
Step 1: Understand Kahn's Algorithm goal
Kahn's Algorithm is designed to produce a linear ordering of vertices in a Directed Acyclic Graph (DAG) such that for every directed edge u -> v, u comes before v in the ordering.
Step 2: Compare with other graph algorithms
Other options describe different algorithms: cycle detection, shortest path, and minimum spanning tree are unrelated to Kahn's Algorithm.
Final Answer:
To find a topological ordering of a Directed Acyclic Graph (DAG) -> Option A
Quick Check:
Topological sort = Kahn's Algorithm purpose [OK]
Hint: Kahn's Algorithm always finds order in DAGs [OK]
Common Mistakes:
Confusing topological sort with cycle detection
Thinking it works on undirected graphs
Mixing it up with shortest path algorithms
2. Which of the following Python code snippets correctly initializes the in-degree array for Kahn's Algorithm given a graph adjacency list graph with n nodes?
easy
A. in_degree = [0] * n
for u in range(n):
for v in graph[u]:
in_degree[v] += 1
B. in_degree = [0] * n
for v in range(n):
for u in graph[v]:
in_degree[v] += 1
C. in_degree = [1] * n
for u in range(n):
for v in graph[u]:
in_degree[v] += 1
D. in_degree = [0] * n
for u in graph:
for v in graph[u]:
in_degree[u] += 1
Solution
Step 1: Understand in-degree calculation
In-degree of a node is the count of edges coming into it. For each edge u -> v, increment in_degree[v].
Step 2: Analyze code snippets
in_degree = [0] * n
for u in range(n):
for v in graph[u]:
in_degree[v] += 1 correctly loops over each node u and its neighbors v, incrementing in_degree[v]. in_degree = [0] * n
for v in range(n):
for u in graph[v]:
in_degree[v] += 1 increments the source node incorrectly. in_degree = [1] * n
for u in range(n):
for v in graph[u]:
in_degree[v] += 1 initializes in_degree with 1s, which is wrong. in_degree = [0] * n
for u in graph:
for v in graph[u]:
in_degree[u] += 1 increments in_degree[u], which is incorrect.
Final Answer:
in_degree = [0] * n
for u in range(n):
for v in graph[u]:
in_degree[v] += 1 -> Option A
Quick Check:
Increment in_degree of destination node [OK]
Hint: Increment in-degree of target nodes, not source [OK]
Common Mistakes:
Incrementing in-degree of source node instead of target
Initializing in-degree with wrong default values
Swapping loop variables u and v
3. What is the output of the following Python code implementing Kahn's Algorithm on the graph with edges: 0->1, 0->2, 1->3, 2->3?
from collections import deque
graph = {0: [1, 2], 1: [3], 2: [3], 3: []}
n = 4
in_degree = [0]*n
for u in range(n):
for v in graph[u]:
in_degree[v] += 1
queue = deque([i for i in range(n) if in_degree[i] == 0])
topo_order = []
while queue:
node = queue.popleft()
topo_order.append(node)
for neighbor in graph[node]:
in_degree[neighbor] -= 1
if in_degree[neighbor] == 0:
queue.append(neighbor)
print(topo_order)
medium
A. [0, 1, 2, 3]
B. [3, 1, 2, 0]
C. [0, 2, 1, 3]
D. [1, 2, 0, 3]
Solution
Step 1: Calculate in-degree and initialize queue
In-degree: 0=0, 1=1, 2=1, 3=2. Queue starts with nodes having in-degree 0: [0].
Step 2: Process nodes in queue
Pop 0, append to topo_order: [0]. Decrement in-degree of 1 and 2 to 0, add both to queue: [1, 2]. Pop 1, append: [0,1]. Decrement in-degree of 3 to 1. Pop 2, append: [0,1,2]. Decrement in-degree of 3 to 0, add 3 to queue: [3]. Pop 3, append: [0,1,2,3].
Final Answer:
[0, 2, 1, 3] -> Option C
Quick Check:
Topological order = [0, 2, 1, 3] [OK]
Hint: Start with zero in-degree nodes, process neighbors reducing in-degree [OK]
Common Mistakes:
Ignoring order of nodes with zero in-degree
Not updating in-degree after popping nodes
Appending nodes before in-degree reaches zero
4. Identify the error in the following Kahn's Algorithm code snippet and choose the correct fix:
from collections import deque
graph = {0: [1], 1: [2], 2: [0]}
n = 3
in_degree = [0]*n
for u in range(n):
for v in graph[u]:
in_degree[u] += 1
queue = deque([i for i in range(n) if in_degree[i] == 0])
topo_order = []
while queue:
node = queue.popleft()
topo_order.append(node)
for neighbor in graph[node]:
in_degree[neighbor] -= 1
if in_degree[neighbor] == 0:
queue.append(neighbor)
print(topo_order)
medium
A. Initialize in_degree with ones instead of zeros
B. Change 'in_degree[u] += 1' to 'in_degree[v] += 1' to correctly count incoming edges
C. Replace deque with list for queue implementation
D. Remove the while loop to avoid infinite processing
Solution
Step 1: Identify incorrect in-degree update
The code increments in_degree[u] for edges u->v, but it should increment in_degree[v] because in-degree counts incoming edges to v.
Step 2: Understand impact on queue initialization
Incorrect in-degree causes wrong zero in-degree nodes, so queue may be empty or incorrect, breaking the algorithm.
Final Answer:
Change 'in_degree[u] += 1' to 'in_degree[v] += 1' to correctly count incoming edges -> Option B
Quick Check:
Increment in-degree of target node, not source [OK]
Hint: In-degree counts edges coming into a node, increment target node [OK]
Common Mistakes:
Incrementing in-degree of source node
Not detecting cycles due to wrong in-degree
Using wrong data structure for queue
5. Given a Directed Acyclic Graph (DAG) with nodes 0 to 5 and edges: 5->2, 5->0, 4->0, 4->1, 2->3, 3->1, which of the following is a valid topological order produced by Kahn's Algorithm?
Pop 4: order=[4], decrement in-degree of 0 to 1 and 1 to 1. Pop 5: order=[4,5], assuming graph[5]=[0,2], decrement 0 to 0 and append 0, decrement 2 to 0 and append 2: queue=[0,2]. Pop 0: order=[4,5,0]. Pop 2: order=[4,5,0,2], decrement 3 to 0, append 3. Pop 3: order=[4,5,0,2,3], decrement 1 to 0, append 1. Pop 1: order=[4,5,0,2,3,1].
Final Answer:
[4, 5, 0, 2, 3, 1] -> Option D
Quick Check:
Valid topological order respects edges [OK]
Hint: Start with zero in-degree nodes, remove edges, add new zero in-degree nodes [OK]
Common Mistakes:
Ignoring in-degree counts leading to invalid order
Placing nodes before their dependencies
Assuming any order is valid without checking edges
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.