Docsity

Minimum Spanning Trees: Lecture 13 from CS 473G, Fall 2005, Study notes of Computer Science

Toggle sidebar
bg1
CS 473G Lecture 13: Minimum Spanning Trees Fall 2005
13 Minimum Spanning Trees (November 3)
13.1 Introduction
Suppose we are given a connected, undirected, weighted graph. This is a graph G= (V, E) together
with a function w:E→IR that assigns a weight w(e) to each edge e. For this lecture, we’ll assume
that the weights are real numbers. Our task is to find the minimum spanning tree of G,i.e., the
spanning tree Tminimizing the function
w(T) = X
e∈T
w(e).
To keep things simple, I’ll assume that all the edge weights are distinct: w(e)6=w(e′) for any
pair of edges eand e′. Distinct weights guarantee that the minimum spanning tree of the graph
is unique. Without this condition, there may be several different minimum spanning trees. For
example, if all the edges have weight 1, then every spanning tree is a minimum spanning tree with
weight V−1.
8 5
10
2 3
18 16
12
14
30
4 26
A weighted graph and its minimum spanning tree.
If we have an algorithm that assumes the edge weights are unique, we can still use it on graphs
where multiple edges have the same weight, as long as we have a consistent method for breaking
ties. One way to break ties consistently is to use the following algorithm in place of a simple
comparison. ShorterEdge takes as input four integers i, j, k, l, and decides which of the two
edges (i, j) and (k, l ) has ‘smaller’ weight.
ShorterEdge(i,j, k , l)
if w(i, j)< w(k , l) return (i, j)
if w(i, j)> w(k , l) return (k, l)
if min(i, j)<min(k, l ) return (i, j)
if min(i, j)>min(k, l ) return (k, l)
if max(i, j)<max(k, l ) return (i, j)
hhif max(i,j) <max(k,l) ii return (k, l)
13.2 The Only MST Algorithm
There are several different ways to compute minimum spanning trees, but really they are all in-
stances of the following generic algorithm. The situation is similar to the previous lecture, where we
saw that depth-first search and breadth-first search were both instances of a single generic traversal
algorithm.
The generic MST algorithm maintains an acyclic subgraph Fof the input graph G, which we
will call an intermediate spanning forest.Fis a subgraph of the minimum spanning tree of G,
and every component of Fis a minimum spanning tree of its vertices. Initially, Fconsists of n
1
pf3
pf4
pf5

Partial preview of the text

Download Minimum Spanning Trees: Lecture 13 from CS 473G, Fall 2005 and more Study notes Computer Science in PDF only on Docsity!

13 Minimum Spanning Trees (November 3)

13.1 Introduction

Suppose we are given a connected, undirected, weighted graph. This is a graph G = (V, E) together with a function w : E → IR that assigns a weight w(e) to each edge e. For this lecture, we’ll assume that the weights are real numbers. Our task is to find the minimum spanning tree of G, i.e., the spanning tree T minimizing the function

w(T ) =

e∈T

w(e).

To keep things simple, I’ll assume that all the edge weights are distinct: w(e) 6 = w(e′) for any pair of edges e and e′. Distinct weights guarantee that the minimum spanning tree of the graph is unique. Without this condition, there may be several different minimum spanning trees. For example, if all the edges have weight 1, then every spanning tree is a minimum spanning tree with weight V − 1.

8 5 10 2 3 18 16 12 14

30

4 26

A weighted graph and its minimum spanning tree.

If we have an algorithm that assumes the edge weights are unique, we can still use it on graphs where multiple edges have the same weight, as long as we have a consistent method for breaking ties. One way to break ties consistently is to use the following algorithm in place of a simple comparison. ShorterEdge takes as input four integers i, j, k, l, and decides which of the two edges (i, j) and (k, l) has ‘smaller’ weight.

