public static function factoryCycleFromPredecessorMap(array $predecessors, Vertex $vertex, $by = null, $desc = false)
    {
        // find a vertex in the cycle
        $vid = $vertex->getId();
        $startVertices = array();
        do {
            if (!isset($predecessors[$vid])) {
                throw new InvalidArgumentException('Predecessor map is incomplete and does not form a cycle');
            }

            $startVertices[$vid] = $vertex;

            $vertex = $predecessors[$vid];
            $vid = $vertex->getId();
        } while (!isset($startVertices[$vid]));

        // find negative cycle
        $vid = $vertex->getId();
        // build array of vertices in cycle
        $vertices = array();
        do {
            // add new vertex to cycle
            $vertices[$vid] = $vertex;

            // get predecessor of vertex
            $vertex = $predecessors[$vid];
            $vid = $vertex->getId();
            // continue until we find a vertex that's already in the circle (i.e. circle is closed)
        } while (!isset($vertices[$vid]));

        // reverse cycle, because cycle is actually built in opposite direction due to checking predecessors
        $vertices = array_reverse($vertices, true);

        // additional edge from last vertex to first vertex
        $vertices[] = reset($vertices);

        return self::factoryCycleFromVertices($vertices, $by, $desc);
    }