Universal traversal sequences for paths and cycles

Universal traversal sequences for paths and cycles

JOURNAL OF ALGORITHMS 8, 395-404 (1987) Universal Traversal Sequences for Paths and Cycles* MICHAEL F. BRIDGLAND The MITRE Corporation, Burlingto...

552KB Sizes 0 Downloads 73 Views

JOURNAL

OF ALGORITHMS

8, 395-404 (1987)

Universal Traversal Sequences for Paths and Cycles* MICHAEL F. BRIDGLAND The MITRE

Corporation,

Burlington

Road, Bedford

Massachusetts

01730

Received July 26,1986

One approach to the reachability problem for rooted undirected graphs G is to order the neighbors of each vertex and traverse all vertices of the connected component containing the root by means of a sequence of positive integers (s~)~ 5.t interpreted as instructions of the form, “Move to the s,th neighbor of the current vertex.” A sequence that does this for all d-regular connected graphs with n vertices is called an (n, d)-universal traversal sequence. Although the existence of universal traversal sequences is easy to verify, known methods for their construction involve some sort of exhaustive search. In this paper, a recursive algorithm for the construction of (n. 2)universal traversal sequences in space O(logsn) is described. 0 1987 Academic

Press, Inc.

1. INTRODUCTION For a vertex w in a rooted (di-)graph, the reachability problem is to determine whether there is a (directed) path from the root to W. Let L and NL denote the classes of languages recognizable in log space by deterministic and nondeterministic Turing machines, respectively. It was shown in [3] that the reachability problem for digraphs is log-space-complete for NL; that is, it is solvable in log space iff NL = L. The reachability problem for undirected graphs is less well understood: it belongs to a class of equivalent problems (see [4]) that are neither known to be solvable in log space, nor known to be complete for NL. For d-regular rooted graphs represented with adjacency lists, one approach to the reachability problem is to generate a walk beginning at the root by means of a (1,. . . , d }-valued sequence as instructions of the form, “Move to tsi)i 217 whose terms are interpreted the s,th neighbor of the current vertex.” A walk that visits every vertex in a *This work was supported by the MITRE-Sponsored

Research Program.

395 CR%-6774/87 $3.00 Copyright 0 1987 by Academic Press, Inc. All rights of reproduction in my form RSCIV~~.

396

MICHAEL

F. BRIDGLAND

