SIAM J.COMPUT. Vol. 4,No. 1,March1975
FINDING ALL THE ELEMENTARY
CIRCUITS OF A DIRECTED GRAPH*
DONALD B. JOHNSON
Abstract.Analgorithm ispresentedwhich finds alltheelementarycircuits-ofadirectedgraphin timebounded byO((n +e)(c + 1))and space boundedbyO(n+e), wherethere arenvertices,eedges
andcelementarycircuits in the graph.Thealgorithm resembles algorithmsbyTiernan andTarjan, but is faster because it considerseachedgeat mosttwicebetween anyonecircuitand thenextinthe outputsequence.
Keywords,algorithm, circuit, cycle, enumeration,digraph, graph
1. Introduction. Broadly speaking, there are two enumeration problems on setsof objects. The one,which we call counting, isdetermining how many objects there are inthe set. The other,which we callfinding, is the constructionof every objectintheset exactlyonce. Indeed, objects may always be counted by finding themif amethodto do so is athand.Butknowing thecount isusually oflittle aid infindingtheobjects.
Wegive an algorithm for finding theelementarycircuits ofadirected graph which isfasterin theworstcase than algorithms previouslyknown.
As
far as we know,it isalsothe fastest method known for thegeneralenumerationproblemas well(see
1, p.2263).
Specific countingproblems are, of course, solved.Forexample, thereareexactlyi=1 n-i+
(n i)!
elementarycircuits in acompletedirectedgraphwithnvertices.Thus the number ofelementarycircuits in a directedgraphcangrow fasterwith nthan the exponen-tial2".Soit isclearthatouralgorithm,whichhasatimeboundof
O((n
+
e)(c
+
1)) on any graph with n vertices, eedges and c elementary circuits, is feasible for a substantially larger class of problems than the best algorithms previouslyknown [2], [3],which realize a timebound ofO(n.e(c
+
1)).
A
directed graph G (V,E)
consists ofanonempty andfinite set of vertices V anda setEof ordered pairs ofdistinct vertices called edges. Thereare n vertices andeedgesinG.A
pathinGis asequence ofvertices Pv,(v
v1,v2, "",vk u) such that(v
i,vi+1)e
Efor _< < k.A
circuitis apath inwhich thefirstand lastvertices areidentical.
A
pathiselementaryif no vertexappears twice.A
circuit is elementary if no vertex but the first and last appears twice. Two elementary circuits are distinct if one is not a cyclic permutation of the other. There are c distinctelementarycircuits inG.Ourdefinitionsexcludegraphswithloops(edges of the form (v,v))
and multiple edges between thesamevertices.Itis obviousthat for any circuitqv, there exists a vertex u such that q is composed ofa path followed by edge (u,v).
If qv iselementary, then pu is also elementary.Receivedbythe editors December 10,1973,and in final revised formJune10, 1974.
-ComputerScience Department,PennsylvaniaStateUniversity, University Park, Pennsylvania 16802.
78 DONALDB. JOHNSON
F is .asubgraph
of
Ginducedby Wif W_
1/andF (I/V,{(u,
v)lu,v Wand (u,v)
El).
An
inducedsubgraphFis a(maximal)strongcomponentof
Giffor all u,v Wthereexist pathsP,vand pv, andthisproperty holds for nosubgraphofG inducedbyavertexsetW
suchthat WcW
_
1/.Theliterature containsseveral algorithmswhich findthe elementarycircuits of any direct graph. In the algorithms ofTiernan
[4]
and ofWeinblatt [5], time exponentialinthesizeofthe graph may elapsebetween theoutput ofonecircuit andthenext[2].
Tarjan[2]
presentsavariationofTiernan’salgorithmin which at most O(n.e)
time elapses between the output of any two circuits in sequence, givingabound ofO(n. e(c
+
1))
for the runningtimeof the algorithmon anentire graph in theworst case. Ehrenfeucht, Fosdick, and Osterweil [3] give a similar algorithmwhich realizesthe samebound.Inthe case of Tarjan’s algorithm, the worst-case time bound is realized, for instance,onthegraph showninFig. 1. Weassumethatthe algorithm beginswith vertex and,in anysearch fromvertices through k
+
1,it visits verticesk+
2 through2k+
beforeafirst visit to vertex2k+
2.Inthecourseof finding each of the kelementarycircuits which contain vertex 1, thesubgraphonvertices 2k+
2 through3k+
3 willbe explored k times, once for each of theverticesk+
2 through 2k+
1.Thus exploration fromvertex aloneconsumesO(k
3)
time. Sincethereare exactly 3k elementary circuits in the entire graph, the running time is at leastO(n.e(c +
1)).
The worst-case time bound for Tarjan’s algorithm is also realized on the graphinFig: 2.Assuminga start at vertex1, the algorithm takes
O(k)
time tofind the oneelementarycircuitofthegraph.Thenthefruitlesssearches fromvertices2 through k takeO(k
2)
time, which isO(n. e(
+
1)).
So we seethat there are two ways inwhich the time bound is realized in Tarjan’s algorithm. Oneis through repeated fruitless searching of a subgraph while seeking circuits with a certain least vertex; the other isthroughfruitless searchesfrom manyvertices whichare leastvertices in noelementary circuit.The graphs of Figs. and 2 are strongly connected, and their undirected versions arebiconnected.Onsuchgraphs,obvious preprocessing techniques, such asreducing thegraphtoitsstrongcomponents
(as
Weinblattdoes[5])
orto com-ponents whichin
addition to strong connectivity have biconnected undirected versions, donot improve the performance of Tarjan’s algorithm.Stronger
tech-niquesareneeded togetabetter asymptotic runningtimein theworstcase.2k+5
k+2 2k+4,
:
2k+l
FINDINGELEMENTARYCIRCUITS 79
2 3 k-I k
FIG.2.Asecondworst-caseexampleforTarjan’salgorithm
2. Thealgorithm.In ouralgorithm, thetimeconsumed between the output of two consecutive circuits aswell as before thefirstandafter the lastcircuits never exceedsthesize ofthegraph,
O(n
+
e).
We employthe basic notion of Tiernan’s algorithm. Elementary circuits are constructed from a root vertex s in the sub-graphinduced by sand vertices "largerthan s"in some ordering of thevertices. Thus the outputisgrouped accordingtoleast vertices ofthecircuits.To avoidduplicating circuits, a vertex v is blocked whenit is added to some elementary path beginningins. Itstays blockedaslongaseverypathfromv to s intersects the current elementary path at a vertex other than s. Furthermore, a vertex doesnotbecome a root vertexfor constructing elementary paths unlessit is theleastvertex in atleastoneelementarycircuit.Thesetwofeaturesavoidmuch of the fruitless searching of Tiernan’s,Weinblatt’sand Tarjan’s algorithmsand of the algorithm ofEhrenfeucht,Fosdick, and Osterweil.
The algorithm acceptsa graph G represented by an adjacencystructure
AG
composed ofanadjacency listAG(v)
for eachv V.ThelistA(v)
contains u ifand onlyifedge(v,u)
E.The algorithmassumesthatverticesarerepresented by inte-gers from to n.The algorithm proceeds by building elementary paths from s. The vertices of thecurrentelementary patharekepton astack.
A
vertexisappendedtoan ele-mentarypath byacalltothe procedureCIRCUITandisdeletbd
uponreturnfrom thiscall. When a vertex v is appended to a path it isblocked by setting blocked(v)
true,sothatv cannotbe usedtwice onthe samepath.Upon
returnfromthe callwhichblocks v,however,vis notnecessarilyunblocked.Unblockingisalways delayed sufficiently so that anytwo unblockings ofv areseparated
by either an output ofa newcircuit or areturntothe mainprocedure.CIRCUIT-FINDING
ALGORITHM
begin
integer list array
A(n), B(n);
logicalarray blocked(n);integers; logicalprocedure CIRCUIT(integervalue v);begin logicalf;
procedure UNBLOCK(integervalueu); begin
blocked
(u):=
false; forwB(u)
dobegin
deletewfrom
B(u);
if
blocked(w)
thenUNBLOCK(w); endendUNBLOCK
end;
stack v;
blocked(v):-
true; L forwAK(v)
doifw- sthen begin
output circuit composed ofstack followed by s;
f
:- true; endelseif--blocked(w)then
if
CIRCUIT(w)
thenf
true; L2:iff
thenUNBLOCK(v)
else for
wA:(v)
doifvqB(w)thenputvon B(w); unstackv;
CIRCUIT :=
f;
end
CIRCUIT;
empty stack; s:=l; whiles <n do
begin
A:=
adjacency structure of strong component K with least vertex insubgraph of Ginduced by{s,
s+
1,n};
if
A
-
thenbegin
s:= least vertex in
V;
foriVu,do begin
blocked(i) :-- false; B(i) :--
,;
end;
L3: dummy :- CIRCUIT(s);
s:=s+l; end
else s n;
end
The correctness ofthe algorithm depends on no vertex remaining blocked when there is a path from the vertex to s which intersects the stack only at s.
Onthe otherhand,the boundonrunningtimedependson allvertices remaining blockedaslong aspossible,consistent withthe requirements ofcorrectness.The following lemma establishes these properties. It will be seen that the B-lists are used to rememberinformation obtained from searches ofportions ofthe graph which do not yield an elementary circuit. The procedure UNBLOCK has the property thatifthereis acall
UNBLOCK(x)
andvertexyis on listB(x),thenthere will beacallUNBLOCK(y)followingwhichblocked(y)willbefalse.LEMMA 1. At
L2,for
anyvertexx 4:s,thereis acallUNBLOCK(v)
whichsetsFINDINGELEMENTARYCIRCUITS 81 (i) there is apath, containing v,
from
x .to s onwhich onlyv ands areonthestack,and
(ii) thereis nopath.fromxtos onwhich onlys is onthe stack.
Proof.
Assume,
to the contrary, that there is an execution of L2 at which the lemma first fails and that the lemma fails for no vertex before it fails for vertex y.Two
cases arepossible under this assumption.Case 1.
Suppose
that the path conditions, (i) and (ii), hold for y at L2, but blocked(y)is not set false.Because
there is anedge (v,z) onthe path from yto s,f
is true atL2.Thisfactisimmediateif z s.Ifz-:
s, itfollows fromour assump-tionthat the lemma holds forzbefore thereturnfrom the callCIRCUIT(z).
Thus there is a callUNBLOCK(v)
and, without loss of generality, a path (yre,
"’",/)k =/)) on which only/)is on the stack and only yis notunblocked as a result of thecallUNBLOCK(v)
atL2. But when ywaslast blocked,ywas on the stack. Since yremained blocked when y was removed from the stack,y was put on listB(/)e).
So there was a call UNBLOCK(y), acontradiction.Case 2. Supposethat thereis acall
UNBLOCK(/))
at L2and thatblocked(y) is setfalse,but thateither(i) or(ii)is not satisfied.Itcannotbethat/) sbecause it is clear that the lemma holdswhenonly/) sis onthe stack, and scannotbe stacked more than once. Sincef
is true there is an edge (/),z) such that either z s orf
was set truewhen the callCIRCUIT(z)
returned. It follows from our assumptionthat,whenf
was settrue, therewas apath fromztosonwhichonlys was on the stack. It may be that several calls to CIRCUIT occur afterf
is set trueand before the current callUNBLOCK(/)).
In any event, the current stack (whenthe callUNBLOCK(/)) occurs)
isidenticaltothe stackwhenf
was settrue, so there is a path (/),z,...,
s)on which only/) and s are on the stack. Since/) is onthe stack, (i)and(ii)wouldbesatisfied ifySoy :/:/),and thereis some vertex which isunblocked before yisunblocked such that yis on
B(t). By
assumption, thereis apath from tos onwhichonly and s are onthe stack. Furthermore,when ywaslast puton B(t),blocked(t)
was true and ywas on the stack. Butblocked(t)
hasto remain true until the current callUNBLOCK(/)).
Otherwise y would have been removed fromB(t).
Since y musthave been unstacked after ywasputonB(t),
therewas some executionof L2 wherethe stackwas aprefix of thecurrentstack, (i)and(ii)held fort,andblocked(t)
was not setfalse. Butby assumption, the lemmadid not failthen fort.From this contradiction,wefindthat Case2isalso impossible.
COROLLARY 1. The algorithmoutputsonly elementarycircuits.
Proof.
Certainly onlycircuits are output.By
Lemma 1, avertexis onlyun-blockedif it willbe offthestackbefore anycall toCIRCUITcanoccur. Thusno vertexcanberepeatedonthestack.
LEMMA2. The algorithmoutputsevery elementarycircuitexactlyonce.
Proof.
No circuit is output more thanonce since, for any stack(s..,/)k)
with/)kontop, once/)isremoved the same stack cannot reoccur.Let
(/)1,/)e, "’",/)l,/)1)
be an elementary circuit suchthat/)1
-</)i, _<=<
I.
Afirst call
CIRCUIT(v1)
willeventually occur at L3since thereis astrong com-ponentwithleastvertex/)1.Since no vertex isblocked when thisfirstcalloccurs,itfollowsbyinductionusingLemma that whenever thestack is(s vl,/)e, "’",
for < l, the stack will later be (s vl,/)2,"’,/)i+
1)"
Thus every elementary circuit isoutput.The foregoing results show that the algorithm doesindeedfind allthe elemen-tary circuits ofa directed graph. The bound on running time follows from the nextlemma.
LFMMa3. Atmost
O(n
+
e)
timecanelapsein acallatL3 toCIRCUITbefore
either thecallreturnsor acircuit is output.
Proof.
First we show that no vertex can be unblocked twice in successionunlessacircuit isoutput. Thenweshow thatnomorethan
O(n
+
e)
time canelapse beforesome vertex isunblockedasecondtime.Supposeacircuit isoutput and thensome vertexyisunblocked.
By
Lemma1, as soon as v isunstacked thereis apath (y v,/)2, /)kS)
onwhichonlysis onthestack.Letsome vertex
v,
__<
< k,be thefirst vertex on thispathtobe putonthe stack again.Weseebyinduction ontheexecutionofthe algorithm that eventuallythe stack willbe (s,...,
v,
v+,...,
v_
)
and a new circuit output. Untilthenewcircuit isoutput,no vertex onthepathwillbe unstacked. Thusno vertexcanbe unblockedmorethanoncebeforeacircuit isoutput.Chargea unitofcost to a vertex if it is anargumentto a procedure call and aunitofcost to anedgeif considerationofthisedge by theforloopatL1 in CIR-CUITdoes not result in aprocedure call. The cost ofall work in the procedure CIRCUITwillbe boundedbyaconstanttimesthe number ofunitscharged.For anyvertexx, calls toCIRCUIT and UNBLOCKmust alternate. Consequently, no more than three units can be charged to each vertex before some vertex is unblocked twice.
As
toedge charges, let some edge originatein vertex x.A
unit may bechargedtothisedgeonly whenblocked(x)
is trueand,once aunit ischarged, xmust be unblocked and blocked again before a secondunit can becharged to thesameedge. Itfollows thatatmost twounits canbechargedtoanyedgebefore some vertexisunblockedtwice. [3COlOIIAgY2. The algorithmrunsin
O((n
+
e)(c
+
1))
timeandusesO(n
+
e)
storagespaceplus thespaceused
for
output.Proof.
Thetimebound follows directly fromLemma3andaknown algorithm[6]for finding strong componentsin
O(n
+
e)
time.The space boundis immediate fromtheobservation thatno vertexappearsmorethanonceonanyB-list.3. Discussion of running time. We have shown that in the worst case, our algorithmisasymptotically faster than algorithms previously known.Withrespect to Tarjan’s algorithm, a stronger statement can be made. There is a constant factorwhichboundshow much slower ouralgorithmcanbecompared tohis on anygraph,provided the sameadjacency structure is used as inputto both algo-rithms. Such a constant, of course, is implementation dependent. Its existence follows fromtwofacts. First, thetimespentbyouralgorithm in finding the strong componentwithleastvertexs
>=
kisofnogreater order than the searchinTarjan’s algorithm for circuits with least vertex k, for=<
k=<
n. Second, ifthe calls to UNBLOCKare ignored, on identical adjacencystructures the sequence of edge explorations generated by ouralgorithm is embeddedinthe sequence generated by Tarjan’s.Butfor everyedgeinthe sequence, forouralgorithm therecan occur atmost onecalltoUNBLOCK. Therefore,sincethesearchtime ineachalgorithm isrelatedbyconstantfactorstothe number ofedgeexplorations, the effort spent byour algorithminfinding circuitsfrom agiven base vertex, s, is boundedby aFINDINGELEMENTARYCIRCUITS 83
constantfactor times the effortexpendedby Tarjan’salgorithmfor the correspond-ing searchonthesameadjacencystructureforany graph.
Experimental results are shown in Tables and 2. The algorithms were implemented in ALGOL W
[7]
and were run on anIBM 370/168
with virtual address hardware inoperative. The benefit predicted for our algorithm on worst casesisapparent in the resultsinTable 1. Table 2 shows superiorperformance by our algorithm on complete graphs as well. Although only a constant factor is involved,thissecond resultissomewhat surprisingsince oncomplete graphs,both algorithms make the same number of edge explorations. The result, however, appearstobe explainedbytwofeatures of Tarjan’s algorithm. Hemaintainstwo vertexstacks andtests intheinnermostloop for elimination ofverticeslessthans. If his algorithm were redesigned to correct these problems, "he analysis of theTABLE
Runningtimes onthe familyofgraphsofFig.
Number of vertices 5 10 20 40 60 80 Numberof circuits (Johnson’s algorithm)
Running time IBM 370/168, second (Tarjan’s algorithm) 15 30 60 120 180 240 .03 .11 .32 1.17 2.61 4.46 .06 .27 1.67 11.51 36.89 86.66 2 2.5 5.2 9.8 14.1 19.4
Timerresolution 1/60second.Becauserunningtimes fluctuate with system load, all data shown takenfrom computer Results averages oftwotimes roundedto thesecond decimalplace.
Numberof vertices 2 3 4 5 6 7 8 9 TABLE2
Runningtimes oncomplete directed graphs
Number of circuits 5 20 84 409 2365 16064 125664
Running time IBM 370/168, seconds (Johnson’s algorithm) 0 0 .02 .02 .07 .35 2.43 20.17 (Tarjan’s algorithm) 0 0 0 .02 .08 .51 3.63 30.13 1,1 1.5 1.5 1.5
Timer resolution1/60second.Becauserunning times fluctuate with system load, all data shown taken from computer Results averages oftwo timesroundedtothe second decimalplace.
precedingparagraph would stillhold. Inboth tests, the space bound of
O(n
+
e)
wasconfirmed.
4. Conclusions. Thealgorithmwehave shownis fasterasymptotically in the worst casethan algorithms previously known. The algorithmappears particularly suited for general usebecause of the stronger property, which wehave shownin relation toTarjan’s algorithm, of beingneversloweronanygraphbymorethana constantfactor.In fact,inthetestsrun,ouralgorithmwasalwaysfaster excepton trivially smallgraphs.
Acknowledgment.Credit isduetothe refereesfor suggesting that, for compara-tive tests, a family ofgraphs be found in which the members are strongly con-nected and whoseundirected versions are biconnected, and forasuggestion that ledto animprovement,by a constantfactor,inrunningtime.
REFERENCES
[1] F. HARARYANDE. PALMER, GraphicalEnumeration,AcademicPress,NewYork, 1973.
[2] R. TARJAN, Enumerationoftheelementarycircuitsofadirectedgraph,thisJournal, 2 (1973), pp. 211-216.
[3] A. EHRENFEUCHT, L.FOSDICKANDL.OSTERWEIL, Analgorithmforfindingtheelementarycircuits
ofadirectedgraph,Tech.Rep.CU-CS-024-23,Dept. ofComputerSci., Univ. of Colorado,
Boulder, 1973.
[4] J. C. TIERNAN, Anefficient search algorithmtofindthe elementary circuits ofagraph, Comm. ACM, 13(1970), pp.722-726.
[5] H. WENBLAX,A newsearchalgorithmforfindingthesimple cyclesofafinitedirectedgraph, J.
Assoc. Comput. Mach.,19(1972), pp.43-56.
[6] R.TARJAN, Depth-firstsearch and lineargraphalgorithms, this Journal, (1972), pp.146-160. [7]. R. L.Sxa’ES, Algol Wreferencemanual,Tech.Rep.STAN-CS-71-230,ComputerSci. Dept.,