Skip to content

Commit 83ad082

Browse files
Max Flow notes
1 parent 8256c53 commit 83ad082

6 files changed

Lines changed: 219 additions & 0 deletions
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Alg-Graph
6+
---
7+
# 05.2 - Max Flow - Max-Flow Min-Cut
8+
## Outline
9+
1. max-flow = min st-cut theorem
10+
1. image segmentation
11+
2. Correctness of Ford-Fulkerson algorithm
12+
13+
## Ford-Fulkerson Alg
14+
**input:** flow network
15+
- directed graph: $G=(V,E)$
16+
- start node and end node: $s,t \in V$
17+
- capacities: $c_e > 0$
18+
19+
**output:**
20+
- flow $f^*$ of max size
21+
- $size(f)=f_{out}(s)=f_{in}(t)$
22+
23+
When does Ford-Fulkerson stop? It stops when there is no augmenting path in residual $G^{f^*}$.
24+
25+
**Lemma:** For a flow $f^*$, if no augmenting path in $G^{f^*}$ exists, then $f^*$ is a max-flow in $G$
26+
27+
## Verifying Max Flow
28+
> **Q:** Given a flow network and a flow $f$, what is the time to check whether or not $f$ is a max flow?
29+
>
30+
> **A:** $O(n+m)$
31+
32+
There is no augmenting path in $G^{f^*}$ if there is no path from $s$ to $t$. This can be accomplished with a DFS, which is $O(n+m)$.
33+
34+
## Min-Cut Problem
35+
- A $cut$ is a partition of $V=L \cup R$
36+
- An "st-cut" is a cut where $s \in L$ and $t \in R$
37+
- Neither $L$ nor $R$ needs to be a connected set.
38+
39+
![[Pasted image 20260311195056.png]]
40+
41+
Below is the "capacity" of the cut. This is the sum of edges $vw$ where $v$ is in $L$ and $w$ is in $R$.
42+
43+
$$capacity(L,R)=\sum_{vw \in E, \space v \in L, \space w \in R}c_{vw}$$
44+
45+
## Problem Formulation
46+
**input:** flow network
47+
48+
**output:**
49+
- st-cut$(L,R)$ with minimum capacity.
50+
51+
On the flow network from [[05.1.1 - Max Flow - FF]], the screenshot below demonstrates the minimum cut. The minimum cut is 12, which equals the max flow on the same flow network. This proves the max-flow = min-cut theorem (/s).
52+
53+
![[Pasted image 20260311195427.png]]
54+
55+
56+
## Theorem
57+
> For a given flow network, the size of the max flow equals the minimum capacity of an st-cut.
58+
59+
### Proof 1 - max-flow $\le$ min st-cut
60+
For any flow $f$ and any st-cut$(L,R)$:
61+
62+
$$size(f) \le capacity(L,R)$$
63+
$$
64+
\max_f \{size(f)\} \le \min_{(L,R)} \{capacity(L,R)\}$$
65+
66+
**Claim:** $size(f) = f_{out}(L) - f_{in}(L)$
67+
68+
$$f_{out}(L)-f_{in}(L)=$$
69+
$$\sum_{vw \in E, \space v \in L, \space w \in R} f_{vw} - \sum_{wv \in E, \space w \in R, \space v \in L }f_{wv}+\sum_{vw \in E, \space v \in L, \space w \in L } f_{vw}-\sum_{wv \in E, \space w \in L, \space v \in L}f_{wv}$$
70+
71+
- First summation
72+
- The sum of the flows for each edge (v,w) where v is in L and w is in R.
73+
- This is the sum of the flows leaving L to R
74+
- Second term
75+
- The sum of the flows for each edge (w,v) where w is in R and v is in L.
76+
- This is the sum of flows leaving R to L
77+
- Third term
78+
- The sum of the flows for each edge (v,w) where v is in L and w is in L
79+
- This is the sum of flows which are internal to L
80+
- Fourth term
81+
- The sum of flows for each edge (w,v) where w is in L and v is in L
82+
- This is another sum of flows which are internal to L
83+
- The third term and fourth term appear to cancel out.
84+
85+
The first and third terms give us $f_{out}(L)$.
86+
$$f_{out}(L)=\sum_{v \in L}f_{out}(v)$$
87+
88+
The second and fourth terms give us $f_{out}(v)$
89+
$$f_{out}(v)=\sum_{v \in L}f_{in}(v)$$
90+
91+
The set $L$ contains the source vertex $s$. We know that the flow in to $s$ is $0$, and that the flow out of $s$ contains the flow out of $L$.
92+
93+
$$f_{out}(L)-f_{in}(L)=\bigg(\sum_{v \in (L - s)}\big(f_{out}(v)-f_{in}(v)\big)\bigg) + f_{out}(s)$$
94+
95+
What do we know about the summation? We know that the flow out of any vertex must equal the flow in to the vertex. That's one of the conditions for a valid flow. Therefore, we can drop the whole summation.
96+
97+
$$\sum_{v \in (L - s)}\big(f_{out}(v)-f_{in}(v)\big)=0$$
98+
$$f_{out}(L)-f_{in}(L)=f_{out}(s)=size(f)$$
99+
100+
$$size(f)=f_{out}(L)-f_{in}(L) \le f_{out}(L) \le cap(L,R)$$
101+
102+
### Proof 2 - max-flow $\ge$ min st-cut
103+
104+
$$\max_f \{ size(f) \} \ge \min_{(L,R)} cap(L,R)$$
105+
106+
- Take flow $f^*$ from Ford-Fulkerson alg
107+
- $f^*$ has no st-path in residual $G^{f^*}$
108+
- we'll construct $(L,R)$ where:
109+
110+
$$\max_f \{ size(f) \} \ge size(f^*) = cap(L,R) \ge \min_{(L,R)} cap(L,R)$$
111+
112+
The operative bit in this proof is $size(f^*) = cap(L,R)$. Take flow $f^*$ with no st-path in residual $G^{f^*}$. Let $L=$ vertices reachable from $s$ in $G^{f^*}$.
113+
- We know $t \notin L$.
114+
- Let $R = V - L$
115+
116+
What are the properties of the cut we just constructed?
117+
- Flow $f^*$ with no st-path in $G^{f^*}$
118+
- $L=$ vertices reachable from $s$ in $G^{f^*}$
119+
120+
See below for an example graph with residuals labelled on the original graph. The graph on the right is the residual network where only some amount of capacity remains.
121+
122+
The set $L$ is labelled on the graph. The remaining 3 vertices are set $R$. The edge capacities are no longer important. We can view the graph as an unweighted directed graph, and simply check for connectivity via DFS / BFS / SCC.
123+
124+
![[Pasted image 20260311223624.png]]
125+
126+
Notice how edges $sc$, $dc$, and $dt$ do not appear in the residual network. This means that in this residual graph, $f^*_{out}(L)=cap(L,R)$.
127+
128+
Notice also how edge $ef$ DOESN'T appear in the residual network. That's because there is no flow running along edge $fe$. If there was flow running along that edge, then $f$ would be included in the residual network. From this, we can conclude that $f^*_{in}(L)=0$
129+
130+
$$size(f^*)=cap(L,R)-0$$
131+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Alg-Graph
6+
---
7+
# 05.3 - Max Flow - Edmonds-Karp (EK)
8+
## Ford-Fulkerson (FF) vs Edmonds-Karp (EK)
9+
10+
FF
11+
- Find augmenting paths using DFS or BFS
12+
- $O(mC)$ where $C$ is the size of the max flow
13+
- assumes integer capacities
14+
15+
EK
16+
- Finds augmenting paths using BFS
17+
- $O(m^2n)$ time
18+
- Does not have assumptions on capacities
19+
20+
For a reminder of how FF works, see
21+
- [[05.1.1 - Max Flow - FF]]
22+
- [[05.1.2 - Max Flow - FF Supplement]]
23+
24+
## FF Algorithm
25+
1. Set $f_e=0$ for all $e \in E$
26+
2. Build the residual network $G^f$ for current flow $f$
27+
3. Check for a st-path in $G^f$ using DFS or BFS. If no such path, then output $f$
28+
4. Given $P$, let $c(P)=$ min capacity along $P$ in $G^f$
29+
5. Augment $f$ by $c(P)$ units along $P$
30+
6. Repeat from step 2 until no such st-path
31+
32+
## EK Algorithm
33+
Does not assume that capacities are integer values. Does require that capacities are positive.
34+
35+
1. Set $f_e=0$ for all $e \in E$
36+
2. Build the residual network $G^f$ for current flow $f$
37+
3. Check for a st-path $P$ in $G^f$ **using BFS**. If no such path, then output $f$
38+
4. Given $P$, let $c(P)=$ min capacity along $P$ in $G^f$
39+
5. Augment $f$ by $c(P)$ units along $P$
40+
6. Repeat from step 2 until no such st-path
41+
42+
The key property of EK is that at least one edge is removed from $G^f$ (or added back in to $G^f$) on each iteration.
43+
44+
## Proof Outline
45+
**Running Time:** $O(m^2n)$
46+
47+
- We need to prove that the number of rounds is $\le mn$
48+
- In every round, residual graph changes ($\ge 1$ edge deleted)
49+
- **Key Lemma:** For every edge $e$, $e$ is deleted and reinserted later $\le n/2$ times.
50+
- Since there are $m$ edges, there are $nm/2$ total rounds, which is $\le mn=O(mn)$
51+
52+
## BFS Properties
53+
54+
(Continue from here next time)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
---
6+
# 2026-03-11 - Office Hours with Dr. Brito
7+
## Solution 1 to HW
8+
We can modify Dijkstra such that we track the size of the largest/smallest edge on the path so far, instead of the total
9+
10+
This runs in $O((n+m) \space log \space n)$.
11+
12+
We can't modify the black-box algorithms in this course. Therefore, this solution is off-limits. The point was the force the actual solution, which involves Prim's and/or Kruskal's.
13+
14+
[[04.0.1 - Graphs - Black Box Algorithms]]
15+
16+
## Solution 2
17+
- $m=min\space\{w(e):e \in P\}$
18+
- $M=min \space\{w(e) : e \in P\}$
19+
20+
We want the path from $s$ to $t$ with the largest "smallest edge".
21+
22+
- Let $G_d=(V,E_d)$
23+
- $E_d = \{e \in E : w(e) \ge d\}$
24+
25+
- For $d : M \rightarrow m:$
26+
- Explore($G_d, s)$
27+
- Stop when we find the first time that $t$ is visited!
28+
29+
This outputs the correct result. We iterate backwards through edges in order of weight. Once we add enough edges to $G_d$ such that there is a path from $s$ to $t$, then that path maximizes the minimum weight of all edges from $s$ to $t$. This algorithm is $O((n+m)(M-m))$.
30+
31+
If we binary search over $(M, ..., m)$, then we can get this down to $O((n+m) \space log \space M)$
32+
33+
## Solution 3
34+
"Maximum Spanning Tree"
78.1 KB
Loading
95.5 KB
Loading
368 KB
Loading

0 commit comments

Comments
 (0)