21/32
Part 5 of 7 · Rounds and timesLearn and do
With enough workers, the done time is the longest chain in minutes, and one pass finds it
The pipeline is done at 16, the largest finish, where one worker would take 25. 16 is the longest chain measured in minutes, and that chain is the critical path, the chain that sets the done time.
The booksite's critical path method schedules jobs that must wait for other jobs the same way: each job starts at the length of its longest path from the start.
The obvious way to get a longest chain is to list every chain of prerequisites that ends at a task, and keep the longest. The number of chains can double with every step. Take a ladder: stages of two tasks, each task needing both tasks of the stage before, each one minute.
def ladder(stages):
tasks = [f"{side}{i}" for i in range(stages) for side in "ab"]
needs = [(f"{x}{i + 1}", f"{y}{i}") for i in range(stages - 1) for x in "ab" for y in "ab"]
return tasks, needs
def finish_by_chains(tasks, needs, minutes):
prerequisites = defaultdict(list)
for task, prerequisite in needs:
prerequisites[task].append(prerequisite)
def longest(task):
return minutes[task] + max((longest(p) for p in prerequisites[task]), default=0)
return {task: longest(task) for task in tasks}
Here is a ladder of 4 stages:
How many chains of prerequisites end at a3, counting a3 alone?
Pick an answer above to see the rest