-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kruskal.java
43 lines (38 loc) · 1.26 KB
/
Kruskal.java
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
package graphMinSpanningTree;
import java.util.List;
import java.util.ArrayList;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class Kruskal<T> {
public List<GraphEdge<T>> weightedMST(Graph<T> g) {
ArrayList<GraphEdge<T>> mst = new ArrayList<>();
List<GraphEdge<T>> edges = g.edges();
UnionFind<T> uf = new UnionFind(g.nodes().iterator());
for(GraphEdge<T> edge: edges) {
if (!uf.find(edge.src).equals(uf.find(edge.dest)) ) {
mst.add(edge);
uf.union(edge.src,edge.dest);
}
}
return mst;
}
public static void main(String[] args) {
Graph<String> g = new Graph<>();
String[] nodeNames = { "a", "b", "c", "d", "e", "f", "g", "h" };
for (String s : nodeNames )
g.addNode(s);
g.addEdge("a","b", 1);
g.addEdge("a","c", 7);
g.addEdge("b","c", 5);
g.addEdge("b","e", 8);
g.addEdge("e","d", 3);
g.addEdge("e","c", 9);
g.addEdge("c","d", 11);
g.addEdge("a","d", 10);
g.addEdge("g","h", 6);
g.addEdge("g","f", 4);
g.addEdge("f","h", 2);
Kruskal<String> k = new Kruskal<>();
List<GraphEdge<String>> mst = k.weightedMST(g);
System.out.println("minimal spanning tree = "+mst);
}
}