Network Flow [00RN]
- November 9, 2026
We now turn to study another important classical model with many (surprising!) applications, network flow.
Definition Flow network [00RX]
- November 11, 2026
A flow network is a directed graph G = (V,E) with
- a distinguished source vertex s \in V, with indegree 0;
- a distinguished target (or sink) vertex t \in V, with outdegree 0;
- a capacity function c : E \to \mathbb {N} assigning a non-negative integer capacity c(e) to each edge e \in E.
Capacities can easily be generalized to non-negative rational or even real numbers, but we will stick with integers.
★ Generalized weights [00RY]
- November 11, 2026
The weights in a weighted graph can be used to model various concepts. How we combine the weights—or even what type of values are used for the weights—depends on what we have in mind. In general, the important questions to ask about weights are:
- How do we combine the weights of two paths in series (i.e. when the two paths are sequenced end-to-end into a longer path)?
- How do we combine the weights of two paths in parallel (i.e. when we have two different paths between the same two start and end vertices)?
- How do we decide which of two weights is "better"?
For example, when weights represent a cost:
- We add the weights of two paths in series.
- We take the minimum of the weights of parallel paths.
- Smaller weights are better.
However, if weights represent a capacity, we might answer these questions differently:
- When paths are in series, we take the minimum weight: the smallest-capacity path is the bottleneck and determines the capacity of the whole composed path.
- When paths are in parallel, we either take the maximum (if we want to pick the higher-capacity path) or we add (if we are allowed to use both paths at once and we know the paths don't overlap at all).
- Bigger weights are better.
As another example, if weights represent some kind of probability, then we might multiply weights in series and take the maximum in parallel. Weights don't even have to be numbers! If weights are, say, strings, we might concatenate them in series and take the shorter of two strings in parallel (what would a "best path" mean in this context?).
We introcued algorithms like Dijkstra and Floyd-Warshall as algorithms to find shortest paths, but really they are algorithms for finding "best" paths: we can plug in any operations for combining weights we like (as long as they satisfy some nice algebraic properties like distributivity). For example, if we substitute the operations (\min , \max ) in place of (+, \min ), then Dijkstra and Floyd-Warshall find maximum-bottleneck paths: i.e. paths such that the minimum capacity along the path is as large as possible.
Definition Flow [00RZ]
- November 11, 2026
A flow on a flow network G is a function f: E \rightarrow \mathbb {N} assigning a natural number flow value to each edge, such that
- 0 \leq f(e) \leq c(e) for each edge e \in E (that is, the flow along an edge is limited by the edge’s capacity).
- What flows in must flow out: for each vertex (besides s and t), \sum _{e \text { entering } v} f(e) = \sum _{e \text { leaving } v} f(e).
We will abbreviate the above as f^{\mathrm {in}}(v) = f^{\mathrm {out}}(v).
Definition Flow value [00S0]
- November 11, 2026
The value of a flow f is defined as the amount of flow leaving the source: v(f) = f^{\mathrm {out}}(s).
Note that because the total flow must be preserved at each vertex, it should be intuitively clear that f^{\mathrm {out}}(s) = f^{\mathrm {in}}(t) so we could equally well define the value of a flow v(f) as the amount of flow arriving at the target. In fact, proving this formally is a good exercise.
Lemma ★ Flow value preservation [00S1]
- November 11, 2026
f^{\mathrm {out}}(s) = f^{\mathrm {in}}(t), that is, “what leaves s must eventually arrive at t”.
Proof.
- November 11, 2026
Note first that \sum _{v \in V} f^{\mathrm {out}}(v) = \sum _{e \in E} f(e) = \sum _{v \in V} f^{\mathrm {in}}(v). The middle sum is just the sum of the flows along every edge. But since every edge has exactly one start vertex, the flow along any given edge gets included exactly once in the left-hand sum (as part of f^{\mathrm {out}}(v) for its start vertex). Likewise, each edge is included exactly once in the right-hand sum, with its end vertex.
Since the left- and right-hand sums are equal, we have 0 = \left (\sum _{v \in V} f^{\mathrm {out}}(v)\right ) - \left (\sum _{v \in V} f^{\mathrm {in}}(v)\right ) = \sum _{v \in V} (f^{\mathrm {out}}(v) - f^{\mathrm {in}}(v)). But by definition of a flow (property 2), f^{\mathrm {out}}(v) - f^{\mathrm {in}}(v) = 0 for every vertex v other than s or t. Note also that f^{\mathrm {in}}(s) = 0 and f^{\mathrm {out}}(t) = 0. Hence everything in this sum cancels except f^{\mathrm {out}}(s) - f^{\mathrm {in}}(t). Thus we have shown f^{\mathrm {out}}(s) - f^{\mathrm {in}}(t) = 0, that is, f^{\mathrm {out}}(s) = f^{\mathrm {in}}(t).
Playing around with a few examples leads us to an obvious question.
Definition Max flow [00S2]
- November 11, 2026
The max flow problem asks, given a flow network G, what is the maximum possible value of a flow (and what is an example of such a maximum flow)?
Before presenting a solution to this problem, we will first explore some applications.
Applications of maximum flow [00SI]
- November 12, 2026
Maximum bipartite matching [00SJ]
- November 12, 2026
Definition Matching [00SK]
- November 12, 2026
Given an undirected graph G, a matching on G is a subset of edges such that no two edges share an endpoint. In other words, a matching on G is a way to match or "pair up" some of vertices.
A maximum matching is a matching which contains as many edges as possible.
Max bipartite matching via max flow [00TP]
- November 25, 2026
Finding maximum matchings for graphs in general is difficult, but it's much easier for bipartite graphs:
- Add a new source vertex s with an edge to each vertex in L.
- Add a new target vertex t with an edge from each vertex in R.
- Give every edge a capacity of 1.
- A valid flow of value k must have k units of flow going from L to R; since the edges all have capacity 1 there must therefore be k edges with flow on them. Moreover, none of them can share an endpoint since then two units of flow would have to enter or exit the same vertex, but this is impossible since the edges from s and the edges to t all have capacity 1 as well.
- Conversely, given any k edges which share no endpoints, simply put one unit of flow along each edge, and continue the flow to s and t. This will be a valid flow for the same reasons.
Hence, a maximum flow must correspond to a maximum matching, and vice versa. So if we had an algorithm to find maximum flows, we could use it to compute maximum matchings in bipartite graphs.
Committee assignment [00TQ]
- November 25, 2026
Consider the following scenario:
- There are a number of committees; each committee has a desired number of people.
- There is a set of people; each person has (1) a list of committees they are willing to be on, and (2) a maximum total number of committees they are willing to be on.
We can solve this by creating a flow network, as follows:
- There is a vertex for each person, a vertex for each committee, and special source and sink vertices s and t.
- We add an edge from s each each person, with capacity equal to the total number of committees that person is willing to be on.
- From each person, we add edges of capacity 1 to each committee they are willing to be on.
- From each committee, we add an edge to the sink t with capacity equal to the desired size of the committee.
Since valid flows correspond to valid assignments, it is possible to fill all the committees if and only if a maximum flow saturates all the edges going into t.
Escape route planning [00TR]
- November 25, 2026
As another example application for finding max flows, imagine trying to plan escape routes for evacuating a city. Formally, we model a city as a directed graph. Each vertex has a weight corresponding to the number of people who start there (for example, we might assign every vertex some number corresponding to population density; or if we are trying to model evacuation from large events we might just assign a few vertices some large number, etc.). Certain vertices are designated as "escapes" (which could correspond to shelters, or simply to vertices on the edge of the graph), each having a capacity corresponding to the number of people it can handle. Each edge also has a capacity corresponding to how many people it can handle. The goal is to see how many people we can get from their starting locations to an escape, and plan which routes they should use.
Once again, we can make this into a flow network as follows:
- Create a source vertex s, and add an edge from s to each vertex v with capacity equal to the number of people that start at v.
- Create a sink vertex t, and add an edge from each escape vertex to t with capacity equal to the capacity of the escape.
Other max flow applications [00TS]
- November 25, 2026
There are many other interesting applications of maximum flows, including:
- Baseball elimination: max flow can be used to compute which baseball teams can still make the playoffs, and which have been definitely eliminated.
- Image segmentation: max flow can be used to separate images into background and foreground.
- Airline scheduling: a generalization of max flow which includes lower bounds on flows can be used to figure out optimal ways to schedule flights and crews for an airline.
Attempt: greedy max flow [00S3]
- November 11, 2026
How should we design an algorithm to solve the maximum flow problem? Well, let’s first try a greedy strategy, which looks for an unsaturated path from s to t (that is, a path for which the flow along every edge is less than the edge’s capacity), increases (augments) the flow along that path as much as possible, and then repeats until no unsaturated path is left.
Algorithm Greedy Flow [00S4]
- November 11, 2026
Greedy flow counterexample [00S5]
- November 11, 2026
Unfortunately, this greedy flow algorithm does not work. We may get stuck in a “local optimum” where there are no more unsaturated paths, but we have not found the globally maximum flow. Here is a counterexample.
However, all is not lost! We can actually keep the basic outline of the greedy algorithm. We really just need to allow ourselves to undo or “push back” flow if we find somewhere better for it to go.
Residual networks [00SB]
- November 12, 2026
For a given network G and flow f, we define the residual network G_f. The residual network has the same vertices as G and essentially has two edges for each edge e = (u,v) in G: one from u to v with weight equal to the remaining capacity c(e) - f(e), and one “backwards” edge from v to u with weight equal to the flow flow f(e) along e. The backwards edge allows us to “retract” some of the flow along e. The only wrinkle is that we don’t include edges with zero capacity, so some edges of G only have one corresponding edge in G_f: those with zero flow (which have only a corresponding forward edge) or those at maximum capacity (which have only a corresponding backward edge). Formally:
Definition Residual network [00SC]
- November 12, 2026
Given a flow network G=(V,E) and a flow f on G, we define the residual network G_{f} as a directed, weighted graph G_{f}=(V,E_f), with E_f = \{ e \in E \mid c(e) - f(e) > 0 \} \cup \{ e^R \mid e \in E, f(e) > 0 \} (where e^R denotes the reverse of edge e) and we define the weights of edges in E_f by
- w(e)=c(e)-f(e), and
- w(e^{R}) = f(e).
The Ford-Fulkerson algorithm [00SD]
- November 12, 2026
We can now define the Ford-Fulkerson algorithm, which is similar to the greedy flow algorithm but uses the residual network instead of the original network to find augmenting paths. Every time an augmenting path traverses a "forwards" edge in the residual network, we add flow to the corresponding edge in G; when an augmenting path traverses a "backwards" edge, we subtract flow from the corresponding edge in G.
Algorithm Ford-Fulkerson [00SE]
- November 12, 2026
We take a moment to record a few observations/lemmas about this algorithm.
Lemma Augmenting preserves flow validity [00SF]
- November 12, 2026
Augmenting along an s-t path in the residual network always preserves the flow properties.
Proof.
- November 12, 2026
- If we augment along two consecutive "forwards" edges in G_f, we add the same amount of flow to both incoming and outgoing edges of their common vertex in G.
- If we augment along two consecutive "backwards" edges, we subtract the same amount of flow to both an incoming and outgoing edge in G.
- If we augment along a forwards edge followed by a backwards edge, we add flow to one incoming edge and subtract flow from another incoming edge. Likewise, if we augment along a backwards edge followed by a forwards edge, we add and subtract the same amount of flow from two outgoing edges.
In addition, because of the way weights are defined on edges in the residual network, we can never add flow beyond capacity, or reduce a flow below zero.
Lemma Augmenting increases flow value [00SG]
- November 12, 2026
Augmenting along an s-t path in the residual network always increases the value of the flow in G.
Proof.
- November 12, 2026
Lemma Ford-Fulkerson terminates [00SH]
- November 12, 2026
The Ford-Fulkerson algorithm terminates (the flow can’t keep getting bigger forever).
Proof.
- November 12, 2026
Runtime analysis [00SM]
- November 12, 2026
The Ford-Fulkerson algorithm can be implemented to run in O(CE) time, where C is the total capacity of outgoing edges from the source.
- At every iteration, the flow value must increase by at least 1. Hence C is an upper bound on the number of iterations of the while loop.
- Finding an augmenting path can be done in \Theta (V+E) using something like DFS. In fact, there's not much point running this algorithm on a disconnected graph, so we can assume E is \Omega (V). Hence this is just \Theta (E).
- Augmenting along the path P and updating the residual network appropriately can be carried out in \Theta (E) worst-case time (constant time per edge of P) with careful choices of data structures. (In practice we store the information about flow and about G_f in the same data structure; we also need to keep pointers from each "backwards" edge to the corresponding forwards edge in order to augment efficiently.)
★ Maximum flow algorithms in practice [00SL]
- November 12, 2026
Ford-Fulkerson is actually a kind of meta-algorithm: it specifies the method of repeatedly augmenting along s-t paths in the residual network (augmenting paths) but it doesn’t actually specify how to find augmenting paths. Without specifying how to find augmenting paths at all, we proved that Ford-Fulkerson will run in O(EC) time, where E is the number of edges and C is the total capacity leaving the source (or the total capacity entering the sink, whichever is smaller). But if we are more careful about how we find augmenting paths we can do better.
- The most straightforward way is to simply use a DFS from s to t in the residual network. This works fine but not particularly well; depending on the graph it can exhibit the worst case O(EC) behavior.
- We can use BFS in the residual network to find the shortest augmenting path on each iteration. This is called the Edmonds-Karp algorithm, and it can be shown (though the proof is outside the scope of this course) that it runs in O(VE^2)—in particular, the running time does not depend on the capacities. This algorithm works very well in practice, and often runs much faster than the worst case, behaving more like \Theta (E^{3/2}).
- We could use a variant of Dijkstra’s algorithm to find the maximum capacity augmenting path on each iteration. This seems like it would be a good idea, but in practice it is usually slower than Edmonds-Karp.
- Finally, there is a clever method, Dinitz’s Algorithm, which instead of finding just a single augmenting path each iteration, sends flow along multiple augmenting paths at once. (Note: it is often known as Dinic’s Algorithm, but this is due to a misspelling of the name of its inventor, Yefim Dinitz.) It works as follows:
- Do a BFS from s in the residual network and label every vertex with its level in the BFS tree.
- Now do a DFS from s in the residual network, only taking edges which go from level i to i+1; as we recurse through the graph, keep track of the available flow and the minimum residual capacity along the path to each point. Once we reach s we can unroll the recursion and update flow values with the minimum capacity seen along the path, and subtract the augmenting flow from the available flow. Instead of stopping, however, we keep going to try to augment along as many paths as we can. If we encounter a vertex from which it’s impossible to reach t, we mark it so we know not to visit it if we ever encounter it again.
There are many other approaches to finding maximum flows as well (preflow-push/push-relabel, simplex…), which can have even better asymptotic running times, work better on certain types of graph, or solve more complex max flow variants (max circulation, min-cost max flow, …), but for most practical max flow problems you will encounter, Edmonds-Karp or Dinitz should work just fine!
Max flow/min cut theorem [00SN]
- November 12, 2026
We now present one of the great duality results in computer science—that the maximum flow of a network is equivalent to the minimum cut of a network. Along the way, we will also show that f is a max flow if and only there are no augmenting paths in the residual network G_{f}, which proves the correctness of Ford-Fulkerson.
Definition s-t cut [00SO]
- November 16, 2026
An s-t cut in a flow network G = (V,E) is a partition of V into two sets (A,B) where s \in A and t \in B.
Definition Cut capacity [00SP]
- November 16, 2026
The capacity of an s-t cut, denoted c(A,B), is the total capacity of all edges crossing the cut. That is, c(A,B) = \sum _{e : A \to B} c(e). (The notation e : A \to B denotes any edge which goes from some vertex in A to some vertex in B.)
Definition Minimum cut [00SQ]
- November 16, 2026
Definition Net flow [00SR]
- November 16, 2026
Remark [00SS]
- November 16, 2026
If we imagine the vertices of A being “clumped together” around the source vertex, then it’s clear what should happen: there will be no flow coming in to A, and the flow going out will just be equal to the value of the flow. But A could be crazier—it could consist of a bunch of vertices scattered all through the graph. They don’t even have to be connected. So what can we say about the net flow in general?
Lemma Flow Value Lemma [00ST]
- November 16, 2026
Let f be any flow and let (A,B) be any s-t cut in a flow network G. Then the net flow of (A,B) with respect to f equals the value of f. That is, v(A,B) = v(f).
Proof.
- November 16, 2026
The remaining step that needs explanation is the central one that goes from \sum _{v \in A} (f^{\mathrm {out}}(v) - f^{\mathrm {in}}(v)) to \sum _{e : A \to B} f(e) - \sum _{e : B \to A} f(e). To see why this is true, consider all the edges with at least one endpoint in A.
- If an edge has both endpoints in A, its flow contributes twice to the first sum, once positively and once negatively, and the two cancel out. (Intuitively: moving stuff around within A does not affect its net exports.)
- Edges leaving A contribute positively to the sum.
- Edges entering A contribute negatively to the sum.
Lemma Bottleneck lemma [00SU]
- November 16, 2026
Let f be any flow and (A,B) be any s-t cut. Then the capacity of the cut is at least the value of the flow, that is, v(f) \leq c(A,B).
Proof.
- November 16, 2026
Remark
- November 16, 2026
From the proof we can see that v(f) = c(A,B) exactly when (1) there is no flow entering A and (2) every edge leaving A is saturated (that is, f(e) = c(e)).
Corollary Saturated cut = max flow + min cut [00T2]
- November 17, 2026
Given some flow f and s-t cut (A,B), if v(f) = c(A,B) then f is a max flow and (A,B) is a min cut.
Proof.
- November 17, 2026
Theorem Max flow/min cut [00T3]
- November 17, 2026
Let f be any flow on a flow network G. The following are equivalent:
- v(f) = c(A,B) for some cut (A,B).
- f is a max flow.
- There are no s-t paths in the residual network G_{f}.
Remark
- November 17, 2026
As an aside, theorems of this form ("the following are equivalent") do not come up as often in computer science, but they are a lot of fun. (We already saw one other similar example earlier.) How do we prove theorems of this form? If we think of each proposition as a vertex, and proving that P implies Q corresponds to adding a directed edge from P to Q, then our goal is to ensure that the resulting graph is strongly connected, so there is a path from any proposition to any other. Since implication is transitive, this shows that all the propositions are logically equivalent.
Proof.
- November 17, 2026
- 1 \Rightarrow 2 is just Saturated cut = max flow + min cut.
- 2 \Rightarrow 3 because Augmenting increases flow value: if there were an s-t path in G_{f}, then we could increase the flow along that path, so f would not be a max flow. (This is the contrapositive of 2 \Rightarrow 3.)
(3 \Rightarrow 1) is more interesting. Suppose there are no s-t paths in G_f; then we must somehow come up with a cut (A,B) whose capacity is equal to that of the flow! To this end, define an s-t cut (A,B) as follows:
- A = all nodes reachable from s in G_{f}.
- B = everything else.
Notice that
- s \in A (s is trivially reachable from itself).
- t \in B (t can’t be in A, since we assumed there are no s-t paths in G_{f}).
The Flow Value Lemma tells us that the value of the flow is equal to the net flow across the cut (A,B), that is, v(f) = v(A,B) = \sum _{e : A \to B} f(e) \;\; - \sum _{e : B \to A} f(e).
Note that each edge (u,v) leaving A must be filled to capacity, since otherwise there would be a forwards edge from u to v with the remaining capacity in G_f, but then by definition we would have v \in A since v would be reachable in G_f. Similarly, each edge entering A must have 0 flow since if it didn’t, there would be a backwards edge from v to u in G_f, and then u would be in A as well.
Hence, we have
Corollary Ford-Fulkerson is correct [00T4]
- November 17, 2026
The Ford-Fulkerson algorithm is correct.
Proof.
- November 17, 2026