Chapter 18: Shortest Paths and Activity Networks
Introduction
Learning Outcomes
Set up city/railway and network-delay problems as weighted directed graphs.
Find shortest paths from one source to all destinations greedily; trace the O(n²) code and print paths with the predecessor array.
Apply Bellman-Ford to negative edges and Floyd-Warshall to all pairs.
Find a linear order consistent with precedence in AoV networks using counts + a stack; detect cycles.
Compute earliest and latest start times in AoE networks by forward and backward passes.
Compute slack and identify the critical activities and the critical path.
Shortest Paths
The Shortest Path Problem
Horowitz–Sahni example
| Source | Target | Cost |
|---|---|---|
| 0 | 5 | No path! |
| 0 | 2 | 10 |
| 0 | 3 | 25 |
| 0 | 1 | 45 |
| 0 | 4 | 45 |
- Vertices are cities, edges are rail distances between them: which is the shortest path from one city to another?
- Or the edges are packet delays in a network (congestion): how should a router be written for this data?
- Two questions: 1) Is there a path from x to y? 2) If there are several, which is the shortest?
- Visiting every vertex once and returning to the start (the traveling salesman) is a different and much harder problem.
Shortest Paths
Single Source, Single Target: Brute Force
K4 is planar; K5 is not
K4 (0 → 3), 5 paths for n = 4: 0-1-2-3, 0-1-3, 0-2-1-3, 0-2-3, 0-3
K5 (0 → 4), 1 + 3 + 6 + 6 = 16 paths for n = 5: 0-1-2-3-4, 0-1-2-4, 0-1-3-2-4, 0-1-3-4, 0-1-4, 0-2-1-3-4, 0-2-1-4, 0-2-3-1-4, 0-2-3-4, 0-2-4, 0-3-1-2-4, 0-3-1-4, 0-3-2-1-4, 0-3-2-4, 0-3-4, 0-4
Dijkstra
Toward an Efficient Solution: the Greedy Idea
- S: vertices whose shortest paths are final (initially just the source 0).
- For w ∉ S,
dist[w]is the length of the shortest path that starts at 0, passes only through vertices in S, and ends at w. - Pick the u with the smallest
dist[u]and add it to S. - Adding u opens new paths through u: if
dist[u] + cost(<u,w>) < dist[w], update dist[w] (no intermediate vertex between u and w).
new paths to u's neighbors through u
Dijkstra
Step by Step: from v0 to All Vertices
S = {v0}. The neighbors' distances are the edge costs: v1 = 10, v3 = 30; the rest are ∞.
v1 (10) has the smallest distance and joins S. Updated: v2 = 20.
v2 (20) has the smallest distance and joins S. Updated: v4 = 30.
v3 (30) has the smallest distance and joins S. Updated: v5 = 50, v6 = 32.
v4 (30) has the smallest distance and joins S. Updated: v5 = 40. A shorter path to v5 through v4 was found (50 → 40)!
v6 (32) has the smallest distance and joins S. No updates. The order depends on distance, not on vertex number; 32 < 40, so v6 comes first.
v5 (40) has the smallest distance and joins S. No updates.
Dijkstra
C Code: the Cost Matrix and choose
#include <limits.h> #define MAX_VERTICES 6 #define INF 1000 /* no edge */ int cost[][MAX_VERTICES] = { { 0, 50, 10, 1000, 45, 1000}, {1000, 0, 15, 1000, 10, 1000}, { 20, 1000, 0, 15, 1000, 1000}, {1000, 20, 1000, 0, 35, 1000}, {1000, 1000, 1000, 30, 0, 1000}, /* missing row */ {1000, 1000, 1000, 3, 1000, 0}}; /* 100 -> 1000 */ int choose(int dist[], int n, short found[]) { int i, min = INT_MAX, pos = -1; for (i = 0; i < n; i++) if (dist[i] < min && !found[i]) { min = dist[i]; pos = i; } return pos; }
Details:
- Write
!found[w];!found(w)calls the array as if it were a function. <limits.h>forINT_MAX; FALSE/TRUE must be defined.choosemay return −1 (no unmarked vertex left); the caller must check. An unreachable vertex can also be chosen:INF(1000) <INT_MAX.
Dijkstra
shortest_path(): the Main Function
void shortest_path(int v, int cost[][MAX_VERTICES], int dist[], int pred[], int n, short found[]) { int i, u, w; for (i = 0; i < n; i++) { /* A1 */ found[i] = FALSE; dist[i] = cost[v][i]; pred[i] = v; /* kept for the path */ } found[v] = TRUE; dist[v] = 0; for (i = 2; i < n; i++) { /* n-2 rounds */ u = choose(dist, n, found); if (u < 0) break; /* none left */ found[u] = TRUE; for (w = 0; w < n; w++) if (!found[w] && dist[u] + cost[u][w] < dist[w]) { dist[w] = dist[u] + cost[u][w]; pred[w] = u; } } }
- A1: initial values; the distances are the direct edge costs.
- A2: the outer loop runs n − 2 rounds: the last remaining vertex's distance is already final.
- A3:
chooseis O(n). - A4: relaxation for every w, O(n).
pred[]is kept to trace the path back.
shortest_path · Step through the code
Code Trace: shortest_path(0, …)
After A1: dist = row 0 of the matrix (1000 = ∞); pred[] = 0. The source is marked.
choose: the smallest distance among unmarked vertices is 2 (10). Final.
w = 3: 10 + 15 = 25 < old value → dist[3] = 25, pred[3] = 2.
choose: the smallest distance among unmarked vertices is 3 (25). Final.
w = 1: 25 + 20 = 45 < old value → dist[1] = 45, pred[1] = 3.
choose: the smallest distance among unmarked vertices is 1 (45); 1 and 4 tie at 45, and since the test is < the lower index 1 wins. Final.
No distance gets shorter through 1 (e.g. 45 + 10 = 55 > 45).
choose: the smallest distance among unmarked vertices is 4 (45). Final.
No distance gets shorter through 4 (its only out-edge is <4,3> and 3 is already marked).
n − 2 = 4 rounds done. Result: 2 → 10, 3 → 25, 1 → 45, 4 → 45; no path to 5 (still 1000).
found[v] = TRUE; dist[v] = 0; for (i = 2; i < n; i++) { /* n-2 rounds */ u = choose(dist, n, found); if (u < 0) break; /* none left */ found[u] = TRUE; for (w = 0; w < n; w++) if (!found[w] && dist[u] + cost[u][w] < dist[w]) { dist[w] = dist[u] + cost[u][w]; pred[w] = u; } }
Dijkstra
Analysis, and Better Structures
n + n[n + n] = n + 2n² = O(n²), from A1, A2, A3 and A4 in turn.
Keep the distances in a min heap: log n instead of n in A3. With adjacency lists, degree(v) instead of n in A4.
A common mistake is to say O(n lg n); every relaxation also updates the heap, so the right bound is O((n + e) log n) (Fibonacci heap: O(e + n log n)).
Brute force O(nⁿ) · matrix O(n²) · heap + lists O((n + e) log n). On dense graphs the O(n²) version is still good.
“By the time he finds the largest element in the matrix, I have the shortest paths from v0 to every vertex.”
Predecessor array · Step through the code
Code Trace: print_path(pred, 0, 1)
h = 1 ≠ 0: print the predecessor's path first → print_path(…, 3).
h = 3 ≠ 0: print the predecessor's path first → print_path(…, 2).
h = 2 ≠ 0: print the predecessor's path first → print_path(…, 0).
h = s = 0: the source is reached → print 0 and return.
Back in print_path(2): print “ -> 2”.
Back in print_path(3): print “ -> 3”.
Back in print_path(1): print “ -> 1”.
Path: 0 -> 2 -> 3 -> 1, cost 10 + 15 + 20 = 45. We walked back from the target; the call stack put the output in the right order.
void print_path(int pred[], int s, int h) { if (h == s) { printf("%d", s); return; } print_path(pred, s, pred[h]); /* pred first */ printf(" -> %d", h); }
Dijkstra
Finding the Path: the Predecessor Array
Each vertex's shortest path is its predecessor's shortest path plus one edge (optimal substructure). The paths form a tree: the shortest-path tree.
On a successful relaxation, pred[w] = u. When the cost changes, so does the predecessor.
pred[] walks from the target back to the source; recursion or a stack reverses it for printing.
If dist[h] ≥ INF, print “no path”; pred[] is meaningless.
Dijkstra
Software Engineering Concepts
FindWeight(…);. What is this? ‘What's wrong with it?’ What does it return? ‘It returns an int.’ So where does it go? The return value is left hanging in the air. ‘Oh, I forgot about that.’” Solutions should be modular, simple, readable, reusable and easy to maintain. Keep a software engineering book at hand:
That is also why the code in this chapter is split into small functions: choose, print_path, find…
Negative Edges and All Pairs
Why Dijkstra Fails with Negative Edges (extra topic)
| Vertex | Dijkstra | Correct |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 4 | 2 |
| 2 | 5 | 5 |
| 3 | 6 | 4 |
Dijkstra finalizes 1 at 4 (no smaller candidate). Then edge 2 → 1 (−3) reaches 1 at cost 2, but 1 is already marked. 3 stays wrong too.
Dijkstra assumes a chosen vertex can never be reached later by a cheaper path; that holds only when w ≥ 0.
Bellman-Ford: relax every edge for n − 1 rounds. With a negative cycle shortest paths are undefined; a relaxation in round n detects it.
Bellman-Ford (extra topic) · Step through the code
Code Trace: Bellman-Ford (source 0)
d[] = ∞, source d[0] = 0.
Round 1, 0→1 (4): d[0] + 4 = 4 is smaller → d[1] = 4.
Round 1, 0→2 (5): d[0] + 5 = 5 is smaller → d[2] = 5.
Round 1, 1→3 (2): d[1] + 2 = 6 is smaller → d[3] = 6.
Round 1, 2→1 (-3): d[2] + (−3) = 2 is smaller → d[1] = 2.
Round 2, 0→1 (4): 0 + 4 = 4 ≥ d[1] = 2, no change.
Round 2, 0→2 (5): 0 + 5 = 5 ≥ d[2] = 5, no change.
Round 2, 1→3 (2): d[1] + 2 = 4 is smaller → d[3] = 4.
Round 2, 2→1 (-3): 5 + (−3) = 2 ≥ d[1] = 2, no change.
Round 3: no edge relaxes (d is already final). In general n − 1 rounds are needed: a shortest path has at most n − 1 edges.
No relaxation in round n either → no negative cycle, return 0. Result d = [0, 2, 5, 4]. Cost O(n·e).
for (i = 0; i < n; i++) d[i] = INF; d[s] = 0; for (t = 1; t <= n - 1; t++) /* n-1 rounds */ for (j = 0; j < e; j++) { u = E[j].u; v = E[j].v; if (d[u] != INF && d[u] + E[j].w < d[v]) d[v] = d[u] + E[j].w; } for (j = 0; j < e; j++) /* round n */ if (d[E[j].u] != INF && d[E[j].u] + E[j].w < d[E[j].v]) return -1; return 0; /* -1: negative cycle */
All pairs (extra topic) · Step through the code
Code Trace: Floyd-Warshall
A⁻¹ = the cost matrix (Horowitz example; there is no direct edge 2 → 1).
k = 0: paths may now pass through vertex 0 (and lower-numbered ones).
A[2][0] + A[0][1] = 3 + 4 = 7 < ∞ → A[2][1] = 7.
k = 1: paths may now pass through vertex 1 (and lower-numbered ones).
A[0][1] + A[1][2] = 4 + 2 = 6 < 11 → A[0][2] = 6.
k = 2: paths may now pass through vertex 2 (and lower-numbered ones).
A[1][2] + A[2][0] = 2 + 3 = 5 < 6 → A[1][0] = 5.
Done: A = shortest distances between all pairs. Three nested loops → O(n³); negative edges are allowed, negative cycles are not.
for (i = 0; i < n; i++) for (j = 0; j < n; j++) A[i][j] = cost[i][j]; for (k = 0; k < n; k++) /* via vertex k */ for (i = 0; i < n; i++) for (j = 0; j < n; j++) if (A[i][k] + A[k][j] < A[i][j]) A[i][j] = A[i][k] + A[k][j];
Negative Edges and All Pairs
Shortest Paths: Which Algorithm, When?
| Problem | Algorithm | Time | Condition |
|---|---|---|---|
| Unweighted, single source | BFS | O(n + e) | all edges equal |
| Single source, w ≥ 0 | Dijkstra (matrix) | O(n²) | dense graph |
| Single source, w ≥ 0 | Dijkstra (heap + lists) | O((n + e) log n) | sparse graph |
| Single source, negative edges | Bellman-Ford | O(n · e) | detects negative cycles |
| DAG, single source | Relax in topological order | O(n + e) | no cycles (activity networks!) |
| All pairs | Floyd-Warshall | O(n³) | no negative cycle |
| All pairs, sparse | n × Dijkstra | O(n (n + e) log n) | w ≥ 0 |
Activity Networks
Activity Networks: a Project = Activities
| Code | Course | Prerequisites |
|---|---|---|
| C1 | Programming I | – |
| C2 | Discrete Math. | – |
| C3 | Data Structures | C1, C2 |
| C4 | Calculus I | – |
| C5 | Calculus II | C4 |
| C6 | Linear Algebra | C5 |
| C7 | Analysis of Algorithms | C3, C6 |
| C8 | Assembly Lang. | C3 |
| C9 | Operating Systems | C7, C8 |
| C10 | Programming Langs. | C7 |
| C11 | Compiler Design | C10 |
| C12 | Artificial Intelligence | C7 |
| C13 | Computational Theory | C7 |
| C14 | Parallel Algorithms | C13 |
| C15 | Numerical Analysis | – |
A project ends when its subtasks (activities) end: graduation = courses. In construction: lay the foundation, the 2nd floor 21 days later… When does the project end? Which tasks are critical? Which have slack?
Activity Networks
Activity-on-Vertex (AoV) Networks and Precedence
- AoV (activity on vertex): activities are vertices, the precedence relation gives the edges: <i, j> if course i is a prerequisite of j.
- C3 and C6 are immediate predecessors of C7.
- C9, C10, C12 and C13 are immediate successors of C7.
- C14 is a successor of C3, but not an immediate one.
- Precedence is transitive and must be acyclic: A cannot precede B while B precedes A.
Activity Networks
Definition: Topological Order
It goes back to the planning methods (PERT, 1958) developed during the Cold War to speed up missile (Polaris) production. Two valid topological orders of the courses (checked by program):
C1, C2, C4, C5, C3, C6, C8, C7, C10, C13, C12, C14, C15, C11, C9
C4, C5, C2, C1, C6, C3, C8, C15, C7, C9, C10, C11, C12, C13, C14
For every edge <i, j>, does i come before j in the order? All 14 edges pass.
Course plans, build order (make), task scheduling, spreadsheet cells, earliest times.
Activity Networks
Finding a Topological Order: Delete a Vertex with No Predecessor
a) v0 has no predecessor → the first vertex to choose.
b) v0 is output and its out-edges deleted. Any vertex with no predecessors left (orange) may be chosen. If v3 is chosen, <v3,v4> and <v3,v5> are deleted.
c) v3 is output and its out-edges deleted. Any vertex with no predecessors left (orange) may be chosen.
d) v2 is output and its out-edges deleted. Any vertex with no predecessors left (orange) may be chosen.
e) v5 is output and its out-edges deleted. Any vertex with no predecessors left (orange) may be chosen.
f) v1 is output and its out-edges deleted. Any vertex with no predecessors left (orange) may be chosen.
g) One possible topological order: v0, v3, v2, v5, v1, v4.
Each time, take a vertex with no predecessors, output it and delete its out-edges; repeat to the end. If vertices remain but none is free of predecessors: a cycle.
Activity Networks
Data Structure: a Predecessor Count in the Header
typedef struct node *node_pointer; typedef struct node { int vertex; node_pointer link; } node; typedef struct { int count; /* in-degree: edges <i,j> into j */ node_pointer link; } hdnodes; hdnodes graph[MAX_VERTICES];
- Vertices are numbered 0..n−1.
count: the vertex's number of predecessors (in-edges); at 0 it can be output.- While reading the edges:
graph[j].count++.
Lists with counts for the v0..v5 example
Activity Networks
The Topological Sort Function
void topsort(hdnodes graph[], int n) { int i, j, k, top = -1; node_pointer p; for (i = 0; i < n; i++) /* count 0: push */ if (!graph[i].count) { graph[i].count = top; top = i; } for (i = 0; i < n; i++) { if (top == -1) { printf("cycle"); break; } j = top; top = graph[top].count; printf("%d ", j); for (p = graph[j].link; p; p = p->link) { k = p->vertex; if (--graph[k].count == 0) { graph[k].count = top; top = k; } } } }
- Vertices with no predecessors left are chained together through their
countfields: count now means “the vertex below me on the stack”. This resembles the parent array of disjoint sets. - The pair
count--; if (!count)is written here as--count == 0(same meaning).
topsort · Step through the code
Code Trace: topsort(graph, 6)
v0's count is 0 → push: count = top (−1: the bottom of the stack), top = 0.
The first loop is done: only v0 has no predecessors. The other counts hold predecessor counts.
Pop: j = 0, top = count[0] = -1. Output v0.
<0,1>: v1's count drops to 0 → push: count[1] = -1, top = 1.
<0,2>: v2's count drops to 0 → push: count[2] = 1, top = 2.
<0,3>: v3's count drops to 0 → push: count[3] = 2, top = 3.
Pop: j = 3, top = count[3] = 2. Output v3.
<3,5>: v5's predecessor count drops by 1 → 1; not ready yet.
<3,4>: v4's predecessor count drops by 1 → 2; not ready yet.
Pop: j = 2, top = count[2] = 1. Output v2.
<2,4>: v4's predecessor count drops by 1 → 1; not ready yet.
for (i = 0; i < n; i++) /* count 0: push */ if (!graph[i].count) { graph[i].count = top; top = i; } for (i = 0; i < n; i++) { if (top == -1) { printf("cycle"); break; } j = top; top = graph[top].count; printf("%d ", j); for (p = graph[j].link; p; p = p->link) { k = p->vertex; if (--graph[k].count == 0) { graph[k].count = top; top = k; } } }
topsort · Step through the code
Code Trace: topsort() (continued)
<2,5>: v5's count drops to 0 → push: count[5] = 1, top = 5.
Pop: j = 5, top = count[5] = 1. Output v5.
Pop: j = 1, top = count[1] = -1. Output v1.
<1,4>: v4's count drops to 0 → push: count[4] = -1, top = 4.
Pop: j = 4, top = count[4] = -1. Output v4.
Every vertex is output: 0 3 2 5 1 4. With a cycle, some round would see top == −1 and print “cycle”.
for (i = 0; i < n; i++) /* count 0: push */ if (!graph[i].count) { graph[i].count = top; top = i; } for (i = 0; i < n; i++) { if (top == -1) { printf("cycle"); break; } j = top; top = graph[top].count; printf("%d ", j); for (p = graph[j].link; p; p = p->link) { k = p->vertex; if (--graph[k].count == 0) { graph[k].count = top; top = k; } } }
Activity Networks
Cycle Detection and Analysis
A cycle means A and B each precede the other, which is an error. “With a cycle the project never ends.” If the stack is empty in some round while vertices remain, there is a cycle.
The first for is O(n), the second O(n); the inner for runs dj times (out-degree). Total O(Σdi + n) = O(n + e).
The push order follows the order of the adjacency lists; with a queue (Kahn) you would get a different but still valid order.
Depending on how the graph is connected, e ranges from O(n) to O(n²).
Activity Networks
Activity-on-Edge (AoE) Networks
v0 start, v8 finish: 11 activities (edges), 9 events (vertices)
Unlike AoV, directed edges are activities (with durations) and vertices are events.
A milestone the project must reach: “the work that must be finished is expressed by a vertex”.
a0, a1 and a2 start in parallel; a6 and a7 start once a3 and a4 finish; a10 needs a7 and a8 finished.
Activity Networks
Graph Drawing: from AoV to AoE, Dummy Activities
In AoV, activity 4 waits for 2 and 3; in AoE this needs a dummy edge of cost 0.
An edge of cost 0. E.g. if a6 and a7 above had to wait for a3, a4 and a5, the dummy activity <v5, v4> would be needed.
Drawing the graph with the fewest dummy activities is NP-hard, a research topic with thesis-level work on it.
PERT (Program Evaluation and Review Technique, 1958), CPM (Critical Path Method, late 1950s), RAMPS (Resource Allocation and Multi-Project Scheduling).
Early/Late Times
The Critical Path
Critical paths: v0-v1-v4-v6-v8 and v0-v1-v4-v7-v8 (length 18)
- Summing activity durations, the longest path from v0 to v8 is the critical path. There may be more than one.
- Here 6 + 1 + 9 + 2 = 18 and 6 + 1 + 7 + 4 = 18 → the project ends at the earliest in 18 days.
- If its prerequisites finish as fast as possible, when does an activity start? How late can it start without delaying the project?
- To the crews on critical tasks: “Finish in 4 days, don't let it slip to 5. If it's done in 3, I'll be happy.” An operations research problem.
Early/Late Times
Earliest Start Times
- If ai = <k, m>, then
early(i) = earliest[k]: the activity starts when the event at its tail occurs. - E.g. a1: k = v0 → 0; a5: k = v3 → 5 (a2); a8: k = v5 → 5 + 2 = 7.
- Two paths lead to v4: a3 ends at the earliest at 6 + 1 = 7, a4 at 4 + 1 = 5. Both must finish → wait for the longer (7).
earliest[m] = max { earliest[k] + dur(<k,m>) }, k ∈ P(m) (immediate predecessors).- The array starts at 0: day 0 of the project (e.g. “May 8”); with 10, everything would shift by 10.
- Compute in topological order: the predecessors' values must be known first.
/* in topsort, before decrementing count: */ if (earliest[k] < earliest[j] + p->duration) earliest[k] = earliest[j] + p->duration;
A duration field is added to the list node; the value is updated when a longer path is found.
Forward pass · Step through the code
Code Trace: earliest[] (in topological order)
Start: earliest[] = 0; the first loop pushed v0, which has no predecessors.
v0 is popped and output (earliest[0] = 0 is now final).
<0,1> (duration 6): 0 + 6 = 6 > 0 → earliest[1] = 6; v1 is pushed.
<0,2> (duration 4): 0 + 4 = 4 > 0 → earliest[2] = 4; v2 is pushed.
<0,3> (duration 5): 0 + 5 = 5 > 0 → earliest[3] = 5; v3 is pushed.
v3 is popped and output (earliest[3] = 5 is now final).
<3,5> (duration 2): 5 + 2 = 7 > 0 → earliest[5] = 7; v5 is pushed.
v5 is popped and output (earliest[5] = 7 is now final).
<5,7> (duration 4): 7 + 4 = 11 > 0 → earliest[7] = 11.
v2 is popped and output (earliest[2] = 4 is now final).
<2,4> (duration 1): 4 + 1 = 5 > 0 → earliest[4] = 5.
j = top; top = graph[top].count; /* pop */ printf("%d ", j); for (p = graph[j].link; p; p = p->link) { k = p->vertex; if (earliest[k] < earliest[j] + p->duration) earliest[k] = earliest[j] + p->duration; if (--graph[k].count == 0) { graph[k].count = top; top = k; /* push */ } }
Forward pass · Step through the code
Code Trace: earliest[] (continued)
v1 is popped and output (earliest[1] = 6 is now final).
<1,4> (duration 1): 6 + 1 = 7 > 5 → earliest[4] = 7; v4 is pushed.
v4 is popped and output (earliest[4] = 7 is now final).
<4,6> (duration 9): 7 + 9 = 16 > 0 → earliest[6] = 16; v6 is pushed.
<4,7> (duration 7): 7 + 7 = 14 > 11 → earliest[7] = 14; v7 is pushed.
v7 is popped and output (earliest[7] = 14 is now final).
<7,8> (duration 4): 14 + 4 = 18 > 0 → earliest[8] = 18.
v6 is popped and output (earliest[6] = 16 is now final).
<6,8> (duration 2): 16 + 2 = 18 ≤ 18, no change; v8 is pushed.
v8 is popped and output (earliest[8] = 18 is now final).
j = top; top = graph[top].count; /* pop */ printf("%d ", j); for (p = graph[j].link; p; p = p->link) { k = p->vertex; if (earliest[k] < earliest[j] + p->duration) earliest[k] = earliest[j] + p->duration; if (--graph[k].count == 0) { graph[k].count = top; top = k; /* push */ } }
Early/Late Times
Worksheet for the Earliest Start Times
| earliest | [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | Stack |
|---|---|---|---|---|---|---|---|---|---|---|
| Initial | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| Output v0 | 0 | 6 | 4 | 5 | 0 | 0 | 0 | 0 | 0 | 3, 2, 1 |
| Output v3 | 0 | 6 | 4 | 5 | 0 | 7 | 0 | 0 | 0 | 5, 2, 1 |
| Output v5 | 0 | 6 | 4 | 5 | 0 | 7 | 0 | 11 | 0 | 2, 1 |
| Output v2 | 0 | 6 | 4 | 5 | 5 | 7 | 0 | 11 | 0 | 1 |
| Output v1 | 0 | 6 | 4 | 5 | 7 | 7 | 0 | 11 | 0 | 4 |
| Output v4 | 0 | 6 | 4 | 5 | 7 | 7 | 16 | 14 | 0 | 7, 6 |
| Output v7 | 0 | 6 | 4 | 5 | 7 | 7 | 16 | 14 | 18 | 6 |
| Output v6 | 0 | 6 | 4 | 5 | 7 | 7 | 16 | 14 | 18 | 8 |
| Output v8 | 0 | 6 | 4 | 5 | 7 | 7 | 16 | 14 | 18 | – |
Early/Late Times
Latest Start Times
- A backward pass: reverse every arrow and work in the topological order of that graph (v8 first, v0 last). Structure: the inverse adjacency lists, built in O(n + e).
- Last vertex:
latest[n−1] = earliest[n−1]= 18. The array starts filled with 18. - ai = <k, m>:
late(i) = latest[m] − dur. v7 at the latest 18 − 4 = 14, v6 at the latest 18 − 2 = 16. - With several successors take the smallest:
latest[k] = min { latest[m] − dur }. E.g. v0: 6 − 6 = 0 via v1, 6 − 4 = 2 via v2, 8 − 5 = 3 via v3 → 0. Taking the largest (3) would delay v1 and push the project to 21.
if (latest[k] > latest[j] - p->duration)
latest[k] = latest[j] - p->duration;Backward pass · Step through the code
Code Trace: latest[] (on the reversed graph)
Start: latest[] = 18; v8, the only vertex with no predecessors in the reversed graph, is on the stack.
v8 is popped (latest[8] = 18 final). Predecessors: latest[6] = 16, latest[7] = 14. Pushed: v6, v7.
v7 is popped (latest[7] = 14 final). Predecessors: latest[4] = 7, latest[5] = 10. Pushed: v5.
v5 is popped (latest[5] = 10 final). Predecessors: latest[3] = 8. Pushed: v3.
v3 is popped (latest[3] = 8 final). Predecessors: latest[0] = 3.
v6 is popped (latest[6] = 16 final). [4] does not change. Pushed: v4.
v4 is popped (latest[4] = 7 final). Predecessors: latest[1] = 6, latest[2] = 6. Pushed: v1, v2.
v2 is popped (latest[2] = 6 final). Predecessors: latest[0] = 2 (changed).
v1 is popped (latest[1] = 6 final). Predecessors: latest[0] = 0 (changed). Pushed: v0.
v0 is popped (latest[0] = 0 final). No predecessors (the start event).
j = top; top = graph[top].count; /* reversed list */ printf("%d ", j); for (p = graph[j].link; p; p = p->link) { k = p->vertex; /* k: pred */ if (latest[k] > latest[j] - p->duration) latest[k] = latest[j] - p->duration; if (--graph[k].count == 0) { graph[k].count = top; top = k; } }
Early/Late Times
Worksheet for the Latest Start Times
| latest | [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | Stack |
|---|---|---|---|---|---|---|---|---|---|---|
| Initial | 18 | 18 | 18 | 18 | 18 | 18 | 18 | 18 | 18 | 8 |
| Output v8 | 18 | 18 | 18 | 18 | 18 | 18 | 16 | 14 | 18 | 7, 6 |
| Output v7 | 18 | 18 | 18 | 18 | 7 | 10 | 16 | 14 | 18 | 5, 6 |
| Output v5 | 18 | 18 | 18 | 8 | 7 | 10 | 16 | 14 | 18 | 3, 6 |
| Output v3 | 3 | 18 | 18 | 8 | 7 | 10 | 16 | 14 | 18 | 6 |
| Output v6 | 3 | 18 | 18 | 8 | 7 | 10 | 16 | 14 | 18 | 4 |
| Output v4 | 3 | 6 | 6 | 8 | 7 | 10 | 16 | 14 | 18 | 2, 1 |
| Output v2 | 2 | 6 | 6 | 8 | 7 | 10 | 16 | 14 | 18 | 1 |
| Output v1 | 0 | 6 | 6 | 8 | 7 | 10 | 16 | 14 | 18 | 0 |
| Output v0 | 0 | 6 | 6 | 8 | 7 | 10 | 16 | 14 | 18 | – |
Early/Late Times
Slack and Critical Activities
| Act. | Edge | Dur. | Earliest | Latest | Slack |
|---|---|---|---|---|---|
| a0 | (0,1) | 6 | 0 | 0 | 0: critical |
| a1 | (0,2) | 4 | 0 | 2 | 2 |
| a2 | (0,3) | 5 | 0 | 3 | 3 |
| a3 | (1,4) | 1 | 6 | 6 | 0: critical |
| a4 | (2,4) | 1 | 4 | 6 | 2 |
| a5 | (3,5) | 2 | 5 | 8 | 3 |
| a6 | (4,6) | 9 | 7 | 7 | 0: critical |
| a7 | (4,7) | 7 | 7 | 7 | 0: critical |
| a8 | (5,7) | 4 | 7 | 10 | 3 |
| a9 | (6,8) | 2 | 16 | 16 | 0: critical |
| a10 | (7,8) | 4 | 14 | 14 | 0: critical |
thick red edges: slack 0 (critical)
- Slack = latest − earliest (ai = <k,m>: latest[m] − dur − earliest[k]). E.g. a4: earliest 4, latest 6 → it can slip 2 days.
- An activity with slack 0 is critical: it must start at its earliest time. Project-management software charts the slack.
- This is total float: the 3 days of slack on the path a2-a5-a8 are shared; if a2 or a5 uses them, a8 becomes critical.
Early/Late Times
Let the Program Find the Critical Path (extra topic)
/* after computing earliest[] and latest[] */ for (i = 0; i < n; i++) for (p = graph[i].link; p; p = p->link) { int early = earliest[i]; int late = latest[p->vertex] - p->duration; printf("<%d,%d> early=%d late=%d slack=%d%s\n", i, p->vertex, early, late, late - early, late == early ? " CRITICAL" : ""); }
- The critical activities form one (or more) paths from v0 to v8: the critical path.
- The whole computation: forward pass O(n + e) + transposed lists O(n + e) + backward pass O(n + e) = O(n + e).
- In a DAG the “longest path” is easy in topological order; in a general graph the longest simple path is NP-hard.
- Free float: slack usable without delaying the earliest time of any successor; 0 for a2 and a5, 3 for a8.
Early/Late Times
Exam Question: Run the Earliest / Latest Times
- Write the topological order and the stack at every step.
- Fill in the earliest[] and latest[] tables.
- Find the slack of every activity; mark the critical path.
Solution
earliest = [0, 3, 2, 7, 5, 10] · latest = [0, 4, 2, 8, 5, 10]. The project ends at the earliest in 10 days.
Slack: a0=1, a1=0, a2=1, a3=4, a4=0, a5=1, a6=0. Critical activities: a1, a4, a6 → critical path v0-v2-v4-v5.
Lab · C code step by step
Lab: Dijkstra
Summary
Complexity Summary
| Algorithm | Structure | Time |
|---|---|---|
| Brute force (one target) | all paths | O(nⁿ) |
| Dijkstra (basic) | cost matrix + arrays | O(n²) |
| Dijkstra (improved) | min heap + adjacency lists | O((n + e) log n) |
| Printing the path | predecessor array | O(path length) |
| Bellman-Ford | edge list | O(n · e) |
| Floyd-Warshall | n × n matrix | O(n³) |
| Topological order | lists with counts + embedded stack | O(n + e) |
| Earliest / latest times, slack | topological order + inverse lists | O(n + e) |
Summary
Common Mistakes
dist[u] + cost.Summary
Exercises
- On the directed sample graph, which distances would shortest_path find from source 2?
- Why does the outer loop of shortest_path run n − 2 rounds? What if it ran n − 1?
- In the step-by-step example (v0…v6), if edge v3–v6 cost 15 instead of 2, in what order would the vertices join S?
- If topsort used a queue instead of a stack, what would it print for the v0..v5 example?
- In the AoE example, if a8 took 8 instead of 4, what would the project length and critical path be?
- In the Floyd-Warshall example, why did A[1][0] change in round k = 2?
Answers / hints
- d(2→0)=20, d(2→3)=15, d(2→1)=35 (via 3), d(2→4)=45 (via 1); no path to 5.
- When the last vertex is chosen, no unmarked vertex is left to update; one more round is harmless but useless.
- v1 (10), v2 (20), v3 and v4 (both 30; on a tie the lower index first: v3, v4), then v5 (40), v6 (45).
- With a queue (Kahn): 0 1 2 3 5 4 (v3's list holds v5 before v4).
- Via v5, v7 gets 7 + 8 = 15 > 14 → earliest[7] = 15, the project takes 19; critical path v0-v3-v5-v7-v8 (5 + 2 + 8 + 4).
- k = 2: the path 1 → 2 → 0 costs 2 + 3 = 5 < 6.
Summary
Chapter Summary
Brute force O(nⁿ); Dijkstra is greedy and needs w ≥ 0: O(n²) or O((n + e) log n).
The predecessor array alone is enough; print it backward (recursion/stack).
Negative edges: Bellman-Ford O(ne). All pairs: Floyd-Warshall O(n³).
Counts + an embedded stack; O(n + e); it also detects cycles.
Forward: earliest with max; backward (reversed graph): latest with min. Slack = late − early.
The activities with zero slack; they set the project's length.
Summary
References
- E. Horowitz, S. Sahni, S. Anderson-Freed, Fundamentals of Data Structures in C, 2nd ed., §6.4 Shortest Paths, §6.5 Activity Networks.
- T. H. Cormen et al., Introduction to Algorithms, 4th ed., Section 20.4 (Topological Sort), Chapters 22 (Single-Source Shortest Paths) and 23 (All-Pairs).
- R. Sedgewick, Algorithms in C, Part 5, Chapter 21 (Shortest Paths).