A new algorithm for finding weak components

A new algorithm for finding weak components

Volume 3, number 1 INFORMATIONPROC!!SSiNG LEI TERS A NEW ALGORITHM FOR July 1974 FINDINGWEA COMPON~TS Robert Endre TARJAN! Depwvnetttof Elect&d E...

437KB Sizes 18 Downloads 163 Views

Volume 3, number 1

INFORMATIONPROC!!SSiNG LEI TERS

A NEW ALGORITHM FOR

July 1974

FINDINGWEA COMPON~TS

Robert Endre TARJAN! Depwvnetttof Elect&d Enginewing andBmgurcrSciences, Wniversity of GdifoMa,Berkeley, G&ford, USA Received 5 April 1974

connectivity

directed guph

strongcoPrpof&nts

topologicd

sorting

weak components

Let LSand w be two vertices in a directed graph C. There is a mmpath from u to w if thers is no path from u to w. If there is a non-path from ui to u~+~for 1 Q i Q n, then ul, 9, * , vn is a sequence of non-pathsteps from ul to 5. Vertices u and w are in the same wk cmpment of G if they arein the same strongcomponent jthcre &a path from v to w and from w to u) or if there is a sequence of non-path steps from v to ts,and from w to u. The relation ‘Zrand w are in the same weak component” is an equivalence relation on the veitkes of G [ 11. Strong components are the finest partition of the vertkes on which the edges of G induce a partial ordering; weak components are the finest partition on which the edges induce a total ordering. Pacault [2] has devised an algorithm to find the weak components of C in o( Y+E) time and spa=, %G has bhm’thx~ and E edges.Here is another o(V+Q algorithut which is directly related to Pacault’s but is more st@Mfomd. It giveathe weak components of certain subgraphs of G as well as of the entire graph. The weak components of G are unions of its strong components. (1) Rfnd the strongcomponents Cl of G and shrink each to a single vertex i, with an edge (i.j) in the new graph ifandonlyifthereisanedge(u,w)inGwithuECtandlwE~. The weak components of the res&ing acyclic graph give the weak component of G. Shrinking the strong components requiresO(V+Sy time using depth-firstsearch [3]. He,lceforth, assume that G is acyclic. (2) Number the vertices of:G from 1 to Yso that all arcs run from a lower numbered to a higher numbered vertex. This operadon is &ei; tapoloeiapav WJ?& the vertices of G and may be carried out in 0(Vi-E) time using depth-fust search [4] or other methods [S1. I n fact, steps (1) and (2) may 1.fdesired be carried out using a single depth-fbst search.Henceforth,assumethat ver&es are identified by number. l l

tRnrma 1. ‘Ibe W&CM of afly weak CWNBPC, ttentof G ale consecutive, p)rrof,Let i
ww partially Yupportedby &National search FeIIowship.

Science Four&don, Grant Number NSF-G8-3560

2.LstfQ;k<~,~hl:q=min{~E~~lvf>max(B(k+l)u(l))). :~)~&(k+l)thenLk.,l ={v~,~;~*,q,k+l);otherwiseL~+~

@

July 1974

INPORkiAT?ON plieOcEsSrnG LErnRS

a,rmI

=(vl)v~,*~*,.v*_l,k+l).

Nr~x k + 1 has no edges Ieadlingto smaller numbered vertices; thus, if i, j G k there is a path frorrr,i isrpathfromitojinG k+l . It follows that the we& components of Gk+l are unions of [ktl) ponents oft+. For i > ! there is a non+th from us to k + ),, so &>I &(q) 2 ftfk+l(k+ 1). If there is a-non-path from some vertex in S&) fgi’kt 1, by the deftiitioa of I; hence . There is a non-$& from every vertcX in Wkt ) to every vertex in &(vi) for f > i, so there every vertex in wk(uI) to every vertex ti wk(vs for ahj > i mus, there is no sequence of I to k t 1; and l/s $@I) g B(k+fl),there is raosuch sequence &+l :has*he valireg&en in r[A),atatement (B) follows im, Ct thsltkf=k+l h@k?S Sk(k) $ B(k+ I). Q.ED. b&w is a pgntm la AlgoLlihen&ion wh@h uses Lemma 2 to c&uMe weak components, The is a !itt& &i&y. At the begin-g of the kth iteration of for loop Q,if veLk then t(v) is the n+i f Lk (of s#?roif v isthe lowest element of &). Also, S&[v) = {sqv)lr

3 0,squ)

