-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKruskalMST.java
More file actions
36 lines (30 loc) · 1.12 KB
/
Copy pathKruskalMST.java
File metadata and controls
36 lines (30 loc) · 1.12 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
public class KruskalMST {
/* Builds a MST from an EWG using Kruskal's algorithm */
// represent the MST as a queue
private Queue<Edge> mst;
/* Constructor */
public KruskalMST(EdgeWeightedGraph G) {
mst = new Queue<Edge>();
// Step 1: Get edges in orger of weights (use a priority queue)
MinPQ<Edge> pq = new MinPQ<Edge>();
// add edges to the priority queue
for (Edge e : G.edges(false))
pq.insert(e);
// Step 2: Add edges to the MST in order, if they do not create a cycle
// Check for cycles using the Union Find data structure
WeightedQuickUnionUF uf = new WeightedQuickUnionUF(G.V());
while (!pq.isEmpty && mst.size() < (G.V()-1)) {
// get edge with minimum weight in the PQ
Edge e = pq.delMin();
int v = e.either(), w = e.other(v);
if (!uf.connected(v, w)) { // cycle is not created
uf.union(v, w);
mst.enqueue(e);
}
}
}
/* API: Get all edges in the MST */
public Iterable<Edge> edges() {
return mst;
}
}