Automata with Nested Pebbles Capture First-Order Logic with Transitive Closure

String languages recognizable in (deterministic) log-space are characterized either by two-way (deterministic) multi-head automata, or following Immerman, by first-order logic with (deterministic) transitive closure. Here we elaborate this result, and match the number of heads to the arity of the transitive closure. More precisely, first-order logic with k-ary deterministic transitive closure has the same power as deterministic automata walking on their input with k heads, additionally using a finite set of nested pebbles. This result is valid for strings, ordered trees, and in general for families of graphs having a fixed automaton that can be used to traverse the nodes of each of the graphs in the family. Other examples of such families are grids, toruses, and rectangular mazes. For nondeterministic automata, the logic is restricted to positive occurrences of transitive closure. The special case of k=1 for trees, shows that single-head deterministic tree-walking automata with nested pebbles are characterized by first-order logic with unary deterministic transitive closure. This refines our earlier result that placed these automata between first-order and monadic second-order logic on trees.


Introduction
The complexity class DSPACE(log n) of string languages accepted in logarithmic space by deterministic Turing machines, has two well-known distinct characterizations.The first one, see e.g., [30] (or Corollary 3.5 of [32]), is in terms of deterministic two-way automata with several heads working on the input tape (and no additional storage).Second, Immerman [33] showed that these languages can be specified using first-order logic with an additional deterministic transitive closure operator -it is one of the main results in the field of descriptive complexity [16,35].Similar characterizations of NSPACE(log n) hold for their nondeterministic counterparts [33,34,61].
So we have a match between two distinct ways of specifying languages (two-way multihead automata and first-order logic with transitive closure) that both have a natural parameter indicating the relative complexity of the mechanism used.For multi-head automata the parameter is the number of heads used to scan the input; indeed, two heads are more powerful than a single one, as two heads can be used to accept nonregular languages like {a n b n | n ∈ N}, whereas single-head two-way automata only accept regular languages [53,58].In fact, it is known that in general k + 1 heads are better than k even for a singleletter input alphabet [44].For transitive closure logics, the parameter is the arity of the transitive closure operators used: if φ(x, y) denotes a formula that relates two sequences x, y of k variables each, then φ * (x, y) denotes the transitive (and reflexive) closure of φ -we will call this k-ary transitive closure, and it is said to be deterministic if φ determines y as a function of x.Clearly, binary transitive closure is more powerful than unary: {a n b n | n ∈ N} can easily be described in first-order logic with binary deterministic transitive closure, but first-order logic with unary transitive closure defines the regular languages [2,16,52].It seems to be open whether (k + 1)-ary transitive closure is more powerful than k-ary transitive closure, see [29].
In [2], Bargury and Makowsky set out to characterize the formulas that capture the power of automata with k heads, and they found a class of formulas, called k-regular, with this property.Apart from first-order concepts, the formulas only use k-ary transitive closure.They show how k-head automata can be described by k-ary transitive closure (both deterministically and nondeterministically) but for the converse the k-ary regular formulas only work in the nondeterministic case: "the modification of the k-regular formulas needed to take out the nondeterminism will spoil their elegant form, and we do not pursue this further" [2].
Here we set out from the other side.Starting with the full set of deterministic kary transitive closure formulas we want to obtain an equivalent notion of deterministic automata.Indeed, we succeed in doing this, i.e., we have an automata-theoretic characterization of first-order logic with deterministic k-ary transitive closure, but we pay a price.The two-way automaton model we obtain has k heads, as expected, but is augmented with the possibility to put an arbitrary finite number of pebbles on its input tape, to mark positions for further use.If these pebbles can be used at will it is folklore [54,51] that we obtain again DSPACE(log n), a family too large for our purpose.Instead we only allow pebbles that are used in a LIFO (or nested) fashion: all pebbles can be 'seen' by the automaton as usual, but only the last one dropped can be picked up [26,19,43,65,22,49].On the other hand our pebbles are more flexible than the usual ones: they can be 'retrieved from a distance', i.e., a pebble can be picked up even when no head is scanning the position of the pebble (cf. the "abstract markers" of [4]).
In the nondeterministic case we have to restrict ourselves to formulas with positive occurrences of the k-ary transitive closure operator, as we do not know whether the class of languages accepted by nondeterministic k-head two-way automata with nested pebbles is closed under complement.
In fact, our equivalence result (Theorem 5.3) is stated and proved for ranked trees in general, of which strings are a special case.Our automaton model (a tree-walking automaton) visits the nodes of an input tree, moving up and down along the edges of the tree.The moves (and state changes) are determined by the state of the automaton and the label of (and pebbles on) the nodes it visits; additionally we assume that the children of each node are consecutively numbered and that the automaton can distinguish this number.
In Section 4 we translate logical formulas into automata, following [19] and additionally using the technique of Sipser [59] to deterministically search a computation space.Section 5 considers the reverse: translating automata into logical formulas.As in [2] we adapt Kleene's construction to obtain regular formulas from automata, thus getting rid of the states of the automaton (Lemma 5.1), but we need to iterate that construction: once for each nested pebble.
In Section 6 we summarize the results of our paper for single-head automata on trees, the context of our previous papers [19,21] dealing with tree-walking automata and the quest of obtaining simple tree automaton models that are inherently sequential (unlike the classic tree automata) and still capture the full power of regular tree languages.
Finally, in Section 7 we discuss how to extend our results to more general graph-like structures, such as unranked trees (important for XML [43,65,47,37,57]), cycles, grids (as in [2]; important for picture recognition [4,56,25,41,40]), toruses, and, for k ≥ 2, mazes [5,12,31].To have a meaningful notion of graph-walking automaton we only consider graphs with a natural locality condition: a node cannot have two incident edges with the same label and the same direction.Note that unranked trees satisfy this condition when we view them as graphs with 'first child' and 'next sibling' edges in the usual way.Twodimensional grids satisfy it by distinguishing between horizontal and vertical edges.For all such graphs, our result holds in one direction: from automata to logical formulas.The other direction holds for all families of such graphs for which there exists a single-head deterministic graph-walking automaton (with nested pebbles) that can traverse each graph of the family, visiting each node at least once.This includes all families mentioned above (except mazes, for which the automaton has two heads), and also, trivially, the family of 'ordered' graphs, i.e., all graphs in which the successor relation of a total order is determined by edges with a specific label.Note that the existence of such an automaton is needed to implement even simple logical formulas such as 'all nodes have label σ'.
The main result of this paper, but only for trees and k = 1 (cf.Section 6), was first presented at a workshop in Dresden in March 1999, but unfortunately did not make it into the proceedings [66].Muscholl, Samuelides, and Segoufin [45] have 'reconstructed' our missing result, independently obtaining the closure under complementation of the tree languages accepted by deterministic tree-walking automata with nested pebbles, taking special care to minimize the number of pebbles needed.The results of this paper were then presented at STACS 2006 [20].

