# Does this algorithm traverse trees in logspace?

Does this algorithm traverse trees (correctly) in logspace?

Background: Assume each vertex is expressed as an integer. A vertex is larger than another if the corresponding integer is larger. A tree is a connected undirected graph with N-1 edges.

Define nextNeighbor(current, previous):

If current has a neighbor larger than previous, then return the smallest such neighbor.

Otherwise, return the smallest neighbor of current.

Initialize current to the vertex you want to traverse from.

Initialize previous to the largest neighbor of current. (Assume current has at least 1 neighbor.)

Repeatedly call nextNeighbor 2*(N-1) iterations (you'll walk each of the N-1 edges twice). For each iteration, update previous to current and current to the output of nextNeighbor.

Implementation:

• Your algorithm works in logspace. – Yuval Filmus Jun 4 at 6:19