graph G is called a fruoersul of G, and a sequence that generates a traversal for every representation with adjacency lists of every connected d-regular rooted graph with n vertices is called an (n, d)-universal traversal sequence. A vertex w in a regular graph is reachable from the root iff it appears in the walk generated by a universal traversal sequence. As will be shown below, it is not difficult to extend this method to handle nonregular graphs. It is easy to construct a long (n, d)-universal traversal sequence by concatenating many sequences, each of which is chosen to generate a traversal in a single rooted graph. To provide a proof that the reachability problem for regular graphs is in L, however, an algorithm for the construction of (n, d)-universal traversal sequences must have space complexity O(log n), and that restriction imposes a polynomial bound on the length of the sequence. In response to a question of Cook, whether such short universal traversal sequences even exist, Aleliunas et al. [l] used clever probabilistic arguments to establish the existence of (n, d)-universal traversal sequences of length O(n310g n) (for a careful estimate of the implied constant, see [2, pp. 247-2481). Of course, probabilistic methods are inherently nonconstructive. Even for d = 2, no known algorithm for the construction of (n, d)-universal traversal sequences has space complexity O(n), although it is a trivial exercise to devise a log-space algorithm for reachability in 2-regular graphs. Section 2 describes an algorithm with space complexity O(log2 n) for the construction of (n, 2)-universal traversal sequences of length 0( nc+log “). In the remainder of this section, we consider only connected, undirected graphs without loops and multiple edges. For a vertex U, let N(u) denote the set of neighbors of u. The degree of u is S(u) := IN(u)], and A(G) := max{ 6( u) : u E V(G)}. We understand an adjacency list for u to be a bijection A,:{l,2,. . ., 6(u)} + N(u). An adjacency list representation (G, A) consists of a graph G and a family A = {A, : u E V(G)} of adjacency lists. For each (G, A) and every vertex u E V(G), every sequence s = (~~)~>i of positive integers generates a pseudowalk with initial vertex u, i.e., a sequence of vertices w = (w,JkzO such that w0 = u and consecutive vertices are adjacent or identical: if sk+i s S(w,), then wk+i = A,,,,(s~+‘); otherwise, wk = wk-i. A traversal is a pseudowalk in which every vertex appears. We may now drop our earlier restriction to (1, * * *, d}-valued sequences: an arbitrary sequence of positive integers will be called an (n, d)-uniuersal traversal sequence if it generates a traversal for each choice of an initial vertex (root) in every (G, A) for which G is a d-regular graph with n vertices. We can also drop the regularity requirement: a sequence of positive integers will be called an (n, d) *-uniuersal truuersal sequence if it generates a traversal from each initial vertex in every (G, A) with n vertices such that A(G) I d.

UNIVERSAL TRAVERSAL SEQUENCES

Our first goal is to show that there is no loss of generality restriction to regular graphs:

391

in the

1.1. PROPOSITION. Let n and d be positive integers, and fix an even integer k 2 d + 1. Every (kn, d)-universal traversal sequence is also an (n, d) *-universal traversal sequence. Proof Let (s,),~~ be a (kn, d)-universal traversal sequence, and fix (G, A) such that (V(G)\ 5 n and A(G) I d. Fix an initial vertex w0 in G, be the sequence of vertices in (G, A) generated by (s,). ad let b,A,~o We wish to show that (w,,,) is a traversal of G. Construct a graph G’ as follows: to k disjoint copies of G, add edges, each joining two distinct copies of the same vertex, in such a way that the k copies of each v E V(G) induce a regular subgraph of G’ with degree d - 6(v) (this is possible, since k is even and sufficiently large). Fix a family A’ of adjacency lists on G’ that coincides with A on each copy of G. Since G’ is connected and d-regular, the sequence s generates a traversal (w;) in (G’, A’) beginning at the first copy w; of we. The fact that A and A’ agree on each copy of G in G’ implies that WA is a copy of w, for each m 2 0. Consequently, (w,) is a traversal of (G, A). q

One consequence of the preceding result is that a universal traversal sequence for cycles with 4n vertices is also a universal traversal sequence for all paths and cycles with n vertices.

2. CYCLE TRAVERSALSEQUENCES

For each integer n > 2, every 2-regular graph with n vertices is isomorphic to the n-cycle C, with vertex set Z, = {O,l, . . . , n - 1) and edge set E(C,) = {{i, j} : lj - iI = 1 mod n}. It will be convenient to include C, and C, in our discussion, although the former is not a graph and the latter is not 2-regular. For n > 2, there is a natural one-to-one correspondence between families of adjacency lists A = { Ai : i E Z,,} and signings a,: Z, + { - 1,l) of the vertices: = i + u,(i)

mod n.

Ai(2) = i - u”(i)

mod n.

A,(l)

Moreover, signings also make sense for n 5 2. Henceforth, we shall deal exclusively with signed cycles (C,, u). For a vertex v in a signed cycle, a term a E { - LO, l} generates a step (v, a), i.e., a pseudowalk of length 1, as follows: (v, a)a := v and (v, a)l := v + a(v the direction of the step is u(v)a. Similarly, an arbitrary { - LO, l}-valued sequence s generates a

398 pseudowalk of steps

MICHAEL

F. BRIDGLAND

(u, 3) = (u, s)” beginning at (a, s)~ := u as a concatenation

For a sequence s of positive integers, let s * be the ( - l,O, I}-valued sequence defined as

s*m :=

1, -1, 0,

s,=l s,=2 otherwise.

For each initial vertex uO, the pseudowalk in (C,, A) generated by s is exactly the pseudowalk ( uO,s *) generated by s * in (C,,, aA). Thus there is no loss of generality in replacing integer-valued sequences with { - 1, 1}-valued sequences. In what follows, it will be assumed that every sequence is { - 1, l}-valued, and that its first index is 1. Every walk will be understood to be associated with a generating sequence. The concatenation of sequences s and t will be denoted as st, and the length of s will be denoted as I(s). A walk (u, s) is a traversal of (C,, a) if, for each w E Z,, there is an integer i 2 1 such that (u, s)~ = W. The sequences to be constructed will generate a stronger kind of traversal. A walk (u, s) will be said to traverse a vertex w if there is a step terminating at w followed immediately by another step in the same direction, i.e., if there is an integer i 2 2 such that (u, s>~ = w and u((u, s)~-~)s~ = ~((0, s)~)s~+~. A sequence s such that (s, u) traverses every vertex for each vertex u in every signed cycle with n or fewer vertices will be called an n-cycle traversal sepence. The simplest signed cycles to traverse are the constant signed cycles, i.e., those in which all vertices have the same sign; in such a cycle, every sufficiently long constant sequence generates walks that traverse every vertex. However, in a nonconstant cycle, a walk generated by a sufficiently long constant sequence terminates at a vertex whose sign differs from that of at least one of its neighbors; such a vertex will be called a crossover. Similarly, in an alternating signed cycle (signs alternate between 1 and - l), every sufficiently long alternating sequence generates a walk that traverses every vertex. In all other signed cycles, a walk generated by a sufficiently long alternating sequence will terminate at a vertex whose sign coincides with that of at least one of its neighbors. The behavior of constant and alternating sequences suggests the following informal strategy for the recursive generation of cycle traversal sequences: First, obtain sequences s+ and s- such that, for appropriate vertices u in a signed cycle of length n or less, the walks (u, s’) and

UNIVERSAL

TRAVERSAL

SEQUENCES

399

(u, s-) may be viewed as steps in opposite directions in another signed cycle of length m or less, with m < n. Then replace the positive and negative terms in an m-cycle traversal sequence with the sequences s+ and s-, respectively, to obtain an n-cycle traversal sequence. With slight modifications, a formal version of this strategy yields the desired construction. The walks to be viewed as steps in another cycle are defined as follows: Say that a walk (u, s) is a directed walk if at least one of each pair of consecutive steps is in the same direction as the initial step of the walk. Define the direction of a directed walk to be the direction of its initial step. The proof of the following simple facts about directed walks is left to the reader: 2.1. LEMMA. Let (u, s) and (w, t) be directed walks with direction a. Zf (u, s) visits the vertices v + ia! for 0 I i I j, then it traverses all of them except possibly v and v + ja. Moreover, if (v, s) terminates at w, then ( u, st ) is a directed walk that trauerses w. The simplest way to generate directed walks is with constant sequences. For each positive integer r and a E { - 1, l}, let con(a, r) be the sequence of length 2r + 1 whose every term was value a. Consider a walk (u, con( a, r)) in a signed cycle (C,,, a) for which n/2 < r. As long as every vertex encountered has the same sign as v, every step must have the same direction as the first step. Since there are at least n + 2 steps, the walk will either traverse every vertex, or reach a crossover. In the latter case, the sign of the first crossover reached will coincide with that of v, and the next step will reach another crossover, this time of opposite sign. The walk will reverse direction every step thereafter, moving back and forth between the two crossovers. The terminal vertex of the walk will thus be the first crossover among the vertices v + (2k - l)a(v)a, where k 2 1. The fact that the walk does not necessarily terminate at the first crossover after v complicates the simple strategy outlined earlier. Since a crossover v may be separated from one of the crossovers nearest to it by an odd number of noncrossovers, the directed walk (u, con(a, r)) cannot always be viewed as a step from u to a neighboring crossover. Two crossovers separated by an odd number of noncrossovers will be said to be equivalent. Two equivalent crossovers must have the same sign, so every crossover is adjacent to a crossover that is not equivalent to it. Thus, the relation so defined is an equivalence.relation with two or more equivalence classes, each of which consists of one or two crossovers. For each crossover u in (C,, a), the walk (v,con(a, r)) terminates at the first crossover reached that is not equivalent to u. Let c be the number of equivalence classes of crossovers. Suppose that c > 0, and let C denote the set of crossovers. Consider an order-preserving surjection f: C + Z, such that f(w) = f(x) iff w and x are equivalent crossovers. Let 7 be the signing of

400

MICHAEL

F. BRIDGLAND

C, induced by a; i.e., ~(f( w)) = a( w ) for every w E C. The signed cycle (Cc, T) will be called the cro.ssOuerreduction of (C,, u). For a crossover u in a signed cycle, a directed walk (u, con(oL, r)) corresponds to the step (f( u),cu) in its crossover reduction. The walk and the step have the same direction, and the walk terminates at a crossover u’ such that (f(u), a) = f(O

Define the constant expansion Con(s, r) of a sequence s as follows: to the sequence con(1, r), concatenate the sequence obtained from s via replacement of each term s, with the sequence con(s,, r). Thus, for 1 I m 2 I(s) and 1 I i I (I(s) + l(2r + l), Con(s, r)i :=

1, s rn,

iI2r+l. i=m(2r+l)+jandO
2.2 LEMMA. Let (C,, a) be a signed cycle with at least one crossouer, and fix an integer r > n/2. If each of the walks generated by a sequence s in (C,, T) traverses euev uertex, then every walk in (C,,, a) generated by Con( s, r ) trauerses every uertex. Proof Fix a crossover u in (C,, a). For an appropriate choice of vertices xi, the walk (u, Con(s, r)) is the concatenation of the directed walks (u, con(1, r)), (xO,con(slr r)), . . . , (x,, con(si+i, r)), . . . . Consequently, each vertex xi, being the terminal vertex of a walk generated by con(cu, r), is a crossover, and f (xj) = (f (x,), sj) for each j. Suppose that (x0, s) traverses every vertex of (C,, 7). For each vertex w in C, then, there is a j such that x, is a crossover equivalent to w and the directed walks (xi-l, con(sj, r)) and (Xi, cOn(sj+,, r)) have the same direction. It follows from 2.1 that the concatenation of those two walks traverses xi, as well as all vertices between xi-r and xj, and all vertices between xi and xi + 1; in particular, it traverses w. Since every vertex of (C,, a) either is a crossover or lies between two (possibly identical) crossovers, the assertion follows. q

There is one remaining flaw in the strategy we are pursuing: it may be that all vertices (or most of them) are crossovers, in which case the reduction just obtained is of no value. Even if (C,, a) has few (equivalence classes of) crossovers, (C,, 7) consists entirely of crossovers, no two of which are equivalent, so it would be pointless to repeat the crossover reduction. Alternating sequences also generate directed walks. As long as the sign of each vertex reached is the opposite of its predecessor’s sign, every step must have the same direction as the first step. If there exist consecutive vertices with the same sign, and there are sufficiently many terms in the alternating sequence, then the walk will eventually reach a vertex whose sign is identical to that of its predecessor, and every step thereafter will reverse

UNIVERSAL

TRAVERSAL

SEQUENCES

401

direction. If the final term of the alternating sequence is 1, then the terminal vertex will be the first vertex w on the walk that satisfies the condition a(w - u(w)) = u(w). Such a vertex will be called an arrowhead. Define the sequences alt( cy,r) for cr E { - 1, l} and positive integers r as i=l (yy 2IiI2r+l. i (-l)‘+‘, That is, alt(a, r) consists of the term (Yfollowed by an alternating sequence of length 2r terminating with 1. For an arrowhead u in a signed cycle (C,, a) with n/2 -z r, and OLE { - 1, l}, the walk (u, alt(cu, r)) is directed, and its terminal vertex is the first arrowhead reached (which will be u iff it is the only arrowhead). Let a be the number of arrowheads. Suppose that u > 0, and let J@denote the set of arrowheads. Consider an order-preserving bijection g: &+ Z,, and let + be the signing of C, induced by a; i.e., +(g( w)) = u(w) for all w E ~4. The signed cycle (C,, +) will be called the arrowhead reduction of (C,,, a). For an arrowhead u in a signed cycle, a directed walk (u, alt(u, r)) corresponds to the step (g(u), a) in its arrowhead reduction. The walk and the step have the same direction, and the walk terminates at an arrowhead u’ such that (g(u), a) 1 = g( u’). Define the alternating expansion Alt(s, r) of a sequence s as follows: to the sequence alt(1, r), concatenate the sequence obtained from s via replacement of each term s, with the sequence alt(s,, r). Thus, for 1 I i I (I(s) + 1)(2r + 1) and 1 I m I l(s), alt(a, r)i :=

smy

i<2r+l i = m(2r + 1)

(-l)‘+‘,

i=m(2r+l)+jandl
1, Alt(s, r)i :=

As the proof of the following result is analgous to that of 2.2, it is omitted: 2.3. LEMMA. Let (C,, a) be a signed cycle with at least one arrowhead, and fix an integer r > n/2. Zf each of the walks generated by a sequences in (C,, +) traverses every uertex, then every walk in (C,, u) generated by Alt(s, r) truuersesevery vertex. Although the arrowhead reduction suffers essentially the same deficiency as the crossover reduction (it may not have significantly fewer vertices than the original cycle), it can be combined with crossover reduction to provide a recursive construction of cycle traversal sequences. 2.4 THEOREM. Let t[Ol denote a sequence whose initial two terms are identical. Fix positive integers r, > 2k-’ for k 2 1, and define the sequences

402

MICHAEL

F. BRIDGLAND

tl“] and ul“] for k 2 1 as

ul“] := Alt(ttk-‘I, tIkl

:= con(

dkl,

rk), rk).

For each k 2 1 and each vertex v in a signed cycle with at most 2k vertices and at most 2k-’ arrowheads, the walk (v, uLkl) traverses every vertex. For each k 2 0, the seqirence trk] is a 2k-cycIe traversal sequence. Proof Observe first that the first two steps of a walk generated by tl”] in a signed l-cycle traverse the sole vertex. The remainder of the proof is by induction on k. Fix an integer k 2 1, and set r := rk. Suppose that trk-‘] is a 2k-1-cycle traversal sequence. We will show that utkl and tIk] have the asserted properties. Fix integers n I 2k and a I 2k-1, and a vertex v in a signed cycle (C,, a) having a arrowheads. On the one hand, if a = 0, then the walk generated by the initial 2r + 1 terms of utkl--that is, the walk (v, alt(1, r)) -traverses every vertex in (C,, a). On the other hand, if a > 0, then the fact that each walk in (C,, $B) generated by tLk-‘l traverses every vertex implies that (v, ~1~1) traverses every vertex of (C,, u), by 2.3. Now let v be a vertex in an arbitrary signed cycle (C,, a) with c equivalence classes of crossovers. If c = 0, then the walk generated by the initial 2r + 1 terms of tlkl-- namely, (v, con(1, r)) - traverses every vertex of (C,,, u). Suppose, then, that c > 0. In the crossover reduction (C,, T), there cannot be two consecutive arrowheads with the same sign, since every vertex is a crossover. Therefore, there are at most 2k-1 arrowheads in (C,, T), and every walk generated by u Ikl traverses every vertex. It follows from 2.2 that (v, PI) traverses every vertex of (C,, 6). 0

It is now a straightforward matter to generate an n-cycle traversal sequence by means of the procedures t-term and u-term defined below. It is assumed that each of the global variables v, d, k, ai and ci initially has value 0. pmxdure traverse( n ) begin d := [log nl; k := d;

t-term; whilev#Odobegin

write (v); t-term end end

UNIVERSAL

TRAVERSAL

SEQUENCES

403

procedure z-term besin if k = 0 then if ck = 2 then u := 0 else begin ‘k := Ck ” := 1

+

1;

end else if ck E (0, 2k + 2) then begin u-term; if u # 0 then ck := 1 end else ck := ck + 1 end procedure u-term begin if uk E (0, 2k + 2) then begin k := k - 1; f-term; k := k + 1; if u # 0 then ak := 1 end else begin ak := Uk + 1; 0 := (-lp+l end end

Each call of t-term (respectively, u-term) leaves u with the value tjk] (respectively, ujkl), where i is the number of times the procedure has been called with k having its current value. Since there are neither passed parameters nor local variables, the space requirement is dominated by the global variables. In particular, a, and ci require i + 1 bits each, for 0 s i I [log nl. Consequently, the total space required is O(log* n).

ACKNOWLEDGMENT The author is grateful to Tom Leighton for suggesting the problem addressed here.

MICHAEL

F. BRIDGLAND REFERENCES

R. M. KAKP, R. J. LIPTON, L. LOVASZ, AND C. RACKOFF, Random walks, universal traversal sequences, and the complexity of maze problems, in “Proc. 20th IEEE Symp. on Foundations of Computer Science,” pp. 281-223, 1979. 2. B. BOLLOBAS, “Random Graphs,” Academic Press, London/Orlando, 1986. 3. N. D. JONES, Space-bounded reducibility among combinatorial problems, J. Comput. 1. R. ALELIUNAS,

System 4. N.

Sci. 11 (1975),

D. JONES,

68-75.

Y. E. LIEN,

log space, M&h.

Sy~rems

AND

New problems complete for nondeterministic (1976) 1-17.

W. T. LAASER,

Theoty

10