Preliminaries
Trees.A ranked alphabet is a finite set Σ together with a mapping rank : Σ → N. Terms over Σ are recursively defined: if σ ∈ Σ is of rank n, and t 1 , . . ., t n are terms, then σ(t 1 , . . ., t n ) is a term.In particular σ is a term for each symbol σ of rank 0.
As usual, terms are visualized as trees, which are special labelled graphs; σ(t 1 , . . ., t n ) as a tree which has a root labelled by σ and outgoing edges labelled by 1, . . ., n leading to the roots of trees for t 1 , . . ., t n .The roots of subtrees t 1 , . . ., t n are said to have child number 1, . . ., n, respectively; by default the child number of the root of the full tree equals 0. The set of all trees (terms) over ranked alphabet Σ is denoted by T Σ .
Tree-walking automata.For k ≥ 1, a k-head tree-walking automaton is a finite-state automaton equipped with k heads that walks on an input tree (over a given ranked alphabet Σ) by moving its heads along the edges from node to node 1 .At each moment it determines its next step based on its present state, and the label and child number of the nodes visited.Accordingly, it changes state and, for each of its heads, it stays at the node, or moves either up to the parent of the node, or down to a specified child.If the automaton has no next step, we say it halts.
The language L(A) ⊆ T Σ accepted by the k-head tree-walking automaton A is the set of all trees over Σ on which A has a computation starting with all its heads at the root of the tree in the initial state and halting in an accepting state, again with all heads at the root of the tree.The family of languages accepted by k-head deterministic tree-walking automata is denoted by DW k A, for nondeterministic automata we write NW k A.
Such an automaton is able to make a systematic search of the tree (which can be tuned to be, e.g., a preorder traversal), even using a single head, as follows.When a node is reached for the first time (entering it from above) the automaton continues in the direction of the first child; when a leaf is reached, the automaton goes up again.If a node is reached from below, from a child, it goes down again, to the next child, if that exists; otherwise the automaton continues to the parent of the node.The search ends when the root is entered from its last child.This traversal is often used in constructions in this paper.
In both [48] and [50], as an example, the authors explicitly construct a deterministic 1head tree-walking automaton that evaluates boolean trees, i.e., terms with binary operators 'and' and 'or' and constants 0 and 1.
Again, strings form a special case.Tree-walking automata on monadic trees are equivalent to the usual two-way automata on strings.A tree-walking automaton is able to recognize the root of a tree as well as its leaves (using child number and rank of the symbols).This corresponds to a two-way automaton moving on a tape, where the input string is written with two endmarkers so the automaton knows the beginning and end of its input.
Logic for Trees.For an overview of the theory of first-order and monadic second-order logic on both finite and infinite strings and trees in relation to formal language theory, see [63].
In this paper our primary interest is in first-order logic, describing properties of trees.The logic has node variables x, y, . . ., which for a given tree range over its nodes.There are four types of atomic formulas over Σ: lab σ (x), for every σ ∈ Σ, meaning that x has label σ; edg i (x, y), for every i at most the rank of a symbol in Σ, meaning that the i-th child of x is y; x ≤ y, meaning that x is an ancestor of y; and x = y, with obvious meaning.The formulas are built from the atomic formulas using the connectives ¬, ∧, and ∨, as usual; variables can be quantified with ∃ and ∀.
If t is a tree over Σ, φ is a formula over Σ such that its free variables are x 1 , . . ., x n , and u 1 , . . ., u n are nodes of t, then we write t |= φ(u 1 , . . ., u n ) if formula φ holds for t where the free x i are valuated as u i .
For fixed k ≥ 1, by overlined symbols like x we denote k-tuples of objects of the type referred to by x, like logical variables, nodes in a tree, or pebbles used by an automaton.By x[i] we then denote the i-th component of x.
We consider the additional operator of k-ary transitive closure.Let φ(x, y) be a formula where x, y are k-tuples of distinct variables occurring free in φ.We use φ * (x, y) to denote the transitive closure of φ with respect to x, y.Informally, φ * (x, y) means that we can make a series of jumps from k-tuple x to k-tuple y such that each pair of consecutive k-tuples x ′ , y ′ connected by a jump satisfies φ(x ′ , y ′ ).More formally, let φ have 2k + m free variables x, y, z 1 , . . ., z m .For tree t and nodes u, v, w 1 , . . ., w m of t we have t |= φ * (u, v, w 1 , . . ., w m ) if there exists a sequence of k-tuples of nodes u 0 , u 1 , . . ., u n , n ≥ 0, such that u = u 0 , v = u n , and t |= φ(u i , u i+1 , w 1 , . . ., w m ) for each 0 ≤ i < n.In particular, t |= φ * (u, u, w 1 , . . ., w m ) for every k-tuple u of nodes of t.
Formally we should specify the k-tuples x, y, or rather x ′ , y ′ , of free variables with respect to which to take the transitive closure, like for the usual universal and existential quantification, and write (tc(x ′ , y ′ )φ(x ′ , y ′ ))(x, y) instead of φ * (x, y).
A predicate φ(x, y) with free variables x, y is functional (in x, y) if for every tree t and k-tuple of nodes u there is at most one k-tuple v such that t |= φ(u, v).If φ has more free variables than x, y, this should hold for each fixed valuation of those variables.The transitive closure φ * (x, y) is deterministic if φ is functional (in the variables with respect to which the transitive closure is taken).Instead of requiring φ to be functional we could, equivalently, require it to be of the form ψ(x, y) ∧ ∀z(ψ(x, z) → y = z).This has the advantage of being a decidable property, but it is less convenient in proofs.
The tree language defined by a closed formula φ over Σ consists of all trees t in T Σ such that t |= φ.The family of all tree languages that are first-order definable is denoted by FO; if one additionally allows k-ary transitive closure or deterministic transitive closure we have the families FO+TC k and FO+DTC k , respectively.General transitive closure and deterministic transitive closure (i.e., over unbounded values of k) characterize the complexity classes NSPACE(log n) and DSPACE(log n) (for strings, or more generally, ordered structures), respectively, see [16,35].
By LFO we denote the family of tree languages definable in local first-order logic, i.e., dropping the atomic formula x ≤ y.One should note however, that x ≤ y is the transitive closure of the functional predicate 'x parent of y', i.e., i edg i (x, y).Hence x ≤ y is expressible in LFO+DTC 1 , and the families FO+DTC k , etc., of tree languages definable in first-order logic with transitive closure, do not change by this restriction.
In Section 6 we study the specific case k = 1, i.e., we consider unary transitive closure only.The family FO+TC 1 is included in the family of regular tree languages, i.e., the tree languages that are definable in monadic second-order logic, which additionally has node set variables X, Y, . . ., ranging over sets of nodes of the tree; it allows quantification over these variables, and has the predicate x ∈ X, with its obvious meaning.
Example 2.1.It is proved in [8] that there is a regular tree language T that cannot be accepted by any single-head nondeterministic tree-walking automaton.Here we illustrate how to construct an FO+DTC 1 formula for that language.
Let Σ = {a, b, c}, where a and b are nullary (labelling, of course, the leaves of the trees over Σ) and c is binary (labelling the internal nodes).The language T consists of all trees over Σ for which the path to each leaf labelled by a contains an even number of 'branching' nodes, i.e., internal nodes for which both the left and right subtree contain an a-labelled leaf.
It is easy to construct a first-order formula expressing that a node is branching.Let ψ(x, y) specify that y is the lowest branching ancestor of x, and let φ(x, y) ≡ (∃z)(ψ(x, z) ∧ ψ(z, y)) to claim y is the second lowest branching ancestor of x.Observe that φ is functional.Now T is specified by the FO+DTC 1 formula (∀x)(lab a (x) → (∃y)[ φ * (x, y) ∧ ¬(∃z)ψ(y, z) ] ).
In fact T even belongs to FO, as observed in [8], but earlier in [52,Lemma 5.1.8].

