detect cycle in a directed graph