Network flow: shortest path, minimal spanning tree, maximal flow
Dijkstra's shortest-path labelling, Kruskal and Prim minimal spanning trees, and Ford–Fulkerson maximal flow with the max-flow min-cut theorem.
Drafted with Aria, reviewed by the AiCanCode.org team. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Plant layouts, material-handling routes, pipelines, power and data cables, and supply chains are all networks of nodes and arcs. Three classic questions cover most of their design: the cheapest or quickest route between two points (shortest path), the cheapest way to connect every point (minimal spanning tree), and the most material that can be pushed through limited capacities (maximal flow). Each has a simple, exact hand algorithm.
Key ideas
Vocabulary. A network has nodes (vertices) and arcs or edges, each with a length, cost or capacity. Arcs may be directed (one-way) or undirected. A path is a sequence of connected arcs; a cycle returns to its start; a tree is a connected network with no cycle; a spanning tree connects all n nodes using exactly n − 1 edges.
Shortest path — Dijkstra's algorithm (all arc lengths ≥ 0).
- Give the source a permanent label 0; all other nodes a temporary label ∞.
- From the most recently made permanent node u, update each neighbour v: label(v) = min(label(v), label(u) + l(u, v)), recording the predecessor.
- Make the smallest temporary label permanent. Repeat until the destination (or every node) is permanent.
- Trace predecessors back to read the route. A permanent label is final because all arc lengths are non-negative. With negative arcs use Bellman–Ford (which also detects negative cycles).
Minimal spanning tree (MST).
- Kruskal: sort edges by weight; add the next lightest edge if it does not form a cycle; stop at n − 1 edges.
- Prim: start at any node; repeatedly add the lightest edge joining the connected set to a new node. Both are greedy and exact. The MST minimises total edge length, not the distance between any particular pair of nodes — it is not a shortest-path tree.
Maximal flow — Ford–Fulkerson (augmenting paths).
- Start with zero flow.
- Find a path from source to sink on which every arc has residual capacity > 0 (forward arcs: capacity − flow; reverse arcs: flow already sent, which can be cancelled).
- Send the bottleneck (smallest residual) along the path and update residuals.
- Repeat until no augmenting path exists. Flow is conserved at every intermediate node (in = out). Choosing the shortest augmenting path each time (Edmonds–Karp) guarantees a polynomial number of steps.
Max-flow min-cut theorem. A cut separates the source from the sink; its capacity is the sum of capacities of arcs going from the source side to the sink side. The maximum flow equals the minimum cut capacity. The min cut identifies the bottleneck arcs worth expanding.
Formulas
- Dijkstra update:
d(v) = min{ d(v), d(u) + l(u, v) } - Edges in a spanning tree:
n − 1 - Flow conservation at node k (not source/sink):
Σ f(i, k) = Σ f(k, j) - Capacity limit:
0 ≤ f(i, j) ≤ c(i, j) - Bottleneck of a path:
δ = min residual capacity on the path - Max-flow min-cut:
max flow value = min over cuts of Σ c(i, j), i on source side, j on sink side
Symbols: d(v) = current shortest-distance label of node v (km, min, ₹); l(u, v) = arc length; n = number of nodes; f(i, j) = flow on arc i→j (units/h); c(i, j) = arc capacity (units/h).
Worked examples
Example 1 (shortest path and MST on the same network). Undirected edges (km): 1–2: 7, 1–3: 9, 1–6: 14, 2–3: 10, 2–4: 15, 3–4: 11, 3–6: 2, 4–5: 6, 5–6: 9. Find the shortest route from 1 to 5 and the MST.
- Permanent 1 (0). Temporary: 2 = 7, 3 = 9, 6 = 14.
- Make 2 permanent (7). Update: 3 via 2 = 17 (keep 9), 4 = 7 + 15 = 22.
- Make 3 permanent (9). Update: 4 = min(22, 9 + 11) = 20, 6 = min(14, 9 + 2) = 11.
- Make 6 permanent (11). Update: 5 = 11 + 9 = 20.
- Make 4 (20) and 5 (20) permanent. Predecessors of 5: 6 → 3 → 1.
- Shortest route 1–3–6–5, length 20 km.
- Kruskal for the MST: 3–6 (2) ✓, 4–5 (6) ✓, 1–2 (7) ✓, 1–3 (9) ✓, 5–6 (9) ✓ — five edges for six nodes, stop (2–3 and 3–4 would close cycles). MST length = 2 + 6 + 7 + 9 + 9 = 33 km.
Example 2 (GATE-type maximal flow). Directed capacities (units/h): S→A 10, S→B 8, A→B 2, A→C 6, A→T 5, B→C 7, C→T 9. Find the maximum flow from S to T.
- Path S–A–T: bottleneck min(10, 5) = 5. Flow = 5. Residual: S→A 5, A→T 0.
- Path S–A–C–T: bottleneck min(5, 6, 9) = 5. Flow = 10. Residual: S→A 0, A→C 1, C→T 4.
- Path S–B–C–T: bottleneck min(8, 7, 4) = 4. Flow = 14. Residual: C→T 0.
- No path remains: both arcs into T (A→T and C→T) are saturated.
- Min cut: {S, A, B, C} | {T} with capacity 5 + 9 = 14, equal to the flow.
- Maximum flow = 14 units/h. Raising A→T or C→T capacity is the only way to increase it.
Common mistakes
- Making a node permanent before it has the smallest temporary label, or using Dijkstra with negative arcs.
- Confusing the MST with the shortest path: the MST route between two nodes is generally not their shortest path.
- Adding an edge in Kruskal that closes a cycle, or stopping before n − 1 edges.
- In max flow, forgetting reverse (cancellation) arcs, which can stop the algorithm too early.
- Counting arcs that point from the sink side back to the source side in a cut capacity.
- Assuming the source's out-arc capacities give the max flow; the bottleneck may be anywhere.
For GATE PI
- Shortest distance between two nodes in a 6–8 node network by Dijkstra (NAT).
- MST total length by Kruskal or Prim; number of edges in a spanning tree.
- Maximum flow and identifying the minimum cut.
- Practise keeping a clean label table — most errors are bookkeeping, not concept.
Quick check
- How many edges does a spanning tree of 9 nodes have?
- Why can Dijkstra not be used with negative arc lengths?
- A cut's forward arcs have capacities 4, 6 and 5. What is the most the max flow can be?
- Does Kruskal's algorithm work when some edge weights are negative?
Answers: 1. 8. 2. A permanent label could later be improved through a negative arc, breaking the algorithm's logic. 3. 15 (any cut is an upper bound on the flow). 4. Yes — only the order of the weights matters.
Interview questions
All Operations Research interview questionsTry answering each one aloud before you open it.
1.What is the shortest path problem in network flow?Concept
The shortest path problem involves finding the path between two nodes in a graph such that the sum of the weights of its constituent edges is minimized. This is commonly used in routing and navigation systems to determine the most efficient route.
2.Explain the concept of a minimal spanning tree.Concept
A minimal spanning tree (MST) is a subset of the edges of a connected, edge-weighted graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. It is used in network design, such as designing the layout of electrical grids or computer networks.
3.What is the maximal flow problem in network flow?Concept
The maximal flow problem involves finding the greatest possible flow from a source node to a sink node in a flow network, subject to capacity constraints on the edges. This is used in various applications like traffic engineering and network routing.
4.Why is Dijkstra's algorithm used for finding the shortest path?Application
Dijkstra's algorithm is used for finding the shortest path because it efficiently computes the shortest paths from a single source vertex to all other vertices in a graph with non-negative edge weights. It is widely used due to its simplicity and effectiveness in many practical applications.
5.What happens if you apply Kruskal's algorithm to a graph with negative edge weights?Application
Kruskal's algorithm can still be applied to graphs with negative edge weights, as it only requires sorting the edges by weight and does not depend on the sign of the weights. The algorithm will still find the minimal spanning tree, as it focuses on minimizing the total edge weight.
6.Explain how the Ford-Fulkerson method finds the maximal flow in a network.Concept
Starting from zero flow, it repeatedly finds an augmenting path from source to sink in the residual network — forward arcs with spare capacity, or reverse arcs that cancel flow already sent — and pushes the path's bottleneck capacity along it. When no augmenting path remains, the flow is maximal. At that point the nodes reachable from the source define a cut whose capacity equals the flow, which is the max-flow min-cut theorem and identifies the bottleneck arcs.
7.How does the Bellman-Ford algorithm differ from Dijkstra's algorithm?Application
The Bellman-Ford algorithm differs from Dijkstra's algorithm in that it can handle graphs with negative edge weights and can detect negative weight cycles. While Dijkstra's algorithm is more efficient for graphs with non-negative weights, Bellman-Ford is more versatile for graphs where negative weights are present.
8.Calculate the shortest path from node A to node D in a graph with edges: A-B (1), B-C (2), A-C (2), C-D (1).Numerical
- Start at node A. Possible paths are A-B-C-D and A-C-D.
- Calculate the total weight for A-B-C-D: 1 (A-B) + 2 (B-C) + 1 (C-D) = 4.
- Calculate the total weight for A-C-D: 2 (A-C) + 1 (C-D) = 3.
- The shortest path is A-C-D with a total weight of 3.
9.Find the minimal spanning tree for a graph with nodes A, B, C, D and edges: A-B (4), B-C (1), C-D (3), A-D (2).Numerical
- List the edges in increasing order of weight: B-C (1), A-D (2), C-D (3), A-B (4).
- Start with the smallest edge B-C (1).
- Add A-D (2) as it does not form a cycle.
- Add C-D (3) as it does not form a cycle.
- The minimal spanning tree includes edges B-C, A-D, and C-D with a total weight of 6.
10.Why is the Edmonds-Karp algorithm used in network flow problems?Application
The Edmonds-Karp algorithm is used in network flow problems because it is a specific implementation of the Ford-Fulkerson method that uses breadth-first search to find augmenting paths. This ensures that the algorithm runs in polynomial time, making it more efficient for larger networks.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?