ShorterEdge(i, j, k, l) if w(i, j) < w(k, l) return (i, j) if w(i, j) > w(k, l) return (k, l) if min(i, j) < min(k, l) return (i, j) if min(i, j) > min(k, l) return (k, l) if max(i, j) < max(k, l) return (i, j) 〈〈if max(i,j) < max(k,l) 〉〉 return (k, l)

13.2 The Only MST Algorithm

There are several different ways to compute minimum spanning trees, but really they are all in- stances of the following generic algorithm. The situation is similar to the previous lecture, where we saw that depth-first search and breadth-first search were both instances of a single generic traversal algorithm. The generic MST algorithm maintains an acyclic subgraph F of the input graph G, which we will call an intermediate spanning forest. F is a subgraph of the minimum spanning tree of G, and every component of F is a minimum spanning tree of its vertices. Initially, F consists of n

one-node trees. The generic MST algorithm merges trees together by adding certain edges between them. When the algorithm halts, F consists of a single n-node tree, which must be the minimum spanning tree. Obviously, we have to be careful about which edges we add to the evolving forest, since not every edge is in the eventual minimum spanning tree. The intermediate spanning forest F induces two special types of edges. An edge is useless if it is not an edge of F , but both its endpoints are in the same component of F. For each component of F , we associate a safe edge—the minimum-weight edge with exactly one endpoint in that component.^1 Different components might or might not have different safe edges. Some edges are neither safe nor useless—we call these edges undecided. All minimum spanning tree algorithms are based on two simple observations.

Lemma 1. The minimum spanning tree contains every safe edge and no useless edges.

Proof: Let T be the minimum spanning tree. Suppose F has a ‘bad’ component whose safe edge e = (u, v) is not in T. Since T is connected, it contains a unique path from u to v, and at least one edge e′^ on this path has exactly one endpoint in the bad component. Removing e′^ from the minimum spanning tree and adding e gives us a new spanning tree. Since e is the bad component’s safe edge, we have w(e′) > w(e), so the the new spanning tree has smaller total weight than T. But this is impossible—T is the minimum spanning tree. So T must contain every safe edge. Adding any useless edge to F would introduce a cycle. 

u v

e

e’

Proving that every safe edge is in the minimum spanning tree. The ‘bad’ component of F is highlighted.

So our generic minimum spanning tree algorithm repeatedly adds one or more safe edges to the evolving forest F. Whenever we add new edges to F , some undecided edges become safe, and others become useless. To specify a particular algorithm, we must decide which safe edges to add, and how to identify new safe and new useless edges, at each iteration of our generic template.

13.3 Bor ˙uvka’s Algorithm

The oldest and possibly simplest minimum spanning tree algorithm was discovered by Bor ˙uvka in 1926, long before computers even existed, and practically before the invention of graph theory!^2 The algorithm was rediscovered by Choquet in 1938; again by Florek, Lukaziewicz, Perkal, Stienhaus, and Zubrzycki in 1951; and again by Sollin some time in the early 1960s. Because Sollin was the only Western computer scientist in this list—Choquet was a civil engineer; Florek and his co-authors were anthropologists—this algorithm is frequently but incorrectly called ‘Sollin’s algorithm’, especially in the parallel computing literature. (^1) This is actually a special case of a more general definition: For any partition of F into two subforests, the minimum-weight edge with one endpoint in each subforest is light. A few minimum spanning tree algorithms require this more general definition, but we won’t talk about them here. (^2) The first book on graph theory, written by D. K¨onig, was published in 1936. Leonard Euler published his famous theorem about the bridges of K¨onigsburg (HW3, problem 2) in 1736. K¨onigsburg was not named after that K¨onig.

even ‘the Prim/Dijkstra algorithm’, even though by 1958 Dijkstra already had another algorithm (inappropriately) named after him. In Jarn´ık’s algorithm, the forest F contains only one nontrivial component T ; all the other components are isolated vertices. Initially, T consists of an arbitrary vertex of the graph. The algorithm repeats the following step until T spans the whole graph:

Jarn´ık: Find T ’s safe edge and add it to T.

