我想执行一些图形聚类,并且由于我非常依赖 Java,因此决定尝试使用 java 包 Jung。作为一个简单的图形,我创建了两个相互连接的每个 5vertices 的集群。我使用一个边缘连接两个集群。我希望在图形聚类之后检索两个大小均为 5 的集群,但我得到不同的结果。这是代码:
import edu.uci.ics.jung.algorithms.cluster.VoltageClusterer;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseGraph;
import java.io.IOException;
import java.util.Collection;
import java.util.Set;
public class CreateGraph {
public static void main(String[] args) throws IOException {
// Graph<V, E> where V is the type of the vertices
// and E is the type of the edges
Graph<Integer, String> g = new SparseGraph<Integer, String>();
for (int i = 0; i < 5; i++) {
g.addVertex((Integer) i);
}
for (int i = 0; i < 5; i++) {
for (int ii = 0; ii < 5; ii++) {
if (i != ii) {
g.addEdge("EdgeA-" + i + ii, i, ii);
}
}
}
// cluster 2
for (int i = 5; i < 10; i++) {
g.addVertex((Integer) i);
}
for (int i = 5; i < 10; i++) {
for (int ii = 5; ii < 10; ii++) {
if (i != ii) {
g.addEdge("EdgeB-" + i + ii, i, ii);
}
}
}
System.out.println(g.toString());
g.addEdge("Edge-connector", 1, 5);
System.out.println("Creating voltageclusterer");
VoltageClusterer<Integer, String> vc = new VoltageClusterer<Integer, String>(g, 2);
Collection<Set<Integer>> clusters = vc.cluster(2);
for (Set<Integer> s : clusters) {
System.out.println("set is " + s.size());
for (Integer ss : s) {
System.out.println("Element " + ss);
}
}
}
}
和输出:+
设置为 1
- 元素 8
设置为 9
- 元素 0
- 元素 1
- 元素 2
- 元素 3
- 元素 4
- 要素 5
- 元素 6
- 元素 7
- 元素 9
有人知道吗?(也欢迎有关其他方法的建议,只要它们是在 Java 中的)。