![Page 1: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/1.jpg)
CSE 326: Data StructuresNetwork Flow and APSP
Ben Lerner
Summer 2007
![Page 2: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/2.jpg)
2
Network Flows
• Given a weighted, directed graph G=(V,E)
• Treat the edge weights as capacities
• How much can we flow through the graph?
A
C
B
D
FH
G
E
17
11
56
4
12
13
23
9
10
4I
611
20
![Page 3: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/3.jpg)
3
Network flow: definitions
• Define special source s and sink t vertices• Define a flow as a function on edges:
– Capacity: f(v,w) <= c(v,w)– Skew symmetry: f(v,w) = -f(w,v)– Conservation: for all u
except source, sink
– Value of a flow:
– Saturated edge: when f(v,w) = c(v,w)
Vv
vuf 0),(
v
vsff ),(
![Page 4: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/4.jpg)
4
Network flow: definitions
• Capacity: you can’t overload an edge
• Skew symmetry: sending f from uv implies you’re “sending -f”, or you could “return f” from vu
• Conservation: Flow entering any vertex must equal flow leaving that vertex
• We want to maximize the value of a flow, subject to the above constraints
![Page 5: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/5.jpg)
5
Example (1)
3
2
2
1
2
2
4
4
Capacity
![Page 6: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/6.jpg)
6
Example (2)
3/3
0/2
0/2
1/1
2/2
2/2
0/4
3/4
Flow / CapacityAre all the constraints satisfied?
![Page 7: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/7.jpg)
7
Main idea: Ford-Fulkerson method
• Start flow at 0• “While there’s room for more flow, push
more flow across the network!”– While there’s some path from s to t, none of
whose edges are saturated– Push more flow along the path until some
edge is saturated
– Called an “augmenting path”
![Page 8: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/8.jpg)
8
How do we know there’s still room?
• Construct a residual graph: – Same vertices– Edge weights are the “leftover” capacity on
the edges– Add extra edges for backwards-capacity too!
– If there is a path st at all, then there is still room
![Page 9: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/9.jpg)
9
Example (1)
A
B C
D
FE
3
2
2
1
2
2
4
4
Flow / Capacity
Initial graph – no flow
![Page 10: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/10.jpg)
10
Example (2)
A
B C
D
FE
0/3
0/2
0/2
0/1
0/2
0/2
0/4
0/4
Flow / CapacityResidual Capacity
3
2
4
1
2
4
2
2
Include the residual capacities
![Page 11: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/11.jpg)
11
Example (3)
1/3
0/2
0/2
1/1
0/2
0/2
0/4
1/4
Flow / CapacityResidual Capacity
2
2
4
0
2
3
2
2
A
B C
D
FE
Augment along ABFD by 1 unit (which saturates BF)
![Page 12: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/12.jpg)
12
Example (4)
3/3
0/2
0/2
1/1
2/2
2/2
0/4
3/4
Flow / CapacityResidual Capacity
0
2
4
0
0
1
0
2
A
B C
D
FE
Augment along ABEFD (which saturates BE and EF)
![Page 13: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/13.jpg)
13
Now what?
• There’s more capacity in the network…
• …but there’s no more augmenting paths
• But we broke our own rules – didn’t build the residual graph correctly
![Page 14: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/14.jpg)
14
Example (5)
3/3
0/2
0/2
1/1
2/2
2/2
0/4
3/4
Flow / CapacityResidual CapacityBackwards flow
0
2
4
0
0
1
0
2
2
1
2
3
3
A
B C
D
FE
Add the backwards edges, to show we can “undo” some flow
![Page 15: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/15.jpg)
15
Example (6)
3/3
2/2
2/2
1/1
0/2
2/2
2/4
3/4
Flow / CapacityResidual CapacityBackwards flow
0
0
2
0
0
1
2
0
2
1
2
3
3
A
B C
D
FE2
Augment along AEBCD (which saturates AE and EB, and empties BE)
![Page 16: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/16.jpg)
16
Example (7)
3/3
2/2
2/2
1/1
0/2
2/2
2/4
3/4
Flow / CapacityResidual CapacityBackwards flow
A
B C
D
FE
Final, maximum flow
![Page 17: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/17.jpg)
17
How should we pick paths?
• Two very good heuristics (Edmonds-Karp):– Pick the largest-capacity path available
• Otherwise, you’ll just come back to it later…so may as well pick it up now
– Pick the shortest augmenting path available• For a good example why…
![Page 18: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/18.jpg)
18
Bad example
A
B
C
D
0/2000 0/2000
0/2000 0/2000
0/1
Augment along ABCD, then ACBD, then ABCD, then ACBD…
Should just augment along ACD, and ABD, and be finished
![Page 19: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/19.jpg)
19
Running time?
• Each augmenting path can’t get shorter…and it can’t always stay the same length– So we have at most O(E) augmenting paths to
compute for each possible length, and there are only O(V) possible lengths.
– Each path takes O(E) time to compute
• Total time = O(E2V)
![Page 20: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/20.jpg)
20
One more definition on flows
• We can talk about the flow from a set of vertices to another set, instead of just from one vertex to another:
– Should be clear that f(X,X) = 0– So the only thing that counts is flow between
the two sets
Xx Yy
yxfYXf ),(),(
![Page 21: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/21.jpg)
21
Network cuts
• Intuitively, a cut separates a graph into two disconnected pieces
• Formally, a cut is a pair of sets (S, T), such that
and S and T are connected subgraphs of G
{}
TS
TSV
![Page 22: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/22.jpg)
22
Minimum cuts
• If we cut G into (S, T), where S contains the source s and T contains the sink t,
• What is the minimum flow f(S, T) possible?
![Page 23: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/23.jpg)
23
Example (8)
A
B C
D
FE
3
2
2
1
2
2
4
4
TS
Capacity of cut = 5
![Page 24: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/24.jpg)
24
Coincidence?
• NO! Max-flow always equals Min-cut• Why?
– If there is a cut with capacity equal to the flow, then we have a maxflow:
• We can’t have a flow that’s bigger than the capacity cutting the graph! So any cut puts a bound on the maxflow, and if we have an equality, then we must have a maximum flow.
– If we have a maxflow, then there are no augmenting paths left• Or else we could augment the flow along that path, which would
yield a higher total flow.– If there are no augmenting paths, we have a cut of capacity
equal to the maxflow• Pick a cut (S,T) where S contains all vertices reachable in the
residual graph from s, and T is everything else. Then every edge from S to T must be saturated (or else there would be a path in the residual graph). So c(S,T) = f(S,T) = f(s,t) = |f| and we’re done.
![Page 25: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/25.jpg)
25
![Page 26: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/26.jpg)
26
Single-Source Shortest Path• Given a graph G = (V, E) and a single
distinguished vertex s, find the shortest weighted path from s to every other vertex in G.
All-Pairs Shortest Path:• Find the shortest paths between all
pairs of vertices in the graph.
• How?
![Page 27: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/27.jpg)
27
Analysis
• Total running time for Dijkstra’s:O(|V| log |V| + |E| log |V|) (heaps)
What if we want to find the shortest path from each point to ALL other points?
![Page 28: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/28.jpg)
28
Dynamic Programming
Algorithmic technique that systematically records the answers to sub-problems in a table and re-uses those recorded results (rather than re-computing them).
Simple Example: Calculating the Nth Fibonacci number.
Fib(N) = Fib(N-1) + Fib(N-2)
![Page 29: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/29.jpg)
29
Floyd-Warshallfor (int k = 1; k =< V; k++)
for (int i = 1; i =< V; i++)
for (int j = 1; j =< V; j++)
if ( ( M[i][k]+ M[k][j] ) < M[i][j] )M[i][j] = M[i][k]+ M[k][j]
Invariant: After the kth iteration, the matrix includes the shortest paths for all pairs of vertices (i,j) containing only vertices 1..k as intermediate vertices
![Page 30: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/30.jpg)
30
a b c d e
a 0 2 - -4 -
b - 0 -2 1 3
c - - 0 - 1
d - - - 0 4
e - - - - 0
b
c
d e
a
-4
2-2
1
31
4
Initial state of the matrix:
M[i][j] = min(M[i][j], M[i][k]+ M[k][j])
![Page 31: CSE 326: Data Structures Network Flow and APSP Ben Lerner Summer 2007](https://reader035.vdocuments.site/reader035/viewer/2022062714/56649d145503460f949e87d3/html5/thumbnails/31.jpg)
31
a b c d e
a 0 2 0 -4 0
b - 0 -2 1 -1
c - - 0 - 1
d - - - 0 4
e - - - - 0
b
c
d e
a
-4
2-2
1
31
4
Floyd-Warshall - for All-pairs shortest path
Final Matrix Contents