Topological Sorting & Network Flow
Understand how to order dependencies using Topological Sorting, and learn how to calculate the maximum possible data/liquid throughput in a network using the Ford-Fulkerson method.
Learning Goals
- Identify the graph properties required for Topological Sorting (Directed Acyclic Graphs).
- Calculate the in-degree of vertices to trace Kahn's algorithm for Topological Sort.
- Trace the Ford-Fulkerson algorithm to find the maximum flow in a network.
- Identify augmenting paths and bottleneck capacities in a residual graph.
Topological Sorting
Topological Sorting is used to linearly order the vertices of a graph such that for every directed edge , vertex comes before in the ordering. This is fundamentally used for dependency resolution (e.g., scheduling tasks, compiling software where library A must be built before library B).
The Strict Constraint: Topological Sorting is only possible in Directed Acyclic Graphs (DAGs).
- If the graph is undirected, there is no strict dependency.
- If the graph has a cycle (A depends on B, B depends on C, C depends on A), it creates a deadlock, and sorting is impossible.
Time Complexity: Since it is based on Depth-First Search (DFS) or Breadth-First Search (Kahn's Algorithm), the time complexity is .
Trace: Kahn's Algorithm for Topological Sort
- 1Step 1
Kahn's algorithm relies on calculating the In-Degree of every vertex (the number of incoming edges). A vertex with an in-degree of
0has no dependencies and can be processed immediately. - 2Step 2
- Calculate the in-degree of all vertices.
- Enqueue all vertices with an in-degree of
0. - While the queue is not empty:
- Dequeue a vertex and add it to the final sorted output.
- For every neighbor of , theoretically 'remove' the edge by decreasing the in-degree of by 1.
- If the in-degree of becomes
0, enqueue .
- 3Step 3
If the queue becomes empty but the sorted output does not contain all vertices, it means the remaining vertices all have an in-degree . This mathematically proves the graph contains a cycle, and topological sorting has failed.
Network Flow: The Ford-Fulkerson Method
(Directly answers the 7-mark numerical from 2024 Q7b)
In a flow network, edges represent pipes with a specific Capacity. There is a Source (S) that produces infinite flow, and a Sink (T) that absorbs it. The goal is to find the Maximum Flow that can be pushed from S to T without exceeding any pipe's capacity.
Core Rules:
- Capacity Constraint: Flow through an edge cannot exceed its capacity.
- Flow Conservation: For every node (except S and T), the total incoming flow must exactly equal the total outgoing flow.
Trace: Ford-Fulkerson Max Flow Algorithm
- 1Step 1
Consider the exact network from the 2024 Q7b exam:
- 2Step 2
We find any path from S to T that still has capacity. Let's take
S -> A -> B -> T.- The capacities are: SA(10), AB(4), BT(10).
- The Bottleneck (minimum capacity on this path) is 4.
- We push 4 units of flow.
- Residual Capacities left: SA = 6, AB = 0, BT = 6.
- Max Flow so far: 4.
- 3Step 3
We find another path:
S -> C -> D -> T.- The residual capacities are: SC(10), CD(9), DT(10).
- The Bottleneck is 9.
- We push 9 units of flow.
- Residual Capacities left: SC = 1, CD = 0, DT = 1.
- Max Flow so far: 4 + 9 = 13.
- 4Step 4
Are there any paths left from S to T?
- S can send to A (6 left) or C (1 left).
- From A, we can only go to C (2 left) because AB is full (0 left).
- From C, we cannot go anywhere because CD is full (0 left).
Because the pipe
C -> DandA -> Bare completely bottlenecked, we can no longer reach the Sink T.
- 5Step 5
Since no more augmenting paths exist, the algorithm terminates. The Final Maximum Flow Value is 13. (Note: This matches the capacity of the minimum cut through edges AB and CD: 4 + 9 = 13, proving our answer is optimal via the Max-Flow Min-Cut theorem).
Knowledge Check
Topological sorting is only possible in: