I have to solve the following problem:

Consider the problem Connected:

Input: An unweighted, undirected graph $G$.

Output: True if and only if $G$ is connected.

Show that Connected can be decided in polynomial time.

I have been at this for hours, and I can't seem to find a way to prove this. Any hints?

  • 1
    $\begingroup$ Suppose you know what the connected components are for part of the graph. Now you look at an edge you haven't yet dealt with. What would you do? $\endgroup$ – András Salamon Apr 10 '13 at 21:43

Make a BFS/DFS traversal on the graph. If you visited every vertex then it is connected otherwise not. Note: You have to apply BFS/DFS only one time on the graph


Two hints:

  • How would you solve the problem at all?
  • Which graph algorithms do you know? Can you use one of them to solve the problem?

Besides the usual deterministic DFS/BFS approaches, one could also consider a randomized algorithm. I will shortly describe a randomized algorithm for deciding if two vertices $s$ and $t$ are connected. It can also be used to decide if the whole graph is connected. The main benefit is that this method requires $O(\log |V|)$ bits of space, whereas a BFS/DFS requires $\Omega(|V|)$ space.

The cover time of an undirected graph $G=(V,E)$ is the maximum over all vertices $v \in V(G)$ of the expected time to visit all of the nodes in the graph by a random walk starting from $v$. Using some theory of Markov chains, it is not too hard to prove the cover time of $G$ if bounded from above by $4|V|\cdot|E|$.

The algorithm for deciding if $s$ and $t$ are connected is simple:

Input: two vertices s,t
1. Start a random walk from s.
2. If t is reached within 4|V|^3 steps, return true. Otherwise, return false.

Clearly, if there is no path between $s$ and $t$ the algorithm returns the correct answer. If there is a path, the algorithm errs if it is not found within $4|V|^3$ steps. The cover time of $G$ is bounded from above by $4|V||E| < 2|V|^3$. Using Markov's inequality, the probability that a random walk takes more than $4|V|^3$ steps to reach $t$ from $s$ is at most $1/2$. In other words, the algorithm returns the correct answer with probability $1/2$, and only errs by saying $s$ and $t$ are not connected when they in fact are.

  • $\begingroup$ Any ideas on how the probability scales with multiple executions? Does the err chance multiplies? As in (1/2)^n with n runs? $\endgroup$ – Kahler May 28 '18 at 9:20
  • 1
    $\begingroup$ @Kahler Yes, absolutely, they are independent runs. Just do more runs to get a probability you want. $\endgroup$ – Juho May 28 '18 at 20:26

You can find the Laplacian matrix of the graph and check the multiplicity of eigenvalue zero of the Laplacian matrix, if the multiplicity of zero is one then graph is connected, if multiplicity of eigenvalue zero of Laplacian matrix of the graph is two or more then it is disconnected.

  • 2
    $\begingroup$ You could do but... It doesn't seem like this answer will be at all helpful to somebody who doesn't even know if you can check connectivity in polytime. $\endgroup$ – David Richerby Oct 29 '18 at 12:06
  • $\begingroup$ Question was posted in 2013, so hopefully he knows by now. $\endgroup$ – gnasher729 Oct 29 '18 at 13:21
  • 1
    $\begingroup$ @gnasher729 It's fair to add answers to old questions -- if they add something that can be assumed to be of use for googlers. (Buzzword Encyclopedia Stack Exchange) $\endgroup$ – Raphael Oct 29 '18 at 13:34
  • $\begingroup$ Also, this is probably a lot less efficient than the simple algorithms given in other answers. $\endgroup$ – Raphael Oct 29 '18 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.