-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLazyPrimMST.java
More file actions
44 lines (36 loc) · 1.24 KB
/
Copy pathLazyPrimMST.java
File metadata and controls
44 lines (36 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class LazyPrimMST {
/* Compute the MST of an EWG using Prim's algorithm */
/* Instance variables */
private boolean[] marked;
// no need for a UF data structure...tree is built continuously...not in discrete steps
private Queue<Edge> mst;
private MinPQ<Edge> pq;
/* Constructor */
public LazyPrimMST(EdgeWeightedGraph G) {
this.marked = new boolean[G.V()];
this.mst = new Queue<Edge>();
this.pq = new MinPQ<Edge>();
// Start with vertex 0
visit(G, 0);
while (!pq.isEmpty() && mst.size() < G.V()-1) {
Edge e = pq.delMin();
int v = e.either(), w = e.other(v);
// disregard edge if already on MST
if (marked[v] && marked[w]) continue;
mst.enqueue(e);
if (!marked[v]) visit(G, v);
if (!marked[w]) visit(G, w);
}
}
/* Visit vertex v and add adjacent edges not in MST to PQ */
private void visit(EdgeWeightedGraph G, int v) {
marked[v] = true;
for (Edge e : G.adj(v))
if (!marked[e.other(v)])
pq.insert(e);
}
/* API: Iterate through all edges of the MST */
public Iterable<Edge> edges() {
return mst;
}
}