Critical path (AOE network)
In an activity network, compute each event's earliest and latest time and find the activities that cannot be delayed at all.
In an AOE network (activity on edge), each edge is an activity with a duration and each node is an event ("all incoming activities are done"). The questions:
Read the full explanation ↓- Time:
O(V + E) - Space:
O(V) - Horowitz–Sahni, chapter 6
- CLRS, section 22.2
Nine events and eleven activities; the project takes 18, and there are two critical paths from 0 to 8.
def critical_path(graph, src):# The project starts at time 0; src is ignored.seen = set()order = []for s in graph:if s not in seen:topo(graph, s, seen, order)order.reverse()early = {v: 0 for v in graph}for u in order:for v, w in graph[u]:if early[u] + w > early[v]:early[v] = early[u] + wfinish = max(early.values())late = {v: finish for v in graph}for u in reversed(order):for v, w in graph[u]:if late[v] - w < late[u]:late[u] = late[v] - wcritical = []for u in graph:for v, w in graph[u]:if early[u] + w == late[v]:critical.append((u, v, w))return finish, criticaldef topo(graph, u, seen, order):seen.add(u)for v, _ in graph[u]:if v not in seen:topo(graph, v, seen, order)order.append(u)
Start
- Current node
(u) - Shortest-path tree so far
- Edge being checked
Recursion tree
Filled orange = running call · Orange outline = still on the stack · Grey = finished
Local variables
Orange = changed in this step
graph dict
- '0': [('1', 6), ('2', 4), ('3', 5)]
- '1': [('4', 1)]
- '2': [('4', 1)]
- '3': [('5', 2)]
- '4': [('6', 9), ('7', 7)]
- '5': [('7', 4)]
- '6': [('8', 2)]
- '7': [('8', 4)]
- '8': []
Call stack
- critical_path()line 1
How the work grows with input size
Average of 5 random graphs per n (fixed seeds). Hollow marker = the input on screen.
- forward
- backward
- current input (n=9)
Show as a table
| n | forward | backward |
|---|---|---|
| 4 | 5.2 | 5.2 |
| 8 | 13.8 | 13.8 |
| 12 | 20.8 | 20.8 |
| 16 | 28 | 28 |
| 20 | 35.4 | 35.4 |
| 24 | 37.2 | 37.2 |
| 28 | 47.2 | 47.2 |
Explanation
The idea
In an AOE network (activity on edge), each edge is an activity with a duration and each node is an event ("all incoming activities are done"). The questions:
- How long does the project take at least?
- Which activities are critical, meaning any delay to them delays the whole project?
Two passes
- Forward (in topological order):
early[v] = max(early[u] + w)over incoming edges. The largestearlyis the project's length. This is the longest path. - Backward (in reverse order):
late[u] = min(late[v] − w)over outgoing edges.
An activity u → v of duration w has no slack and is critical if early[u] + w = late[v]. The critical paths
are made of critical edges.
What to watch in the trace
- The number by each node is its
earlytime;lateis in the memory panel. - The coloured edges are the critical activities.