Sub topic 3
Paths through the network and their lengths
This chapter assumes you can read a task list with durations and predecessors, and that you know the terms task, duration, milestone, and dependency. The code uses basic Python (dictionaries, loops, functions). It does not need the forward or backward pass, which come in later chapters.
What a path is
An activity network is a set of tasks joined by dependency links. A path is a sequence of tasks that you can follow from a start task to an end task, moving along dependency links in their direction. A start task has no predecessors. An end task has no successors.
The length of a path is the sum of the durations of the tasks on it. Duration here means elapsed working time, so a path's length is the shortest time in which that chain can be completed if each task starts the moment its predecessor finishes.
Consider four tasks:
| Task | Duration (days) | Predecessors |
|---|---|---|
| A | 3 | none |
| B | 2 | A |
| C | 4 | A |
| D | 3 | B, C |
Two paths run from the start task A to the end task D:
- A, B, D has length 3 + 2 + 3 = 8 days.
- A, C, D has length 3 + 4 + 3 = 10 days.
Why the longest path sets the duration
Tasks on different paths can run at the same time, so their durations do not add together. Task D needs both B and C to be finished. B takes 2 days and C takes 4 days, and both start after A, so D cannot begin until the slower of the two is done. The project cannot finish before the longest chain of dependent work is complete.
This gives the central rule of the whole tutorial:
- The minimum project duration equals the length of the longest path.
- Shorter paths have spare time. Here, the path through B finishes 2 days before D could start.
- Making a shorter path faster does not shorten the project. Making the longest path shorter does, until another path becomes the longest.
The longest path is called the critical path. This chapter covers how to find paths and measure them. A later chapter covers how float identifies the critical path without listing every path.
Start and end points
Real networks often have several start tasks, several end tasks, or both. Two conventions keep path counting simple:
- Add a zero-duration start milestone that precedes every task with no predecessors.
- Add a zero-duration end milestone that follows every task with no successors.
Zero-duration milestones do not change any path length, because they add 0 days. They guarantee that every path begins at one node and ends at one node. They also reveal a modelling error: if a task you expected to be connected shows up as a second start or end, it has a missing link. The next chapter covers this check in detail.
Where durations live
In an activity-on-node network, which is what this tutorial uses, each task is a node and carries its own duration. Edges are plain dependencies, so path length is the sum of node durations. In an activity-on-arrow network, tasks are the edges, and path length is the sum of edge durations. Both give the same project duration. If you read an older schedule drawn on arrows, expect extra zero-duration "dummy" edges that exist only to express a dependency.
Later chapters add lags to dependencies. A lag is a waiting time between two tasks, so it counts toward path length, and you add it as part of the link, not as part of either task.
Counting paths grows quickly
A path is a choice at each fork. Every time the network splits into two branches that rejoin, the number of paths doubles. Three diamonds in a row, each like the A, B, C, D example, give 2 × 2 × 2 = 8 paths. Twenty such diamonds give over a million paths.
| Diamonds in series | Paths from start to end |
|---|---|
| 1 | 2 |
| 3 | 8 |
| 10 | 1,024 |
| 20 | 1,048,576 |
Real schedules with hundreds of tasks therefore cannot be analysed by listing every path. You can still list the paths of a small network, which is a good way to build intuition and to check a calculation by hand.
Listing every path in code
The program below builds a successor lookup from the predecessor lists, then walks forward from each start task. It uses recursion: the paths from a task are that task followed by each path from each of its successors.
tasks = {
"A": (3, []),
"B": (2, ["A"]),
"C": (4, ["A"]),
"D": (3, ["B", "C"]),
}
# Build successors from predecessors
succs = {t: [] for t in tasks}
for t, (dur, preds) in tasks.items():
for p in preds:
succs[p].append(t)
starts = [t for t, (dur, preds) in tasks.items() if not preds]
def all_paths(node):
"""Return every path that begins at node and ends at an end task."""
if not succs[node]:
return [[node]]
paths = []
for s in succs[node]:
for rest in all_paths(s):
paths.append([node] + rest)
return paths
results = []
for s in starts:
for path in all_paths(s):
length = sum(tasks[t][0] for t in path)
results.append((length, path))
for length, path in sorted(results, reverse=True):
print(length, " -> ".join(path))
# 10 A -> C -> D
# 8 A -> B -> DThe output lists paths from longest to shortest. The first line gives the minimum project duration of 10 days and the chain that determines it.
This approach re-walks shared sub-paths, so its running time grows with the number of paths, which is the exponential growth shown above. It is fine for teaching and for small networks. For real schedules you compute the critical path with the forward and backward passes, which take time proportional to the number of tasks and links.
Common errors
- Adding all durations. This gives the total effort in sequence, not the duration. The project length is a maximum over paths, not a sum over tasks.
- Following the shortest path. The shortest path tells you how much slack a branch has, but it says nothing about when the project ends.
- Missing a path through a task with several successors. If a task has two successors, both continuations are separate paths. Checking that each fork was followed in both directions catches most hand-counting errors.
- Mixing units. Durations must use one unit, and that unit must be working days or calendar days consistently. A path mixing 3 working days with 3 calendar days has no meaningful length.
- Treating ties as errors. Two paths can have the same longest length. That is legitimate and carries extra risk, which a later chapter covers under multiple critical paths.
Exercises
Exercise
A project has these tasks. P (2 days, no predecessors), Q (5 days, after P), R (3 days, after P), S (4 days, after R), T (1 day, after Q and S). List every path from P to T, give each length, and state the minimum project duration.
Show answer
There are two paths. P, Q, T has length 2 + 5 + 1 = 8 days. P, R, S, T has length 2 + 3 + 4 + 1 = 10 days. The minimum project duration is the longest path, 10 days, so the critical chain is P, R, S, T. Task Q's branch finishes 2 days before T can start.
Exercise
A network is built from 6 diamonds in series, each with two parallel branches. How many start-to-end paths does it have, and why can you not simply add every task's duration to get the project length?
Show answer
It has 2^6 = 64 paths. Adding every task's duration would also count tasks on parallel branches, which run at the same time. The project length equals the length of the longest single path, so you add only the durations along one chain.