0
$\begingroup$

So how to determine in $\mathcal{O}(|V|)$ if a graph $G=(V,E)$ is a forest?

Hope somebody has an idea.

  • 0
    how is the graph saved?2017-01-31
  • 0
    I can't think any possible algorithm that says there is no cycle without having seen all edges2017-01-31
  • 0
    @JorgeFernández - From your answer below, isn't it sufficient to check the 2nd condition only - "if the graph has cycles?" Why do we need to verify the first condition? I think a graph with cycles will always have edges >= vertices. Please rectify me if I am wrong.2017-10-16

1 Answers 1

1

If your graph is saved with adjacency lists you can recover the degree of a vertex in constant time. Using this you can check if $e\leq v-1$ in $\mathcal O(v)$ time. If this is not the case then the graph is not a forest.

Now that we know that $e\leq v-1$ we can explicitly look for cycles using dfse's, and since the number of edges is less than $v$ this has complexity $\mathcal O(v)$.

  • 0
    Isn't it sufficient to check the 2nd condition only - "if the graph has cycles?" Why do we need to verify the first condition? I think a graph with cycles will always have edges >= vertices. Please rectify me if I am wrong.2017-10-16
  • 1
    Well, if you look for cycles first you run the risk of the algorithm being O(E)2017-10-16