Sub topic 15
Computing the Critical Path in Code
This chapter assumes you know tasks, durations, and predecessor links. You should also understand basic Python dictionaries, loops, and functions. The algorithm assumes a directed acyclic graph (DAG): every dependency points forward, and no task eventually depends on itself.
Represent the network
Store each task as a pair containing its duration and its predecessors. List predecessors before the tasks that depend on them. This ordering lets a simple loop perform the forward pass.
The main values produced by the calculation are:
| Value | Meaning |
|---|---|
| ES | Earliest start time |
| EF | Earliest finish time |
| LS | Latest start without delaying the project |
| LF | Latest finish without delaying the project |
| Total float | LS minus ES |
| Free float | Earliest successor start minus EF |
Implement both passes
The forward pass calculates earliest times. A task can start only after all predecessors finish, so its earliest start is the largest predecessor finish.
The backward pass starts at the project end. A task's latest finish is the smallest latest start among its successors. Using the smallest value matters because every successor must remain on time.
tasks = {
# name: (duration in days, predecessors)
# Predecessors appear earlier in this dictionary.
"A": (3, []),
"B": (2, ["A"]),
"C": (4, ["A"]),
"D": (3, ["B", "C"]),
}
# Forward pass
es, ef = {}, {}
for task, (duration, predecessors) in tasks.items():
es[task] = max((ef[p] for p in predecessors), default=0)
ef[task] = es[task] + duration
project_end = max(ef.values())
# Build successor links for the backward pass and free float
successors = {task: [] for task in tasks}
for task, (_, predecessors) in tasks.items():
for predecessor in predecessors:
successors[predecessor].append(task)
# Backward pass
ls, lf = {}, {}
for task in reversed(list(tasks)):
lf[task] = min(
(ls[successor] for successor in successors[task]),
default=project_end,
)
ls[task] = lf[task] - tasks[task][0]
total_float = {task: ls[task] - es[task] for task in tasks}
free_float = {
task: min(
(es[successor] for successor in successors[task]),
default=project_end,
) - ef[task]
for task in tasks
}
critical = [task for task in tasks if total_float[task] == 0]
print("Project duration:", project_end)
print("Total float:", total_float)
print("Free float:", free_float)
print("Critical tasks:", critical)The output is:
Project duration: 10
Total float: {'A': 0, 'B': 2, 'C': 0, 'D': 0}
Free float: {'A': 0, 'B': 2, 'C': 0, 'D': 0}
Critical tasks: ['A', 'C', 'D']Here, task B has two days of total float. It can start as late as day 3 and finish by day 5 without delaying D. Tasks A, C, and D have zero float, so they form the critical path. The minimum project duration is 10 days.
Validate the implementation
The dictionary order is convenient for teaching, but production code should not rely on input order. First perform a topological sort, which orders every task after its predecessors. A cycle prevents such an ordering and indicates an invalid schedule.
Also check that every predecessor name exists. Without this check, a missing link can produce a confusing lookup error or an incomplete network.
When several paths have the same length, the zero-float result can contain more than one critical path. Code that reconstructs only one longest path is therefore useful for illustration, but it does not replace float analysis.
Exercise
If task B's duration increases from 2 days to 4 days, will the project duration change?
Show answer
No. The A-B-D path becomes 10 days, equal to the A-C-D path. The project still lasts 10 days, but both paths become critical.