function components(g) {
  var results = [];
  var visited = new Set();

  function dfs(v, component) {
    if (!visited.has(v)) {
      visited.add(v);
      component.push(v);
      g.neighbors(v).forEach(function(w) {
        dfs(w, component);
      });
    }
  }

  g.nodes().forEach(function(v) {
    var component = [];
    dfs(v, component);
    if (component.length > 0) {
      results.push(component);
    }
  });

  return results;
}
Finds all [connected components][] in a graph and returns an array of these
components. Each component is itself an array that contains the ids of nodes
in the component.

This function only works with undirected Graphs.

[connected components]: http://en.wikipedia.org/wiki/Connected_component_(graph_theory)

@param {Graph} g the graph to search for components