Sub topic 7
Calculate start and finish dates with a forward pass
This chapter assumes you can already write a task list with durations and finish-to-start dependencies (task B cannot start until task A finishes). It also assumes you can read basic Python. The forward pass is the step that turns those inputs into dates. The Gantt chart needs it, and so does the critical path in the next chapter.
What the forward pass computes
A forward pass walks through the project from the first task to the last. For each task it computes two numbers:
- Earliest start (ES): the first moment the task can begin, given its predecessors.
- Earliest finish (EF): ES plus the task's duration.
Two rules give you everything:
- A task with no predecessors starts at the project start, which is day 0.
- A task with predecessors starts when its latest predecessor finishes:
ES = max(EF of every predecessor).
The max is the whole idea. If Integration needs both Frontend and Backend, it waits for whichever finishes last. The faster predecessor does not matter for Integration's start date.
A worked example
Take five tasks with durations in working days:
| Task | Duration | Predecessors |
|---|---|---|
| Wireframes | 5 | none |
| Mockups | 7 | Wireframes |
| Backend | 12 | Wireframes |
| Frontend | 10 | Mockups |
| Integration | 4 | Frontend, Backend |
Work through it in order:
- Wireframes has no predecessors, so ES = 0 and EF = 0 + 5 = 5.
- Mockups and Backend both depend only on Wireframes, so both have ES = 5. Mockups finishes at 5 + 7 = 12. Backend finishes at 5 + 12 = 17.
- Frontend depends on Mockups, so ES = 12 and EF = 22.
- Integration depends on Frontend (EF 22) and Backend (EF 17). The maximum is 22, so ES = 22 and EF = 26.
The project takes 26 working days. Backend finishes five days before Integration needs it. That gap is slack, and the chapter on slack covers it. The chain Wireframes, Mockups, Frontend, Integration has no gaps, and it sets the project length.
The finish-date convention
Pick one convention and keep it. This tutorial treats a task as occupying the half-open interval from ES up to, but not including, EF. Wireframes runs from 0 to 5, so it occupies days 0, 1, 2, 3 and 4. Mockups starts at 5 and does not overlap it.
This convention has two benefits:
- Chained tasks line up with no off-by-one correction: the next task's ES equals the previous task's EF.
- A milestone, which has zero duration, has ES equal to EF and occupies no time.
Calendar tools often display the inclusive last day instead. Wireframes would show as ending on day 4, which is Friday of week one. If your numbers disagree with a tool's by one day, this is almost always why.
Implementing the forward pass
The simplest version loops over the tasks once. It works only if every task appears after its predecessors:
tasks = {
"Wireframes": (5, []),
"Mockups": (7, ["Wireframes"]),
"Backend": (12, ["Wireframes"]),
"Frontend": (10, ["Mockups"]),
"Integration": (4, ["Frontend", "Backend"]),
}
start, finish = {}, {}
for name, (duration, preds) in tasks.items():
start[name] = max((finish[p] for p in preds), default=0)
finish[name] = start[name] + duration
print(max(finish.values())) # 26The default=0 argument handles tasks with no predecessors, because max of an empty sequence would otherwise raise an error. The project length is the largest finish value, not the last task's finish. A task that is listed last is not necessarily the one that finishes last.
Remove the ordering requirement
Real task lists are rarely sorted by dependency. If you list Integration first, the loop above fails with a KeyError, because finish["Frontend"] does not exist yet. The standard library can sort the tasks for you with graphlib, available in Python 3.9 and later. It also detects circular dependencies:
from graphlib import CycleError, TopologicalSorter
def forward_pass(tasks):
"""tasks maps name -> (duration, [predecessor names]).
Returns (start, finish) dictionaries in working-day offsets.
"""
graph = {}
for name, (_, preds) in tasks.items():
for p in preds:
if p not in tasks:
raise KeyError(f"{name!r} depends on unknown task {p!r}")
graph[name] = set(preds)
try:
order = list(TopologicalSorter(graph).static_order())
except CycleError as err:
raise ValueError(f"Circular dependency: {err.args[1]}") from err
start, finish = {}, {}
for name in order:
duration, preds = tasks[name]
start[name] = max((finish[p] for p in preds), default=0)
finish[name] = start[name] + duration
return start, finish
if __name__ == "__main__":
shuffled = {
"Integration": (4, ["Frontend", "Backend"]),
"Frontend": (10, ["Mockups"]),
"Backend": (12, ["Wireframes"]),
"Mockups": (7, ["Wireframes"]),
"Wireframes": (5, []),
}
s, f = forward_pass(shuffled)
for name in shuffled:
print(f"{name:12} {s[name]:>3} {f[name]:>3}")
print("Project length:", max(f.values()))TopologicalSorter takes a mapping from each node to its predecessors and yields every node only after all of its predecessors. The explicit check for unknown names turns a typo in a predecessor list into a clear message. Without the check, graphlib would silently treat the typo as a new task with no duration.
Convert offsets to calendar dates
Offsets become dates by counting only working days. Define the calendar once, then map each offset onto it. Append this to the same file, after forward_pass:
from datetime import date, timedelta
HOLIDAYS = {date(2026, 1, 19)} # a Monday
def is_workday(d):
return d.weekday() < 5 and d not in HOLIDAYS
def add_workdays(d, n):
"""Return the date n working days after d. d must be a working day."""
while n > 0:
d += timedelta(days=1)
if is_workday(d):
n -= 1
return d
base = date(2026, 1, 5) # project start: a Monday, not a holiday
tasks = {
"Wireframes": (5, []),
"Mockups": (7, ["Wireframes"]),
"Backend": (12, ["Wireframes"]),
"Frontend": (10, ["Mockups"]),
"Integration": (4, ["Frontend", "Backend"]),
}
start, finish = forward_pass(tasks)
for name in tasks:
first = add_workdays(base, start[name])
last = add_workdays(base, max(finish[name] - 1, start[name]))
print(f"{name:12} {first:%b %d} to {last:%b %d}")The finish - 1 converts the half-open interval into an inclusive last working day. The max(..., start) keeps a zero-duration milestone on its own start date. The output is:
| Task | Start offset | Finish offset | First day | Last day |
|---|---|---|---|---|
| Wireframes | 0 | 5 | Jan 05 | Jan 09 |
| Mockups | 5 | 12 | Jan 12 | Jan 21 |
| Backend | 5 | 17 | Jan 12 | Jan 28 |
| Frontend | 12 | 22 | Jan 22 | Feb 04 |
| Integration | 22 | 26 | Feb 05 | Feb 10 |
Mockups spans more than seven calendar days because it crosses a weekend and the holiday on Jan 19. Offsets 9 and 10 fall on Friday Jan 16 and Tuesday Jan 20. The holiday and the weekend do not consume offsets.
Edge cases and failure modes
- Circular dependencies. If A needs B and B needs A, no task can be first, so no valid order exists. The
graphlibversion raises an error. Treat it as a planning problem: the tasks usually need to be split into smaller pieces. - Missing predecessors. A task you forgot to link starts on day 0. The result looks plausible but is too optimistic. Check that every task except the first has at least one predecessor.
- Zero-duration tasks. Milestones work with no special case. Their EF equals their ES, and they pass that date to successors.
- Lag and lead. A lag of 2 days means "start 2 days after the predecessor finishes". A negative lag (a lead) allows overlap. To support them, store the lag with each link and compute
max(finish[p] + lag for p, lag in links). Use leads sparingly. They hide real dependencies and make the chart harder to trust. - Other dependency types. Start-to-start and finish-to-finish links change which of the predecessor's dates you read. This chapter uses finish-to-start only.
- Date constraints. A rule like "not before March 1" turns the formula into
max(constraint, latest predecessor finish). Apply constraints as extra inputs to the samemax. - Calendar start.
add_workdaysassumes the base date is a working day. If it isn't, move the base forward first.
Using the result
The forward pass gives each task its earliest dates. These are exactly what a Gantt chart needs to place bars: bar position comes from the start and bar length from the duration. The project length comes from the largest EF. The next chapter uses these numbers to find which tasks have no room to slip.
Exercise
Add a task "Testing" with a duration of 3 days that depends on Integration. Using the example's numbers, what are its ES and EF, and what is the new project length?
Show answer
Integration finishes at day 26, so Testing has ES = 26 and EF = 26 + 3 = 29. The project length becomes 29 working days.
Exercise
Suppose Backend grows from 12 days to 20 days. Recompute Backend's EF, then Integration's ES and EF, and give the new project length. Why did the project length change by a different amount than Backend's duration?
Show answer
Backend still starts at 5, so its EF is 5 + 20 = 25. Integration's ES is max(22, 25) = 25, and its EF is 25 + 4 = 29. The project length is 29 days, an increase of 3 days even though Backend grew by 8. Backend had 5 days before it affected the project, so only the excess past day 22 (3 days) pushes the finish date.
Exercise
In the loop-only version of the forward pass, you list "Frontend" before "Mockups" in the dictionary. What happens, and which two approaches fix it?
Show answer
The line computing Frontend's start reads finish["Mockups"], which has not been set yet, so Python raises a KeyError. Fix it by reordering the dictionary so predecessors come first, or by sorting the tasks with graphlib.TopologicalSorter before the loop.