Exemple #1
0
  public LazyPrimMST(EdgeWeightedGraph G) {
    pq = new PriorityQueue<>(Collections.reverseOrder());
    marked = new boolean[G.V()];

    mst = new LinkedList<>();
    visit(G, 0);
    while (!pq.isEmpty()) {
      Edge e = pq.poll();
      int v = e.either();
      int w = e.other(v);
      if (marked[v] && marked[w]) continue;
      mst.offer(e);

      if (!marked[v]) visit(G, v);

      if (!marked[w]) visit(G, w);
    }
  }
Exemple #2
0
 private void visit(EdgeWeightedGraph G, int v) {
   marked[v] = true;
   for (Edge e : G.adj(v)) {
     if (!marked[e.other(v)]) pq.offer(e);
   }
 }