Tree-Walking Automata with Nested Pebbles
A k-head tree-walking automaton with nested pebbles is a k-head tree-walking automaton that is additionally equipped with a finite set of pebbles.During the computation it may drop these pebbles (one by one) on nodes visited by its heads, to mark specific positions.It may test the currently visited nodes to see which pebbles are present.Moreover, it may retrieve a pebble from anywhere in the tree, provided the life times of the pebbles are nested.This can be formalized by keeping a (bounded) stack in the configuration of the automaton, pushing and popping pebbles when they are dropped and retrieved, respectively.Note that since this stack is bounded by the number of pebbles, it can also be kept in the finite control of the automaton. 2Pebbles can be reused any number of times (but there is only one copy of each pebble).Accepting computations should start and end with all heads at the root without pebbles on the input tree.
The family of tree languages accepted by deterministic k-head tree-walking automata with nested pebbles is denoted by DPW k A, the nondeterministic variant by NPW k A.
Some specific properties of these pebbles must be stressed.First, as stated above, pebbles are used in a lifo manner, as in [26,19,43,65,22,49], which means that only the last one dropped can be retrieved, and thus their life times on the tree are nested.Without this restriction again the classes DSPACE(log n) and NSPACE(log n) would be obtained.Second, this is rather nonstandard, the automaton need not return one of its heads to the position where a pebble was dropped in order to pick it up: at any moment the last pebble dropped can be retrieved.This means that the pebble behaves as a pointer : we can store the address of a node when we know it (which is the case when we visit it) and we can later wipe the address from memory without the need to return to the node itself.Such pebbles were called "abstract markers" in [4] (to distinguish them from the usual "physical markers").Finally, as opposed to [26], during the computation all pebbles dropped remain visible to the automaton (and not only the one or two on top of the stack).
Example 3.1.The regular tree language T from Example 2.1 cannot be accepted by any single-head nondeterministic tree-walking automaton (without pebbles), as proved in [8].As an example, here we show how to accept T by a (single-head) deterministic tree-walking automaton with two nested pebbles.
Using a preorder traversal of the input tree, the first pebble is placed consecutively on leaves labelled by a.For each such position, starting at the leaf we follow the path upwards to the root counting the number of branching nodes.To test whether an internal node is branching we place the second pebble on the node and test whether its other subtree, i.e., the subtree that does not contain the first pebble, contains an a-labelled leaf (using again a preorder traversal of that subtree, the root of which can be recognized through the second pebble which marks the parent of that root).After testing the node, we pick up the second pebble.At the root, we reject whenever we count an odd number of such nodes on a path; otherwise we return to the position of the first pebble (using another preorder traversal of the tree).
2 If the automaton uses pebbles x1, . . ., xn, then the contents of the stack can be any string over {x1, . . ., xn} in which each xi occurs at most once.It can be assumed w.l.o.g. that the stack always contains x1x2 • • • xi for some i, but we will do this only in the proof of Lemma 5.2 (where, in fact, the order is reversed).
In fact, the language can be accepted with just one pebble3 (which is nested trivially).As explained above, the pebble can be used to detect all branching nodes, which, together with all a-labelled leaves, can be viewed as a binary tree.To check, for that tree, that the path to each leaf is of even length, the automaton performs a preorder traversal and counts its number of steps, modulo 2. At each leaf the count should be 0.
To fix the model, a k-head tree-walking pebble automaton is specified as a tuple A = (Q, Σ, X, q 0 , A, I), where Q is a finite set of states, Σ is a (ranked) input alphabet, X a finite set of pebbles, q 0 ∈ Q the initial state, A ⊆ Q the set of accepting states, and I the finite set of instructions.
Each instruction is a triple of the form p, ψ, q or p, ϕ, q or p, ∼ϕ, q , where p, q ∈ Q are states, ψ is an operation, and ϕ a test.There are four types of operations: up i , down i,j (moves), drop i (x), and retrieve(x) (pebble operations), and three types of tests: lab i,σ , peb i (x), and chno i,j , where in each case i indicates a head (1 and x ∈ X is a pebble.An instruction p, χ, q is called an outgoing instruction of state p.
The automaton A is deterministic if for any pair p, χ 1 , q 1 , p, χ 2 , q 2 of distinct instructions starting in the same state, either A configuration of A on tree t over Σ is a triple [p, u, α], where p ∈ Q is a state, u is a k-tuple of nodes of t indicating the positions of the k heads, and α = (x 1 , w 1 ) • • • (x m , w m ) the stack of pebbles dropped at their positions (m ≥ 0, x j ∈ X, w j a node of t).The initial configuration equals [q 0 , root, ε], where root consists of k copies of the root of t, and ε is the empty stack.
The semantics of the pebble automaton is defined using the step relation ⊢ A,t on configurations for automaton A on input tree t.We have or in case of the negative tests, χ = ∼ lab i,σ , ∼ peb i (x), ∼ chno i,j , the tests above are negated, whereas head positions and pebble stack remain unchanged (u = v, α = β).
A configuration c is halting if there is no c ′ such that c ⊢ A,t c ′ , and it is accepting if it is halting and c = [p, root, ε] for some p ∈ A. Then the language accepted by A is defined as A,t c for some accepting configuration c }. Example 3.2.Let Σ be as in Example 2.1.We write, in our formalism, a deterministic single-head tree-walking automaton A (without pebbles) such that L(A) consists of all trees over Σ that have a-labelled leaves only.The automaton performs a preorder traversal of the input tree.The main states are as follows.In state 1 we move down to the left child until we reach a leaf, in state 2 we are at a left child and move to its right sibling, and in state 3 we move up until we are at a left child or at the root.
As the automaton has only a single head, we omit the head number in the instructions.The initial state is 1, the only accepting state is h.The automaton has the following instructions: ( up, 2 ′ ), (2 ′ , down 2 , 1).

From Logic to Nested Pebbles
We now generalize the inclusion FO ⊆ DPW 1 A from Section 5 of [19], on the one side introducing k-ary transitive closure, on the other side allowing k heads.Note also the result is for the 'pointer' variant of pebbles, rather than pebbles that have to be picked up where they were dropped.Lemma 4.1.For trees over a ranked alphabet, and Proof.The proof is by induction on the structure of the formula.For each first-order formula with deterministic transitive closure we construct a deterministic tree-walking automaton with nested pebbles that always halts (with all its heads at the root).The additional effort we have to take to make it always halting, will pay itself back when we deal with negation, but is also helpful when considering disjunction and existential quantification.Generally speaking, each variable of the formula acts as a pebble for the automaton.In case of k-ary transitive closure we need 3k pebbles to test the formula by an automaton.Most features can be simulated using a single head, moving pebbles around the tree, only for transitive closure we need all the k heads.
As intermediate formulas may have free variables we need to extend our notion of recognizing a tree by an automaton: a valuation of the free variables is fixed by putting pebbles on the tree, one for each variable, and the automaton should evaluate the formula according to this valuation.
More formally, let φ = φ(x 1 , . . ., x n ) be a formula with free variables x 1 , . . ., x n .The automaton A for φ should check whether t |= φ(u 1 , . . ., u n ) for nodes u 1 , . . ., u n in a tree t, as follows.It is started in the initial state with all heads at the root of the tree t, where u 1 , . . ., u n are marked with pebbles x 1 , . . ., x n .During the computation A may use additional pebbles (in a nested fashion) and it may test x 1 , . . ., x n , but it is not allowed to retrieve them.The computation should halt again with all heads at the root of t with the original configuration of pebbles.The halting state is accepting if and only if t |= φ(u 1 , . . ., u n ).
For the atomic formulas it is straightforward to construct (single-head) automata.As an example, for φ = x ≤ y the automaton searches the tree for a marked node representing y.From that position the automaton walks upwards to the root, where it halts, signalling whether x was found on the path from y to the root.For edg i (x, y) the automaton searches for x, then determines whether x has an i-th child (the arity of the node can be seen from its label) and moves to that child.There the automaton checks whether pebble y is present.
For the negation φ = ¬φ 1 of a formula we use the original automaton for φ 1 , but change its accepting states to the complementary set.This construction works thanks to the fact that the automata we build are always halting.
A similar argument works for the conjunction φ = φ 1 ∧φ 2 , or the disjunction φ = φ 1 ∨φ 2 , of two formulas.We may run the two automata constructed for the two constituents consecutively.Note that the free variables in φ 1 and φ 2 need not be the same, but the extra pebbles that need to be present for φ are ignored by each of the two automata.
For quantification φ = (∀x)φ 1 the automaton makes a systematic traversal through the tree, using a single head.When it reaches a node (for the first time) it places a pebble x at that position.Then it returns to the root, and runs the automaton for φ 1 as a subroutine; the free variable x of φ 1 is marked by the pebble, as requested by the inductive hypothesis.When this test for φ 1 (x) is positive, i.e., the subroutine halts at the root in an accepting state, the automaton returns to the node marked x (by searching for it in the tree), picks up the pebble, and places it on the next node of the traversal.When the automaton has successfully run the test for φ 1 for each node it accepts.The formula (∃x)φ 1 is treated similarly.
The main new element of this proof compared to [19] is the introduction of k-ary transitive closure.Here we need to program a walk from one k-tuple of nodes to another ktuple with 'jumps' specified by a 2k-ary formula.We cannot do this in a straightforward way, as we might end 'jumping around' in a cycle without noticing.Such an infinite computation violates the requirement that our automaton should always halt.We use a variant of the technique of Sipser [59] to avoid this trap, and run this walk backwards.
So, let φ = φ * 1 be the transitive closure of a functional 2k-ary predicate φ 1 .Given a tree with 2k nodes marked by pebbles x and y we have to construct an automaton A that decides whether we can connect the k-tuples x and y by a series of intermediate k-tuples such that φ 1 holds for each consecutive pair.In what follows we assume that φ 1 has 2k free variables x and y (with respect to which the transitive closure is taken), and we disregard the remaining free variables of φ 1 (the values of which are fixed by pebbles).Now consider the set of k-tuples of nodes of the input tree t, spanning the (virtual) computation space of A. We build a directed graph on these k-tuples by connecting vertex4 u to vertex v if t |= φ 1 (u, v), i.e., the pair (u, v) in t satisfies φ 1 (x, y).As φ 1 is functional, for each vertex there is at most one outgoing arc.Thus, if we fix a vertex v (of k nodes in t) and throw away the outgoing arc of v (if it exists) the component of vertices connected to v in this graph forms a tree t k (v), with arcs pointing towards the root v rather than towards the leaves.Note this is a directed tree in the graph-theoretical sense; there is no bound on the number of arcs incident to each vertex.
Of course, this tree t k (v) with φ 1 -arcs consists of all vertices u that satisfy t |= φ(u, v), and fixing v to be the vertex marked by the pebbles y the new automaton A traverses that tree t k (v) and tries to find the vertex marked by pebbles x.
However, the tree t k (v) is not explicitly available, and has to be reconstructed while walking on the input tree t, using the automaton A 1 for φ 1 as a subroutine.In particular, we want to implement a traversal on t k (v).As the vertices of t k (v) consist of k-tuples of nodes of t, we order these k-tuples in a natural way using the lexicographical ordering based on the preorder in t.In this way we impose an ordering on the children of each vertex of t k (v), thus allowing the usual preorder traversal of t k (v) as described below.To find the successor of a k-tuple z in the lexicographical ordering we act like adding one to a k-ary number: change the last coordinate of the tuple z into its successor (here the preorder successor in t) if that exists, otherwise reset that coordinate to the first element (here the root of t), and consider the last-but-one coordinate, etc.
We implement a preorder traversal of t k (v), which means we compute the preorder successor of each vertex of the tree t k (v) whose arcs are defined by φ 1 , and where the ordering between sibling vertices is based on the lexicographical ordering of nodes in t: if it exists, the first child of u, else, on the path of u to the root v, the right sibling of the first vertex that has one.
We traverse the tree t k (v), with 2k pebbles x and y fixed, with the help of 3k additional pebbles x ′ , y ′ , and z ′ .During this traversal, A keeps track of the current vertex of t k (v) with its k heads.Initially the heads move to y, i.e., to v. Note that the order of dropping the pebbles x ′ and y ′ differs in the two cases below: in the first case we have to check φ 1 (x ′ , y ′ ) 'backwards', finding x ′ given y ′ , while in the second case it is the other way around.This is reflected in the order of dropping x ′ and y ′ .
First, we describe how to check whether the current vertex has a first child in t k (v), and to go there if it exists.We drop pebbles y ′ to fix the current vertex, and we systematically place pebbles x ′ on each candidate vertex, i.e., each k-tuple of nodes of the tree t (except v).Thus, lexicographically, in each step the last pebble of x ′ is carried to the next node in t (with respect to the preorder in t), but when that pebble has been at all nodes, it is lifted, the last-but-one pebble is moved to its successor node in t, and the last pebble is replaced on the root, etc.For each k-tuple x ′ we check φ 1 (x ′ , y ′ ) using automaton A 1 as a subroutine.If the formula is true, we have found the first child in t k (v) and we move the k heads to the nodes marked by x ′ , lift pebbles x ′ , and retrieve pebbles y ′ (from a distance).If the formula is not true, we move x ′ to the next candidate vertex as described above (but v is disregarded).If none of the candidates x ′ satisfies φ 1 (x ′ , y ′ ), the vertex y ′ obviously has no child in t k (v).
Second, we describe how to check for a right sibling in t k (v), and go there if it exists, or go up (to the parent of the current vertex) otherwise.The problem here is to keep the pebbles in the right order, adhering to the nesting of the pebbles.First drop pebbles x ′ on the current vertex.Then determine its parent in t k (v); this is the unique vertex that satisfies φ 1 (x ′ , y ′ ), where y ′ marks the parent vertex of x ′ , thanks to the functionality of φ 1 .It can be found in a traversal of all k-tuples of nodes of t using pebbles y ′ and subroutine A 1 (as described above for the first child).Leave y ′ on the parent and return to x ′ (by searching for x ′ in the tree t).Using the third set of k pebbles z ′ , traverse the k-tuples of nodes of t from x ′ onwards and try to find the next k-tuple that satisfies φ 1 (z ′ , y ′ ) when z ′ is dropped.If it is found, it is the right sibling of x ′ .Return there, lift z ′ , and retrieve y ′ and x ′ .If no such k-tuple is found, the current vertex has no right sibling, and we go up in the tree t k (v), i.e., we return to y ′ .Here we lift y ′ and retrieve x ′ .
In all these considerations special care has to be taken of the root v.It has no parent in t k (v).Fortunately v is clearly marked by pebbles y.
The number of pebbles needed to compute a formula of FO+DTC k according to the construction above depends only on the nesting of quantifiers and transitive closures in the formula.For each quantifier we count a single pebble, and 3k for transitive closure, and compute the maximum needed over all sequences of nested operators in the formula.
When allowing transitive closure of arbitrary formulas (not requiring them to be functional) it is customary to restrict attention to formulas with only positive occurrences of transitive closure, i.e., within the scope of an even number of negations (see, e.g., [16,35]).Using standard argumentation each such formula is equivalent to one where negation is applied to atomic formulas only.
For such formulas there is a similar, nondeterministic, result as the one above.Atomic formulas and their negations are treated as above, and so are conjunction and universal quantification.For disjunction and existential quantification, the automaton uses nondeterminism in the obvious way.For transitive closure, the Sipser technique we have used in the previous proof is not needed.For a formula φ = φ * 1 the automaton A checks nondeterministically the existence of a path u 0 , u 1 , . . ., u n from vertex x to vertex y in the directed graph determined by φ 1 (described in the proof of Lemma 4.1).When A is at vertex u i , it proceeds to vertex u i+1 using 2k additional pebbles x ′ and y ′ , as follows.It drops x ′ on the current nodes u i and nondeterministically chooses nodes u i+1 , where it drops y ′ and checks that φ 1 (x ′ , y ′ ).Then it returns to y ′ , lifts y ′ , and retrieves x ′ .
We denote the positive restriction of FO+TC k by FO+posTC k , and similarly for the deterministic case.Thus, for trees over a ranked alphabet, nondeterministic k-head treewalking automata can compute positive k-ary transitive closure: FO+posTC k ⊆ NPW k A.

