Professional Documents
Culture Documents
16 MST 06
16 MST 06
849 PVD
ORD 187
6/22/2006 2:12 PM Minimum Spanning Tree 1 6/22/2006 2:12 PM Minimum Spanning Tree 2
7
6/22/2006 2:12 PM Minimum Spanning Tree 3 6/22/2006 2:12 PM Minimum Spanning Tree 4
1
Minimum Spanning Tree 6/22/2006 2:12 PM
Partition Property
Cycle Property Partition Property:
Consider a partition of the vertices U V
of G into subsets U and V. Let e be f 7
an edge of minimum weight across 4
ORD 10 the partition. There is a minimum 5
9
spanning tree of G containing edge e 2
1 8
In other words: PIT Proof: 8 e 3
in any cycle of the DEN 6 7 Let T be an MST of G
7
9 If T does not contain e, consider the
graph, the non-
cycle C formed by e with T and let f Replacing f with e yields
spanning tree edge 3 DCA be an edge of C across the partition another MST
(dotted line) has 4 STL By the cycle property,
U V
max weight. 8 5 2
weight(f) ≤ weight(e)
f 7
Thus, weight(f) = weight(e) 4
We obtain another MST by replacing f 9
DFW with e 2 5
ATL 8
8 e 3
7
6/22/2006 2:12 PM Minimum Spanning Tree 5 6/22/2006 2:12 PM Minimum Spanning Tree 6
from s adjacent to u
We store with each vertex v a label d(v)
representing the smallest weight of an
edge connecting v to any vertex in the
cloud (as opposed to the total sum of edge weights on a path from
the start vertex to u).
6/22/2006 2:12 PM Minimum Spanning Tree 7 6/22/2006 2:12 PM Minimum Spanning Tree 8
2
Minimum Spanning Tree 6/22/2006 2:12 PM
Algorithm PrimJarnik(G):
Input: A weighted graph G.
Output: A minimum spanning tree T for G.
Use a priority queue Q whose keys are D labels, and whose elements are vertex- pick any vertex v of G
edge pairs. D[v] ← 0
Key: distance for each vertex u ≠ v do
Element: vertex-edge pair D[u] ← ∞
Initialize T ← ∅
For example, an entry of Q is ((z, (u,z)), D[z]) for a vertex z, where (z, (u,z)) is Initialize priority queue Q with an entry ((u, null), D[u]) for each vertex u,
the element and D[z] is the key of the vertex z.
where (u, null) is the element and D[u]) is the key.
while Q ≠ ∅ do {pull u into the cloud C}
Any vertex v can be the starting vertex. (u, e) ← Q.removeMin()
We still initialize all the D[u] values to INFINITE, but we also initialize the add vertex u and edge e to T
for each vertex z adjacent to u such that z is in Q do
edge associated with u to null.
{perform the relaxation operation on edge (u, z) }
Return the minimum-spanning tree T. if weight(u, z) < D[z] then
We can reuse code from Dijkstra’s, and we only have to change a few things. Let’s look at D[z] ←weight(u, z)
the pseudocode.... change to (z, (u, z)) the element of z in Q
change to D[z] the key of vertex z in Q
return tree T
6/22/2006 2:12 PM Minimum Spanning Tree 9 6/22/2006 2:12 PM Minimum Spanning Tree 10
3
Minimum Spanning Tree 6/22/2006 2:12 PM
Prim-Jarnik… Analysis
Why It Works Graph operations
Method incidentEdges is called once for each vertex
Label operations
This is an application of the Cycle Property! We set/get the labels of vertex z O(deg(z)) times
Setting/getting a label takes O(1) time
Let the minimum edge at some iteration be Priority queue operations
Each vertex is inserted once into and removed once from the
(u,v). If there is an MST not containing
priority queue, where each insertion or removal takes O(log n) time
(u,v), then (u,v) completes a cycle. Since The key of a vertex w in the priority queue is modified at most
(u,v) was considered before some other deg(w) times, where each key change takes O(log n) time
edge connecting v to the cluster, it must Prim-Jarnik’s algorithm runs in O((n + m) log n) time provided
have weight equal to or lower than that the graph is represented by the adjacency list structure
other edge. A new MST can be formed by Recall that Σv deg(v) = 2m
swapping. The running time is O(m log n) since the graph is connected
6/22/2006 2:12 PM Minimum Spanning Tree 13 6/22/2006 2:12 PM Minimum Spanning Tree 14
6/22/2006 2:12 PM Minimum Spanning Tree 15 6/22/2006 2:12 PM Minimum Spanning Tree 16
4
Minimum Spanning Tree 6/22/2006 2:12 PM
Kruskal’s Algorithm…
Why It Works Kruskal’s Algorithm
A priority queue stores Algorithm KruskalMST(G)
the edges outside the for each vertex V in G do
This is an application of the Partition cloud
define a Cloud(v) of Å {v}
let Q be a priority queue.
Property! Key: weight Insert all edges into Q using their
Element: edge weights as the key
TÅ∅
If the minimum edge at some iteration is At the end of the while T has fewer than n-1 edges do
algorithm edge e = T.removeMin()
(u,v), then if we consider a partition of G We are left with one
Let u, v be the endpoints of e
with u in one cluster and v in the other, if Cloud(v) ≠ Cloud(u) then
cloud that encompasses Add edge e to T
then the partition property says that there the MST Merge Cloud(v) and Cloud(u)
must be an MST containing (u,v). A tree T which is our return T
MST
6/22/2006 2:12 PM Minimum Spanning Tree 17 6/22/2006 2:12 PM Minimum Spanning Tree 18
6/22/2006 2:12 PM Minimum Spanning Tree 19 6/22/2006 2:12 PM Minimum Spanning Tree 20
5
Minimum Spanning Tree 6/22/2006 2:12 PM
Kruskal
Partition-Based Implementation Example 2704
BOS
A partition-based version of Kruskal’s Algorithm 867
performs cloud merges as unions and tests as 849 PVD
finds. ORD 187
Algorithm Kruskal(G): 740 144
Input: A weighted graph G. 1846 621 JFK
Output: An MST T for G. 184 1258
802
Let P be a partition of the vertices of G, where each vertex forms a separate set. SFO
1391 BWI
Let Q be a priority queue storing the edges of G, sorted by their weights
1464
Let T be an initially-empty tree 337 1090
while Q is not empty do
DFW 946
(u,v) ← Q.removeMin() LAX 1235
if P.find(u) != P.find(v) then
Running time: 1121
Add (u,v) to T
O((n+m)log n) MIA
P.union(u,v)
2342
return T
6/22/2006 2:12 PM Minimum Spanning Tree 21 6/22/2006 2:12 PM Minimum Spanning Tree 22
Example 2704
Example 2704
867 BOS 867 BOS
6/22/2006 2:12 PM Minimum Spanning Tree 23 6/22/2006 2:12 PM Minimum Spanning Tree 24
6
Minimum Spanning Tree 6/22/2006 2:12 PM
Example 2704
Example 2704
867 BOS 867 BOS
6/22/2006 2:12 PM Minimum Spanning Tree 25 6/22/2006 2:12 PM Minimum Spanning Tree 26
Example 2704
Example 2704
867 BOS 867 BOS
6/22/2006 2:12 PM Minimum Spanning Tree 27 6/22/2006 2:12 PM Minimum Spanning Tree 28
7
Minimum Spanning Tree 6/22/2006 2:12 PM
Example 2704
Example 2704
867 BOS 867 BOS
6/22/2006 2:12 PM Minimum Spanning Tree 29 6/22/2006 2:12 PM Minimum Spanning Tree 30
Example 2704
Example 2704
867 BOS 867 BOS
6/22/2006 2:12 PM Minimum Spanning Tree 31 6/22/2006 2:12 PM Minimum Spanning Tree 32
8
Minimum Spanning Tree 6/22/2006 2:12 PM
Example 2704
Example 2704
867 BOS 867 BOS
6/22/2006 2:12 PM Minimum Spanning Tree 33 6/22/2006 2:12 PM Minimum Spanning Tree 34
Example Example
2704
867 BOS
G G
849 B 8 B 8
PVD 4 4
ORD 187 9 E E
5 6 9 6
1 F 1 5 F
740 144 C 11 3 C 11 3
1846 621 JFK 2 2
7 D H 7 H
184 1258 D
802 A 10 A 10
SFO BWI
1391
1464
337 1090
G G
DFW 946 B 8 B 8
4 4
LAX 1235 9 E 9 E
5 6 5 6
1 F 1 F
1121 C 11 3 C 11 3
MIA 2 2
7 D H 7 H
D
2342 A A
10 10
6/22/2006 2:12 PM Minimum Spanning Tree 35 6/22/2006 2:12 PM Minimum Spanning Tree 36
9
Minimum Spanning Tree 6/22/2006 2:12 PM
s
ep
salesperson tours
st
G G
Example of traveling
o
B 8 tw B 8 The traveling salesperson problem
4 4
9 E
6 9 E
6
(TSP) is a major open problem in salesperson tour
1 5 F 1 5 F computer science
C 11 3 C 11 3 (with weight 17)
2 2 Find a polynomial-time algorithm
7 D H 7 D H computing a traveling salesperson
A 10 A 10 tour or prove that none exists
6/22/2006 2:12 PM Minimum Spanning Tree 37 6/22/2006 2:12 PM Minimum Spanning Tree 38
TSP Approximation
We can approximate a TSP tour
with a tour of at most twice the
weight for the case of Euclidean
graphs
Vertices are points in the plane
Every pair of vertices is
connected by an edge
The weight of an edge is the
length of the segment joining the
points
Approximation algorithm
Compute a minimum spanning tree
Form an Eulerian circuit around
the MST
Transform the circuit into a tour
10