4/32
Part 1 of 7 · The graphLearn
Two dicts hold the graph: what each task unblocks, and how many prerequisites it waits for
To run the tasks, the code needs the graph in a form it can walk quickly.
from collections import defaultdict
def build_graph(tasks, needs):
unblocks = defaultdict(list)
# every task, even one in no pair
waiting = dict.fromkeys(tasks, 0)
for task, prerequisite in needs:
# the arrow: prerequisite -> task
unblocks[prerequisite].append(task)
waiting[task] += 1
return unblocks, waiting
unblocks, waiting = build_graph(tasks, needs)
for task in tasks:
print(f"{task:8} unblocks {unblocks[task]}, waits for {waiting[task]}")
build_graph runs on the pipeline. Which tasks get a count of 0?
Pick one to see what it prints.
Pick an answer above to see the rest