예제 #1
0
  public LazyPrimMST(EdgeWeightedGraph G) {
    pq = new MinPQ<Edge>(G.E());
    mst = new Queue<Edge>();
    marked = new boolean[G.V()];
    visit(G, 0);

    while (!pq.isEmpty()) {
      Edge e = pq.delMin();
      int v = e.either();
      int w = e.other(v);
      if (marked[v] && marked[w]) continue;
      mst.enqueue(e);
      if (!marked[v]) visit(G, v);
      else visit(G, w);
    }
  }