Anda di halaman 1dari 6

Minimum Spanning Tree 5/13/2002 4:52 PM

Outline and Reading

Minimum Spanning Trees Minimum Spanning Trees (§7.3)


„ Definitions
2704
867

849
BOS

PVD
„ A crucial fact
The Prim-Jarnik Algorithm (§7.3.2)
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090

Kruskal's Algorithm (§7.3.1)


DFW 946
LAX 1235
1121
MIA
2342

Baruvka's Algorithm (§7.3.3)

Minimum Spanning Trees 1 Minimum Spanning Trees 2

Minimum Spanning Tree Cycle Property


f 8
Spanning subgraph Cycle Property: 4
„ Subgraph of a graph G ORD 10 „ Let T be a minimum C 9
containing all the vertices of G spanning tree of a 2 6
1 3
Spanning tree PIT weighted graph G e
8 7
„ Let e be an edge of G
„ Spanning subgraph that is DEN 6 7 that is not in T and C let
itself a (free) tree 9 be the cycle formed by e 7
Minimum spanning tree (MST) 3 with T
DCA Replacing f with e yields
„ For every edge f of C,
„ Spanning tree of a weighted 4 STL a better spanning tree
graph with minimum total weight(f) ≤ weight(e)
edge weight 8 5 Proof: 8
2 f
„ By contradiction 4
Applications C
„ If weight(f) > weight(e) we 9
Communications networks DFW 6
„
ATL can get a spanning tree 2
3 e
„ Transportation networks of smaller weight by 7
replacing e with f 8
7
Minimum Spanning Trees 3 Minimum Spanning Trees 4

Partition Property U V
Prim-Jarnik’s Algorithm
f 7
Partition Property: 4 Similar to Dijkstra’s algorithm (for a connected graph)
„ Consider a partition of the vertices of 9 We pick an arbitrary vertex s and we grow the MST as a
G into subsets U and V 2 5
8 cloud of vertices, starting from s
„ Let e be an edge of minimum weight
across the partition 8 e 3 We store with each vertex v a label d(v) = the smallest
„ There is a minimum spanning tree of
7 weight of an edge connecting v to a vertex in the cloud
G containing edge e
Proof: Replacing f with e yields At each step:
„ Let T be an MST of G
another MST „ We add to the cloud the
„ If T does not contain e, consider the U V vertex u outside the cloud
cycle C formed by e with T and let f f 7 with the smallest distance
be an edge of C across the partition 4 label
„ By the cycle property, 9 „ We update the labels of the
weight(f) ≤ weight(e) 2 5
8 vertices adjacent to u
„ Thus, weight(f) = weight(e)
8 e 3
„ We obtain another MST by replacing
f with e 7
Minimum Spanning Trees 5 Minimum Spanning Trees 6

1
Minimum Spanning Tree 5/13/2002 4:52 PM

Prim-Jarnik’s Algorithm (cont.) Example


∞ 7
A priority queue stores Algorithm PrimJarnikMST(G) 7 D 7 D
Q ← new heap-based priority queue 2 2
the vertices outside the s ← a vertex of G B 4 B 4
cloud for all v ∈ G.vertices() 8 9 ∞ 5 9 ∞
Key: distance if v = s 2 5 F 2 5 F
„
C C
Element: vertex setDistance(v, 0) 8 8
„
else 8 3 8 3
Locator-based methods setDistance(v, ∞) E E
setParent(v, ∅) A 7 A 7
„ insert(k,e) returns a 0 7 0 7
l ← Q.insert(getDistance(v), v)
locator setLocator(v,l)
„ replaceKey(l,k) changes while ¬Q.isEmpty() 7 7
7 D D
the key of an item u ← Q.removeMin() 2 2 7
for all e ∈ G.incidentEdges(u) B 4 B
We store three labels z ← G.opposite(u,e) 9 ∞
4
with each vertex: 5 5 5 9 4
r ← weight(e) 2 F 5 F
if r < getDistance(z) C 2 C
„ Distance 8 8
setDistance(z,r) 8 3 3
„ Parent edge in MST setParent(z,e) E 8
Locator in priority queue Q.replaceKey(getLocator(z),r) A 7 E
„
0 7 A 7
0 7
Minimum Spanning Trees 7 Minimum Spanning Trees 8

