• No results found

[2], [3], which realize a time bound of O(n. e(c + 1)).

N/A
N/A
Protected

Academic year: 2021

Share "[2], [3], which realize a time bound of O(n. e(c + 1))."

Copied!
8
0
0

Loading.... (view fulltext now)

Full text

(1)

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, thereareexactly

i=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 last

vertices 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.

(2)

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)strongcomponent

of

Giffor all u,v Wthereexist pathsP,vand pv, andthisproperty holds for nosubgraphofG inducedbyavertexset

W

suchthat Wc

W

_

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 of

O(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 aloneconsumes

O(k

3)

time. Sincethereare exactly 3k elementary circuits in the entire graph, the running time is at least

O(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 take

O(k

2)

time, which is

O(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 which

in

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

(3)

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 list

AG(v)

for eachv V.Thelist

A(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 procedureCIRCUITandis

deletbd

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 are

separated

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; for

wB(u)

do

begin

deletewfrom

B(u);

if

blocked(w)

thenUNBLOCK(w); end

endUNBLOCK

(4)

end;

stack v;

blocked(v):-

true; L for

wAK(v)

do

ifw- sthen begin

output circuit composed ofstack followed by s;

f

:- true; end

elseif--blocked(w)then

if

CIRCUIT(w)

then

f

true; L2:

iff

then

UNBLOCK(v)

else for

wA:(v)

do

ifvqB(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

-

then

begin

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 acall

UNBLOCK(v)

whichsets

(5)

FINDINGELEMENTARYCIRCUITS 81 (i) there is apath, containing v,

from

x .to s onwhich onlyv ands areonthe

stack,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 call

CIRCUIT(z).

Thus there is a call

UNBLOCK(v)

and, without loss of generality, a path (y

re,

"’",/)k =/)) on which only/)is on the stack and only yis notunblocked as a result of thecall

UNBLOCK(v)

atL2. But when ywaslast blocked,ywas on the stack. Since yremained blocked when y was removed from the stack,y was put on list

B(/)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. Since

f

is true there is an edge (/),z) such that either z s or

f

was set truewhen the call

CIRCUIT(z)

returned. It follows from our assumptionthat,when

f

was settrue, therewas apath fromztosonwhichonlys was on the stack. It may be that several calls to CIRCUIT occur after

f

is set trueand before the current call

UNBLOCK(/)).

In any event, the current stack (whenthe call

UNBLOCK(/)) occurs)

isidenticaltothe stackwhen

f

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 ify

Soy :/:/),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. But

blocked(t)

hasto remain true until the current call

UNBLOCK(/)).

Otherwise y would have been removed from

B(t).

Since y musthave been unstacked after ywasputon

B(t),

therewas some executionof L2 wherethe stackwas aprefix of thecurrentstack, (i)and(ii)held fort,and

blocked(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 only

un-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 such

that/)1

-</)i, _<

=<

I.

Afirst call

CIRCUIT(v1)

willeventually occur at L3since thereis astrong com-ponentwithleastvertex/)1.Since no vertex isblocked when thisfirstcalloccurs,it

followsbyinductionusingLemma that whenever thestack is(s vl,/)e, "’",

for < l, the stack will later be (s vl,/)2,"’,/)i+

1)"

Thus every elementary circuit isoutput.

(6)

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 toCIRCUIT

before

either thecallreturnsor acircuit is output.

Proof.

First we show that no vertex can be unblocked twice in succession

unlessacircuit 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, /)k

S)

onwhichonlys

is 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 when

blocked(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. [3

COlOIIAgY2. The algorithmrunsin

O((n

+

e)(c

+

1))

timeanduses

O(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 a

(7)

FINDINGELEMENTARYCIRCUITS 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 an

IBM 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 the

TABLE

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.

(8)

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.,

Figure

FIG. 2. A second worst-case example for Tarjan’s algorithm

References

Related documents

F6150 Internal Options Ethernet or USB Communication Logic Inputs Voltage &amp; Current Relay F6ControlPanel Communications Relay Sources Logic Outputs (Optional) under Test..

When investing offshore, the investor is not limited to the domestic market but has a choice of investing in different currencies, investment vehicles and

Indira Nooyi, CEO PepsiCo Madras Christian College The non‐IIT Faces Satya Nadela, CEO Microsoft Manipal Institute of Technology Rakesh Kapoor, CEO Reckitt Benckiser BITS

The aim of this study was to evaluate the current vac- cination status of the HCWs in all of the Departments different from the Department for the Health of Women and Children of one

Q:\Market ng\Presentations\AGI Presentation\AG Presentation with Design-Const-Too Procure 07-2010.ppt Q:\Marketing\Presentations\AGI Presentation\AGI Interview Pres for Univ

One of the tools of performance measurement that has been used in the past is performance appraisal that has been reintroduced in a new format and design and implementation within the

This study was undertaken to analyze the impacts of the occupational health and safety programme on the organization performance in Arusha airport.. The key

Furthermore, while symbolic execution systems often avoid reasoning precisely about symbolic memory accesses (e.g., access- ing a symbolic offset in an array), C OMMUTER ’s test