JOURNAL
OF ALGORITHMS
Generating
8.592-591(1987)
the Maximum Spanning Weighted Graph
Trees of a
FXNICX GAVRIL* Computer
Science Division,
University Received
of California, April
Davis,
California
9S616
1986
The present paper contains an efficient algorithm for generating all the maximum spanning trees of a weighted graph and a polynomial time algorithm for counting them. As known, the tree representations of an acylic hypergraph are exactly the maximum spanning trees of the weighted intersection graph of its edges. Therefore, they can be generated and counted by these algorithms. 0 1987 academic Pms, I~C.
The graphs G(V, E) considered in this paper are finite, undirected, with no self-loops, having I/ as set of vertices and E as set of edges. Two vertices connected by an edge are called adjacent. Unless otherwise stated, by a subgraph of G we will always mean a vertex subgraph. A clique of G is a maximal set of vertices whose every two elements are adjacent. A tree is a connected graph without simple circuits. A connected subgraph of a tree is called a subtree. A spanning tree of a graph G( V, E) is a tree with vertex set V and edge set E’, where E’ c E. Two spanning trees T’( V, E’), T”( V, E”) are distinct if E” # E’. We will denote by ST(G) the set of spanning trees of a graph G. The number of spanning trees of an undirected graph G( V, E) can be found using Kirchhoff’s formula, as described in [EV]: Let {vi,..., u,-i} be any subset of n - 1 vertices of G( V, E), where ) VJ = n. Construct an (n - 1) x (n - 1) matrix A = (aij) in which every a,, is the degree of ui in G and every aij, i # j, is minus the number of edges connecting u,. and uj in G. Then, the determinant det(A) of A is the number of spanning trees of G. Efficient algorithms for generating all spanning trees of a graph are described in [MI] and [RTI and their time performance is O( ] E) I ST( G) I). *Present
address:
CEMA
(17), P.O. Box 2250, Haifa,
592 Ol%-6774/87 Copyright All rights
$3.00
Q 1987 by Academic Press, Inc. of reproduction in any form reserved.
Israel.
GENERATING
MAXIMUM
SPANNING
TREES
593
A graph G( V, E) is weighted if each edge e has assigned a number w(e) called weight; the weight of G is the sum of its edge weights. A maximum spanning tree is one having a maximum weight among all the spanning trees of G. The set of maximum spanning trees of a graph G, will be denoted MST(G). Gabow [GAB] presents an algorithm for generating the spanning trees of a weighted graph in order of weight. The algorithm can be used to generate the maximum spanning trees. The purpose of the present paper is to describe a slightly faster algorithm for generating all the maximum spanning trees and a polynomial algorithm for counting them. By symmetry, the algorithm can also be used for minimum spanning trees. Without loss of generality we can restrict the problem to weighted graphs with integer weights, since we can replace the weights by their rank, equal weights being replaced by the same rank. A hypergraph H(V, C) consists of a non-empty finite set V of elements called vertices and a finite family C of subsets of V called edges. For a subset C’ of C and V’ = U CE,+, the hypergraph H(P, C’) is called the partial hypergraph of H(V, C) defined by C’. The weighted intersection graph G(C, E) of a hypergraph H(V, C) is a graph whose vertices C E c are in a oneto-one correspondence with the elements c E C, two vertices E,, Cz being adjacent if and only if cr n c2 # 0; the weight of the edge (Cl, C2) is IQ n cz]. A hypergraph H( V, C) is called acyclic if there exists a tree T whose vertices are in a oneto-one correspondence with the elements of C such that for every vertex u E V, the subgraph defined by the set of vertices of T corresponding to the edges containing u is a subtree; the tree T is called a tree representation of H(V, C). Gavril [GAV proves that a graph G(V, E) is chordal if and only if the hypergraph H(V, C) defied by its family of cliques C is acyclic. He presents an efficient algorithm for constructing a tree representation of H, if one exists. This algorithm can also be used for recognizing acyclic hypergraphs. Acyclic hypergraphs are very useful in relational database systems and were extensively discussed in [BFMY] and [GS]. The algorithm described in these papers for recognizing acyclic hy-pergraphs and for constructing tree representations resembles the algorithm described in [GAV; a different algorithm is described in [TY]. Bernstein and Goodman [BG] prove that every tree representation of an acyclic hypergraph is a maximum spanning tree of its weighted intersection graph and conversely. We will present a simple proof to this theorem. Therefore our algorithms can also be used for counting and generating the tree representations of an acyclic hypergraph. Consider a graph G( V, E) and a family of vertex disjoint subgraphs G,(C’,, 41,. . -9 Gk(Vk, E,J of G. The contraction G’ of G relative to G r, . . . , G, is the graph obtained from G by collapsing every subgraph G, into one vertex xi, xi remaining incident to all the edges incident to the vertices of Gj, and by labelling each edge by its original edge in G. The
594
FiNICii
GAVRIL
graph G is called the expansion of G’ relative to G,, . . . , G, and it can be constructed from G’, G,, . . . , G, using the labellings on its edges. THE MAXIMUM
SPANNING TREES
OF
A
WEIGHTED GRAPH
Consider a weighted graph G( V, E), and for every i, let Gj( I’, Ei) be the edge subgraph of G defined by the edges e having w(e) 2 i; G, = G and w(e) + 1, has no edges. For every G,, let {G,i(Fj, E/)}$S, G,, k = m=,,E denote its connected components and let Si denote its contraction relative to { G;+,}i”“l. LEMMA 1. Consider a maximum spanning tree T of G and let T’, T” be the connected components obtained from T by deleting any edge e. For every edge e’ connecting a vertex of T’ to a vertex of T” there exists w(e) 2 w(e’). Proof:
Obvious from the fact that T is maximum.
0
LEMMA 2. If Gi is connected, then, for every maximum spanning tree T of Gi the subgraph of T defined by any q< 1 is connected. Proof. Assume that for some j, the subgraph of T defined by I$!+1 has two connected components T’ and T”. G{+, is connected, hence it contains an edge e between a vertex of T’ and a vertex of T”. By adding e to T we generate a simple circuit containing at least one edge e’ which is not in Gi+l* By deleting e’ from T we obtain two connected components, one containing T’, the other containing T”. Therefore, by Lemma 1, w(e’) 2 w(e) = i + 1 contradicting the fact that e’ is not in G,+i. 0
LEMMA 3. If Gi is connected, a spanning tree T of G, is a maximum spanning tree of Gi if and only if its vertices can be covered by a family { T-t+1 }fi-+i of vertex disjoint subtrees such that for every 1 I j I si+ i, q-$ i is a maximum spanning tree of G/+ 1 and the contraction of T relative to { T-i+,}?~i is a spanning tree of Si. Proof The “if’ part is obvious. For the “only if” part consider a maximum spanning tree T of Gj. By Lemma 2 every I$!! I defines in T a connected subgraph q< 1. Assume that for some j, qL1 is not a maximum spanning tree of G{+, and let T’ be one. By replacing q
Using Lemma 3 we can describe an algorithm for generating the maximum spanning trees of a weighted graph G. We start with k, the smallest integer for which G, has no edges. Assume that at some stage i we have for every 1 I j -< s, the set MST(Gj) of maximum spanning trees of G/. Let {Gi’_,}f-i be the connected components of G,-i. If G/-i is also a connected component Gj of Gi then MST(G,i_ 1) = MST(G~). If Gii_, is not a
GENERATING
MAXIMUM
SPANNING
TREES
595
connected component of Gi, then G/-r is obtained from a number G/I,..., Giip of connected components of Gi, among which there are edges of weight i - 1. Let S/-r be the contraction of Gjl, relative to { GF}!,,. MST(Gii_,) is obtained by constructing all the sparming trees of Sii_, and expending .each spanning tree relative to every combination of maximum spanning trees of { Gi}p,,. A more detailed description of the algorithm is given below: ALGORITHM
STEP
MST(G).
1:
Construct the graphs {Gi}~~,, where G, = G and k is the smallest integer such that G, has no edges. Every connected component G;Gof G, consists of a single vertex, hence MST(GL) contains one maximum spanning tree having a single vertex. Set i = k. STEP 2: if i = 1 then stop. STEP 3: Let { Gj’_,}T:; be the connected components of Giel foreveryl
596
FXNICX
GAVRIL
constructed starting with the connected components of S,-, as leaves and adding at stage i - 1 a vertex S/-i as father of Sj;, . . . , Sijp, by the above method, until the root S, is added. When a spanning tree T,j is given for every vertex Sij of M, a maximum spanning tree of G can be obtained by starting from its last level, replacing, for every Sk, the vertex Gk in its father Sij_, by qh and connecting the vertices of T/r to all the other vertices according to the labels on the edges incident to G,j. Let the sequence of M’s vertices, ordered by level be X = {Sl’, s; )...) Sk )..., s:-, )...) Sfi:r }. With every element S/ of X we activate an algorithm for generating its spanning trees, one spanning tree each time S/ is considered, remembering the parameters necessary for generating the next spanning tree when Sij will be considered again. On X we perform a backtrack algorithm. Assume that we consider Sij. If we arrive at Sij from its right neighbor in X then, if no more spanning trees of Sij can be generated we consider its left neighbor in X, otherwise we construct its next spanning tree and consider its right neighbor in X. If we arrive at S! from its left, we set its parameters for generating spanning trees at start and generate its first spanning tree. If Sij = Sk:{, then every vertex of M has as explained before and consider its left neighbor. Otherwise we consider its right neighbor. When we are back at S,l and no more spanning trees can be generated for it, we finished, and all maximum spanning trees have been generated. The time performance of this algorithm is 0( IEI (MST(G) I). The number of maximum spanning trees can be calculated using the tree-like structure M. For every vertex Sij of M we calculate the number of spanning trees ]ST(S,j)] of the graph Sij using Kirchhoff s formula as described in the Introduction. Going in M from the leaves towards the root we calculate for every vertex S/ the function f(S,j) defined as follows: If S! is a leaf of M then f(S,j) = ]ST(S,j)]. If Sij is not a leaf and Siif-i, . . . , S,!;, are its sons in M, then f(S,j) = ]ST(S,i)lIJ~,,j(S/i;,). Clearly, f(Si) is the number of maximum spanning trees of G(V, E). The time performance of the algorithm is bounded by 0( I V]3.5) needed for calculating the determinants to obtain IST(S,/‘) ( at each vertex Sij of M.
T’HE TREE REPRESENTATIONS
OF AN ACYCLIC
HYPERGRAPH
Consider an acyclic hypergraph H(V, C) and its weighted intersection graph G(C, E). Any tree representation of H is a spanning tree of G. The following theorem appears in [BG]. THEOREM
1.
if it is a maximum
A spanning tree of G is a tree representation of H if and only spanning tree of G.
GENERATING
MAXIMUM
SPANNING
TREES
597
Proof: Consider a tree representation T of H and assume that it is not a maximum spanning tree of G, i.e., there is an edge e = (Cr, C2) in T and an edge e’ = (C3, C4) not in T such that w(e’) > e(e) and by replacing e by e’ in T we obtain a spanning tree of G. Since T is a tree representation of H, for every u E cj n c4 the subtree of T corresponding to u must cross e, hence c3 n c4 c cr n c2, contradicting the fact that w(e’) > w(e). Therefore T must be a maximum spanning tree of G. Conversely, consider a maximum spanning tree T’ of G and let T be a tree representation of H having a maximum number of edges in common with T’. Assume that T # T’, and let e’ = (&, C4) be an edge of T’ which is not in T. By adding it to T we obtain a simple circuit which contains an edge e = (Cr, C2) which is not in T’. As before, cj n cd G cl n c2, thus w(e’) = w(e), otherwise T would be heavier than T’. It follows that cj n c4 = ct n c2 and by replacing e by e’ in T we obtain a tree representation of H having a bigger number of edges in common with T’. Therefore T=T’.o From the above theorem it follows that the tree representations of T can be obtained by generating the maximum spanning trees of G and their number is (MST(G)(.
ACKNOWLEDGMENT I thank M. Yarmakakis for poiutiug out the extended applicability of the algorithm
REFERENCES [BFMY] W-7 WI [GAB] [GAV]
PW WI [RTI [=I
C. BEERI, R. FAGIN, D. MAIER, AND M. YANNARAKIS, On the desirability of acyclic database systems, J. Assoc. Camput. Mach. 30 (1983), 479-513. P. A. BERNSFEINand N. GOODMAN, Power of natural semijoins, SIAM J. Comput. 10 (1981). 751-771. S. EVEN, “Algorithmic Combinatorics,” Macmillan Co., New York, 1973. H. N. GABOW, Two algorithms for generating weighted spanning trees in order, SIAM J. Comput. 6 (1977), 139-150. F. GAVEL, The intersection graphs of subtrees in trees are exactly the chordal graphs, J. Combin. Theory Ser. B 16 (1974), 47-56. N. GOODUN AND 0. SHMUELI, Syntactic characterization of tree database systems, J. Assoc. Comput. Mach. 30 (1983), 767-786. G. J. MINTY, A simple algorithm for listing all the trees of a graph, IEEE Trans. Circuit Theory CT-12 (1%5), 120. R. C. READ AND R. E. TARJAN, Bounds on backtrack algorithms for listing cycles, paths and spanning trees, Nerworlcr 5 (1975), 237-252. R. E. TARJAN AND M. YANNAKAKIS, Simple linear-time algorithms to test chordality of graphs, test acyclicity of hypergraphs, and selectively reduce acyclic hypergraphs, SIAM J. Comput. 13 (1984), 566-579.