From Nested Pebbles to Logic
The classical result of Kleene [38] shows how to transform a finite-state automaton into a regular expression, which basically means that we have a way to dispose of the states of the automaton.Bargury and Makowsky [2] observe that this technique can also be used to transform multi-head automata walking on grids into equivalent formulas with transitive closure: transitive closure may very well specify sequences of consecutive positions on the input, but has no direct means to store states.A similar technique is used here.As our model includes pebbles, this imposes an additional problem, which we solve by iterating the construction for each pebble.Unlike [2] we have managed to find a formulation that works well for both the nondeterministic and deterministic case.
Given a (deterministic) computational finite-state device with k heads on the tree, the step relation of which is specified by logical formulas, we show that the computation relation that iterates consecutive steps can be expressed using k-ary (deterministic) transitive closure.Of course, the consecutive positions of the heads along the tree are well taken care of by the closure operator, but here we additionally require that the states of the device should match the sequence of steps.
Let Φ be a Q × Q matrix of predicates φ p,q (x, y), p, q ∈ Q for some finite set Q (of states), where x, y each are k distinct variables occurring free in all φ p,q .We define the computation closure of Φ with respect to x, y as the matrix Φ # consisting of predicates φ # p,q (x, y) where t |= φ # p,q (u, v) iff there exists a sequence of k-tuples of nodes u 0 , u 1 , . . ., u n and a sequence of states p 0 , p 1 , . . ., p n , n ≥ 1, such that u = u 0 , v = u n , p = p 0 , q = p n , where t |= φ p i ,p i+1 (u i , u i+1 ) for 0 ≤ i < n. ( 5 ) 5 Note that, to simplify the description of computation closure we have disregarded the remaining free variables of the φp,q and φ # p,q .More precisely, if z1, . . ., zm are all the free variables of all φp,q (in addition to x, y), then each φ # p,q has free variables x, y, z1, . . ., zm.In the definition of t |= φ # p,q (u, v, w1, . . ., wm) the z1, . . ., zm have fixed values w1, . . ., wm.
Intuitively t |= φ # p,q (u, v) means that there is a Φ-path of consecutive steps (as specified by matrix Φ) leading from nodes u in state p to nodes v in state q.Note that only nonempty paths are considered (n ≥ 1).
We say that Φ is deterministic if its predicates are both functional and exclusive, i.e., for any p, q, q ′ ∈ Q and 3k nodes u, v, v ′ of any tree t, if both t |= φ p,q (u, v) and t |= φ p,q ′ (u, v ′ ) then q = q ′ and v = v ′ .Moreover, Φ is said to be semi-deterministic if the previous requirement holds for final states q, q ′ only, where q is final if φ q,r is false for all r ∈ Q (and similarly for q ′ ).
Lemma 5.1. ( Proof. 1.Let q, q ′ ∈ Q be final, and assume that for tree t both φ # p,q (u, v) and φ # p,q ′ (u, v ′ ) hold for p ∈ Q and 3k nodes u, v, v ′ of t, with Φ-paths of length n and n ′ as in the definition of computation closure (n, n ′ ≥ 1).
Consider the first steps of both paths.We have φ p,p 1 (u, u 1 ) and φ p,p ′ 1 (u, u ′ 1 ), as well as Due to the determinism of Φ we conclude ) is false.Hence n ′ = 1 and v ′ = u 1 = v and q ′ = p 1 = q as required.For n, n ′ ≥ 2 we continue inductively with p 1 and u 1 .

