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, allowing the algorithm to process nodes in the order they become free of dependencies.
Click to reveal answer
intermediate
What does it mean if after running Kahn's Algorithm, not all nodes are in the sorted list?
It means the graph has at least one cycle, so a topological sort is not possible.
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 the removal of a dependency.
Click to reveal answer
What data structure is primarily used in Kahn's Algorithm to track nodes with zero in-degree?
AQueue
BStack
CPriority Queue
DSet
✗ 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 Kahn's Algorithm?
AGraph is disconnected
BAlgorithm finishes normally
CAll nodes have zero in-degree
DTopological sort is not possible
✗ Incorrect
If a cycle exists, some nodes never reach zero in-degree, so topological sort cannot be completed.
In Kahn's Algorithm, what happens when a node is removed from the queue?
AIts neighbors are added to the queue immediately
BThe algorithm terminates
CIts neighbors' in-degree values are decreased by one
DThe node is reinserted into the queue
✗ Incorrect
Removing a node means its dependencies are resolved, so neighbors' in-degree values decrease.
Which traversal method does Kahn's Algorithm resemble?
ADepth-First Search (DFS)
BBreadth-First Search (BFS)
CPreorder Traversal
DPostorder Traversal
✗ Incorrect
Kahn's Algorithm uses a queue and processes nodes level by level, similar to BFS.
What is the initial step in Kahn's Algorithm?
AFind all nodes with zero in-degree and add them to the queue
BAdd all nodes to the queue
CRemove all edges from the graph
DSort nodes alphabetically
✗ Incorrect
The algorithm starts by identifying nodes with no incoming edges to begin processing.
Explain step-by-step how Kahn's Algorithm performs a topological sort on a directed graph.
Think about how dependencies are removed one by one.
You got /6 concepts.
Describe how Kahn's Algorithm detects cycles in a graph during topological sorting.
Focus on what happens if some nodes never become free of dependencies.
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 find the shortest path between two nodes
C. To detect cycles in an undirected graph
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 uv, vertex u comes before v in the ordering.
Step 2: Compare with other graph algorithms
Shortest path algorithms find paths, cycle detection checks for loops, and minimum spanning tree algorithms work on undirected graphs. Kahn's Algorithm specifically produces a topological order.
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 orders DAG nodes topologically [OK]
Common Mistakes:
Confusing topological sort with shortest path
Thinking it detects cycles in undirected graphs
Mixing it with minimum spanning tree algorithms
2. Which Rust data structure is best suited to keep track of nodes with zero incoming edges in Kahn's Algorithm?
easy
A. HashMap
B. VecDeque (double-ended queue)
C. BinaryHeap
D. LinkedList
Solution
Step 1: Identify the need for queue behavior
Kahn's Algorithm processes nodes with zero incoming edges in FIFO order, so a queue is needed to efficiently pop from front and push at back.
Step 2: Choose the Rust data structure
VecDeque provides efficient push and pop operations at both ends, making it ideal for queue operations. HashMap is for key-value storage, BinaryHeap is a priority queue, and LinkedList is less efficient in Rust.
Final Answer:
VecDeque (double-ended queue) -> Option B
Quick Check:
Queue for zero in-degree nodes = VecDeque [OK]
Hint: Use VecDeque for queue operations in Rust [OK]
Common Mistakes:
Using HashMap instead of a queue
Choosing BinaryHeap which is a priority queue
Using LinkedList which is inefficient in Rust
3. Given the following Rust code snippet implementing Kahn's Algorithm, what will be the printed topological order?
let edges = vec![(0, 1), (0, 2), (1, 3), (2, 3)];
let n = 4;
// Assume graph and in_degree are built correctly
// After running Kahn's Algorithm, the order vector is printed
println!("{:?}", order);
medium
A. [0, 1, 2, 3]
B. [3, 1, 2, 0]
C. [0, 2, 1, 3]
D. [1, 0, 2, 3]
Solution
Step 1: Analyze the graph edges and in-degree
Edges: 0->1, 0->2, 1->3, 2->3. Node 0 has in-degree 0, nodes 1 and 2 have in-degree 1, node 3 has in-degree 2.
Step 2: Perform Kahn's Algorithm BFS
Start with node 0 (in-degree 0). Remove it, reduce in-degree of 1 and 2 to 0. Add 1 and 2 to queue. Next, remove 1, then 2, then 3. Order is [0, 1, 2, 3]. However, since nodes 1 and 2 become zero in-degree simultaneously, the order of processing them can vary. If we process 2 before 1, the order is [0, 2, 1, 3], which is also valid.
Final Answer:
[0, 2, 1, 3] -> Option C
Quick Check:
Topological order respects edges = [0, 2, 1, 3] [OK]
Hint: Start with zero in-degree nodes and process neighbors [OK]
Common Mistakes:
Reversing order of nodes processed
Ignoring in-degree updates
Assuming multiple valid orders without checking edges
4. Identify the bug in this Rust snippet of Kahn's Algorithm:
for &node in &graph[node] {
in_degree[node] -= 1;
if in_degree[node] == 0 {
queue.push_back(node);
}
}
medium
A. Missing initialization of in_degree array
B. Should use 'queue.push_front' instead of 'push_back'
C. Queue should be a stack for correct order
D. Using 'node' as loop variable shadows outer 'node', causing wrong indexing
Solution
Step 1: Understand variable shadowing in the loop
The loop uses 'node' as the loop variable, which shadows the outer 'node' variable used to index graph. This causes confusion and incorrect indexing.
Step 2: Correct the variable naming
Use a different variable name like 'neighbor' in the loop to avoid shadowing and correctly access neighbors.
Final Answer:
Using 'node' as loop variable shadows outer 'node', causing wrong indexing -> Option D
Quick Check:
Variable shadowing causes bugs = Using 'node' as loop variable shadows outer 'node', causing wrong indexing [OK]
Hint: Avoid reusing variable names in nested loops [OK]
Common Mistakes:
Ignoring variable shadowing
Confusing queue with stack usage
Assuming in_degree initialization is the bug
5. You have a graph with edges: [(5, 2), (5, 0), (4, 0), (4, 1), (2, 3), (3, 1)]. Using Kahn's Algorithm, which of the following is a valid topological order?
Start with nodes 4 and 5 (in-degree 0). Remove 5: reduce in-degree of 2 and 0. Remove 4: reduce in-degree of 0 and 1. Then remove 2, 3, 1, 0 in order. One valid order is [5, 4, 2, 3, 1, 0].