function prim(g, weightFunc) {
  var result = new Graph(),
      parents = {},
      pq = new PriorityQueue(),
      u;

  function updateNeighbors(e) {
    var incidentNodes = g.incidentNodes(e),
        v = incidentNodes[0] !== u ? incidentNodes[0] : incidentNodes[1],
        pri = pq.priority(v);
    if (pri !== undefined) {
      var edgeWeight = weightFunc(e);
      if (edgeWeight < pri) {
        parents[v] = u;
        pq.decrease(v, edgeWeight);
      }
    }
  }

  if (g.order() === 0) {
    return result;
  }

  g.eachNode(function(u) {
    pq.add(u, Number.POSITIVE_INFINITY);
    result.addNode(u);
  });

  // Start from an arbitrary node
  pq.decrease(g.nodes()[0], 0);

  var init = false;
  while (pq.size() > 0) {
    u = pq.removeMin();
    if (u in parents) {
      result.addEdge(null, u, parents[u]);
    } else if (init) {
      throw new Error("Input graph is not connected: " + g);
    } else {
      init = true;
    }

    g.incidentEdges(u).forEach(updateNeighbors);
  }

  return result;
}
[Prim's algorithm][] takes a connected undirected graph and generates a
[minimum spanning tree][]. This function returns the minimum spanning
tree as an undirected graph. This algorithm is derived from the description
in "Introduction to Algorithms", Third Edition, Cormen, et al., Pg 634.

This function takes a `weightFunc(e)` which returns the weight of the edge
`e`. It throws an Error if the graph is not connected.

This function takes `O(|E| log |V|)` time.

[Prim's algorithm]: https://en.wikipedia.org/wiki/Prim's_algorithm
[minimum spanning tree]: https://en.wikipedia.org/wiki/Minimum_spanning_tree

@param {Graph} g the graph used to generate the minimum spanning tree
@param {Function} weightFunc the weight function to use