2.
The proof is a logical interpretation of the method of McNaugton and Yamada [42].Without loss of generality we assume that Q = {1, 2, . . ., m}.We show by induction on ℓ how to construct a matrix Φ (ℓ) of formulas φ (ℓ) p,q in FO+TC k which are defined as φ # p,q , except that the intermediate states p 1 , . . ., p n−1 are chosen from {1, . . ., ℓ}.In particular, for ℓ = 0 no intermediate states are allowed, whereas for ℓ = m all states are allowed, so we have Φ (m) = Φ # .
For ℓ = 0, the length of the path is one.This means that Φ (0) = Φ.Given Φ (ℓ) we obtain Φ (ℓ+1) as follows.Assume φ (ℓ+1) p,q (x, y) holds.Either there exists a Φ-path that does not visit state ℓ + 1 (i.e., p i = ℓ + 1 for all 0 < i < n to be precise), or this state is visited one or more times during the path.In the former case φ (ℓ) p,q (x, y) holds, in the latter case we have a path from state p to state ℓ + 1, perhaps looping several times from ℓ + 1 back to itself, and finally there is a path from state ℓ + 1 to state q.Neither of these paths contains ℓ + 1 as intermediate state, so in this case φ (ℓ+1) p,q (x, y) postulates the existence of intermediate nodes x ′ and y ′ such that ℓ+1,q (y ′ , y).

3.
In the previous part of the proof transitive closure was applied to predicates φ (ℓ) ℓ+1,ℓ+1 .However, determinism of Φ entails functionality of predicates of the form φ (ℓ) r,ℓ+1 , by an argument analogous to the one in 1. above.Note that state ℓ + 1 need not be final, but the paths to state ℓ + 1 cannot be extended because (by definition of Φ (ℓ) ) state ℓ + 1 cannot be visited intermediately.Hence, each transitive closure is applied to a functional predicate, i.e., it is a deterministic transitive closure.Proof.Consider a tree-walking pebble automaton with k heads.We assume that (1) accepting states have no outgoing instructions (i.e., if p, χ, q is an instruction, then p is not accepting), (2) the initial state is not accepting, and (3) if there is an instruction (p, drop i (x), q), then there is no instruction (q, retrieve(x), r).The latter two requirements are to ensure that accepting computations, and computations between dropping and retrieving a pebble, are nonempty, allowing the use of Lemma 5.1.
Let the automaton use n pebbles, x n , . . ., x 1 , where pebbles are placed on the tree in the order given, i.e., x n is always placed on the bottom of the pebble stack.We view the automaton as consisting of n+1 'levels' A n , . . ., A 1 , A 0 such that A ℓ is a k-head tree-walking pebble automaton with ℓ pebbles x ℓ , . . ., x 1 , available for dropping and retrieving, whereas pebbles x n , . . ., x ℓ+1 have a fixed position on the tree and the automaton A ℓ may test for their presence.Basically, A ℓ acts as a tree-walking automaton that drops pebble x ℓ , then queries pebble automaton A ℓ−1 with ℓ − 1 pebbles where to go in the tree, moves there, and retrieves pebble x ℓ (from a distance).
We postulate that the number of pebbles dropped is kept in the finite control of the automaton, so we can unambiguously partition the state set as where Q ℓ consists of states where ℓ pebbles are still available.The set Q n contains both initial and accepting states.Automaton A ℓ equals the restriction of the automaton to the states in Q ℓ ; we will not specify initial and accepting states for A ℓ , ℓ < n.
We show how to express the computations of automaton A ℓ , ℓ ≥ 0, on the input tree as FO+DTC k formulas, provided we know how to express computations of automaton A ℓ−1 if ℓ ≥ 1.For A ℓ a matrix Φ (ℓ) is constructed with predicates φ (ℓ) p,q for p, q ∈ Q ℓ .These predicates represent the single steps of A ℓ , so t |= φ (ℓ)# p,q (u, v) iff A ℓ has a nonempty computation from configuration [p, u, α] to configuration [q, v, α].Note that Φ (ℓ) has additional free variables x n , . . ., x ℓ+1 that will hold the positions of the pebbles already placed on the tree, thus representing the pebble stack α.
We first study the steps while the pebble x ℓ has not been dropped.For each of its heads, automaton A ℓ may test the presence of one of the pebbles x n , . . ., x ℓ+1 , or the node label or the child number of the current node, or it may move the head up to the parent or down to a specified child.The semantics of these separate instructions, relations between the current and next configurations [p, u, α] and [q, v, α], are easily expressed in first-order logic.So, we have the following translation table: or in case of the negative tests ∼ lab i,σ , ∼ peb i (x), and ∼ chno i,j , the tests above are negated, whereas head positions remain unchanged, e.g., for p, ∼ lab i,σ , q the formula is In general φ (ℓ) p,q (u, v) is a disjunction of such formulas, as we may have parallel instructions in the automaton.
Additionally when ℓ ≥ 1, A ℓ may drop pebble x ℓ in state p, simulate A ℓ−1 , and retrieve pebble x ℓ returning to state q.Such a 'macro step' from configuration [p, u, α] to [q, v, α] is only possible when there is a pair of pebble instructions (p, drop i (x ℓ ), p ′ ) and (q ′ , retrieve(x ℓ ), q), such that A ℓ−1 has a (nonempty) computation from [p ′ , u, α ′ ] to [q ′ , v, α ′ ], with α ′ = α(x ℓ , u[i]).Hence, A ℓ can take a 'step' from [p, u, α] to [q, v, α] if the disjunction of φ (ℓ−1)# p ′ ,q ′ (u, v) over all such q ′ holds, where the free variable x ℓ in that formula is replaced by u[i], the current position of the i-th head of the automaton, i.e., the position at which that pebble is dropped.Note that in A ℓ−1 , q ′ has no outgoing instructions (and hence q ′ is a final state of Φ (ℓ−1)# ).
Defining the remaining φ (ℓ) p,q to be false, we obtain a step matrix Φ (ℓ) , which is deterministic thanks to the determinism of the automaton and the semi-determinism of Φ (ℓ−1)# , cf.Lemma 5.1 (1).It is in FO+DTC k by Lemma 5.1(3).The computational behaviour of the automaton A ℓ is expressed by Φ (ℓ)# , in general, and more specifically for A n , by the disjunction of all formulas φ (n)# p,q (root, root) with p the initial state and q an accepting state.Note that the last formula is correct by assumption (1) in the beginning of this proof.
Combining the two inclusions in Lemma 4.1 and Lemma 5.2, we immediately get the main result of this paper.Note that it includes the case of strings.As a corollary we may transfer two obvious closure properties of FO+DTC k , closure under complement and union, to deterministic tree-walking automata with nested pebbles, where the result is nontrivial.These properties are a rather direct consequence of the always-halting normal form in the proof of Lemma 4.1, which can be obtained for every deterministic automaton.For k = 1, this normal form is further studied with regard to the number of pebbles needed in [45,9].Corollary 5.4.Let k ≥ 1.For each deterministic k-head tree-walking automaton with nested pebbles we can construct an equivalent one that always halts.
When the tree-walking automaton is not deterministic we no longer can assure the determinism of the formulas Φ (ℓ) in the proof of Lemma 5.2.However, by Lemma 5.1(2) they are in FO+TC k .Theorem 5.5.For trees over a ranked alphabet, and The constructions in the proof of Lemma 5.2 use negation in one place only: it is used on atomic predicates, to model negative tests of the automaton (to check there is no specific pebble on a node).Note that negation is not used to construct the formulas in the proof of Lemma 5.1.Hence we obtain positive formulas, where negation is only used for atomic predicates, and thus the inclusions DPW k A ⊆ FO+posDTC k and NPW k A ⊆ FO+posTC k .In the deterministic case negation of a transitive closure can (for finite structures) be easily expressed without the negation [28], thus in a positive way: With that knowledge the first inclusion is not surprising; for the nondeterministic case we additionally find a new, positive, characterization (cf. the end of Section 4).
Corollary 5.6.For trees over a ranked alphabet, and k ≥ 1, As observed in the Introduction, we do not know whether NPW k A is closed under complement (i.e., whether 'pos' can be dropped from Corollary 5.6).Using the method of [34,61], it is easy to see that, for trees, k∈N NPW k A is closed under complement, which means it is equal to k∈N FO+TC k (and note that it also equals k∈N NW k A).

