undirected graph detect cycle Python English