True if the directed graph dag contains a cycle. False otherwise.

        The algorithm is naive, running in O(V^2) time, and not intended for serious use! For production purposes on
        larger graphs consider implementing Tarjan's O(V+E)-time algorithm instead.

        :type dag: DirectedGraph

def _is_cyclic(dag):
        """True if the directed graph dag contains a cycle. False otherwise.

        The algorithm is naive, running in O(V^2) time, and not intended for serious use! For production purposes on
        larger graphs consider implementing Tarjan's O(V+E)-time algorithm instead.

        :type dag: DirectedGraph
        """
        # pylint: disable=invalid-name
        vertices = dag.vertices()
        for w in vertices:
            stack = [w]
            seen = set()
            while stack:
                u = stack.pop()
                seen.add(u)
                for v in dag[u]:
                    if v == w:
                        return True
                    if v not in seen:
                        stack.append(v)
        return False