Single Head on Trees
More than thirty years ago, single-head tree-walking automata (with output) were introduced as a device for syntax-directed translation [1] (see [23]).Quite recently they came into fashion again as a model for translation of XML specifications [43,65,47,37,57,10].
The control of a single-head tree-walking automaton is at a single node of the input tree.Thus it differs from the more commonly known tree automata.These latter automata work either in a top-down or in a bottom-up fashion and are inherently parallel in the sense that the control is split or fused for every branching of the tree.
The power of the classic tree automaton model is well known.It accepts the regular tree languages (both top-down or bottom-up), although the deterministic top-down variant is less powerful.For tree-walking automata however, the situation was unclear for a long time.They accept regular tree languages only [36,23], but it was conjectured in [18] (and later in [21,19,10]) that tree-walking automata cannot accept all regular tree languages 6 .This was first proved for 'one-visit' automata (for the deterministic case in [6,50], and for the nondeterministic case in [48]).Recently the conjecture was proved, in a very elegant way, for deterministic tree-walking automata in [7], and for nondeterministic tree-walking automata in [8] (see Examples 2.1 and 3.1).
The reason that tree-walking automata cannot fully evaluate trees like bottom-up tree automata is that they easily loose their way.When evaluating a subtree it is in general hard to know when the evaluation has returned to the root of the subtree.In order to facilitate this, in [19] the single-head tree-walking automaton was equipped with pebbles.This was motivated by the ability of pebbles to help finite-state automata find their way out of mazes [5].
In [19] we have shown that all first-order definable tree languages can be accepted by single-head (deterministic) tree-walking automata with nested pebbles, and that tree languages accepted by single-head (nondeterministic) tree-walking automata with nested pebbles are all regular.
As observed before, DSPACE(log n) is the class of languages accepted by single-head two-way automata with (nonnested) pebbles [54,51].Thus, for k = 1 (single-head automata vs. unary transitive closure), our main characterization for tree languages, Theorem 5.3, can be seen as a 'regular' restriction of the result of Immerman characterizing DSPACE(log n); on the one hand only (single-head) automata with nested pebbles are allowed, while on the other hand we consider only unary transitive closure, i.e., transitive closure for φ(x, y) where x, y are single variables.Note that unary transitive closure can be simulated in monadic second-order logic, which defines the regular tree languages.
We compare the family of tree languages FO+DTC 1 = DPW 1 A with several next of kin.In the diagram below we have five families of languages xW 1 A accepted by (single-head) tree-walking automata, which are either deterministic, nondeterministic, or alternating (D, [19] [7] [7] ? [8] ?? N, or A in x), and may use nested pebbles in case x contains P. Lines without question mark denote proper inclusion, those with question mark just inclusion.The inclusion LFO ⊆ DW 1 A was shown in [19], as well as FO ⊆ DPW 1 A. The regular language (aa) * cannot be defined in first-order logic, and shows that DW 1 A ⊆ FO.The strictness of DW 1 A ⊂ NW 1 A was shown in [7]; their example additionally shows that FO ⊆ DW 1 A. The result of [8] shows even that FO ⊆ NW 1 A, cf.Example 2.1.Logical characterizations of DW 1 A and NW 1 A are given in [48], also using transitive closure (but with an additional predicate indicating the level of a node modulo some constant).All families considered here are contained in the family REG of regular tree languages that can be characterized by monadic second-order logic MSO [15,62].The inclusions FO+DTC 1 ⊆ FO+posTC 1 ⊆ FO+TC 1 ⊆ MSO are obvious.In [52] several logics for regular tree languages are studied; it is stated as an open problem whether all regular tree languages can be defined using monadic transitive closure, i.e., whether FO+TC 1 = MSO.
Alternating tree-walking automata are considered in [60].Alternation combines nondeterminism (requiring a successful continuation from a given state) with its dual (requiring all continuations to be successful).It is not difficult to see that a (nondeterministic) topdown tree automaton can be simulated by an alternating tree-walking automaton, but the reverse inclusion is nontrivial: If, instead of with pebbles, single-head tree-walking automata are equipped with a synchronized pushdown or, equivalently, with 'marbles', then they do recognize all regular tree languages [36,23,21], both in the deterministic and nondeterministic case.Synchronization means that the automaton can push or pop one symbol when it moves from a parent to a child or vice versa, respectively.
Questions.Several of the inclusions between the families of trees we have studied are not known to be strict, cf. the figure in this section.These are all left as open problems (but see below).So, for logics, are the inclusions FO+DTC 1 ⊆ FO+posTC 1 ⊆ FO+TC 1 ⊆ MSO strict?For tree-walking automata, are the inclusions Considering the use of pebbles, is there a strict hierarchy for tree languages accepted by (deterministic) tree-walking automata in the number of pebbles these automata use?
The pebbles we have used in this paper are considered as pointers, and differ from the pebbles that can only be picked up at the node they are dropped.We conjecture that our type of pebbles is more powerful than the usual one.For nonnested pebbles the two types have the same power, even when the number of pebbles is fixed (as shown in Theorem 2.2 of [4]).
Alternating tree-walking automata with classic (nested) pebbles were considered in [43], where it is shown that these automata accept the regular tree languages.We did not investigate whether alternation together with pointer pebbles again yields the regular languages, i.e., whether APW 1 A = REG.
Answers.Since the appearance of the report version of this paper, several of the questions above have been answered.
In the recent [9] it is shown that, surprisingly, the two types of nested pebbles have the same power.Moreover, it is shown that the number of pebbles gives rise to a strict hierarchy, both for deterministic and nondeterministic automata, and that the inclusion In the very recent paper [46] it is shown that APW 1 A = REG holds, using an easy variation of the proof technique of [43].
Both tree-walking automata and logical formulas can also be used in a natural way to define binary relations on the nodes of trees, called trips in [21].It is straightforward to show that Theorem 5.3 and Corollary 5.6 also hold for trips.XPath-like formalisms (relevant to XML) that are equivalent with, or closely related to, the trips defined in FO+posTC 1 and FO+TC 1 have recently been studied in [27,13].
Single head on strings.For strings the equivalence MSO = REG between monadic second-order logic and finite-state automata was obtained independently by Büchi, Elgot, and Trakhtenbrot [11,17,64].As observed, tree-walking automata on monadic trees correspond to two-way finite-state automata on strings.Since the latter are equivalent to ordinary finite-state automata (both deterministic and nondeterministic) [53,58] most of the hierarchy is known to collapse in the string case: DW 1 A = REG.Hence for k = 1 our main result gives two additional characterizations of the regular string languages REG = NPW 1 A and REG = FO+DTC 1 .The second equality was shown in [2] (see also Exercise 8.6.3 of [16], and Satz 2.0.1 of [52]).

