My question is concerned with DETECTING if there exists a cycle. I don’t care where the cycle occurs but only if there exist a cycle.
In particular, I am working on the implementation of a (maximally) spanning tree algorithm. I have sorted the edges in descending order and then I pick one edge at the time and put it in the set of the graph edges IFF it doesn’t cause a cycle.
I have found out that for undirected graphs in only enough to check if no_of_edges > no_of_vertices – 1. Is this right? I am trying to find a case when this is not true bt I cannot. Of course this doesn’t imply that this is correct.
Thanks
Just run DFS search; it automatically detects loops because that’s the stopping condition for DFS — you stop when you enter a node that’s already in a stack, and that’s when you have found a cycle.