IEEE TRANSACTIONSON CIRCUITSAND SYSTEMS,VOL. CAS-31, NO. lo, OCTOBER1984 853 Complexity of Computation of a Spanning Tree

R. JAYAKUMAR, K. THULASIRAMAN, AND M. N. S. SWAMY, FELLOW, IEEE

Absrracr -In l!X3, Char [4] presented an algorithm to enumerate all the rithm by minimizing the number of non-tree subgraphs spanning trees of an undirected graph G. This algorithm starts with a generated by the algorithm. Finally, in Section V, we known initial spanning tree of G, and generatesall the other spanning trees along with certain spanning non-tree subgraphs of G. In this paper a discuss a general technique called path compression,which detailed complexity analysis of char’s algorithm and methods to speed up can be used for an efficient implementation of Char’s the algorithm are discussed.Two heuristics for the selection of the initial algorithm to reduce the actual number of comparisons spanning tree are suggested. These heuristics result in a considerable made by the algorithm. reduction in the number of spanning non-tree subgraphs generated. A For terms and notation not defined here, technique called path compression, aimed at reducing the actual number of comparisons, is described. Computational results on several randomly see [6]. Without any loss of generality we also assumethat generated graphs are presented to illustrate the improvement achieved. the graphs considered in this paper are simple biconnected undirected graphs. I. INTRODUCTION II. CHAR'SALGORITHMTO ENUMERATEALLTHE NUMERATING all the spanning trees of a graph SPANNINGTREES E without duplication is one of the widely studied graph problems in Electrical Engineering and Computer Consider a connected undirected graph G = (V, E) with Scienceliterature. Since the number of spanning trees of a IZ= JV] vertices and m = ]E] edges.Let the vertices of G be graph increasesrapidly with the size of the graph, a highly denoted as 1,2; . 0, n. Let h = (DIGIT(l), DIGIT(2); . ., efficient algorithm is desired to enumerate all the spanning DIGIT(n - 1)) denote a (II - 1)-digit sequenceof vertices trees of a graph. Several of varying efficiency such that DIGIT is a vertex adjacent to vertex i in G. have been proposed in the literature. One of the well-known With each such sequence A we can associate a subgraph algorithms is due to Minty [l], which has been shown [2] to Gx = (V,, Eh) of G such that be of complexity O(m + n + mt), where m and II are the number of edgesand the number of vertices of the graph, respectively, and t is the number of spanning trees of the and graph. Another efficient algorithm of complexity O(m + n E,= {(~,DIGIT(~)), (~,DIGIT(~)),..., + nt) is due to Gabow and Myers [3]. (n -l,DIGIT(n -1))). In 1968, Char [4] had presented a conceptually simple and elegant algorithm to enumerate all the spanning trees Char’s algorithm first performs a Breadth-First Search of a graph. However, Char had not presenteda complexity [6] on G and finds a spanning tree called the initial analysis of his algorithm. The recent analysis of Char’s spanning tree. During this search,the vertices of G are also algorithm presented in [5] suggests that this algorithm renumbered as n, n - 1,. . . , 1 in the order in which they are might be the best of all the algorithms available so far for visited. Let h, = (REF(l), REF(2), * . . , REF( n - 1)) be the the spanning tree enumeration problem. In this paper, we sequencecorresponding to this spanning tree. Starting with not only present a more detailed complexity analysis of h,, the algorithm enumeratesall the other spanning trees Char’s algorithm but also discuss different methods to of G by generating the sequencescorresponding to the further improve the speed of the algorithm. spanning trees of G. During this enumeration, the algo- In Section II we give a formal description of Char’s rithm also generatescertain sequenceswhich correspond to algorithm and summarise some of its interesting properties spanning non-tree subgraphs of G. The sequencescorre- reported in [5]. In Section III we give a detailed complexity sponding to spanning trees are referred to as tree sequences analysis of this algorithm and present several of its quanti- and those corresponding to spanning non-tree subgraphs tative and qualitative properties. In Section IV we develop are referred to as non-tree sequences.The sequenceh, is two heuristic procedures which help speedup Char’s algo- called the initial tree sequence. Char’s algorithm classifies a generatedsequence as a tree sequenceif it satisfies the following. Manuscriot received March 31. 1983: revised Seotember 6. 1983 and November 25 1983. This work was su’ ported by ihe Naturh Sciences and Enzineer& Research Council of 8 anada under Grant A-7739 and Tree Compatibility Property under &ant A-2680. The authors are with the Facult of En ‘neering and Computer Science, The sequence(DIGIT(l), DIGIT(2), * . . , DIGIT( n - 1)) Concordia University, Montreal, S.Q., HTG lM8 Canada. represents a spanning tree of graph G if and only if

