You are on page 1of 8

Algorithms for Minimum Weight Spanning Forests in Graphs Some Definitions Definition.

An undirected graph, or briefly, a graph G = (V, E) consists of a non-empty set V of elements called vertices and a set E of unordered pairs of elements of V called edges. Sometimes we will use ab to denote edge {a,b}. Vertices a and b are called the ends of edge {a,b}. We can represent a graph by making a point for each vertex and a curve joining a and b for each edge e = {a,b}. Definition. A path in a graph is an alternating sequence of distinct vertices vi (i=0,,k) and edges ei (i=1,,k) of the form: v0 , e1 , v1, e2 , v2 , , vk-1 , ek , vk, where for each i, the ends of ei are vi-1 and vi . The number of edges in the path is called its length. For a graph G = (V, E), and subset S of V, (S) is the set of edges with one end in S and the other end in V-S. Definition. A circuit is an alternating sequence of distinct vertices vi (i=1,,k) and edges of the form: v1 , {v1 , v2 }, v2 , {v2 , v3 }, , {vk-1 , vk }, vk, {vk, v1 }, v1 .

Definition. A graph G is called connected if for every pair of vertices of G, there is a path in G joining them. The maximal subgraphs of G which are connected are called the connected components of G. Definition. A tree is a graph which is connected and contains no circuits. A spanning tree T in a graph G is a subgraph of G which is a tree that meets all the vertices of G. Definition. A spanning forest F of a graph G is a set of spanning trees, one for each connected component of G. Notation. Given weights on the edges of G, the weight of a spanning tree T (or spanning forest F) is the sum of weights on the edges of T (or F ). Kruskals Algorithm for Minimum Weight Spanning Forest Input: A graph G = (V, E) with n vertices and weights w(j) on the edges j. Output: A minimum weight spanning forest To start, T = . In general, add to T an edge of minimum weight, as long as it does not create a circuit with T. When no more edges can be added to T: If T has n-1 edges, T is a spanning tree. If T has fewer than n-1 edges, then T is a spanning forest; then it can be shown that G is not connected.
2

Proof that Kruskals algorithm works to find a spanning forest of minimum weight Let T* be the spanning forest output by Kruskals algorithm. Call the edges of T* e1, e2, , en-1, where w(e1) w(e2) w(en-1). For any minimum weight spanning forest T, define g(T) to be the smallest i such that ei is not in T, and define g(T) = n if there is no such i (that is, if T is the same as T*). Let T be a minimum weight spanning forest with g(T) as large as possible; that is, g(T) = max{g(T): T is a minimum weight spanning forest} If g(T) = n, then T = T*, so T* is a minimum weight spanning forest. So assume g(T) = k < n. Consider T and T*. Let the edges of T be e1, e2, , ek-1, fk, , fn-1. T* (output of algorithm): w(e1) w(e2) w(ek-1) w(ek) w(ek+1) w(en-1) T (minimum weight spanning forest): w(e1) w(e2) w(ek-1) w(fk) w(fk+1) w(fn-1)

Since { e1, e2, , ek-1, fk } is part of T, fk does not create a circuit with { e1, e2, , ek-1 }. So fk was considered to enter the forest at the same time as ek , but the algorithm chose ek, so w(ek) w(fk). If we add ek to T , a unique circuit C is created. This circuit must contain at least one of the edges fk, , fn-1 since { e1, e2, , ek-1, ek } is part of T*, and thus contains no circuits. Say fq is in C. Then: (1) T = T U { ek } - { fq } is a spanning forest of G. (2) w(ek) w(fk) w(fq) (3) w(T) = w( T ) + w(ek) - w(fq) w( T ) Since T is of minimum weight, so is T. Thus T is a minimum weight spanning forest and g(T) k+1 g(T). This contradicts the choice of T. Thus the minimum weight spanning forest T with g(T) as large as possible does not have g(T) < n, but rather, g(T) = n; that is, T = T*, so T* is a minimum weight spanning forest.

Prims Algorithm Input: Output: A graph G = (V, E) with n vertices and weights wj on the edges j. A minimum weight spanning tree or a non-empty set S of vertices, S V, with (S) = (which shows that G is not connected.)

Choose some vertex to be the starting tree. In general, we have a set of tree vertices S. Choose an edge e I n (S) with smallest weight. Add e to the tree, and add the end of e not already in S to S. If (S) = , then if S = V, T is a minimum weight spanning tree. Stop. if S V, then S is a non-empty set of vertices with (S) = , so G is not connected. Stop. Worst-case complexity Let n be the number of vertices of G and m the number of edges. There are at most n stages of the algorithm, because at each stage, a vertex enters the tree. At each stage of the algorithm, we choose an edge with one end in S and the other end not in S. There could be O(m) such edges. We calculate the min of these weights using O(m) comparisons. Thus the worst case complexity of the algorithm is O(n(O(m)+O(m))) = O(nm).

Proof that Prims algorithm works. The T constructed is clearly connected, and we can see it has no circuits as follows: If circuit C is contained in T, let e be the last edge of C to enter T. Then when e entered T, both of its ends were already in T, which contradicts the algorithm. The rest of the proof that Prims algorithm works follows from the lemma below. Lemma. Let G = (V, E) be a connected graph with weights wj on the edges j. Let E be a connected subset of edges in some minimum weight spanning tree T of G. Let V be the set of vertices meeting E. Let e* be a minimim weight edge of (V). Then E U {e*} is a subset of a minimum spanning tree. Proof . If e* is an edge of T, the conclusion is immediate. Suppose e* = xy is not an edge of T. Since, e* is in (V), say x is in V and y is not in V. Since T is connected, there is a path P in T from x to y. Let uv be the first edge in P with u in V and v not in V. Where E(T) = E(T) {uv} U {xy}, T = (V, E) is a spanning tree of G, and E U {xy} is contained in T. By choice of xy, wxy wuv. Thus T is a minimum weight spanning tree.
6

Dijkstra-Prim Algorithm Input: Output: A graph G = (V, E) with n vertices and weights wj on the edges j. A minimum weight spanning tree or a non-empty set S of vertices, S V, with (S) = (which shows that G is not connected.)

Choose some vertex to be the starting tree. Set V(T) = this vertex. E(T) = . In general, if vertex x has just entered the tree: For each edge xy with y not in the tree If wxy < we for ys candidate edge e, or if y has no candidate edge, make ys current candidate edge xy. If there are no candidate edges, Then If V(T) = V, stop: T = (V, E(T)) is a minimum weight spanning tree. If V(T) V, then S = V(T) is a non-empty set of vertices with (S) = , so G is not connected. Otherwise, let e* be the candidate edge of minimum weight. Add e* to E(T). Add the end of e* not already in T to V(T). e* is no longer a candidate edge. Worst-case complexity Let n be the number of vertices of G and m the number of edges. Each time a vertex enters the tree (at most n times), we must update current candidate edges for vertices y with xy an edge of G (at most n-1 such edges, so at most O(n) work) and then calculate the minimum weight of the candidate edges (at most one candidate edge for each vertex not in T, so at most n-1 candidate edges, so at most n-2 comparisons, so O(n) work. Thus the worst case complexity of the algorithm is n(O(n)+O(n)) = O(n2).
7

You might also like