Critical Path Length of a Pipeline DAG
Problem Statement
Compute the minimum total time to run a CI pipeline where independent stages run in parallel — the critical path (longest-duration path) through the dependency DAG.
Background
Each stage has a duration and depends on prior stages. A stage cannot start until all its dependencies finish. The earliest finish time of a stage is duration + max(earliest finish of its deps) (or just its duration if it has none). The pipeline's makespan is the maximum finish time over all stages.
Your Task
def critical_path(stages):
- stages: dict mapping name -> {"duration": number, "deps": [names]}.
- Return the makespan (number). Assume the DAG is acyclic.
Input Format
- stages (dict).
Output Format
- A number (total time).
Sample
print(critical_path({"a": {"duration": 3, "deps": []}, "b": {"duration": 4, "deps": ["a"]}}))
Output:
7
Example
print(critical_path({"a": {"duration": 3, "deps": []}, "b": {"duration": 4, "deps": ["a"]}}))7
- Identify the dependency chain: Stage "b" depends on stage "a", so "a" must finish before "b" can start.
- Calculate the earliest finish time for stage "a": Since it has no dependencies, its finish time is simply its duration, .
- Calculate the earliest finish time for stage "b": It waits for its dependency "a" to finish, so its start time is ; adding its own duration gives .
- Determine the pipeline makespan: This is the maximum finish time across all stages, which is .
- The final output is 7
Constraints
- finish(stage) = duration + max(finish(dep)) (0 if no deps).
- Makespan = max finish over all stages.
- DAG is acyclic; use memoization.