Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Minimum Spanning Tree 6/22/2006 2:12 PM

Outline and Reading


Minimum Spanning Trees (§12.7)
Minimum Spanning Tree „ Definitions
„ A crucial fact
2704

Prim-Jarnik’s Algorithm (§12.7.2)


867 BOS

849 PVD
ORD 187

Kruskal’s Algorithm (§12.7.1)


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

6/22/2006 2:12 PM Minimum Spanning Tree 1 6/22/2006 2:12 PM Minimum Spanning Tree 2

Minimum Spanning Tree Cycle Property


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

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

Prim-Jarnik’s Algorithm Prim-Jarnik’s Algorithm


At each step
Prim-Jarnik’s algorithm for computing an „ We add to the cloud the vertex u with
MST is similar to Dijkstra’s algorithm
(minimum-weight edge) outside the cloud
We assume that the graph is connected with the smallest distance label
We pick an arbitrary vertex s and we grow
We update the labels of the vertices
the MST as a cloud of vertices, starting
„

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

Example Example (contd.)


∞ 7
7 D 7 D
2 2 7
B 4 B 4 7 D
2
8 9 ∞ 5 9 ∞ B 4
2 5 F 2 5 F
C C 5 9 4
8 8 2 5 F
8 3 8 3 C
E E 8
A A 8 3
7 7 7 7
0 0 E
A 3 7
0 7
7 7 D
7 2
7 D D B 4
2 2 7
B 4 B 5 9 4
4 2 5 F
5 9 ∞ 9 4 C
2 5 F 5 5 8
C 2 F 3
8 C 8
3 8 E
8 8 3 A 3
E E 0 7
A 7 A
0 7 7 7
0
6/22/2006 2:12 PM Minimum Spanning Tree 11 6/22/2006 2:12 PM Minimum Spanning Tree 12

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

Dijkstra vs. Prim-Jarnik Kruskal’s Algorithm


Algorithm DijkstraShortestPaths(G, s) Algorithm PrimJarnikMST(G)
Q ← new heap-based priority queue Q ← new heap-based priority queue
s ← a vertex of G
Each vertex is initially stored as its own
for all v ∈ G.vertices() for all v ∈ G.vertices() cluster.
if v = s if v = s
setDistance(v, 0) setDistance(v, 0) At each iteration, the minimum weight edge
else else is added to the spanning tree if it joins 2
setDistance(v, ∞) setDistance(v, ∞)
setParent(v, ∅) setParent(v, ∅) distinct clusters.
l ← Q.insert(getDistance(v), v)
setLocator(v,l)
l ← Q.insert(getDistance(v), v)
setLocator(v,l) The algorithm ends when all the vertices
while ¬Q.isEmpty() while ¬Q.isEmpty() are in the same cluster.
u ← Q.removeMin() u ← Q.removeMin()
for all e ∈ G.incidentEdges(u) for all e ∈ G.incidentEdges(u)
z ← G.opposite(u,e) z ← G.opposite(u,e)
r ← getDistance(u) + weight(e) r ← weight(e)
if r < getDistance(z) if r < getDistance(z)
setDistance(z,r) setDistance(z,r)
setParent(z,e) setParent(z,e)
Q.replaceKey(getLocator(z),r) Q.replaceKey(getLocator(z),r)

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

Data Structure for Kruskal Representation of


Algortihm a Partition
The algorithm maintains a forest of trees Each set is stored in a sequence
An edge is accepted it if connects distinct trees Each element has a reference back to the set
We need a data structure that maintains a „ operation find(u) takes O(1) time, and returns the set
partition, i.e., a collection of disjoint sets, with the of which u is a member.
operations: „ in operation union(u,v), we move the elements of the
-find(u): return the set storing u smaller set to the sequence of the larger set and
-union(u,v): replace the sets storing u and v with update their references
their union „ 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
Whenever an element is processed, it goes into
a set of size at least double, hence each
element is processed at most log n times

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

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

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

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

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

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

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

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

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

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

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

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

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

Example (contd.) Traveling Salesperson Problem


G G A tour of a graph is a spanning
B 8 B 8 cycle (e.g., a cycle that goes
4 4
9 E
6 9 E
6
through all the vertices) 7 D
5 F 5 F
1
C 11 3 1
C 11 3 A traveling salesperson tour of a B 4
2 2 weighted graph is a tour that is 2
7 H 7 H simple (i.e., no repeated vertices or 2 5 F
D D C
A 10 A edges) and has has minimum weight 8
10 3
6
four steps No polynomial-time algorithms are E
A
known for computing traveling 1

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

6/22/2006 2:12 PM Minimum Spanning Tree 39

10

You might also like