0098-4094/84/1000-0853$01.00 01984 IEEE 854 IEEE TRANSACTIONSON CIRCUITSAND SYSTEMS,VOL.CAS-31,N0. 10, OCTOBER1984

for each j 6 n -1 there exists, in G, a sequence of Theorem 1 edges (chosen from among the edges (1, DIGIT(l)), For a complete graph, the number to of spanning non-tree (2, DIGIT(2)); . . , (n - 1, DIGIT(n - 1))) with subgraphsgenerated by Char’s algorithm is independent of ( j, DIGIT( j)) as the starting edge, which leads to a vertex the initial spanning tree. cl k > j. 0 If a sequencedoes not have the above property, then it is Theorem 2 a non-tree sequence. Let G(“-i) be the set of all connected n-vertex graphs Char’s algorithm can be presentedin ALGOL-like nota- having at least one vertex of degree n - 1. For any graph tion as follows. G E G(“-l), to 6 t, if the initial spanning tree is a star tree. Char’s Algorithm to Enumerate all the Spanning Trees cl procedure CHAR; A characterization of each spanning non-tree subgraph comment The graph G is representedby the adjacency lists generatedby Char’s algorithm is also given in [5]. of its vertices. SUCC(DIGIT(i)) is the entry next to DIGIT(i) in the adjacency list of vertex i. III. COMPUTATIONAL COMPLEXITY OF CHAR'S begin ALGORITHM find the initial spanning tree and obtain the initial tree Since the computational complexity of Char’s algorithm sequenceX, = (REF(l), REF(2), . . . , REF( n - 1)); is O(m + n + n(t + to)), any complexity analysis of this renumber the vertices of the graph using the initial algorithm would require a study of the number (t + to). spanning tree; With this objective in view, we first obtain an expression initialize DIGIT(i):= REF(i), 1~ i G n - 1; for (t + to). output the initial spanning tree; Let k:=n -1; n-l while k f 0 do begin T= u q if SUCC(DIGIT(k)) # nil I=0 then begin DIGIT(k): = SUCC(DIGIT(k)); be the set of all the tree sequencessuch that if DIGIT(i), 1~ i 6 n - 1, is a tree sequence (i) To= {A,,}, and then begin / (ii) T., l i, k + 1~ i < n - 1, and DIGIT(l), Theorem 3 DIGIT(2), . . . , DIGIT( k - 1) have the same values as in Let G be a connected n-vertex undirected graph with its the previous sequence.Hence the new sequenceis to be vertices numbered as in Char’s algorithm. Let Gp), 1~ k tested for tree compatibility property only at position k < n - 1, be the graph obtained from G by coalescing the and this test, in the worst case,involves k - 1 comparisons. vertices k, k +l; . ., n and let t(k) be the number of Hence at most n computational steps are required to spanning trees of Gp). If t is the number of tree sequences generate and test a sequence.So, if to is the number of and to is the number of non-tree sequencesgenerated by non-tree sequencesand t is the number of tree sequences the algorithm, then generated by Char’s algorithm, in the worst case n(t + to) n-l computational steps are required to enumerate all the t + to =l+ c (deg(k)-l)t(k) spanning trees of the given graph and hence, Char’s algo- k=l rithm is of 0( m + n + n(t + to)), which where deg( k), 1~ k < n, is the degreeof vertex k in G. also includes the complexity of finding the initial spanning tree. Proof: The following are two of the interesting properties of Consider a tree sequence A, = (DIGIT(~), Char’s algorithm. For other properties, see[5]. DIGIT(2), . . . , DIGIT(k - l), REF(k), REF(k + 1); . . , JAYAKUMAR et al. : SPANNING TREE ENUMERATION ALGORITHM 855