# 0, and p@(v))

= true).

b

the kth iteration of bp a, the boolean Mriable &j) is set trne if and only ffJ’E$k t 1). *am? ~i;(~~i)r!k~,Ifw~k(~~~-B(k+l!,the prograinsHsS(k+b)=k a&i th,~n~~tsp(f) *false for f-j 1) fl& +I(k+ 1).TheboobEn procedure ‘sz&$retums the tiue true E tid &ly if S&(kt 1)) s N&t 1). c~&rre worms this test &j nimbg t@5u& the elements nfS&k : :)) until it finds one, ,~ayj, for #) (pf&e. hwdure WbZt aho Metes from the li&ed list S!L(R+l)) @Ielementsf it encounters for

,fhing,

. cv ~~~-YP,q(l:iy); lje!gin~j;BuoImflrqg,

1

.

Volume 3, number 1

INFOF MATION PRGCESSING LETTERS

July 1974

forjEB(k+l) do begin ifj > nz then m :=j; 411’):= true; end; dk+1) :=tnle; L(k+l) := k; if L(k) < m then begin wmment q = k; if subset then S(kt 1) := 0; elsebegin L(-k+l) := L(k); S(kt1) := k; end end ebe begin comment q
0

end; for jEljr(kt&,) do begin if j > L(k+l) then p(j) := false; q(j-&=fak; end end end;

I

it is WQJ to gee that step (3) is an implementation of thy result in Lemma 2, and L gives the set L, when the algorithm is completed. Array L is used as a (linked) stack. Since on&y V additions are made to the stack, only O(v) time is spent modifying L. The total time spent examining B and changing p and q ia OQ.V+JZ’).Examination of a value w) in procedure subset causesm to become false, or causes S(j) to be deleted from some lis? of sinks, or means q]EB(k+l). Since only V vertices ever become sinks, the total time spent in procedure subset is o(y+E)Thus, the total running time of step (3) is O(VtE), and sops (l)-(3) give an O( Vtli’) algorithm to find weak components. The storage required in step (3) is V t 2E words for B, plus V words for L, plus V words for S, and 2 V bits for p and q. The total storage is 3V + 2E words plus 2 V bits, plus some constant amount of storage. It is possible to coimbinethe L and S arrays to save V words of storage, at the expense of more complicated pro-

gramming and slower running time (by a constant factor). If destruction of B is allowed, we cq save an additional V words of storage. 4 This algorithmproc&es the ve.rticesin order and has the nice feature that we w read off the weak compo: nents of Gk for any k as the calculation proceeds, which is not true of Pacault’s original algorithm. The algorithm cm also be modifkd to &cess vertices fro,m highest to lowest instead of fron; lowest to highest. I ~hish to thank Professor Donald Knuth for his insightful remarks which helped tb Streamline the imFlement&Ion of the algorithm.

Refmnces ill RL Grdwm, D.E. Knuth and T.S. Motzkin, Discrete Math. 2 (1972) 17-30. 121 3.F. Paceult, Cornput@the W&c Components of a Directed Graph, SIAM J. Comput., to appear. [3f R, Taejan, SIAM J, Comput. 1 11972) 146-160. (4; R. Taqjan, Finding Dominators In Directed Graphs, SIAM J. Comput., to appear. orithms (Addison-Wesley, Reading, Mass., 1968) iS j DA. Ibtuth, The Art of Computer Programming,Volume 1: Fundamental 258-265. 15