9
Computing 16, 153--161 (1976) 9 by Springer-Verlag 1976 Algorithm 28 Algorithm for Generating Graphs of a Given Partition K. R. James and W. Riha, Leeds Received August 4, 1974; revised October 25, 1974 Abstract -- Zusammenfassung Algorithm for Generating Graphs of a Given Partition. An algorithm which produces with com- paratively little redundancy all simple graphs realizing a given partition is discussed, and a FORTRAN implementation is given. The program generates all graphs of order 8 in approximately 40 seconds on an ICL 1906A omputer. Algorithmus zur Konstruktion von Graphen mit vorgegebener Partition. Es wird ein Algorithmus zur Konstruktion aller (schlichten) Graphen, die eine vorgegebene Partition realisieren, diskutiert. Bei der Konstruktion treten im allgemeinen Wiederholungen yon isomorphen Graphen auf, deren Anzahl jedoch verhgltnismgl3ig gering ist. Es wird auch ein FORTRAN-Programm angegeben, mit Hilfe dessen auf einem ICL 1906-A-Computer alle Graphen der Ordnung 8 in etwa 40 Sekunden produziert wurden. 1. Introduction A program for generating all simple graphs I of a given order has been dis- cussed recently by Heap [5]. The method of construction produces a large number of isomorphic repetitions which for most purposes have to be eliminated (for elimination algorithms see e.g. [1]). This inherent redundancy makes it a prac- tical impossibility to produce graphs of more than eight nodes in a systematic way. If, however, one considers the subset of graphs realizing a specified partition (sequence of node degrees), then a more direct and efficient approach is possible. The present paper discusses a new method for generating all simple graphs with specified partition. A FORTRAN program is given, and some computational experience is described. The theoretical basis of the algorithm has been discussed elsewhere [8], and it has been shown that the method will produce every graph of a given specification at least once 2. The output contains some 1 i.e. undirected graphs without loops or multiple arcs. 2 It may be remarked that not every partition is graphical; in such cases the algorithm will determine that there are no corresponding graphs. Hakimi [2] has described a method for determining whether a partition is graphical. Parthasarathy [7] discusses a counting formula which in principle could be used to calculate the number of graphs for each partition.

Algorithm 28 algorithm for generating graphs of a given partition

Embed Size (px)

Citation preview

Computing 16, 153--161 (1976) �9 by Springer-Verlag 1976

Algorithm 28

Algorithm for Generating Graphs of a Given Partition

K. R. James and W. Riha, Leeds

Received August 4, 1974; revised October 25, 1974

Abstract - - Zusammenfassung

Algorithm for Generating Graphs of a Given Partition. An algorithm which produces with com- paratively little redundancy all simple graphs realizing a given partition is discussed, and a FORT RAN implementation is given. The program generates all graphs of order 8 in approximately 40 seconds on an ICL 1906A omputer.

Algorithmus zur Konstruktion von Graphen mit vorgegebener Partition. Es wird ein Algorithmus zur Konstruktion aller (schlichten) Graphen, die eine vorgegebene Partition realisieren, diskutiert. Bei der Konstruktion treten im allgemeinen Wiederholungen yon isomorphen Graphen auf, deren Anzahl jedoch verhgltnismgl3ig gering ist. Es wird auch ein FORTRAN-Programm angegeben, mit Hilfe dessen auf einem ICL 1906-A-Computer alle Graphen der Ordnung 8 in etwa 40 Sekunden produziert wurden.

1. Introduction

A program for generating all simple graphs I of a given order has been dis- cussed recently by Heap [5]. The method of construction produces a large number of isomorphic repetitions which for most purposes have to be eliminated (for elimination algorithms see e.g. [1]). This inherent redundancy makes it a prac- tical impossibility to produce graphs of more than eight nodes in a systematic way. If, however, one considers the subset of graphs realizing a specified partition (sequence of node degrees), then a more direct and efficient approach is possible. The present paper discusses a new method for generating all simple graphs with specified partition. A FORTRAN program is given, and some computational experience is described. The theoretical basis of the algorithm has been discussed elsewhere [8], and it has been shown that the method will produce every graph of a given specification at least once 2. The output contains some

1 i.e. undirected graphs without loops or multiple arcs.

2 It may be remarked that not every partition is graphical; in such cases the algorithm will determine that there are no corresponding graphs. Hakimi [2] has described a method for determining whether a partition is graphical. Parthasarathy [7] discusses a counting formula which in principle could be used to calculate the number of graphs for each partition.

154 K.R. James and W. Riha:

redundancy (the amount of redundancy depends on the "regularity" of the degrees), but the number of repetitions is in general much smaller than that produced by alternative methods. Together with a routine for generating partitions of an integer 2 q into n parts [9], the algorithm provides an efficient means of producing all graphs of order n with any specified number of q of arcs.