Walking on Graphs
We generalize our results on trees (and strings) to more general families of graphs.There is a basic problem that has to be resolved.We have to propose a model of graphs that is suitable for both graph-walking automata and logic.The structures for which Immerman has obtained his logical characterization of DSPACE(log n) are ordered, i.e., they are equipped with a total order ≤, which can either be specified directly or, as transitive closure is available in the logical language, by a direct successor relation.In fact, when placing a structure on the tape of a Turing Machine, as has to be done for DSPACE(log n), it almost automatically obtains an implicit order on its elements.A natural way to present a structure (assuming only relations of at most arity two) as input to a graph-walking automaton is to represent all pairs in a binary relation as directed edges in a graph, labelled by a symbol representing the relation; unary relations and constants are translated as labels for the nodes.If we fully represent a total order ≤ in this way, there will be many edges with the same label leaving (and entering) each node.Although the edge to the direct successor can be directly specified in first-order logic we see no natural machine model that can choose this edge among the many candidates.
A solution would be to assume the existence of direct successor edges in the graph, i.e., to consider 'ordered' graphs (cf. the end of the Introduction), but we feel this requirement is too restrictive.On trees, tree-walking automata use the preorder to traverse the nodes of the input.Thus, the order is implicit in the tree, and not explicitly given by edges with a special label.Below we generalize this idea to graphs: we do not assume an explicit total order on the nodes, but we postulate the existence of a special graph-walking automaton, the guide, that can be used as a 'subroutine' to traverse the nodes of any graph in the family of graphs we are considering.For trees the guide follows the preorder, for (two-dimensional) grids the guide visits the nodes row by row (with generalizations to higher dimensions).
Graphs.We consider (families of) nonempty finite directed graphs, where nodes and edges are labelled using a common alphabet Σ.In order to be able to locally distinguish edges we require that no node has two outgoing edges or two incoming edges with the same label.Hence the graphs we consider are of bounded degree.Parallel edges and loops are allowed.Thus, each edge label represents a (partial) injective function on the set of nodes.We consider (weakly) connected graphs only.
Trees over a ranked alphabet fall under this definition since we label the edge from a parent to its i-th child by i.Another example of such a family is formed by grids, where two different labels are used to distinguish edges to horizontal and vertical neighbours.(In our model there is no need to introduce special node labels that mark the boundaries of the grid, cf.[4,2], as the labels of the edges incident to a node are available to our automata).
Automata.A k-head graph-walking automaton with nested pebbles is like its relative for trees in that it visits the nodes of a graph, each of its heads walking along the edges from node to node.It may check the labels of its current nodes, check whether such a node has an incident incoming/outgoing edge with a specific label (generalizing the concept of child number and the rankedness of Σ, respectively), check for the presence of a specific pebble (from a finite set), and it may then move each head along any edge in either direction choosing the edge based on its label.Moreover, it may drop and retrieve pebbles in a nested fashion, as before.
Formally we change the operations and tests available to the automaton.The moves up i and down i,j are replaced by the more symmetric pair inmove i,σ , outmove i,σ to specify a move by the i-th head along an incoming (outgoing) edge with label σ.Likewise we replace the child-number test chno i,j by the tests inedge i,σ and outedge i,σ on the existence of an incoming (outgoing) edge with label σ for the node currently under the i-th head.
Generally graphs do not have a distinguished node (like the root is a distinguished node for trees) and we change the definition of acceptance accordingly.A graph-walking automaton with nested pebbles accepts a given input graph if the automaton has an accepting computation (starting in the initial state, halting in an accepting state) when started with all its heads on any node of the input graph; initially and finally there should be no pebbles on the graph.We require that the existence or nonexistence of an accepting computation is independent of the chosen initial node, as this seems a natural condition, especially in the context of determinism.Note that not all automata satisfy this requirement. 7  7 Formally, we define L(A) to consist of all graphs g over Σ such that acc(u) for every node u of g, where acc(u) means that [q0, u k , ε] ⊢ * A,g [q, v, ε] for some halting configuration [q, v, ε] with q ∈ A and v is a k-tuple For trees over a ranked alphabet this definition is obviously equivalent to the old one as a tree-walking automaton can always move all its heads to the root.Disregarding pebbles, for grids the definition is equivalent to the k-head automaton of [2] (and for k = 1 to the 2-dimensional automaton of [4]).
In order to avoid an abundance of new notation, we keep the notation DPW k A, etc., for families accepted by (deterministic) k-head automata (with nested pebbles) even in the more general context of graphs.
Logic.The first-order logic for graphs over the label alphabet Σ has atomic formulas lab σ (x), σ ∈ Σ, for a node x with label σ, edg σ (x, y), σ ∈ Σ, for an edge from x to y with label σ, and x = y.
As for automata, we keep the notation for families defined by our first-order logic with several variants of transitive closure.Note that we do not allow the predicate x ≤ y, which makes the logic more like the local variant of first-order logic.As we have seen in Section 2, for trees this predicate is definable in first-order logic with (positive deterministic) transitive closure, and the families FO+DTC k , etc., of tree languages definable in first-order logic with transitive closure, do not change by this restriction.
For arbitrary families of graphs the computation of a graph-walking automaton with nested pebbles can be specified in first-order logic with transitive closure, like in Section 5. Proof.In the proof of Lemma 5.2, where we consider trees instead of graphs, no reference is made to properties particular to that domain.The only place where the proof given has to be adapted is where the accepting condition is phrased in the logic.Here any node must have an accepting computation when we start the automaton with all heads on that node, so we write (∀x)(∃y) q∈A φ (n)# p,q (x k , y) where p is the initial state, the disjunction is taken over the set A of accepting states, and x k is the k-tuple consisting of k copies of x.Note that we might as well take ∃x instead of ∀x.
The reverse inclusion needs an additional notion, the ability to search each graph in a given family of graphs.
of nodes of g (and u k is the k-tuple of k copies of u).Alternatively, we could require this for some node u of g, because, as discussed above, A is restricted to satisfy the requirement that for all graphs g over Σ and all nodes u1, u2 of g, if acc(u1) then acc(u2).We note, however, that this restriction is not essential for our results: it is easily shown that for searchable graphs (see below) the restriction can be dropped.
We give several elementary examples of this notion.Trees over a ranked alphabet form a searchable family: from any node we may walk to the root to start a preorder traversal through the tree.Similarly binary trees -which are basically ranked trees where every node has rank two, but where either child of a node can be missing-are a searchable graph family.
Unranked (ordered) trees, where there is no bound on the number of children of a node, are important in the theory of data representation using XML [47,37,57].Here they are considered in their natural encoding as binary trees, where each node carries possibly two pointers (edges), one to its first child, one to its right sibling.In this way they are a searchable graph family.The (single-head) automata we obtain using this representation may move to the first child or to the next sibling of a node (and back), exactly as customary in the literature [47,50] (albeit without pebbles).For an overview on logics for unranked trees, see [39].
Rectangular (directed) grids, edges pointing to the right or downwards, with edge labels distinguishing these two types of edges, form another example.This can be generalized to higher dimensional grids8 [2].
All the above are superseded by acyclic, connected graphs with a single source (and the local restriction on edge labels).We can effectively turn those graphs into trees by placing an ordering on the labels and ignoring all incoming edges of a node except the edge which has minimal label among those edges.Graph-walking automata (with one head and no pebbles) on such graphs were considered in [36].
Obviously, a family of graphs such that, for some Σ ′ ⊆ Σ, the graphs induced by the Σ ′ -labelled edges belong to one of the above families, is searchable too.This includes the family of all 'ordered' graphs.
The above families are traversed in a rather standard way.In the next example we use a pebble to find and 'break' a cycle.
Example 7.2.Consider an ordinary binary tree, and take one of its leaves.If we identify this leaf with the root (i.e., we remove it and redirect the incoming edge to the root) we obtain a single directed cycle from which trees radiate, like charms hanging from a bracelet.
We argue that the family of these graphs is searchable.If started on a node in the cycle a graph-walking automaton may drop a pebble at that node.This effectively reduces the graph to a tree with the marked node as root, and a preorder traversal can be made.
We claim that a node on the cycle can be found, with the help of a single pebble, starting on an arbitrary node of the graph.Put the pebble on the initial node.Search the graph 'below' the pebble as if it is a tree; if indeed it is a tree, then we finally return to the pebble from below.Otherwise we are on the cycle, and we enter the leaf that has been identified with the root, and will find the pebble from above.Thus, in order to find a node on the cycle we repeat the above search, each time moving the pebble one node up while not on the cycle.Cyclic grids, or toruses, where the last node of each row has an edge to the first node of that row, and similarly for columns, can be searched using two pebbles.We search the grid row-by-row: the first pebble marks the position we start with (in order to stop when all rows are visited; we do not move this pebble during the traversal), the second pebble moves down in the first column to mark the position in which we started the row (in order to stop when we finish the row; we then move the pebble down to the next row until we meet the first pebble).This can be generalized to d pebbles in the d-dimensional case. 9 It is an open question whether mazes (modelled as connected subgraphs of grids) form a searchable family of graphs, that is, with a single head and the help of nested pebbles.They cannot be searched without the help of pebbles, or with only a single (classic) pebble [12,31].According to [5] these graphs can be searched by single-head graph-walking automata with two (classic) pebbles which are unfortunately not used in a nested fashion, or using two heads (without pebbles).
The family of all graphs (over a given alphabet with at least two elements) is not searchable, not even with nonnested pebbles or with several heads.This follows from results of Cook and Rackoff [14].The deterministic k-head graph-walking automaton with n pebbles is a special case of the jumping automaton of [14] with k + n pebbles.A jumping automaton may move its pebbles along the edges of the graph, or move one of its pebbles to the location of another pebble ('jumping').Retrievability of pebbles at a distance is a particular case of this jumping facility.Basically, [14, Theorem 4.9] states that the number of pebbles needed to visit all nodes of a d-dimensional torus grows as a function of d.Then [14, Theorem 4.13] concludes there is no jumping automaton that searches all graphs over a three letter alphabet, by coding arbitrary graph alphabets (like the d dimension edge labels for toruses) into a three letter alphabet.A slight adaptation of the proof (because of the graph model used) makes it valid in our setting, with two letters instead of three.For classic pebbles (and a single head) it is shown in [55] that the family of all planar graphs is not searchable.
For an overview of approaches to the exploration of mazes and graphs by finite automata see [24,3].
For searchable graphs one can obtain the converse inclusion from our previous result, and translate logic into automata.
Theorem 7.3.For every searchable family of graphs, and k ≥ 1, DPW k A = FO+DTC k .Proof.By the previous lemma it suffices to show FO+DTC k ⊆ DPW k A for searchable graphs, i.e., to reconsider the proof of Lemma 4.1.There is no need to return to a 'root' (we do not have one) to test acceptance by some automaton (the induction hypothesis used as a 'subroutine'); in the definition of acceptance we have required acceptance for any initial node, so we merely move all heads to the same position: drop a pebble, let each head search for it, and lift the pebble. 10 At several points in that proof the automaton is required to make a systematic traversal through the input tree, or more specifically, to find the preorder successor of a given node, a function easily implemented for trees.At some other points we are supposed to move a head to a specific position marked by a pebble: the pebble again is found by a systematic traversal of the tree.
Here we instead use the 'guide', the automaton that makes the family searchable, as a subroutine to determine the successor of a node (and to make traversals of the graph).We have to mind some details, however.First, the guide may use its own pebbles during its computation, as is required to traverse a torus; leaving these pebbles on the graph would 9 A d-dimensional torus is a d-dimensional grid with additional i-labelled edges from (x1, . . ., ki, . . ., x d ) to (x1, . . ., 1, . . ., x d ).
10 Dropping a temporary pebble is also a technique to show we may assume heads to be sensing, i.e., the automaton can check whether two heads occupy the same position.
block the removal of the other pebbles on the graph.Second, during the traversal of the graph the guide may visit the same node several times.In order to define the notion of successor properly, we have to distinguish a particular visit; for this we take the first visit to the node (as in the preorder of the nodes of a tree).Third, the order of visiting the nodes may depend on the node at which the guide is started, which makes the notion of successor undefined (think torus again).Thus, we need to explain how the unique successor can be found for each node in the graph (except the last).First, at the very beginning of its computation, the automaton we construct for a given formula drops a pebble on the node where it is started (i.e., the node where all its heads are initially positioned; this may be any node of the graph).This pebble, let us call it 'the origin', remains at its position during the full computation to serve as a fixed position in the graph.Every successor will be determined in the order when starting the guide in the origin.Now the task of finding the successor of a (marked) node using a single head is executed as follows.First we use the guide (from wherever we are) to move the head to the origin.When the origin is reached, we 'reset' the guide (removing its pebbles from the graph, returning to its initial state) and again start a traversal of the graph looking for our marked node.When we reach the marked node, we continue running the guide until we reach a node that is visited for the first time: our (well-defined) successor.In order to check whether a node is visited for the first time we leave all pebbles of the guide at their position and drop a new pebble on the node.We then start a copy of the guide at the origin and run it until we find the new pebble.If the copy of the guide is in the same state and has the same positions for its pebbles as the original guide, then it visits the node for the first time: due to its determinism the guide cannot visit a node twice in the same configuration.After this test we stop the copy of the guide and retrieve its pebbles.We either have found the successor node, retrieve the pebbles of the guide and proceed as needed, or we lift the new pebble and continue the guide.If the guide halts without finding a first visit, the marked node was the last node in the order.
As in Corollary 5.6, we have in the nondeterministic case NPW k A = FO+posTC k for every searchable family of graphs, and k ≥ 1.

