Docsity

Minimum Spanning Trees: Finding the Optimal Subgraph, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Algorithms Lecture 12: Minimum Spanning Trees
We must all hang together, gentlemen, or else we shall most
assuredly hang separately.
— Benjamin Franklin, at the signing of the
Declaration of Independence (July 4, 1776)
It is a very sad thing that nowadays there is so little useless
information.
— Oscar Wilde
Computers are useless. They can only give you answers.
— Pablo Picasso
A ship in port is safe, but that is not what ships are for.
— Rear Admiral Grace Murray Hopper
12 Minimum Spanning Trees
12.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(e0)for any pair of
edges eand e0. 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. SH ORT ER EDGE takes as input four integers i, j, k, l, and decides which of the two
edges (i, j)and (k, l )has ‘smaller’ weight.
1
pf3
pf4
pf5

Partial preview of the text

Download Minimum Spanning Trees: Finding the Optimal Subgraph and more Study notes Algorithms and Programming in PDF only on Docsity!

We must all hang together, gentlemen, or else we shall most assuredly hang separately. — Benjamin Franklin, at the signing of the Declaration of Independence (July 4, 1776) It is a very sad thing that nowadays there is so little useless information. — Oscar Wilde Computers are useless. They can only give you answers. — Pablo Picasso A ship in port is safe, but that is not what ships are for. — Rear Admiral Grace Murray Hopper

12 Minimum Spanning Trees

12.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)

12.2 The Only Minimum Spanning Tree Algorithm

There are several different methods for computing minimum spanning trees, but really they are all instances 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 minimum spanning tree 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 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 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. 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.^1

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. (^1) This is actually a special case of two more general theorems: First, for any partition of the vertices of G into two disjoint subsets, the minimum-weight edge with one endpoint in each subset is in the minimum spanning tree. Second, the maximum-weight edge in any cycle in G is not in the minimum spanning tree. A few minimum spanning tree algorithms require this more general result, but we won’t talk about them here.

Each call to FINDSAFEEDGES takes O(E) time, since it examines every edge. Since the graph is connected, it has at most E + 1 vertices. Thus, each iteration of the while loop in BOR˙UVKA takes O(E) time, assuming the graph is represented by an adjacency list. Each iteration also reduces the number of components of F by at least a factor of two—the worst case occurs when the components coalesce in pairs. Since there are initially V components, the while loop iterates O(log V ) times. Thus, the overall running time of Bor˙uvka’s algorithm is O(E log V ).

Despite its relatively obscure origin, early algorithms researchers were aware of Bor˙uvka’s algo- rithm, but dismissed it as being “too complicated”! As a result, despite its simplicity and efficiency, Bor˙uvka’s algorithm is rarely mentioned in algorithms and data structures textbooks.

12.4 Jarn´ık’s (‘Prim’s’) Algorithm

The next oldest minimum spanning tree algorithm was first described by the Polish mathemati- cian Vojtˇech Jarn´ık in a 1929 letter to Bor˙uvka. The algorithm was independently rediscovered by Kruskal in 1956, by Prim in 1957, by Loberman and Weinberger in 1957, and finally by Dijk- stra in 1958. Prim, Loberman, Weinberger, and Dijkstra all (eventually) knew of and even cited Kruskal’s paper, but since Kruskal also described two other minimum-spanning-tree algorithms in the same paper, this algorithm is usually (incorrectly) called ‘Prim’s algorithm’, or sometimes even ‘the Prim/Dijkstra algorithm’^4 , even though by 1958 Dijkstra already had another algorithm (inap- propriately) 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´IK: 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.

(^4) following the Last Discoverer Rule

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´IKINIT initializes the vertex heap. JARN´IKLOOP is the main algorithm. The input consists of the vertices and edges of the graph, plus the start vertex s.

JARN´IK(V, E, s): JARN´IKINIT(V, E, s) JARN´IKLOOP(V, E, s)

JARN´IKINIT(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´IKLOOP(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´IK 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 Fi- bonacci heap^5 , 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´IK is O(E + V log V ). This is

faster than Bor˙uvka’s algorithm unless E = O(V ).

12.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 typewrit- ten translation (of obscure origin)’ of Bor˙uvka’s original paper, claiming that Bor˙uvka’s algorithm was ‘unnecessarily elaborate’.^6 This algorithm was also rediscovered in 1957 by Loberman and Weinberger, but somehow avoided being renamed after them.

KRUSKAL: Scan all edges in increasing weight order; if an edge is safe, add it to F. (^5) See Non-Lecture B. (^6) 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 in this lecture note. Kruskal was apparently unaware of Bor˙uvka’s second paper. Stupid Iron Curtain.

minimum spanning tree. Using union-by-rank and path compression allows us to perform each UNION or FIND in O(α(E, V )) time, where α is the not-quite-constant inverse-Ackerman function. So ignoring the cost of sorting the edges, the running time of this algorithm is O(E α(E, V )). We need O(E log E) = O(E log V ) additional time just to sort the edges. Since this is bigger than the time for the UNION-FIND data structure, the overall running time of Kruskal’s algorithm is O(E log V ) , exactly the same as Bor˙uvka’s algorithm, or Jarn´ık’s algorithm with a normal (non- Fibonacci) heap.