Example (contd.) Analysis


Graph operations
7
7 D „ Method incidentEdges is called once for each vertex
2
B 4 Label operations
5 9 4 „ We set/get the distance, parent and locator labels of vertex z O(deg(z))
2 5 F times
C
8 Setting/getting a label takes O(1) time
8 3 „

E Priority queue operations


A 3 7
0 7 Each vertex is inserted once into and removed once from the priority
7 D „
2 queue, where each insertion or removal takes O(log n) time
B 4
9 4 „ The key of a vertex w in the priority queue is modified at most deg(w)
5 5 times, where each key change takes O(log n) time
2 F
C
8 Prim-Jarnik’s algorithm runs in O((n + m) log n) time provided the
8 3
E
graph is represented by the adjacency list structure
A 3
0 7 „ Recall that Σv deg(v) = 2m
The running time is O(m log n) since the graph is connected
Minimum Spanning Trees 9 Minimum Spanning Trees 10

Data Structure for


Kruskal’s Algorithm Kruskal Algortihm
A priority queue stores Algorithm KruskalMST(G) The algorithm maintains a forest of trees
the edges outside the for each vertex V in G do
define a Cloud(v) of Å {v} An edge is accepted it if connects distinct trees
cloud let Q be a priority queue.
„ Key: weight Insert all edges into Q using their We need a data structure that maintains a partition,
„ Element: edge weights as the key i.e., a collection of disjoint sets, with the operations:
TÅ∅
At the end of the while T has fewer than n-1 edges do -find(u): return the set storing u
algorithm edge e = T.removeMin()
Let u, v be the endpoints of e -union(u,v): replace the sets storing u and v with
We are left with one
„
cloud that encompasses
if Cloud(v) ≠ Cloud(u) then their union
Add edge e to T
the MST Merge Cloud(v) and Cloud(u)
„ A tree T which is our return T
MST

Minimum Spanning Trees 11 Minimum Spanning Trees 12

2
Minimum Spanning Tree 5/13/2002 4:52 PM

Representation of a Partition-Based
Partition Implementation
Each set is stored in a sequence A partition-based version of Kruskal’s Algorithm
Each element has a reference back to the set performs cloud merges as unions and tests as finds.
Algorithm Kruskal(G):
„ operation find(u) takes O(1) time, and returns the set of
which u is a member. Input: A weighted graph G.
Output: An MST T for G.
„ in operation union(u,v), we move the elements of the
smaller set to the sequence of the larger set and update Let P be a partition of the vertices of G, where each vertex forms a separate set.
their references Let Q be a priority queue storing the edges of G, sorted by their weights
Let T be an initially-empty tree
„ the time for operation union(u,v) is min(nu,nv), where nu
and nv are the sizes of the sets storing u and v while Q is not empty do
(u,v) ← Q.removeMinElement()
Whenever an element is processed, it goes into a if P.find(u) != P.find(v) then
set of size at least double, hence each element is Running time:
Add (u,v) to T
processed at most log n times O((n+m)log n)
P.union(u,v)
return T
Minimum Spanning Trees 13 Minimum Spanning Trees 14

Kruskal
Example 2704
867 BOS
Example 2704
867 BOS

849 PVD 849 PVD


ORD 187 ORD 187
740 144 740 144
1846 621 JFK 1846 621 JFK
184 1258 184 1258
802 802
SFO SFO BWI
1391 BWI 1391
1464 1464
337 1090 337 1090
DFW 946 DFW 946
LAX 1235 LAX 1235
1121 1121
MIA MIA
2342 2342

Minimum Spanning Trees 15 Minimum Spanning Trees 16

Example 2704
867 BOS Example 2704
867 BOS

849 PVD 849 PVD


ORD 187 ORD 187
740 144 740 144
1846 621 JFK 1846 621 JFK
184 1258 184 1258
802 802
SFO BWI SFO BWI
1391 1391
1464 1464
337 1090 337 1090
DFW 946 DFW 946
LAX 1235 LAX 1235
1121 1121
MIA MIA
2342 2342

