DFS approach to detect cycle in directed and undirected graphs