2. The Construction Algorithm

The nodes are represented as labels 1, 2, ..., n in non-increasing order of degrees. The algorithm is based on successively refined partitions of this node set into equivalence classes. Initially, the equivalence classes correspond to the node degrees (however, these classes may be further refined if it is required to distinguish between nodes of the same degree). The main operation is then to scan the nodes in order of labels: for a given node r of current degree m, it is required to place m arcs from node r to higher labelled nodes. In general, there may be zero, one or more than one distinct way of placing these arcs, depending on the number and size of equivalence classes of the higher labelled nodes. The selection of nodes from within any class is arbitrary, and the convention is adopted of selecting highest labelled nodes from each class. A set of m nodes chosen in this way as end points of the m arcs is called an m-se lec t ion [8].

A particular placing of the m arcs from node r to higher labelled nodes will induce a refinement of the remaining node classes: any nodes selected from a given class will be reduced in degree by 1 and hence will be distinguishable from any remaining nodes in the same class. Furthermore, nodes which have been reduced to degree zero can be ignored subsequently. When the degrees have been thus modified and the refined classes determined, the procedure continues if possible with node r + 1.

Ultimately, every possible placing of arcs at every level will have to be con- sidered. The core of the algorithm is therefore a procedure for producing in a systematic way all m-selections from a given partition, this being in- corporated in the traversal of a tree structure. The problem can be recast in the following terms:

Given an integer m and a vector of upper bounds (P~, P2,..., Pc), to produce all integer vectors x_ = (x l , x z . . . . , xc) subject to the constraints

x j = m , O<_xj<_Pj , j = l ( 1 ) c . j = l

A procedure for generating the vectors x in a systematic way will now be described.

Input: positive integer m, vector (P1, P2 . . . . , Pc) with positive integer elements.

Output: vectors _x=(xl, x2 . . . . xc) produced in lexicographic decreasing order (with components reversed).

Algorithm for Generating Graphs of a Given Partition 155

Step 0: i f m > ~ Pj goto step 6, j = l

initialize xj: =0 for j = l(1)c, i : = C ,

Step 1: i fm_P~ goto step 2, xi:---Pi, m: ~ - m - P i , i: = i - 1 , goto step 1,

Step 2: x~:=m, m : = 0 , OUTPUT _x, if i= 1 goto step 4,

i - 1 Step 3: i f m _ > ~ Pj goto step 4,

j = l

Xi:=Xi--1 , m : = m + l , i : = i - - 1 , goto step 1,

Step 4: m:=m+x~, xi:=O,

Step 5: if i=c goto step 6, i : = i + 1 , if xi > 0 goto step 3, goto step 5,

Step 6: stop.

Finally in this context, we remark that it may be possible to reduce the number of repetitions by requiring a suitable ordering in the sequence of m-selections cor- responding to an acceptable graph, rejecting those sequences which are not ordered in a specific way. It remains to be shown that no distinct constructions are omitted in consequence. A number of possibilities are under consideration.

3. Details of Implementation

The following remarks refer to the FORTRAN program listed in 4.

Input: Q: integer vector of N N elements specifying the node degrees in nonincreasing order, d 1 _ dz > ... --- dNN > 0. (If it is required to distinguish between nodes of the same degree, this can be effected by a change of sign in the above sequence.) NN(>_I): number of nodes.

Output: The number of graphs produced, including repetitions, is returned in the argument NUM. The information output for each graph is sent to the notional SUBROUTINE OUTPUT (NN, NM, L, J, S) [see lines 34, 143]. The arguments of the output list are:

156 K.R. James and W. Riha:

N N (_>1):

NM(_>O):

L (>_1):

J (>o):

S

number of nodes (order of the graph),

number of arcs,

number of components in the graph,

number of elements in the vector S,

integer vector of J elements specifying the adjacency mapping [8] of the graph.

The nodes are labelled 1, 2, ..., NN. Each node u in the domain of the adjacency mapping appears with a minus sign in S, and this is followed by the subset of the node set to which u is mapped. (Nodes which are mapped to the empty set are suppressed.)

Example: The two component graph consisting of a triangle and a single line joining two points is represented by the string:

5 4 2 7 - 1 2 3 - 2 3 - 4 5.

The following modifications which enhance efficiency and generality have been included in the program:

(i) Reduction of order. At any stage in the construction it will be possible to reduce the effective order of the problem if one or more nodes in the highest labelled positions have been reduced to degree zero. This reduction is carried out by the program, the current effective order being denoted by N.

(ii) Compact representation. The entries in each row of the adjacency matrix A are packed in a representation similar to that of the output vector S. This device is well suited to the operation of the program, requiring a minimal amount of rewriting. This is particularly effective, of course, for sparse graphs.

