added graph and cycle detection