8 5 10 2 3 18 16 12 14

30

4 26

18

8 5 10 2 3 16 12 14

30

26

8 5 10 2 3 18 16 30

26

8 5 10 3 16 30

26

8 5

16 30

26

16 30

26 Jarn´ık’s algorithm run on the example graph, starting with the bottom vertex. At each stage, thick edges are in T , an arrow points along T ’s safe edge, and dashed edges are useless.

To implement Jarn´ık’s algorithm, we keep all the edges adjacent to T in a heap. When we pull the minimum-weight edge off the heap, we first check whether both of its endpoints are in T. If not, we add the edge to T and then add the new neighboring edges to the heap. In other words, Jarn´ık’s algorithm is just another instance of the generic graph traversal algorithm we saw last time, using a heap as the ‘bag’! If we implement the algorithm this way, its running time is O(E log E) = O(E log V ). However, we can speed up the implementation by observing that the graph traversal algorithm visits each vertex only once. Rather than keeping edges in the heap, we can keep a heap of vertices, where the key of each vertex v is the length of the minimum-weight edge between v and T (or ∞ if there is no such edge). Each time we add a new edge to T , we may need to decrease the key of some neighboring vertices. To make the description easier, we break the algorithm into two parts. Jarn´ıkInit initializes the vertex heap. Jarn´ıkLoop is the main algorithm. The input consists of the vertices and edges of the graph, plus the start vertex s.

Jarn´ık(V, E, s): Jarn´ıkInit(V, E, s) Jarn´ıkLoop(V, E, s)

Jarn´ıkInit(V, E, s): for each vertex v ∈ V \ {s} if (v, s) ∈ E edge(v) ← (v, s) key(v) ← w(v, s) else edge(v) ← Null key(v) ← ∞ Insert(v)

Jarn´ıkLoop(V, E, s): T ← ({s}, ∅) for i ← 1 to |V | − 1 v ← ExtractMin add v and edge(v) to T for each edge (u, v) ∈ E if u /∈ T and key(u) > w(u, v) edge(u) ← (u, v) DecreaseKey(u, w(u, v))

The running time of Jarn´ık is dominated by the cost of the heap operations Insert, Extract- Min, and DecreaseKey. Insert and ExtractMin are each called O(V ) times once for each vertex except s, and DecreaseKey is called O(E) times, at most twice for each edge. If we use a Fibonacci heap, the amortized costs of Insert and DecreaseKey is O(1), and the amortized cost of ExtractMin is O(log n). Thus, the overall running time of Jarn´ık is O(E + V log V ). This is faster than Bor ˙uvka’s algorithm unless E = O(V ).

13.5 Kruskal’s Algorithm

The last minimum spanning tree algorithm I’ll discuss was first described by Kruskal in 1956, in the same paper where he rediscovered Jarnik’s algorithm. Kruskal was motivated by ‘a typewritten translation (of obscure origin)’ of Bor ˙uvka’s original paper, claiming that Bor ˙uvka’s algorithm was ‘unnecessarily elaborate’.^3 This algorithm was also rediscovered in 1957 by Loberman and Weinberger.

Kruskal: Scan all edges in increasing weight order; if an edge is safe, add it to F.

8 5 10 2 3 18 16 12 14

30

4 26

8 5 10 3 18 12 16 14

30 4 26

8 5 10 18 12 16 14

30 4 26

8 5 10 18 12 16 14

30 26

18

8 10 12 16 14

30 26

10 12 16 14

30 26

12 16 14

30 26

16 18 18 14

30 26

30 16 18 26

18

30 26

(^18 )

26

30

Kruskal’s algorithm run on the example graph. Thick edges are in F. Dashed edges are useless. (^3) To be fair, Bor ˙uvka’s original paper was unnecessarily elaborate, but in his followup paper, also published in 1927, simplified his algorithm essentially to the form presented here. Kruskal was apparently unaware of Bor ˙uvka’s second paper. Stupid Iron Curtain.