detect cycle in an undirected graph