Skip to content

Commit 8b6a1d6

Browse files
committed
[Function add]
1.Add the function of finding MST using Kruskal algorithm,
1 parent aa4529c commit 8b6a1d6

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ca.mcmaster.chapter.four.graph.mstree;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.PriorityQueue;
9+
10+
import ca.mcmaster.chapter.one.unionfind.UnionFind;
11+
import ca.mcmaster.chapter.one.unionfind.WeightedUnionFind;
12+
13+
public class KruskalMST implements MST {
14+
private List<Edge> mst; //Queue used to save the minimum spanning tree
15+
public KruskalMST(EdgeWeightedGraph g) {
16+
mst = new LinkedList<>();
17+
PriorityQueue<Edge> pq = new PriorityQueue<>();
18+
for(Edge e : g.edges())
19+
pq.add(e);
20+
UnionFind uf = new WeightedUnionFind(g.V());
21+
while(!pq.isEmpty() && mst.size() < g.V() - 1){
22+
Edge edge = pq.poll();
23+
int v = edge.either(); int w = edge.other(v);
24+
if(uf.connected(v, w)) continue;
25+
else{
26+
mst.add(edge);
27+
uf.union(v, w);
28+
}
29+
}
30+
}
31+
@Override
32+
public Iterable<Edge> edges() {
33+
return mst;
34+
}
35+
36+
@Override
37+
public double weight() {
38+
double res = 0d;
39+
for(Edge e : mst)
40+
res += e.weight();
41+
return res;
42+
}
43+
public static void main(String[] args) throws FileNotFoundException {
44+
FileInputStream is = new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/mstree/mediumEWG.txt"));
45+
EdgeWeightedGraph graph = new EdgeWeightedGraph(is);
46+
MST mst = new KruskalMST(graph);
47+
Iterable<Edge> edges = mst.edges();
48+
for(Edge e : edges)
49+
System.out.println(e.toString());
50+
System.out.println("Weight: " + mst.weight());
51+
}
52+
}

Diff for: DataStructrue/Graph/MinimumSpanningTrees.md

+45
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,49 @@ public class PrimMst implements MST {
224224
}
225225
}
226226
}
227+
```
228+
229+
#### Kruskal算法
230+
1. 将所有的边加入优先队列,comparable实现权重的比较。最终所有边按照从小到大排序。
231+
2. 从上到下将所有的边加入最小树中,如果会生成环则当前边失效。通过并查集的方法判断两个顶点是否已经connected,如果是添加一条新的边链接这两个顶点将会构成环。
232+
```Java
233+
public class KruskalMST implements MST {
234+
private List<Edge> mst; //Queue used to save the minimum spanning tree
235+
public KruskalMST(EdgeWeightedGraph g) {
236+
mst = new LinkedList<>();
237+
PriorityQueue<Edge> pq = new PriorityQueue<>();
238+
for(Edge e : g.edges())
239+
pq.add(e);
240+
UnionFind uf = new WeightedUnionFind(g.V()); //通过创建一个并查集来判断两个顶点是否相连。
241+
while(!pq.isEmpty() && mst.size() < g.V() - 1){
242+
Edge edge = pq.poll();
243+
int v = edge.either(); int w = edge.other(v);
244+
if(uf.connected(v, w)) continue; //如果v和w已经连接的,在两个顶点之间加一条边一定会构成一个环。
245+
else{ ////将边加入最小树中并将顶点加入并查集。
246+
mst.add(edge);
247+
uf.union(v, w);
248+
}
249+
}
250+
}
251+
@Override
252+
public Iterable<Edge> edges() {
253+
return mst;
254+
}
255+
@Override
256+
public double weight() {
257+
double res = 0d;
258+
for(Edge e : mst)
259+
res += e.weight();
260+
return res;
261+
}
262+
public static void main(String[] args) throws FileNotFoundException {
263+
FileInputStream is = new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/mstree/mediumEWG.txt"));
264+
EdgeWeightedGraph graph = new EdgeWeightedGraph(is);
265+
MST mst = new KruskalMST(graph);
266+
Iterable<Edge> edges = mst.edges();
267+
for(Edge e : edges)
268+
System.out.println(e.toString());
269+
System.out.println("Weight: " + mst.weight());
270+
}
271+
}
227272
```

0 commit comments

Comments
 (0)