[Solved]: Can we test whether two vertices are connected in time linear in the number of nodes?

Problem Detail: Consider the problem:

Given an undirected graph and two of its vertices, is there a path between them?

I often read that this problem can be solved in linear time in the number of vertices! I am not sure why this claim holds. Can this really be done in linear time (not amortized) without preprocessing?

Asked By : Luke

Answered By : Yuval Filmus

It is not possible to decide $s$-$t$ connectivity in $O(n)$, in the adjacency matrix model. In fact, here is an $Omega(n^2)$ lower bound. Let $|S| = |T| = n/2$ be a partition of the vertex set, and choose some $s in S$ and $t in T$. Consider the graph in which $S$ and $T$ are both cliques. In this graph $t$ is not reachable from $s$. If we add any edge from $S$ to $T$, then $t$ is reachable from $s$. A simple adversary argument shows that any algorithm that decides where $t$ is reachable from $s$ has to potentially check all $|S| cdot |T| = n^2/4$ potential edges: if it didn’t query some edge $(x,y)$, then it wouldn’t be able to distinguish the case in which there is no edge from $S$ to $T$ (and so $t$ is not reachable from $s$) from the case in which $(x,y)$ is the unique edge from $S$ to $T$ (and so $t$ is reachable from $s$).
Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/23320