REF(n - 1)) generated by Char’s algorithm. Let G, = w(i, j) denotes the weight of the edge (i, j). Let i be any (V,, Ek) be the spanning tree corresponding to X, and vertex of G(W) and let l?(i) be the set of vertices adjacent G; = (V;, EL) be the spanning 2-tree obtained from G, by to vertex i in G(w). Let deleting the edge (k, REF(k)). Since REF(i) > i, 1~ i < n - 1, it follows that in G; the edges(k + 1, REF( k + l)), di= C w(i, j). j E r(i) (k+2,REF(k+2));.*, (n-l,REF(n-1)) are in one component, say the component G;,, = (Vl,,, EL i), and the By pivotal condensationat vertex i in G(w) we mean the vertex k is in the other component, say the component following operation: For each pair of vertices j,, j, E r(i),

G; 2 = V’,,,, EL,2). Note that in G;,, and in G;,2 there if the edge( j,, j,) is already present in G(w), then increase exists an unique path between every pair of vertices. its weight by w(i, j,)w(i, j2)/di; otherwise add to G(w) Consider any vertex u # REF(k), adjacent to vertex k. the edge (j,, j,) with the weight w(i, j,)w(i, j,)/di. After Let ht = (DIGIT(l), DIGIT(2), . . . , DIGIT(k - l), u, all possible pairs of neighbors of the vertex i are consid- REF(k + l), REF(k + 2); . . , REF(n - 1)) and Gt = ered, delete from G(w) the vertex i and all the edges (V/, EL u(k, u)) be the subgraph corresponding to A%. incident on it. Now the following two casesarise. Let N be a resistive network consisting of one Siemens (i) If u E V/ 1, then Gz is a spanning tree of G. Thus the admittances and G(N) be the graph of N in which all the sequence h$ with u E V/, is a tree sequencepassing the edgesare of unit weight. Let A be a subset of the vertex set tree compatibility test at ‘position k. v= {1,2;. *, n } of N. Let the networks NA and NAo be (ii) If u E v/J, then in G,$ the edge (k, u), along with defined as the unique path in G;,2 between the vertices k and u, NA the network that results after coalescing all the forms a circuit passing through the vertex k, and so Gz is vertices of N which do not belong to A, a non-tree subgraph of G. Thus the sequence X% with Nj the network that results after suppressingall the UEVL, is a non-tree sequencefailing the tree compatibil- vertices of N which belong to A. ity test at position k. Since vertex k is adjacent to deg(k)-1 vertices other If T(N), T(N,), and T(Nj) denote the sum of tree-admit- than REF( k), there are deg( k)- 1 distinct Xt’s which have tance products of the networks N, NA, Ni, respectively, the same DIGIT(l), DIGIT(2), . . . , DIGIT(k - 1) as X,. then it has been shown in [7] that Each one of these sequencesis either a tree sequenceor a non-tree sequence and so all these sequencesbelong to T(N)=T(N,)T(N,O). (2) T,UT,‘. Thus if t(k) is the number of all the tree sequences Note that the graph G(Ni) of the network Ni can be of the form xk = (DIGIT(l), DIGIT(2); . ., DIGIT(k -l), obtained from the graph G(N) by performing pivotal REF( k), REF( k + l), . * *, REF( n - l)), then condensation, in G(N), at all the vertices in A. Let A = IT,UT/‘( = (deg(k)-l)t(k). (1) {LZ- * -9k - l}, G,(N) = G(N) and the graph Gi( N), 2 < Since in the spanning tree corresponding to A,, the i < k - 1, be obtained from G,-i(N) by performing a edges(k,REF(k)), (k + l,REF(k +l)); .., (n - l,REF(n pivotal condensation at vertex i -1 in G,-i(N). If d;, 1~ i 6 k - 1, is the sum of admittances of all the edges - 1)) are present, it follows that t(k) is the number of . spanning trees of G in which the edges(k, REF( k)), (k + incident on vertex k in G,(N), then as shown in [7] l,REF(k +l)); * ., (n - 1, REF( n - 1)) are present. Thus T(N) = d,d, . * * d,-,T( N,o). (3) t(k) is the number of spanning trees of the graph obtained from G by coalescing the vertices k, k + 1,. . ., n - 1, Comparing (2) and (3) we get REF(k), REF(k + 1); . ., REF(n - 1). But {k, k + 1; * *, n - 1, REF(k), REF(k + 1); . a, REF(n - l)} = T( N,) = d,d, . . . &I {k,k+l;.., n} becauseREF(i)>i, l

