Title: MST Algorithm Review
1MST Algorithm Review
2Minimal Cost Spanning Trees
- Minimal Cost Spanning Tree (MST) Problem
- Input An undirected, connected graph G.
- Output The subgraph of G that
- 1) has minimum total cost as measured by summing
the weight of all the edges in the subset, and - 2) keeps the vertices connected.
3Partition Property
- Partition Property
- Consider a partition of the vertices of G into
subsets U and V - Let e be an edge of minimum weight across the
partition - There is a minimum spanning tree of G containing
edge e
U
V
7
f
4
9
5
2
8
3
8
e
7
4Prims MST Algorithm
- As with Dijkstras algorithm, uses a priority
queue. - Running time is identical to Dijkstras algorithm
implementations.
B
5
A
7
C
6
1
2
9
D
2
F
E
1
5Prims MST Algorithm
6Prims MST Algorithm
(?)
(?)
B
Step 1
5
A
7
C
(?)
6
1
2
9
D
2
(?)
F
E
(0)
1
(?)
7Prims MST Algorithm
(6,F)
(?)
B
Step 2
5
A
7
C
(2,F)
6
1
2
9
D
2
(2,F)
F
E
(0)
1
(1,F)
8Prims MST Algorithm
(6,F)
(?)
B
Step 3
5
A
7
C
(2,F)
6
1
2
9
D
2
(2,F)
F
E
(0)
1
(1,F)
9Prims MST Algorithm
(6,F)
(9,E)
B
Step 3a
5
A
7
C
(2,F)
6
1
2
9
D
2
(2,F)
F
E
(0)
1
(1,F)
10Prims MST Algorithm
(6,F)
(9,E)
B
Step 4
5
A
7
C
(2,F)
6
1
2
9
D
2
(2,F)
F
E
(0)
1
(1,F)
11Prims MST Algorithm
(5,C)
(7,C)
B
Step 4a
5
A
7
C
(2,F)
6
1
2
9
D
2
(1,C)
F
E
(0)
1
(1,F)
12Prims MST Algorithm
(5,C)
(7,C)
B
Step 5
5
A
7
C
(2,F)
6
1
2
9
D
2
(1,C)
F
E
(0)
1
(1,F)
13Prims MST Algorithm
(5,C)
(7,C)
B
Step 5a
5
A
7
C
(2,F)
6
1
2
9
D
2
(1,C)
F
E
(0)
1
(1,F)
14Prims MST Algorithm
(5,C)
(7,C)
B
Step 6
5
A
7
C
(2,F)
6
1
2
9
D
2
(1,C)
F
E
(0)
1
(1,F)
15Prims MST Algorithm
(5,C)
(7,C)
B
Step 6a
5
A
7
C
(2,F)
6
1
2
9
D
2
(1,C)
F
E
(0)
1
(1,F)
16Prims MST Algorithm
(5,C)
(7,C)
B
Step 7
5
A
7
C
(2,F)
6
1
2
9
D
2
(1,C)
F
E
(0)
1
(1,F)
17Kruskals MST Algorithm
B
5
A
7
C
6
1
2
9
D
2
F
E
1
- Initially, each vertex is in its own MST.
- Merge two MSTs that have the shortest edge
between them. - Use a priority queue to order the unprocessed
edges. Grab the minimum one at each step. - How to tell if an edge connects two vertices
already in the same MST? - Use parent-pointer representation.
18Kruskals MST Algorithm
Step 1
E
D
F
B
C
A
19Kruskals MST Algorithm
Step 2
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
20Kruskals MST Algorithm
Step 3
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
21Kruskals MST Algorithm
Step 4
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
22Kruskals MST Algorithm
Step 5
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
23Kruskals MST Algorithm
Step 6
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
24Kruskals MST Algorithm
Step 7
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
25Kruskals MST Algorithm
- Cost is dominated by the time to remove edges
from the heap. - Can stop processing edges once all vertices are
in the same MST - Total cost Q((n m) log m).
26Baruvkas MST Algorithm
B
5
A
7
C
6
1
2
9
D
2
F
E
1
- Create a forest of n trees
- Loop while (there is gt 1 tree in the forest)
- For each tree Ti in the forest
- Find the smallest edge e (u,v), in the edge
list with u in Ti and v in Tj ?Ti - connects 2 trees from the forest in to one.
- end loop
27Baruvkas MST Algorithm
Step 1
E
D
F
B
C
A
28Baruvkas MST Algorithm
Step 2
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
29Baruvkas MST Algorithm
Step 3
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
30Baruvkas MST Algorithm
Step 4
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
31Baruvkas MST Algorithm
Step 5
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
32Baruvkas MST Algorithm
Step 6
B
5
A
E
D
F
B
C
A
7
C
6
1
2
9
D
2
F
E
1
33Baruvkas Algorithm
- Similar to Kruskals Algorithm, but no Priority
queueS. - Each iteration of the while-loop halves the
number of connected compontents in T. - The running time is O(m log n).
Algorithm BaruvkaMST(G) T ? V just the
vertices of G while T has fewer than n-1 edges
do for each connected component C in T
do Let edge e be the smallest-weight edge from
C to another component in T. if e is not
already in T then Add edge e to T return T
34Network Flow Problem
35Flow Network
- A network N consists of
- A weighted digraph G with nonnegative integer
edge weights, where the weight of an edge e is
called the capacity c(e) of e - Two distinguished vertices, s and t of G, called
the source and sink or target, respectively, such
that s has no incoming edges and t has no
outgoing edges. - Example
36Flow
- A flow f for a network N is an assignment of an
value f(e) to each edge e that satisfies the
following properties - Capacity Rule For each edge e, 0 ? f (e) ? c(e)
37Flow
- A flow f for a network N is an assignment of an
value f(e) to each edge e that satisfies the
following properties - Capacity Rule For each edge e, 0 ? f (e) ? c(e)
- Conservation Rule For each vertex v ? s,t
38Flow
- The value of a flow f , denoted f, is
- the total flow from the source
- which is the same as,
- the total flow into the sink
39Maximum Flow
- A flow for a network N is said to be maximum if
its value is the largest of all flows for N - The maximum flow problem consists of finding a
maximum flow for a given network N - Applications
- Hydraulic systems
- Electrical circuits
- Traffic movements
- Freight transportation
40Maximum Flow
v
1/3
2/6
1/1
t
3/7
3/3
s
w
2/9
4/5
1/1
3/5
z
u
2/2
Flow 2 3 3 1 3 4 8
41A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Flow Graph (Gf)
42A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
43A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
v
3
6
1
t
7
3
s
w
9
5
1
5
z
u
2
44A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
v
3
6
1
t
7
3
s
w
9
3
1
3
z
u
45A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
v
3
6
1
t
7
3
s
w
9
3
1
3
z
u
46A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
v
3
6
1
t
7
s
w
6
1
3
z
u
47A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
v
3
6
1
t
7
s
w
6
1
3
z
u
48A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
v
3
1
t
7
s
w
6
1
3
z
u
49A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
v
3
1
t
7
s
w
6
1
3
z
u
50A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
v
3
1
t
6
s
w
6
2
z
u
51A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
v
3
1
t
6
s
w
6
2
z
u
52A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
v
2
t
5
s
w
6
2
z
u
53A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
/2
2
w
v
w
v
/2
2
/3
3
s
s
/4
4
t
/1
t
1
/2
2
/3
3
z
z
u
u
/3
3
54A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
0/2
2
w
v
w
v
0/2
2
0/3
3
s
s
0/4
4
t
0/1
t
1
0/2
2
0/3
3
z
z
u
u
0/3
3
55A Simple Flow Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
0/2
2
w
v
w
v
0/2
2
3/3
s
s
1
3/4
t
0/1
t
1
0/2
2
3/3
z
z
u
u
0/3
3
Algorithm terminates with an sub-optimal flow
solution
56Ford-Fulkersons Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- Add a reversed path in the residual graph
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
0/2
2
w
v
w
v
0/2
2
0/3
3
s
s
0/4
4
t
0/1
t
1
0/2
2
0/3
3
z
z
u
u
0/3
3
57Ford-Fulkersons Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- Add a reversed path in the residual graph
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
0/2
2
w
v
w
v
0/2
2
3/3
-3
s
s
3/4
1
t
0/1
t
1
-3
0/2
2
3/3
-3
z
z
u
u
0/3
3
58Ford-Fulkersons Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- Add a reversed path in the residual graph
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
0/2
2
w
v
w
v
0/2
2
3/3
-3
s
s
3/4
1
t
0/1
t
1
-3
0/2
2
3/3
-3
z
z
u
u
0/3
3
59Ford-Fulkersons Algorithm
- Initialize all edges of the flow graph with zero
flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual
capacity - from this path
- Reduce the residual capacity of the edges
- Add a reversed path in the residual graph
- end Loop
Residual Graph (Gr)
Flow Graph (Gf)
2/2
-2
w
v
w
v
2/2
-2
3/3
-3
s
s
1/4
3
t
0/1
t
1
-1
2/2
-2
3/3
-3
-2
z
z
u
u
2/3
1
60Analysis
- Each augmenting path increases the flow value by
at least 1. - Let f is a maximum flow, then in the worst
case, Ford-Fulkersons algorithm performs f
flow augmentations. - Finding an augmenting path and augmenting the
flow takes O(n m) time - The running time of Ford-Fulkersons algorithm is
O(f(n m))
61Analysis
v
50
50
Classic worst case example
s
1
t
50
50
u
Flow Graph (Gf)
Residual Graph (Gr)
v
v
50
49
1/50
0/50
-1
s
s
1/1
t
t
-1
-1
49
50
1/50
0/50
u
u
62Analysis
v
50
50
Classic worst case example
s
1
t
50
50
u
Flow Graph (Gf)
Residual Graph (Gr)
v
v
49
49
1/50
1/50
-1
-1
s
s
1
0/1
t
t
-1
-1
49
49
1/50
1/50
u
u