8/32
Part 2 of 7 · Kahn's algorithmLearn and do
Kahn's algorithm queues a task the moment its count reaches 0
The rescan finds each ready task by searching again. Kahn's algorithm never searches: it notices the moment a task becomes ready. Kahn's algorithm keeps the answer up to date instead of working it out again each time:
- Count every task's prerequisites.
build_graphandgraph_from_configalready do. - Queue every task whose count is 0. The queue is a
collections.deque, so taking the oldest task withpopleft()is . - Take the oldest task off the queue and put it in the order. Take one off the count of every task it unblocks. A task whose count reaches 0 goes on the queue.
- Repeat until the queue is empty. The order is the answer.
Kahn's pops compile before assets. Right after compile is popped, what is package's count?
Pick an answer above to see the rest