Algorithms for finding a fundamental set of cycles for an undirected

M. S. LYNN, Editor
Algorithms for Finding a
Fundamental Set of Cycles for
an Undirected Linear Graph
C. C. GOTLIEB AND D . G. CORNEIL
University of Toronto, Toronto, Canada
Given the adjacency matrix of the graph, the algorithm
presented in this paper finds o spanning tree and then constructs the set of fundamental cycles. Our algorithm is slower
than on algorithm presented by Welch by a ratio of N/3 (N
is the number of nodes) but requires less storage. For graphs
with a large number of nodes and edges, when storage is
limited our algorithm is superior to Welch's; however, when
the graphs are small, or machine storage is very large, Welch's
algorithm is superior. Timing estimates and storage requirements for both methods are presented.
l.
Introduction
In the analysis of linear graphs, it is often desirable
to examine the cyclic structure of the graph. For example,
the circuit matrix (c~j equMs 1 if the edge j is in the circuit
i, and 0 otherwise) can be used to determine the cut-set
matrix which in turn is useful for the maximal flow problem [2]. Also, since the set of all cycles of a graph is an
invariant property of the graph under isomorphism, its
determination can be useful in examining whether graphs
are isomorphic [4, 5]. Recently, Welch [6] presented an
algorithm for finding the set of cycles of an undirected
linear graph. This algorithm first finds a fundamental t
set of cycles and then uses this basis to generate the vector
space of cycles.
Both Welch's algorithm and the one in this paper determine the fundamental set of cycles b y first finding a
spanning tree 2 for the given graph. Any edge of the graph,
not in the spanning tree, when added to the tree will
yield one fundamental cycle [1]. Our algorithm will
Department of Computer Science.
1The set of cycles x = (et, . •., ~) (where e~ equals 1 if edge i
belongs to the cycle x, and 0 otherwise) forms a vector space
under symmetric difference of the sets. Thus, a fundamental
set of cycles is a basis for the vector space.
A spanning tree is a tree which is a subgraph of a connected
graph G and which includes all verticles of G [2].
780
C o m m u n i c a t i o n s of t h e ACM
usually require much less computer storage than Welch's
but it is slower. If n is the number of nodes and m the
number of edges in the graph, our Mgorithm needs storage
for three n X n adjacency matrices, whereas, the tested
version of Welch's needs space for two n X m incidence
matrices and two vectors of length m. Timing considerations are discussed in Section 3.
2.
Our Algorithm
Assume that the (undirected) graph is represented
by its adjacency matrix A (alj equals 1 if node i is connected to node j, and 0 otherwise). Further assume that
the graph is connected; otherwise, each connected component can be treated separately. The algorithm has been
coded in MAP for the I B M 7094-II and has been tested
extensively; ml Iverson's language [3] version of the algorithm is also available. A brief description of the
program follows. (See Figure 1.)
BLOCK 1. In blocks, 1, 2, and 3 a spanning tree is
constructed. I:a block 1, the set of n nodes is partitioned
with respect to some chosen edges to form a set of disjoint trees with adjacency matrix B. For each row i of
A, set b3'~ = b~j = 1 if a ~ is the first superdiagonal element of the ith row of A to equal 1; otherwise set bj~ =
b~ = 0.
BLOCK 2. In this block the n nodes of the given graph
are divided into connected components with respect to
the edges chosen in block 1. Initially every element of
x (a vector of length n) is set to zero. Assume i connected
components have been determined. Set x(j) = 1 if node
j belongs to one of these connected components; otherwise,
set x(j) = O. Suppose x(j) = 1 for a l l j < k. We now
determine the connected component to which node k
belongs. This (i + 1)st component is the set of nodes
that are descendents of k. Update vector x, find the next
component, and continue this process until every element
of x = 1. The connected components are denoted b y the
matrix C; namely, c~j equals i if node j belongs to the ith
connected component, and 0 otherwise.
BLOCK 3. Here the connected components are amalgamated by adding appropriate edges to the adjacency
matrix B. This is achieved by repeatedly examining the
component with the smallest number of nodes. There
must exist ~a edge which joins one of the nodes of this
component to some node in ahother component (otherwise the graph would not be connected). Choose one such
V o l u m e 10 / Number 12 / December, 1967
edge, add it to adjacency matrix B, and amalgamate the
two components. This process will terminate with B, the
adjacency matrix of a spanning tree.
BLOCK 4. Each edge of the original graph that is not
in the spanning tree, when added to the spanning tree,
will produce a fundamental cycle. In block 4 all such
fundamental cycles are generated.
E X A M P L E OF
In block 3, edges (2, 5) and (2, 6) are added to B. So
are edges (5, 2) and (6, 2). B now represents a spanning
tree.
3
ALGORITHM:
3
2
7
1
45
]3 is:
1234567
Given adjacency matrix A:
1234
1
o 011001
2
3
4
5
0
1
1
o
6
7
567
001110
001000
110001
100000
o 10o0oo
1 001000
1
2
3
4
5
6
7
0
0
1
0
0
0
0
0
0
0
1
1
1
0
1
0
0
1
0
0
0
0
1
1
0
0
0
1
0
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
1
0
0
0
F r o m block 4, the addition of edge (1, 4) yields the
After block 1, B is:
Construct a set of
disjoint trees by
choosing some edges,
1234567
0010000
0001000
1001000
0110001
0000000
0000000
0001000
i
2,
Divide the set of nodes
of the graph into
connected components
with respect to the edges
chosen in Mock 1
In block 2 the n nodes are divided into connected
components with respect to the edges represented in
matrix B. Initially x = (0, 0, 0, 0, 0, 0, 0). Thus we find
the connected component to which node 1 belongs,
namely, the component determined by nodes 1, 2, 3, 4, 7.
Now
Amalgamate the components
by adding appropriate
edges to yield a
spanning tree
x = (1, 1, 1, 1,0, 0, 1).
The connected component to which node 5 belongs is
determined by node 5 alone. Now
Produce set of
fundamental cycles
+
x = (1, 1, 1, 1, 1, 0, 1).
Node 6 is also an isolated node. Thus,
C=
00010
0 O 0 O i
Volume 10 / Number 12 / December, 1967
FIG. I.
|
J
Flowchart of our algorithm
Communications of the ACCM
781
cycle (1, 4); (3, 4); (1, 3). The addition of edge (1, 7)
yields the cycle (1, 7); (1, 3); (3, 4); (4, 7).
When all the cyles of the graph are to be calculated,
Welch's algorithms 2 and 3 for finding all the cycles given
a fundamental set of cycles are used. To do this, edges of
a cycle are enumerated using the following:
enumeration for edge (a, b) = n(a
--
a(a +
- 2
1)
1)
'-I- b
where a < b N n. This is a one-to-one mapping from all
possible edges to the positive integers from i to n(n -- 1)/2.
3.
Timing Considerations
In this section we examine the dependence of the
processing time on the number of nodes, n, and on the
density of edges, p. (If m is the number of edges: p =
2 m / n ( n - 1).) This is done by examining each block of
the Iverson language program and estimating the number
of steps needed. The following assumptions are made:
the logical operations AND, OR, and ~XCLUSIVE OR take
81 machine cycles; a word compare takes 8~ machine
cycles; word replacement takes 83 machine cycles; and
there are w bits per word. For each block of the algorithm
the largest term in n is found for each of ~ , ~2,83. I t is
assumed that the times for other operations, particularly
indexing operations for loop control and for modified
fetch instructions, are negligible.
In block 1, the maximum number of searches in the
matrix A i s n ( n -
1)/2. T h i s t a k c s n ( n ~ -
1)[;](81 +
machine cycles. Since there can be at most n -- 1 selected
edges, the time taken to copy these edges into B is
T A B L E I.
Storage needed in
machine words
Time in machine cycles
,F1 Vn7
Original
Welch
algorithm
3
.('+[+l)]
pn 3
Modified
Welch
algorithm
n
[iFq
2u I~ l -'I- m -l--n
3n
r+l++r+l+o
n = number of nodes;
m = number of edges;
p = density of edges;
w = number of bits per machine word;
$~ = number of machine cycles for a logical operation;
$2 = number of machine cycles for a word compare;
~3 = number of machine cycles for a word replacement.
782
C o m m u n i c a t i o n s o f t h e ACM
28z).
The maximum time for block 1
+
)
61 -~- ~ 62 "Jr 283 • A
similar examination for
blocks 2 and 3 yields leading terms of ~
62 and
n'l l <++, + +++ + ++.
We now examine block 4. Assume the average length of
the fundamental cycle corresponding to each of the
(m -- n -t- 1) edges that was not chosen is n/2. For each
cycle, the time to search the spanning tree is approximately
constructing the fundamental cycle is ~ ~
(81 -[- ~ 62 +
n
2
machine cycles. For large p and n, (m - n -t- 1) ,~ p ~and
on
the
maximum
term
for
this
block
becomes
81 + g nSs + 83 machine cycles. We see that
this term dominates the expression.
F+l(
the whole program is p n 4 ~
Hence the time for
)
~ 81 -t- g 62 machine cycles
+
plus terms which depend on lower powers of n.
For large values of p and n, this estimate was quite
close to the observed time. On the I B M 7094-II, the
machine cycle is 1.4 ttsec, w = 36, 82 -~- 83 = 2 machine
1-- -1
cycles, 81 = 3 m a c h i n e cycles since t h e p ~ b 4 / ~ / ( ~ l / 4 ) t e r m
results from an AND operation. For n = 50 and p = .9,
the estimated time is 0.394 g i n ; for five random graphs 3
the average observed time is 0.468 min.
For each n, p (where n = 10, 20, . . . , 70 and p =
.1, .2, . . . , .9), five random graphs of size (n, p) were
timed. Figure 2 illustrates a graph of log (lime) vs log
(n) for p = .5. This graph is typical for any p (except
for p = .1 when most graphs were not connected). For
all values of p, the slope of the line was very close to 4,
thereby verifying the estimated dependence of time on n 4.
r- -1
Since / ~ / is 1 or 2 for o u r samples, t h e e s t i m a t e d t i m e
COMPARISON WITH WELCH'S ALGORITHM
2n
Our
is thus n
~
is dependent on n 4 rather than on n 5.
+
algorithm
(n - - 1 ) / ~ / ( 2 8 1
A modified version of Weleh's algorithm was programmed and tested. The first look at Welch's algorithm
indicated a term of n 4 in the time estimate. This term
occurs because of the necessity to retrieve a column from
8 A random graph of n nodes and m edges is a graph which has n
given labelled verticles P1, P2 . . . . , P , and the m edges are
chosen at random among the
0
2 possible edges, so t h a t all
possible choices are supposed to be equiprobable. Note:
graph is linear and there are no loops (edges (Pi , Pal)-
The
V o l u m e 10 / N u m b e r 12 / D e c e m b e r , 1967
10.0
The estimated time for this program is
r_ l
l.O
Our Algorithm
Slope = 3.B
0.I
Algorithm
Slope = 2,4
Welch's
,o
0.01
)
0.001
(
+ oo,
'
'r loo
machine cycles. This estimate turns out to be quite good;
for example, for n = 50, p = .9, the estimated time is
0.026 rain whereas the observed time is 0.032 rain (here,
~1 equals four machine cycles on the IBM 7094-II since
the only logical operation is the EXCL~SIV~ OR operation). On a graph of log (time) vs log (n), the slope of
the observed time is about 2.5 compared to the theoretical value of 3 (see Figure 2).
Comparatively our program uses substantially less
storage, namely, 3 (n × n) binary matrices, 2 binary
vectors of length n and 1 vector of length n. For the
example of n = 70, p = .9 and w = 36, our algorithm
needs 494 computer words compared to the 12,958 needed
by Welch's Mgorithm. From the time estimates for the
two methods, the ratio of the time for our method to the
time for Welch's method is approximately N/3. This is
substantiated by computer tests.
Table I presents the timing estimates and the storage
requirements for the three algorithms.
Acknowledgment. The authors wish to thank the National Research Council of Canada for financial assistance.
0.0001
i
10
LOG
20
I , I I IIII
30
50 70
RECEIVED FEBRUARY, 1967; REWSED SEPTEMBER, 1967
(N) NU,~BEROF NODES
Fio. 2. Log (time) vslog (n) forp = .5
a binary (n X m) matrix. On account of the n 4 term and
the disadvantage of needing 2 (n X m) binary matrices,
this version would not have compared well with our
algorithm. In the modified version time has been saved
whenever possible by using extra storage. This program
needs 2 (n X m) binary matrices, 2 vectors of length
m, and 1 vector of length n. For large values of n and
m, these matrices and vectors would exceed storage
capacity for most computers; for example if n = 70,
p = .9 and w = 36, 12,958 computer words are needed.
REFEREN CES
1. BERGE, C. The Theory of Graphs and Its Applications. John
Wiley, New York, 1962.
2. BUSACKER,1~.., AND SAATY, W. Finite Graphs and Networks-An Introduction with Applications, McGraw-Hill, New York,
1965, pp. 99-109.
3. IVERSON, K. A Programming Language. John Wiley, New
York, 1962.
4. SUSSENGUTtt, E., JR. A graph theoretical algorithm for matching chemical structures. J. Chem. Doc. 5, 1 (Feb. 1965), 36--43.
5. UNGER, S . H . G I T - - a heuristic program for testing pairs of
directed line graphs for isomorphism. Comm. ACM 7, 1 (Jan.
1964), 26-34.
6. WELCH,J. T., JR. A mechanicalanalysis of the cyclicstructure
of undirected linear graphs. J. ACM 13, 2 (April 1966), 205-210.
Correction to Proposed USA Standard
Proposed USA Standard: "Recorded Magnetic Tape for Information Interchange
(200 CPI, NRZI)," Comm. A C M 10, 11 (Nov. 1967), 730-737.
Page 731, 1st Col., Section 5.3 Skew: In the second line, "0.0001 inch" should
read "0.001 inch".
V o l u m e 10 / N u m b e r 12 / December, 1967
Communications
o f t h e ACM
783