# Logspace algorithm for s-t connectivity in undirected forests

It has been shown that the decision problem
$\{(G, s, t)~\mid~ G\text{ is an undirected forest and there is a path from }s \text{ to } t \}$ is complete for logspace (and therefore in $L$).
But the proof involves a series of reductions which didn't seem very intuitive to me.
Is there any natural algorithm that directly solves this problem in log space?

## 1 Answer

This is proved by Cook and McKenzie. We make use of the following notation:

1. $\deg(v)$ is the degree of a vertex $v$.

2. $N(v,1),\ldots,N(v,\deg(v))$ is some fixed ordering of the neighbors of $v$.

We construct a sequence $v_1,v_2,\ldots$ of nodes starting with $v_1 = s$ and $v_2 = N(s,1)$ (if $\deg(s) = 0$ then $s$ is connected to $t$ iff $s = t$). Given $v_{i-2},v_{i-1}$, we construct $v_i$ as follows:

Suppose that $v_{i-2} = N(v_{i-1},j)$. Then $v_i = N(v_{i-1},j+1)$.

(If $j = \deg(v_{i-1})$ then $j+1 = 1$.)

It turns out that if the connected component of $s$ has size $m$ then the first $2(m-1)$ vertices contain each vertex $v$ in the connected component exactly $\deg(v)$ times, and after that the sequence repeats indefinitely. This is because the sequence essentially performs a depth-first search, as the following image from Wikipedia illustrates (the arrows should be ignored): Here $s = F$ and the neighbors are ordered from left to right. The first 16 elements of the sequence are F,B,A,B,D,C,D,E,D,B,F,G,I,H,I,G. The sequence then repeats.

This implies the following simple algorithm: calculate the first $2(n-1)$ members of the sequence, and check whether $t$ ever appears. This can be implemented in logarithmic space.

Reingold gave a (significantly) more complicated algorithm that works for any undirected graph, still using only logarithmic space. If the graph is directed then the problem is complete for NL, and so probably cannot be solved using only logarithmic space (at least not on deterministic Turing machines!).
{"ft_lang_label":"__label__en","ft_lang_prob":0.92268145,"math_prob":0.9997651,"size":1941,"snap":"2021-04-2021-17","text_gpt3_token_len":531,"char_repetition_ratio":0.10635003,"word_repetition_ratio":0.025641026,"special_character_ratio":0.28335908,"punctuation_ratio":0.13429257,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99997425,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-20T07:02:10Z\",\"WARC-Record-ID\":\"<urn:uuid:661616c9-8711-4488-adb2-b21689538721>\",\"Content-Length\":\"165602\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:35259899-29f5-43af-a801-f453cbaee9cd>\",\"WARC-Concurrent-To\":\"<urn:uuid:f202d203-e729-49f0-87ec-4640d2a7f67f>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://cs.stackexchange.com/questions/66041/logspace-algorithm-for-s-t-connectivity-in-undirected-forests\",\"WARC-Payload-Digest\":\"sha1:25JXW4VIERF2MIEL4DCFG2EVON2VAN4L\",\"WARC-Block-Digest\":\"sha1:B2SRDDFPRUUPX7327KR4FGBBL4ESXBCQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618039379601.74_warc_CC-MAIN-20210420060507-20210420090507-00473.warc.gz\"}"}