Minimum Spanning Trees 17 Minimum Spanning Trees 18

3
Minimum Spanning Tree 5/13/2002 4:52 PM

Example 2704
867 BOS Example 2704
867 BOS

849 PVD 849 PVD


ORD 187 ORD 187
740 144 740 144
1846 621 JFK 1846 621 JFK
184 1258 184 1258
802 802
SFO BWI SFO BWI
1391 1391
1464 1464
337 1090 337 1090
DFW 946 DFW 946
LAX 1235 LAX 1235
1121 1121
MIA MIA
2342 2342

Minimum Spanning Trees 19 Minimum Spanning Trees 20

Example 2704
867 BOS Example 2704
867 BOS

849 PVD 849 PVD


ORD 187 ORD 187
740 144 740 144
1846 621 JFK 1846 621 JFK
184 1258 184 1258
802 802
SFO BWI SFO BWI
1391 1391
1464 1464
337 1090 337 1090
DFW 946 DFW 946
LAX 1235 LAX 1235
1121 1121
MIA MIA
2342 2342

Minimum Spanning Trees 21 Minimum Spanning Trees 22

Example 2704
867 BOS Example 2704
867 BOS

849 PVD 849 PVD


ORD 187 ORD 187
740 144 740 144
1846 621 JFK 1846 621 JFK
184 1258 184 1258
802 802
SFO BWI SFO BWI
1391 1391
1464 1464
337 1090 337 1090
DFW 946 DFW 946
LAX 1235 LAX 1235
1121 1121
MIA MIA
2342 2342

Minimum Spanning Trees 23 Minimum Spanning Trees 24

4
Minimum Spanning Tree 5/13/2002 4:52 PM

Example 2704
867 BOS Example 2704
867 BOS

849 PVD 849 PVD


ORD 187 ORD 187
740 144 740 144
1846 621 JFK 1846 621 JFK
184 1258 184 1258
802 802
SFO BWI SFO BWI
1391 1391
1464 1464
337 1090 337 1090
DFW 946 DFW 946
LAX 1235 LAX 1235
1121 1121
MIA MIA
2342 2342

Minimum Spanning Trees 25 Minimum Spanning Trees 26

Example 2704
867 BOS Example 2704
BOS
867
849 PVD 849 PVD
ORD 187
ORD 187
740 144
1846 JFK 740 144
621 1846 JFK
621
184 1258
802 184 1258
SFO 802
1391 BWI SFO
1391 BWI
1464
337 1090 1464
337 1090
DFW 946
LAX 1235 DFW 946
LAX 1235
1121
1121
MIA
MIA
2342
2342
Minimum Spanning Trees 27 Minimum Spanning Trees 28

Baruvka
Baruvka’s Algorithm Example 2704
867 BOS

Like Kruskal’s Algorithm, Baruvka’s algorithm grows many 849


“clouds” at once. PVD
ORD 187
Algorithm BaruvkaMST(G) 740 144
T Å V {just the vertices of G} 1846 JFK
while T has fewer than n-1 edges do 621
for each connected component C in T do 184 1258
Let edge e be the smallest-weight edge from C to another component in T. 802
if e is not already in T then SFO BWI
Add edge e to T 1391
return T 1464
337 1090
Each iteration of the while-loop halves the number of connected DFW 946
compontents in T. LAX 1235
„ The running time is O(m log n). 1121
MIA
2342

Minimum Spanning Trees 29 Minimum Spanning Trees 30

5
Minimum Spanning Tree 5/13/2002 4:52 PM

Example 2704
867 BOS Example 2704
867 BOS

849 187 PVD 849 187 PVD


ORD ORD
740 144 740 144
1846 JFK 1846 JFK
621 621
184 1258 184 1258
802 802
SFO SFO BWI
1391 BWI 1391
1464 1464
337 1090 337 1090
DFW 946 DFW 946
LAX 1235 LAX 1235
1121 1121
MIA MIA
2342 2342

Minimum Spanning Trees 31 Minimum Spanning Trees 32

Anda mungkin juga menyukai