(iii) Count of components. The number of connected components in the graph under construction is counted by a straightforward labelling process. Initi- ally, the nodes are assigned distinct labels recorded in the vector E; subsequently, the labels are modified when nodes are connected. The array D stores previous labels which will be required on returning. A refinement of this procedure is central to the construction of trees [6J.

Computational Experience

As mentioned in the introduction, the algorithm may be used to produce all graphs of a specified order, given the appropriate degree sequences as data. The program has been used to generate all graphs of order 8 with 0--14 arcs (the remaining graphs of order 8, with 15--28 arcs, are just complements of these).

Algorithm for Generating Graphs of a Given Partition 157

The number of distinct graphs is 69963 . The number of graphs produced by the program is approximately 17,000; the time required on the ICL 1906A is 40 seconds with output retained in core (140 seconds with output written to a file).

A useful check on the validity of the program is provided by a further modifica- tion which can be used to generate trees [6]. This is a straightforward application of the component-counting facility, coupled with an elimination procedure which in the case of trees will output only distinct (nonisomorphic) cases. The distinct trees and rooted trees have been produced up to order 15, and the total numbers for each order are found to agree with numbers calculated from counting series (tabulated in [3],p. 232).

4. F O R T R A N S U B R O U T I N E

SUBROU?| NE GENG(Q,NN,NUtl) INTEGER A(20,20) ,X(I 8,2~), p(18, ~0) , PP(18,20).D(!A,20),

�82 S (I 00) ,Q(20) iB (20) , E (20) , F,'R, C NUMI~O F~O N~NN LmNN

E( t )~ I L I .Q(1) Q(I ).XABS (L I ) ImQ(1) DO 2 J~2,N LZmQ(J) Q(J)mIABS(L2) I ~ I * Q ( J ) I F (LI . EQ. 1.2)GOTO 1 C~C,I p(1 , C ) . I LImL2 fiOTO 2

I P(I,C)qP(IwC)~,I 2 E(J)mJ

NMBI/2 I P (2*NM, NE, I ) RETURN ZII (Q(N) ,GTIO)GOTO F~P(I ,C) NmN-F L~LmF C~C,,,I IF(N,GT.O)GOTO 3 NUM~I�82 CALL, OUTPUT(NNINM, p,Ne$) RETURN

3 The number of distinct graphs for each number of arcs up to order 9 is tabulated by Harary ([3], p. 214). See also [4].

158 K.R. James and W. Riha:

3 R.1 JMx2 M~Q(1) IF(N,GT,Z)QOTO 5

6 IF(H.NE.I.0R.QCN).NE.I)GOTO 36 A(ReN)qN IF(E(R).NE.E(N))L~Lnl GOTO 26

$ A(R,R)~O IF(rt.GT.O)GoTO 6 P(R, I ) IP(RI1)* I GOTO 30