Multi-Searchable Graphs.
It is open whether we can search a maze (a connected subgraph of a grid) with a single head using nested pebbles.However with two heads we can search a maze [5].To cover this family we need to extend the notion of searchability: as the automata we are dealing with have k heads, it seems only fair to extend the guide with this commodity.A family of graphs is k-searchable if there is a deterministic guide as before, which now may have k heads.The guide starts its computation with all its heads on an arbitrary node of the graph, and performs a traversal of the graph.Recall that the formal model assumes that at most one head moves in each computation step of the guide; this ensures that at most one node is visited for the first time, thus uniquely defining the order of the nodes.
With this notion the results of the previous section can be extended to the larger class of k-searchable graphs.But there is a catch: we have to extend our automaton model with a new instruction p, jump i (x), q that moves a given head i to the position of a given pebble x (like the 'jumping' instruction from [14]).In the case of searchable graphs a single head can be moved to any pebble, just by running the guide using that head, searching for the pebble.For k > 1 however, in order to move a single head to a pebble, we need several heads, either introducing auxiliary heads (loosing the connection between number of heads and arity of transitive closure) or loosing the position originally held by the heads needed for the search.(Even ignoring the problem of how to move all heads to the same initial position as needed for the guide.) The suggested additional jump-instruction is rather natural if we think of pebbles as pointers as we did before.If the graph-walking automaton stores a finite number of 'addresses' of nodes, it can use a simple assignment to move a head to such a position.Adding this instruction is 'backward compatible': it will not change any of the previous results.On families of searchable graphs (which includes trees) the new instruction can be implemented as explained above.
Note that if a family is k ′ -searchable, then it is k-searchable for k ≥ k ′ .Thus, the next result generalizes Theorem 7.3 (which for trees is Theorem 5.3).
Theorem 7.4.Let the automaton model be extended with the jump-instructions, as discussed above.For every k-searchable family of graphs, k ≥ 1, DPW k A = FO+DTC k .
Proof.For the inclusion DPW k A ⊆ FO+DTC k we note that the new instruction (move head i to pebble x) is directly expressible in the logic as we assume variables for the positions of heads and pebbles, cf. the proof of Lemma 5.2.
For the converse inclusion FO+DTC k ⊆ DPW k A we once more carefully inspect the proof of Lemma 4.1, or rather the proof of Theorem 7.3.
As observed in the latter proof, the guide is used for two purposes: (1) to find the successor of a node, and (2) to move a specific head to a specific pebble (and note that (2) is also used in (1): a kind of bootstrapping).For (2) we now use the new instruction, whereas for (1) we use the k-head guide.Note that to initiate the guide, all heads can be moved to the origin by the new instruction.Note also that before initiating the copy of the guide, pebbles should be dropped on all current head positions of the guide (to be able to compare them with those of the copy of the guide, and to restore them in case the successor has not been found).
Since mazes are 2-searchable, we obtain that for mazes and k ≥ 2, DPW k A = FO+DTC k .
Again we obtain a similar result for the nondeterministic case: NPW k A = FO+posDTC k for every k-searchable family of graphs.
As discussed before Theorem 7.3, our k-head graph-walking automaton with nested pebbles is a special case of the jumping automaton of [14]; this includes the 'jumping' instruction of moving a head to the position of a pebble.Thus, by the results of [14], the family of all graphs (over Σ ⊇ {0, 1}) is not k-searchable for any k.
According to Theorem 7.4, the existence of a k-head guide for a family of graphs entails that every language in FO+DTC k can be implemented by a k-head graph-walking automaton with nested pebbles.This is obvious for the language L 0 defined by the formula (∀x) lab 0 (x): it can be checked directly by the guide whether every node visited has label 0. It is easy to see that this also works the other way around: assuming Σ ⊇ {0, 1}, an automaton for L 0 must visit all nodes of any input graph that has only node label 0, since otherwise it also accepts that graph with the labels of unvisited nodes changed into 1.The automaton can be turned into a guide for the family by making it behave as if every node label equals 0.Here we require that the family is node-label-insensitive: membership of a graph in the family does not depend on the label of any node of the graph.For instance, the families of unranked trees, grids, and toruses we considered are node-label-insensitive.Thus, a node-label-insensitive family of graphs is k-searchable iff L 0 ∈ DPW k A. Hence for such a family Theorem 7.4 also holds in the other direction: if F is a node-label-insensitive family of graphs, then for every k, F is k-searchable iff DPW k A = FO+DTC k .Moreover, F is k-searchable for some k iff DPWA = FO+DTC, where DPWA = k∈N DPW k A, and FO+DTC = k∈N FO+DTC k .Moreover, this holds iff k∈N DW k A = FO+DTC, assuming that the k-head graph-walking automaton (without pebbles) has an additional instruction to move one head to another (i.e., it is the jumping automaton of [14]).
Applying this to the family of all graphs, which is clearly node-label-insensitive, we note that the inclusions DPW k A ⊂ FO+DTC k (cf.Lemma 7.1) and DPWA ⊂ FO+DTC are strict, the language L 0 being in the difference.
Pebbles left unturned.An obvious question that remains open is whether there exists a strict hierarchy in DSPACE(log n) with respect to the number of heads used (allowing nested pebbles), or equivalently with respect to the arity of transitive closure.The same question can be asked for trees and grids.For arbitrary graphs, strictness of the hierarchies FO+TC k and FO+DTC k is shown in [29].Some other unresolved questions were stated in Section 6, regarding single-head automata on trees.The question concerning the nature of our nested pebbles, whether the pointer model is more powerful than the classic model, remains interesting for multi-head automata.Another question is whether our results can be generalized to alternating automata and the alternating transitive closure operator of [33].

Lemma 5 . 2 .
For trees over a ranked alphabet, and k ≥ 1, DPW k A ⊆ FO+DTC k .

Theorem 5 . 3 .
For trees over a ranked alphabet, and k ≥ 1, DPW k A = FO+DTC k .