Theorem 5 0 1 The number of sequencesgenerated by Char’s algorithm (4 is ‘-* deg(k)-1 t+t,=l+t c q k-1 d,-14-2 * * * d, ’ 3 1 1 Now we illustrate the above procedure to compute (t + to) for the graph G in Fig. l(a). This graph has 8 spanning 4 A 2 trees and Char’s algorithm generates11 sequencesfor the 3% vertex numbering shown. The graphs G,, G,, and G, are (b) shown in Fig. l(a)-(c), respectively. Note that d, = 2, d, = 5/2, and d, = 8/5. Thus t = d,d,d, = 8 3 and %i deg(k)-1 t+t,=l+tk=l$ d,-,d,-, . . . d, =“’ 4 %. (4 Using Theorem 5 we can easily prove the following. Fig. 1. (a) Graph G = G1. (b) Graph GZ. (c) Graph GJ.

Theorem 6 For an n-vertex complete graph trees. In other words, t + to will be the samefor all choices n”-’ of initial spanning trees which have identical y1 and I$2 to=nnp2 - 0 for every i, 2 < i < n. 0 [(n -iI’ 1 Consider a graph G E G(“-‘) (defined in Theorem 2) in Corollary 6.1 which vertex x is of degree n - 1. Let G, be any arbitrary For a complete graph to < t, for any choice of the initial spanning tree of G and G2 be a star tree having the vertex rJ spanning tree. x as the star vertex. Suppose we assign the number n to vertex x and number the other vertices using G, and The value of (t + to) given in Theorem 3 depends on the satisfying the tree compatibility property; then an identical number of spanning trees of Gf’, which is obtained from numbering (satisfying Theorem 7) of the vertices of G G by coalescing the vertices k, k + 1; . . , n. So, for two using G, and satisfying the tree compatibility property is different initial spanning trees, the values of t(k) for a possible. Thus if to and t; are the numbers of non-tree given k will be the sameif the set of vertices which receive sequences generated with respect to G, and G,, then the numbers k, k + 1,. . . , n as given by Char’s algorithm is to = t& By Theorem 2 t; < t, where t is the number of identical in both cases.In other words, the value of t(k) spanning trees of G and so to < t. Since the above argu- depends on the set of vertices which are assigned the ments are valid for any arbitrary G, chosen as the initial numbers k, k + 1, * . . , n and not on the edges connecting spanning tree, we get the following theorem which is more these vertices. Since this statement is true for all values of general than Corollary 6.1. k, we get the following result which is more general than Theorem 1. Theorem 8 Theorem 7 For an n-vertex connected graph with maximum degree Let G be a connected undirected graph. Let the vertices n - 1, to < t for any choice of the initial spanning tree in of G be numbered as ~‘1,u:, . . . , u: according to one initial which a vertex with degree n - 1 is assignedthe number n. spanning tree and as u:, ui,. . . , u,” according to another 0 initial spanning tree. Let vi’ = { uf, ui+i,. . . , uk} and y2 = {uf, u;+l,- * *,u,‘}, for 2

Theorem 9 then

If L, is the number of spanning trees of an n-vertex n-l n-2 ladder’ and Li is the number of non-tree sequencesgener- COSTl= c IT/&-k-l)= c ITkl(n-k-1). ated by Char’s algorithm when a star tree is chosen as the k=l k=l initial spanning tree, then But ]Tk]=t(k+l)-t(k)forall k, liken-2. So L; = L,-, n-1 COSTl= c [t(k+l)-t(k)](n-k-l) and k=l n-1 1 =t c -(?r -2). (4) 0 kc2 [ dn-ldn-20ea dk I As regards Type 2 computation, it is required for each Theorem 10 sequencein T,UT,‘, 1~ k < n - 1. If Cr denotes the maxi- If W, is the number of spanning trees of an n-vertex mum number of computational steps required to perform wheel and W,” is the number of non-tree sequencesgener- Type 2 computation for any sequence in TkuTL’, and ated by Char’s algorithm when a star tree is chosen as the COST2 denotes the cost of performing all the Type 2 initial spanning tree, then computations, then W,o=l+ L, n-l COST2 < c crlTkuT;l = t and k-l (5) cl From (4) and (5), it is clear that COST1 4 nt and COST2 < n3t. So the total cost of execution of Char’s algorithm is Next we consider the question of computing the total O(n3t). A better bound for COST2 does not appear to be number of computational stepsrequired in the execution of possible, even though it has been found in a large number Char’s algorithm. Consider a sequence A = (DIGIT(l), of casesthat COST2 6 nt. For example, for all the graphs DIGIT(2), . . . , DIGIT(k - l), x, REF( k + l), . . . , REF( 12 in G(“-‘) COST2 is O(nt) and hence the total cost is - l)), with x # REF(k), generated by the algorithm. This O(d) in such cases. sequencebelongs to T,UT,‘. To generate this sequencethe algorithm explicitly requires setting DIGIT(i) = REF(i) IV. HEURISTICSFOR CHOICE OF INITIAL for each i, k + 1 < i 6 n - 1, in addition to setting SPANNING TREE DIGIT(k) = x. Then h is tested for the tree compatibility Since to and the complexity of Char’s algorithm depends property at position k. Thus generating and testing X on the initial spanning tree, we now consider the problem involves the following two types of computational steps. of choosing the initial spanning tree which leads to a Type 2: (n - k - 1) steps to set DIGIT(i) = REF(i), minimum to. The initial spanning tree can be obtained by k+l

Theorem 11 ‘A ladder is also known as a fan [8]. If vertex k is a leaf m TDFS,then IT/l = 0. 0 858 IEEE TRANSACTIONSONCIRCUITSAND SYSTEMS,VOL. CAS-31,N0.10,OCTOBERl984

Theorem 12 TABLE1 If 6, is the number of descendantsof vertex k in TDFS, NUMBEROFNON-TREESEQUENCESGENERATED then Number of Number of Non-Tree Sequences Graph Spanning

Trees BFS Heuristic 1 Heuristic 2 From Theorem 5, it is clear that if the vertices of the graph G could be numbered in such a way that deg(n - 24672 I 20738 14412 14438 9 l),deg(n -2);. ., deg(1) are in the ascending order and Gz 13931 8778 5308 5310 d, are in the descendingorder, then (t + to) G3 151662 120259 66079 67036 d,-l,dn-2,- . ., 151719 90831 65657 will be reduced considerably. Since deg( n) does not appear G4 65950 G5 1360710 1112223 504279 481506 in the expression for (t + to), we can number the vertex G6 12897990 7559568 7136979 6971221 having the maximum degreein G as n. In other words, we G7 1592512 871944 528193 528128 can start the DFS to find the initial spanning tree at a GE 1820488 1151321 634183 635357 vertex of maximum degree. G9 14689650 11998877 6179924 6207721 26520950 20921468 9096476 8941338 Let l?‘(i) be the set of ancestors of vertex i in TDFs GlO which are adjacent to i in G and let dl= Ir’(i)j. To find the numbers d,, d,, . . . , dnel, we start with the graph G, obtained from G by assigning unit weight to each edge of V. PATHCOMPRESSION G. Recall that d, is the sum of the weights of the edges The heuristics for the selection of the initial spanning incident on i in the graph Gi which is obtained from G, by tree discussedin the previous section are aimed at reducing performing pivotal condensation at the vertices 1,2,. . . , both COST1 and COST2 (defined in Section III). Though i - 1. Since pivotal condensation does not reduce the weight the number of comparisons required in a straightforward of any edge connecting i to any vertex in r’(i), and since implementation is influenced by the initial spanning tree each such edge has a weight of value at least one, it follows chosen, the actual number of comparisons done during the that execution of the algorithm can be reduced considerably by d, b d;, ldi k is encountered. In the latter case, X is a tree Heuristic 1: Start the DFS at a vertex of maximum sequence.Suppose A is a tree sequence.Let P denote the degree. During the search, when we are at vertex i, choose, path k, x, DIGIT(x), DIGIT(DIGIT(x)); * *, j. from among the neighbors of i, the one having the maxi- After generating and identifying the tree sequenceh, the mum number of ancestors in the tree developed so far. If algorithm proceeds to generate sequences in which more than one vertex has this property, then choose, from DIGIT(l), DIGIT(2), . * . , DIGIT( k - l), and DIGIT(k) among these vertices, the one having minimum degreein G. are the same as in X. So the path P will be present in all Heuristic 2: Start the DFS at a vertex of maximum the subgraphs corresponding to such sequences.Consider degree.During the search, when we are at vertex i, choose, now one -suchsequence X which is to be tested for the tree from among the neighbors of i, the one having minimum compatibility property at position i. Clearly i > k. Let degreein G. If more than one vertex has this property, then DIGIT(i) = (Yin A’. Then to test A’ for the tree compatibil- choose, from among these vertices, the one having the ity property, we need to traverse the sequenceP’ of vertices maximum number of ancestorsin the tree developed so far. i, OL,DIGIT( DIGIT(DIGIT(a)), . . . until vertex i or We have implemented Char’s algorithm using each one a vertex greater than i is encountered. If k lies on P’, of the above two heuristics as well as.BFS. In Table I we then the sequence of vertices k, x, DIGIT(x), give the number of non-tree sequencesgenerated in these DIGIT(DIGIT(x)), . . . , j representing P will be a subse- casesfor ten randomly generatedgraphs. From Table I it is quence of P’. Thus while traversing P’, we can proceed to clear that the heuristics considerably reduce the number of j directly from k using the path P. In other words, we can non-tree sequencesgenerated by the algorithm and that the effectively compress P’ if we keep track of the information two heuristics result in approximately the same number of relating to the path P. This technique, called path com- non-tree sequences. pression [9], will considerably reduce the actual number of 859

TABLE11 TABLE III NUMIIEROFCOMPARISONSMADE EXECUTIONTIME

Number of Number of Number of C parisons Number of Execution Time in Seconds

Graph Spanning Non-Tree Heuristic 1 Spanning Heuristic 1 Heuristic 1 With Graph B/I With Trees Sequences Path Compr. Trees Path Compr.

Gl 24672 2.037 1.924 1.767 24672 14412 110374 83342 Gl G2 13931 0.970 0.944 0.928 13931 5308 40711 33811 G2 G3 151662 12.495 10.167 9.462 151662 66079 593753 442127 G3 G4 151719 10.661 10.489 10.205 151719 65657 565449 434929 G4 G5 1360710 102.660 87.835 81.612 1360710 504279 5323910 3841745 G5 G6 12897990 994.735 966.608 894.635 12897990 7136979 64931380 48970813 G6 G7 1592512 113.124 105.868 99.491 1592512 528193 5599546 4080411 G7 G8 1820488 129.747 124.721 115.582 1820488 634183 6749935 4808779 G8 G9 14689650 1193.974 1026.815 946.918 14689650 6179924 66484647 45516982 G9 GlO 26520950 2264.015 1822.345 1662.247 GlO 26520950 9096476 102984126 69897903 I

comparisons made during the execution of Char’s algo- Thus Update 1 and 2 together require (n - k) computa- rithm. tional steps for each tree sequenceof the form A,. The To implement Char’s algorithm with path compression, number of tree sequencesof the form A, is given by we use a new array NEXTVERTEX. Whereas the DIGIT t(k + 1)- t(k), where t(i), 16 i < n - 2, is the number of array keeps the adjacency information of each sequence, spanning trees of the graph Gf) defined in Section III. the NEXTVERTEX array, for a tree, is defined as Thus the total number of computational steps required to NEXTVERTEX = j, where j > i is the first vertex create and update the NEXTVERTEX array is given by reachable from vertex i as we traverse the tree from vertex n-2 i to vertex n. We create and maintain the NEXTVERTEX n-l+ c [t(k+l)-t(k)]@-k) array as follows. Since for the initial tree sequence, k=l DIGIT(i) = REF( i) > i, 1 6 i < n - 1, we initialize n-2 NEXTVERTEX = REF(i), 1~ i 6 n - 1. Whenever a =2t(n-1)-t c t(k), since t(1) =l. tree sequenceX = (DIGIT(l), DIGIT(2), . . . , DIGIT(k - k=2 l), x, REF(k + 1); . a, REF(n - 1)) is generatedby chang- ing the value of DIGIT(k) of the previous tree sequence, the NEXTVERTEX array is updated as follows. Updute I: NEXTVERTEX = REF(i), k + 1~ i G n which is of order O(nt). Thus employing path compression -1. in the implementation of Char’s algorithm does not change Update 2: NEXTVERTEX = j, where j > k is the the complexity of the algorithm. first vertex reachable from vertex k in the Since the total number of comparisons are reduced when tree path from vertex k to vertex n. Char’s algorithm is implemented with path compression, Note that j will be known when the tree compatibility test the execution time of the algorithm with path compression for A is completed. should also be less than the execution time of the algorithm We have implemented Char’s algorithm with path com- without path compression.This can be verified from Table pression using NEXTVERTEX array. In Table II we give III where we have tabulated the execution times (on a the total number of comparisons made by the implementa- CDC Cyber 835 computer) for three implementations of tion of Char’s algorithm with Heuristic 1 and the imple- Char’s algorithm-Char’s implementation where Breadth- mentation with Heuristic 1 and path compression. From First Search (BFS) is used to select the initial spanning Table II it is clear that the use of path compression tree, implementation using Heuristic 1, and implementa- considerably reducesthe total number of comparisons. tion using Heuristic 1 and path compression. Next we compute the number of computational steps required to create and update the NEXTVERTEX array. VI. SUMMARYANDCONCLUSION Note that initially NEXTVERTEX( n - 1) = REF( n - 1) = In this paper we have presented a detailed computa- n. Since Update 2 sets NEXTVERTEX(n - 1) to the first tional complexity analysis-both theoretical and experi- vertex greater than 12- 1 in the tree path from vertex n - 1 mental-of Char’s algorithm to enumerateall the spanning to vertex n, NEXTVERTEX(n - 1) is always equal to n trees of a graph and have also presented several quantita- and so we need to update only NEXTVERTEX( 16 i 6 tive and qualitative properties of the algorithm. n - 2. For each tree sequenceof the form A, = (DIGIT(l), We have described methods to speed up the algorithm. DIGIT(2), * * . , DIGIT( k - l), x, REF( k + l), . . . , REF( II In particular, we have discussed two heuristics to choose - 1)) with x # REF(k), Update 1 requires (n - k - 1) as- the initial spanning tree which lead to a minimum number signments and Update 2 requires exactly one assignment. of spanning non-tree subgraphs.We have also described a 860 IEEE TRANSACTIONSON CIRCUITSAND SYSTEMS,VOL. CAS-31, NO. 10, OCTOBER1984 technique called path compression to reduce the number of K. Thulasiraman was born in Ammayappan, Tamilnadu, India, on June 9, 1942. He received comparisons. Theoretical analysis and experimental results the Bachelor’s and Master’s degrees in electrical presented in this paper establish the superiority of Char’s engineering from the University of Madras, algorithm when implemented using the heuristics and path Madras. India. in 1963 and 1965. resuectivelv. and the Ph.D: degree in electrical e&ineering compression. from the Indian Institute of Technology, Madras, in 1968. He joined the Indian Institute of Technology, ACKNOWLEDGMENT Madras, in 1965, where he was associated with the Department of Electrical Engineering from The authors thank the reviewers for their suggestions the Department of Computer Science from 1973 to 1981. He was promoted to the rank of Professor in January 1977. After which have resulted in considerable improvement in the servine for a vear (1981-1982) at the Deoartment of Electrical Eneineer- presentation of the paper. ing, T;chni&l University of ‘Nova Scoiia, Halifax, Canada, he joined Concordia University, Montreal, as Professor at the Department of Mechanical Engineering where he is now involved in the development of programs in Industrial Engineering at the undergraduate and graduate REFERBNCBS levels. Earlier, he had held visiting positions at Concordia University PI G. J. Minty, “A simple algorithm for listing all the spanning trees of during the periods of 1970-1972, 1975-1976, and 1979-1980. He has a graph,” IEEE Trans. Circuit Theory, vol. CT-12, p. 120, 1965. published over 50 technical papers on different aspects of Electrical PI R. C. Read and R. E. Tarjan, “Bounds on backtrack algorithms for Network Theory, Graph Theory and Design and Analysis of Graph listing cycles, paths and spanning trees,” Networks, vol. 5, pp. Algorithms. He has also coauthored the book Graphs, Networks and 237-252,1975. Algorithms (New York: Wiley-Interscience, 1981). His current research [31 H. N. Gabow and E. W. Myers, “Finding all spanning trees of directed and undirected graphs,” SIAM J. Comput., vol. 7, pp. interests are in Network Theory, Analysis of Algorithms, Computer 280-287, 1978. Networks and VLSI Layout. [41 J. P. Char, “Generation of trees, two-trees and storage of master Dr. Thulasiraman is a Fellow of the Institution of Telecommunication forests,” IEEE Trans. Circuit Theory, vol. 15, pp. 128-138, 1968. Engineers, India. VI R. Jayakumar, “Analysis and study of a spanmng tree enumeration algorithm,” MS. thesis, Dep. of Computer Science, Indian Inst. of + Technol., Madras, India, 1980. Also reported in “Combinatorics and graph theory,” Springer-Verlag Lecture Notes on Mathematics, (S. B. : 7. : M. N. S. Swamy (S’59-M’62-SM’74-F’SO) was Rao, Ed.) no. 885,1981, p. 284-289. born on April 7, 1935. He received the B.Sc. 161 M. N. S. Swamy and l?. Thulasiraman, Graphs, Networks, and Algorithms. New York: Wiley-Interscience, 1981. ; ,_,I (honors) degree in mathematics from Mysore [71 M. N. S. Swamy and K. Thulasiraman, “A Theorem in the Theory of University, Mysore, India, in 1954, the Diploma Determinants and the Number of Spanning Trees of a Graph,” in electrical communication engineering from the Canadian Elect. Eng. J. vol. 8, no. 4, pp. 147-152, 1983. Indian Institute of Science, Bangalore, India, in 181 A. J. W. Hilton, “The number of spanmn trees of labelled wheels, 1957, and the M.Sc and Ph.D. degreesin electri- fans, and baskets,” in Combinatorics, pubhs3 ed by Inst. Math. Appl., cal engineering from the University of 1972, fp. 203;206. Saskatchewan,Saskatoon, Sask., Canada, in 1960 [91 R. E. aqan, Applications of path compression on balanced trees,” J. Ass. Comput. Mach., vol. 26, pp. 690-715, 1979. , and 1963, respectively. He worked as a Senior Research Assistant at the Indian Institute of Science until 1959, when he began graduate study at the University of Saskatchewan.In 1963 he returned to India to work at the Indian Institute of Technology, Madras. From 1964 to 1965 he was an Assistant Professor of Mathmematics at the University of Saskatche- wan. He has also taught as a Professor of Electrical Engineering at the + Technical University of Nova Scotia, Halifax, N.S., Canada, and the Universitv of Calaarv. Calgarv, Aha., Canada. He was chairman of the Department of El&t&l Engineering, Concordia University (formerly Sir Georee Williams Universitv). Montreal. P.O.. Canada, From Julv 1970 until August 1977, when he became the Dean of Engineering and Com- puter Science of the same university. He has published a number of R. Jayakumar was born in Tamilnadu, India, on papers on number theory, semiconductor circuits, control systems, and February 10, 1955. He received the Bachelor’s network theory. He is a coauthor of the book Graphs, Networks and degree in electrical engineering from the Univer- Algorithms (New York: Wiley-Interscience, 1981). sity of Madras, Madras, India, in 1977, and the Dr. Swamy is a Fellow of several professional societies including the MS. degree in computer science from the Indian Engineering Institute of Canada, the Institution of Engineers (India), the Institute of Technology, Madras, India, in 1980. Institution of Electrical Engineers (U.K.), and the Institution of Electron- He is now working towards his Ph.D. degree at ics and Telecommunications Engineering (India). He is Associate Editor Concordia University, Montreal, P.Q., Canada. of the Fibonacii Quarterly and the new journal, Circuits, Systems and His research interests are in graph theory, analy- Signal Processing. He was Vice-President of the IEEE Circuits and sis of algorithms, VLSI layout, and data struc- Systems Society in 1976, and General Chairman of the International tures. Symposium on Circuits and Systems, 1984.