isForest = (graph, connectivity) => {
    if (graph.directed || numberOfConnectedComponents(graph) !== connectivity) {
      return false;
    }
    let numberOfEdges = 0;
    graph.vertices.forEach(vertex => {
      numberOfEdges += graph
        .neighbors(vertex)
        .filter(neighbor => vertex <= neighbor).length;
    });
    return graph.vertices.size === numberOfEdges + connectivity;
  }
Test whether graph is a valid (undirected) forest.
In a forest #vertices = #edges + #components.

@param {Graph} graph
@param {number} connectivity
@return {boolean}