6 P(R,1)Ip(Rmt)-q PP(Rut )xP(R, I ) IP(C.EQ.�82 6 DO ? JW2,C KmP(R#J) IF(K.LT.O)K:O

? PP(ReJ)xPP(R,J-1)*K 8 IF(H.GT.PP(RPC))GOTO 36

B(R)mC D ( R , I ) I N D(RIR)mL DO 9 JwJHeN

90(R,J )qE(J ) IsC KmN JPBR*M LqmmIABS(E(R))

10 JWP(ReI) IF(J.LE.O)QOTO 16 LZmE(K) MHJQ(K)-I IF(LI .EQ. L2)GOTD 17 IF(L2. LT.O)GOTO 14 IF(H. LE.J)GOTO 12 L~L-J X(R,l)mJ MmM.J JJ.K KWK.J*I DO 11 JIK,JJ

11 E(J)ILI KMK~I OOTO 18

12 LaL-M X(R,l)arl J.K KmK.M$1 DO q3 JJaKIJ

t3 E(JJ)aLI G.OTO 21

14 LILml DO 15 JJmJM,N IF(E(JJ).EQ.L2)E(JJ)mLq

15 CONTINUE 60TO 17

16 X(R,I)mO KxK*J GOTO 19

17 IF(rt. LE.J)GOTO 20

Algorithm for Generating Graphs of a Given Partition 159

X(R, I )=J HwH.J JJzK KIK-J

18 A(R,JP)=JJ Q(JJ)mHH JPIJP~t JJmJJ-1 IF(JJ,GT,K)GOTO 18

19 l= I "q GOTO I0

20 X(R, I )~t l J!K K!K.Hel

21 A(R.JP)wJ Q(J)=HH ]F(J.EQ.K)GOTO Z2 JP=JP=�82 JWJ.1 60TO 21

Z2 DO 23 J=JH,N IF(Q(J).EQ,O)GOTO 23 IF(H.EQ.O)~OTO 26 HmO

23 CONT;NUE IF(fI,NE.O)60?O 26 IF(C.EG.f)GOTO 36 GOTO 38

~4 A(RwR)lI DO 25 ImJMeN

Z5 A ( I , R ) = Q ( I ) GOTO 30

Z6 LI=L*F NUHmNUH§ JJ=O DO 28 %=I.R HIQ(1) IF(tJ,EQ.O)GOTO Z8 JJ=JJ*1 $(JJ)==I K=I*I Mr1= Z etl DO 27 J=K.Mtl JJ=JJ*1

27 S ( J J ) = A ( I I J ) 28 CONTINUE

CALL OUTPUT(NNtNHILIwJJ.I) GOTO 36

30 ~4=R RIJH JH=R*I HiQ(R) LI=H DO 31 JwJH,N B(J)=O L2=Q(J) IF(L1.EQ,L2)GOTO 31 e (J )= l IF(LI .6T,O)KmJ~I LI=L2

31 CONTINUE

160 K.R . James and W. Riha:

IF(L2.Eq.O)NgK IF(JM.EQ.N)GOTO 4 IF(C.EQ.I)GOTO 33 CqC~1 J~R DO 32 K.~ ,C J=J*IABS(P(HHoK))

32 B(J)=I C-I

33 K=I DO 35 J=JH,N IF(B(J).EQ,O)GOTO 3~ IF(Q(J~I) ,EQ,O)Km'K P(R,C)IK KMI C=C,I GOTO 35

34 KmK~I 35 CONTINUE

P(R,C)mK GOT~ 5

36 ~IR-1 IF(R.EQ;O)RETURN INA(R,R) IF(I,EQ.O)GOTO 36 CIB(R) IF(C.EQ,I)GOTO 36 JMmR*I LI=~IABS(E(R)) N 'D(R, I ) DO 37 J=JH#N

~? Q(J)mA(J,R) 38 MHBQ(R)

LmD(R,R) H=O IF(I.EQ.q)GOTO k5

40 IF(H.GE.PP(R, I -1))O~70 ~5 X(R, I )mX(Re l ) - I H=H*l JPmR*H DO k2 J=JHIN

~2 E(J)=D(R,J) IF(H.EQ.MM)GOTO 43 K=JP+I MMwR+MM DO k�82 JmK,Mrl JJ=A(R~J) L2=E(JJ) IF(L�82 41 L=L-I E(JJ)=LI IF(L2.GT.O)GOTO ~1 DO k! JJmJH,N IF (E(JJ ) .EQ.L2)E(JJ )=EI

41 CONTINUE 43 DO k4 JmJH=JP

JJmA(RIJ) k4 Q(JJ)mQ(JJ)§

KmJJ*X(R , | ) 'P (R , I ) I=1-1 GOTn 10

Algorithm for Generating Graphs of a Given Partition 161

45

46

MIH*X(R,I) X(R,I)=O IF(I.EQ.C)GOTO 36 I ! I , I IF(X(R,I).EQ,O)GOTO 46 60TO 40 END

References

[1] Corneil, D. G., Gotlieb, C. C.: An Efficient Algorithm for Graph Isomorphism. JACM 17, 51--64 (1970).

[2] Hakimi, S. : On the Realizability of a Set of Integers as Degrees of the Vertices of a Graph. J. SIAM Appl. Math. 10, 496--506 (1962).

[3] Harary, F. : Graph Theory. Addison-Wesley 1969. [4] Harary, F.: The Number of Linear, Directed, Rooted and Connected Graphs. Trans. Amer.

Math. Soc. 78, 445--463 (1955). [5] Heap, B. R.: The Production of Graphs by Computer, in: Graph Theory and Computing

(Read, R. C., ed.), pp. 47--62. Academic Press 1972. [6] James, K. R., Riha, W.: The Production of Trees and Rooted Trees of Order _< 15, Classified

According to Partition. Report No. 47, Centre for Computer Studies, University of Leeds. 1974, [7] Parthasarathy, K. R. : Enumeration of Ordinary Graphs with Given Partition. Canad. J. Math.

20, 40--47 (1968). [8] Riha, W., James, K. R. : The Production of Graphs Realizing a Given Partition. Report No. 43,

Centre for Computer Studies, University of Leeds, 1974. [9] Riha, W., James, K. R.: Efficient Algorithms for Doubly and Multiply Restricted Partitions.

Computing 16, 163--168 (1976). Dr. K. R. James and Dr. W. Riha Centre for Computer Studies University of Leeds Leeds LS2 9JT England

Computing 16/1--2 It