1 package edu.uci.ics.jung.algorithms.shortestpath;
2
3 import java.util.Collection;
4 import java.util.Set;
5
6 import com.google.common.base.Function;
7 import com.google.common.base.Functions;
8 import com.google.common.base.Supplier;
9
10 import edu.uci.ics.jung.algorithms.cluster.WeakComponentClusterer;
11 import edu.uci.ics.jung.algorithms.filters.FilterUtils;
12 import edu.uci.ics.jung.graph.Forest;
13 import edu.uci.ics.jung.graph.Graph;
14 import edu.uci.ics.jung.graph.Tree;
15 import edu.uci.ics.jung.graph.util.TreeUtils;
16
17
18
19
20
21
22
23
24
25
26 @SuppressWarnings("unchecked")
27 public class MinimumSpanningForest2<V,E> {
28
29 protected Graph<V,E> graph;
30 protected Forest<V,E> forest;
31 protected Function<? super E,Double> weights =
32 (Function<E,Double>)Functions.<Double>constant(1.0);
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public MinimumSpanningForest2(Graph<V, E> graph,
49 Supplier<Forest<V,E>> supplier,
50 Supplier<? extends Graph<V,E>> treeFactory,
51 Function<? super E, Double> weights) {
52 this(graph, supplier.get(),
53 treeFactory,
54 weights);
55 }
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public MinimumSpanningForest2(Graph<V, E> graph,
71 Forest<V,E> forest,
72 Supplier<? extends Graph<V,E>> treeFactory,
73 Function<? super E, Double> weights) {
74
75 if(forest.getVertexCount() != 0) {
76 throw new IllegalArgumentException("Supplied Forest must be empty");
77 }
78 this.graph = graph;
79 this.forest = forest;
80 if(weights != null) {
81 this.weights = weights;
82 }
83
84 WeakComponentClusterer<V,E> wcc =
85 new WeakComponentClusterer<V,E>();
86 Set<Set<V>> component_vertices = wcc.apply(graph);
87 Collection<Graph<V,E>> components =
88 FilterUtils.createAllInducedSubgraphs(component_vertices, graph);
89
90 for(Graph<V,E> component : components) {
91 PrimMinimumSpanningTree<V,E> mst =
92 new PrimMinimumSpanningTree<V,E>(treeFactory, this.weights);
93 Graph<V,E> subTree = mst.apply(component);
94 if(subTree instanceof Tree) {
95 TreeUtils.addSubTree(forest, (Tree<V,E>)subTree, null, null);
96 }
97 }
98 }
99
100
101
102
103 public Forest<V,E> getForest() {
104 return forest;
105 }
106 }