Pick the fix
Kahn's algorithm finishes with 3 of 5 tasks done. What does that tell you?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A The other two tasks are on a cycle or wait on one
Right: their in-degree never reaches zero because a loop of rules blocks them.
A task stays blocked only if something it waits on can never finish, and that only happens around a loop.
Why not the others?
- B The graph is disconnected. Disconnected parts are fine; each part's unblocked tasks start in the queue.
- C The queue was processed in the wrong order. Any order of ready tasks finishes everything when there is no cycle.
Steps come from running the Python solution.
Text version
No loop:
Nodes [0, 1, 2, 3, 4]; edges 0→1, 0→2, 1→3, 2→3, 3→4.
- Visit 0, in-degrees [0, 0, 0, 2, 1]. Take 0; frees 1, 2
- Visit 1, in-degrees [0, 0, 0, 1, 1]. Take 1
- Visit 2, in-degrees [0, 0, 0, 0, 1]. Take 2; frees 3
- Visit 3, in-degrees [0, 0, 0, 0, 0]. Take 3; frees 4
- Visit 4, in-degrees [0, 0, 0, 0, 0]. Take 4
Result: true
A loop between 1 and 2:
Nodes [0, 1, 2, 3]; edges 0→1, 1→2, 2→1, 2→3.
- Visit 0, in-degrees [0, 1, 1, 1]. Take 0
- Visit nothing. 3 nodes never reach in-degree 0: there is a cycle
Result: false