Your approach to find the cycle in a undirected graph should be like that:
- for each vertex v in G
- for each edge e(u,v) in G taken one by one
- if Find-set(u) == Find-set(v)
- Union-set(u, v)
- return false
For the directed graph, you should use the Tarjan's strongly connected components algorithm to get the number of strongly components in the graph. Then, you can check if the strongly connected components number is equal to the vertexes number. Since if there is a cycle in the directed graph, there are at least two vertexes in the same strongly connected components. That means the total number of the strongly connected components should be less than the number of vertexes, if the directed graph has a cycle.