Generalized DFS - detect cycles for undirected graphs as well