Professional Documents
Culture Documents
43MinimumSpanningTrees - SQ
43MinimumSpanningTrees - SQ
h t tp://a l g s 4.cs . p r in c et o n . e d u
‣context
4.3 MINIMUM S PANNING T REES
‣introduction
‣greedy algorithm
‣edge-weighted graph API
Algorithms
‣Kruskal's algorithm
‣Prim's algorithm
R OBERT SEDGEWICK | K EVIN W AYNE
graph G
3
M inimum spanning tree
4
M inimum spanning tree
5
M inimum spanning tree
6
M inimum spanning tree
24
4
6 23 9
18
5
11
16
8
7
10 14
21
edge-weighted graph G
7
M inimum spanning tree
24
4
6 23 9
18
5
11
16
8
7
10 14
21
http://www.flickr.com/photos/ewedistrict/21980840
9
M odels of nature
MST of random graph
http://algo.inria.fr/broutin/gallery.html
10
M edical image processing
http://www.bccrc.ca/ci/ta01_archlevel.html
11
M edical image processing
MST dithering
http://www.flickr.com/photos/quasimondo/2695389651
12
Applications
13
4.3 MINIMUM S PANNING T REES
‣introduction
‣greedy algorithm
‣edge-weighted graph API
Algorithms
‣Kruskal's algorithm
‣Prim's algorithm
R OBERT SEDGEWICK | K EVIN W AYNE
5
7
2 10 no two edge
6 4
weights are equal
13
9 1
3
12 8
14 16
20
15
Cut - vetitë
Definim. Një cut në një graf është ndarja e kulmeve në dy bashkësi (jo boshe).
Definim. Një crossing edge lidhë një kulm në njërën bashkësi me një kulm në
tjetrën.
Cut vetitë. Për çdo cut, crossing edge i min weight është në MST.
Definim. Një cut në një graf është ndarja e kulmeve në dy bashkësi (jo boshe).
Definim. Një crossing edge lidhë një kulm në njërën bashkësi me një kulm në
tjetrën.
Cut - vetitë. Për çdo cut, crossing edge i min weight është në MST.
Vërtetim. Supuzo që min-weight crossing edge e nuk është në MST.
・Shtimi i e në MST krijon një cikël.
・Një tjetër segment f në cikël duhet të jetë një crossing edge.
・Fshirja e f dhe shtimi i e është gjithashtu një spanning tree.
・Pasi që pasha e e është më e vogël sesa ajo e f,
ai spanning tree është me peshë më të ulët. f
・Kontradiktë.
e
17
Greedy M ST algorithm demo
6 1-2 0.36
4
4-7 0.37
0-4 0.38
an edge-weighted graph 6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
18
Greedy M ST algorithm demo
1
3
5
7
2
4 6
MST edges
0-2 5-7 6-2 0-7 2-3 1-7 4-5
19
Greedy M ST algorithm:
Vërtetim.
・Cilido segment i zi është në MST (bazuar në vetitë e cut).
・Më pak se V - 1 segmente të zeza ⇒ cut pa crossing edges të zi.
a cut with no black crossing edges fewer than V - 1 edges colored black
20
Removing two simplifying assumptions
1 2 1.00 1 2 1.00
1 3 0.50 1 3 0.50
2 4 1.00 2 4 1.00
3 4 0.50 3 4 0.50
4 5 0.61
4 6 0.62
5 6 0.88
1 5 0.11
2 3 0.35
0 3 0.6
1 6 0.10
can independently compute 0 2 0.22
MSTs of components
21
4.3 MINIMUM S PANNING T REES
‣introduction
‣greedy algorithm
‣edge-weighted graph
Algorithms API
‣Kruskal's algorithm
‣Prim's algorithm
R OBERT SEDGEWICK | K EVIN W AYNE
h t tp://a l g s 4.cs . p r in c et o n . e d u
‣context
Weighted edge API
weight
v w
26
Edge-weighted graph: adjacency-lists implementation
public EdgeWeightedGraph(int V)
{
this.V = V; constructor
adj = (Bag<Edge>[]) new Bag[V];
for (int v = 0; v < V; v++)
adj[v] = new Bag<Edge>();
}
27
M inimum spanning tree API
Iterable<Edge> G)
MST(EdgeWeightedGrap constructor
tinyEWG.txt
V 8
E
16
4 5 0.35
% java MST tinyEWG.txt
MST edge
4 7 0.37 (black) 0-7 0.16
5 7 0.28 1-7 0.19
0 7 0.16
0-2 0.26
1 5 0.32
0 4 0.38 2-3 0.17
2 3 0.17 5-7 0.28
1 7 0.19
0 2 0.26
4-5 0.35
1 2 0.36 6-2 0.40
1 3 0.29 1.81
2 7 0.34
non-MST edge
6 2 0.40
(gray)
3 6 0.52
6 0 0.58
6 4 0.93
30
M inimum spanning tree API
Iterable<Edge> G)
MST(EdgeWeightedGrap constructor
31
4.3 MINIMUM S PANNING T REES
‣introduction
‣greedy algorithm
‣edge-weighted graph API
Algorithms
‣Kruskal's algorithm
‣Prim's algorithm
R OBERT SEDGEWICK | K EVIN W AYNE
0-7 0.16
2-3 0.17
1 1-7 0.19
3
0-2 0.26
5
5-7 0.28
7 1-3 0.29
2
1-5 0.32
0 2-7 0.34
4-5 0.35
6 1-2 0.36
4
4-7 0.37
0-4 0.38
an edge-weighted graph 6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
31
Kruskal's algorithm demo
0-7 0.16
2-3 0.17
1 1-7 0.19
3
0-2 0.26
5
5-7 0.28
7 1-3 0.29
2
1-5 0.32
0 2-7 0.34
4-5 0.35
6 1-2 0.36
4
4-7 0.37
0-4 0.38
a minimum spanning tree 6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
32
Kruskal's algorithm: visualization
33
Kruskal's algorithm: correctness proof
34
Kruskal's algorithm: implementation challenge
Challenge. Would adding edge v–w to tree T create a cycle? If not, add it.
How difficult?
・ E+V
・V run DFS from v, check if w is reachable
(T has at most V – 1 edges)
・ log V
・ log* V use the union-find data structure !
・1
35
Kruskal's algorithm: implementation challenge
Challenge. Would adding edge v–w to tree T create a cycle? If not, add it.
v w v
Case 1: adding v–w creates a cycle Case 2: add v–w to T and merge sets containing v and w
36
Kruskal's algorithm: Java implementation
UF uf = new UF(G.V());
while (!pq.isEmpty() && mst.size() < G.V()-1)
{
Edge e = pq.delMin(); greedily add edges to MST
int v = e.either(), w = e.other(v);
if (!uf.connected(v, w)) edge v–w does not create
{ cycle
uf.union(v, w); merge sets
mst.enqueue(e);
add edge to MST
}
}
}
37
Kruskal's algorithm: running time
delete-min E log E
union V log* V †
connected E log* V †
40
Prim's algorithm demo
1
3
5
7
2
4 6
MST edges
0-7 1-7 0-2 2-3 5-7 4-5 6-2
41
Prim’s algorithm: visualization
42
Prim's algorithm: proof of correctness
43
Prim's algorithm: implementation challenge
Challenge. Find the min weight edge with exactly one endpoint in T.
How
difficult? try all
・E edges
44
Prim's algorithm: lazy implementation
Challenge. Find the min weight edge with exactly one endpoint in T.
45
Prim's algorithm (lazy) demo
4 6 1-2 0.36
4-7 0.37
0-4 0.38
an edge-weighted graph 6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
46
Prim's algorithm (lazy) demo
1
3
5
7
2
4 6
MST edges
0-7 1-7 0-2 2-3 5-7 4-5 6-2
47
Prim's algorithm: lazy implementation
public LazyPrimMST(WeightedGraph G)
{
pq = new MinPQ<Edge>();
mst = new Queue<Edge>();
marked = new boolean[G.V()];
visit(G, 0); assume G is connected
49
Lazy Prim's algorithm: running time
insert E log E
50
Prim's algorithm: eager implementation
0
1 1-7 0.19
2 0-2 0.26 red: on PQ
3 1-3 0.29
4 0-4 0.38
5 5-7 0.28
6 6-0 0.58
7 0-7 0.16
black: on MST
51
Prim's algorithm (eager) demo
4 6 1-2 0.36
4-7 0.37
0-4 0.38
an edge-weighted graph 6-2 0.40
3-6 0.52
6-0 0.58
6-4 0.93
52
Prim's algorithm (eager) demo
5 1 1–7 0.19
2 0–2 0.26
7
2 3 2–3 0.17
5 5–7 0.28
0 4 4–5 0.35
6 6–2 0.40
4 6
MST edges
0-7 1-7 0-2 2-3 5-7 4-5 6-2
53
Indexed priority queue
void decreaseKey(int i, Key key) decrease the key associated with index i
54
Indexed priority queue implementation
i 0 1 2 3 4 5 6 7 8
keys[i] A S O R T I N G -
pq[i] - 0 6 7 2 1 5 4 3
qp[i] 1 5 4 8 7 6 2 3 -
1
A
2 N 3 G
4 O 5 S 6 I 7 T
8 R
55
Prim's algorithm: which priority queue?
unordered array 1 V 1 V2
† amortized
Bottom line.
・Array implementation optimal for dense graphs.
・Binary heap much faster for sparse graphs.
・4-way heap worth the trouble in performance-critical situations.
・Fibonacci heap best in theory, but not worth implementing.
56