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); } }
public double weight() { double w = 0; for (Edge e : mst) w += e.weight(); return w; }
private void visit(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e : G.adj(v)) { if (!marked[e.other(v)]) pq.insert(e); } }