-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazyPrimMST.java
More file actions
65 lines (56 loc) · 1.47 KB
/
Copy pathLazyPrimMST.java
File metadata and controls
65 lines (56 loc) · 1.47 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.MinPQ;
import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.StdOut;
/**
* Created by Mofeng on 2017/5/14.
*/
public class LazyPrimMST {
private boolean[] marked;
private Queue<Edge> mst;
private MinPQ<Edge> pq;
public LazyPrimMST(EdgeWeightedGraph G)
{
pq = new MinPQ<Edge>();
marked = new boolean[G.V()];
mst = new Queue<Edge>();
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);
if (!marked[w]) visit(G,w);
}
}
public void visit(EdgeWeightedGraph G, int v)
{
marked[v] = true;
for (Edge e: G.adj(v))
if (!marked[e.other(v)]) pq.insert(e);
}
public Iterable<Edge> edges()
{
return mst;
}
public double weight()
{
double ww = 0.0;
for (Edge e: edges())
{
ww += e.weight();
}
return ww;
}
public static void main(String[] args)
{
In in = new In (args[0]);
EdgeWeightedGraph G = new EdgeWeightedGraph(in);
LazyPrimMST mst = new LazyPrimMST(G);
for (Edge e: mst.edges())
StdOut.println(e);
StdOut.printf("mst Weight = %.2f\n", mst.weight());
}
}